KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
malloc.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 malloc.h
4 Copyright (C) 2003 Megan Potter
5 Copyright (C) 2015 Lawrence Sebald
6
7*/
8
9/** \file malloc.h
10 \brief Standard C Malloc functionality
11 \ingroup system_allocator
12
13 This implements standard C heap allocation, deallocation, and stats.
14
15 \author Megan Potter
16 \author Lawrence Sebald
17*/
18
19#ifndef __MALLOC_H
20#define __MALLOC_H
21
22#include <sys/cdefs.h>
23__BEGIN_DECLS
24
25/** \defgroup system_allocator Allocator
26 \brief Dynamic memory heap management and allocation
27 \ingroup system
28
29 @{
30*/
31
32/* Unlike previous versions, we totally decouple the implementation from
33 the declarations. */
34
35/** \brief ANSI C functions */
36struct mallinfo {
37 /** \brief non-mmapped space allocated from system */
38 int arena;
39 /** \brief number of free chunks */
41 /** \brief number of fastbin blocks */
42 int smblks;
43 /** \brief number of mmapped regions */
44 int hblks;
45 /** \brief space in mmapped regions */
46 int hblkhd;
47 /** \brief maximum total allocated space */
49 /** \brief space available in freed fastbin blocks */
51 /** \brief total allocated space */
53 /** \brief total free space */
55 /** \brief top-most, releasable (via malloc_trim) space */
57};
58
59/** \brief Allocate a block of memory.
60
61 This function allocates a block of memory of the specified number of bytes
62 on the heap. This memory must later be freed by way of the free() function.
63 The memory *is not* freed simply because a pointer to it goes out of scope.
64
65 \param size Number of bytes of memory to allocate.
66 \return A pointer to the newly allocated block of memory, or
67 NULL on failure.
68
69 \see free
70 \see calloc
71 \note The block of memory returned is completely
72 uninitialized and may contain random data.
73*/
74void *malloc(size_t size);
75
76/** \brief Allocate a block of memory and initialize it to 0.
77
78 This function allocates a block of memory of size * nmemb bytes,
79 initializing it to all zero bytes in the process. In other words, this
80 function allocates an array of nmemb elements that are each size bytes in
81 length. Just as with malloc(), you are responsible for calling free() on the
82 block of memory when you are done with it.
83
84 \param nmemb Number of elements to allocate space for.
85 \param size Size of each element in the array.
86 \return A pointer to the newly allocated block of memory, or
87 NULL on failure.
88
89 \see free
90 \see malloc
91*/
92void *calloc(size_t nmemb, size_t size);
93
94/** \brief Release a previously allocated block of memory.
95
96 This function frees memory that has been previously allocated by way of any
97 of the allocation functions in this file (malloc(), calloc(), memalign(),
98 valloc(), or aligned_alloc()), releasing the memory to be potentially used
99 for any future allocations.
100
101 \param ptr A pointer to the block of memory to be freed.
102
103 \note Passing a NULL pointer to this function has no
104 effect.
105 \note Attempting to free the same block of memory twice
106 exhibits undefined behavior (i.e, it may crash, it
107 may do something else evil, etc).
108 \note Attempting to free a block of memory that was not
109 allocated by way of the normal allocation functions
110 exhibits undefined behavior.
111*/
112void free(void *ptr);
113
114/** \brief Change the size of a previously allocated block of memory.
115
116 This function changes the size of the previously allocated block of memory
117 from its current size to the specified size. This may involve reallocating
118 and copying the data from the original pointer to a new location in memory.
119
120 If this function returns non-NULL, the old pointer is considered invalid and
121 should not be used (unless, of course, the returned pointer is the same as
122 the old pointer). No action is needed to clean up the old pointer, as this
123 function will have freed it if necessary.
124
125 If this function returns NULL, the old pointer is still valid and has not
126 had its size changed.
127
128 \param ptr A pointer to the block of memory to resize. It must
129 have been previously allocated by way of one of the
130 memory allocation functions in this file (or must
131 be NULL).
132 \param size The requested size of the new block of memory.
133 \return A pointer to the newly allocated/resized block of
134 memory on success, or NULL on failure.
135
136 \note If ptr is NULL on a call to this function, the
137 function acts the same as malloc(size).
138 \note If ptr is non-NULL and size is zero, this function
139 *does not* free the block of memory -- it simply
140 returns a block that is of minimal size. By the
141 standard, this pointer must not be dereferenced.
142*/
143void *realloc(void *ptr, size_t size);
144
145/** \brief Allocate a block of memory aligned to a specified block size.
146
147 This function allocates a block of memory such that the lowest address in
148 the block is aligned to a multiple of alignment bytes. This is useful, for
149 instance, for things like DMA that require aligned blocks of memory to work.
150
151 Over-reliance on memalign will most certainly cause memory fragmentation, so
152 you should only use it when it is actually necessary.
153
154 \param alignment The alignment requested for the block of memory.
155 This must be a power-of-two.
156 \param size The number of bytes of memory to allocate.
157 \return A pointer to the newly allocated block of memory on
158 success, or NULL on failure.
159
160 \note All memory allocation functions will have their
161 blocks aligned on 8-byte boundaries. There is no
162 reason to call this function if you need less than
163 16-byte alignment.
164*/
165void *memalign(size_t alignment, size_t size);
166
167/** \brief Allocate a block of memory aligned to the system page size.
168
169 This function allocates a block of memory such that the lowest address in
170 the block is aligned to the system page size (typically 4096 bytes). It
171 basically ends up doing return memalign(PAGESIZE, size).
172
173 \param size The number of bytes of memory to allocate.
174 \return A pointer to the newly allocated block of memory on
175 success, or NULL on failure.
176
177 \see PAGESIZE
178 \see <arch/arch.h>
179*/
180void *valloc(size_t size);
181
182#if !defined(__STRICT_ANSI__) || (__STDC_VERSION__ >= 201112L)
183
184/** \brief Allocate memory aligned to a specified block size.
185
186 This function is the standard-compliant C11 method for allocating aligned
187 blocks of memory. It works mostly the same as memalign(), although the
188 size must be a multiple of the alignment.
189
190 \param alignment Required alignment of the memory block.
191 \param size Number of bytes of memory to allocate.
192 \return A pointer to the newly allocated memory block.
193*/
194void *aligned_alloc(size_t alignment, size_t size);
195
196#endif /* !defined(__STRICT_ANSI__) || (__STDC_VERSION__ >= 201112L) */
197
198/** \brief Sets tunable parameters for malloc related options.
199*/
201
202/* mallopt defines */
203#define M_MXFAST 1
204#define DEFAULT_MXFAST 64
205
206#define M_TRIM_THRESHOLD -1
207#define DEFAULT_TRIM_THRESHOLD (256*1024)
208
209#define M_TOP_PAD -2
210#define DEFAULT_TOP_PAD 0
211
212#define M_MMAP_THRESHOLD -3
213#define DEFAULT_MMAP_THRESHOLD (256*1024)
214
215#define M_MMAP_MAX -4
216#define DEFAULT_MMAP_MAX 65536
217int mallopt(int, int);
218
219/** \brief Debug function
220*/
221void malloc_stats(void);
222
223/** \brief Determine if it is safe to call malloc() in an IRQ context.
224
225 This function checks the value of the internal spinlock that is used for
226 malloc() to ensure that a call to it will not freeze the running process.
227 This is only really useful in an IRQ context to ensure that a call to
228 malloc() (or some other memory allocation function) won't cause a deadlock.
229
230 \retval 1 If it is safe to call malloc() in the current IRQ.
231 \retval 0 Otherwise.
232*/
234
235/** \brief Only available with KM_DBG
236*/
237int mem_check_block(void *p);
238
239/** \brief Only available with KM_DBG
240 */
242
243/** @} */
244
245__END_DECLS
246
247#endif /* __MALLOC_H */
void * memalign(size_t alignment, size_t size)
Allocate a block of memory aligned to a specified block size.
int mem_check_block(void *p)
Only available with KM_DBG.
void * calloc(size_t nmemb, size_t size)
Allocate a block of memory and initialize it to 0.
void * valloc(size_t size)
Allocate a block of memory aligned to the system page size.
void * aligned_alloc(size_t alignment, size_t size)
Allocate memory aligned to a specified block size.
int malloc_irq_safe(void)
Determine if it is safe to call malloc() in an IRQ context.
void * malloc(size_t size)
Allocate a block of memory.
void malloc_stats(void)
Debug function.
int mem_check_all(void)
Only available with KM_DBG.
void * realloc(void *ptr, size_t size)
Change the size of a previously allocated block of memory.
int mallopt(int, int)
void free(void *ptr)
Release a previously allocated block of memory.
ANSI C functions.
Definition malloc.h:36
int smblks
number of fastbin blocks
Definition malloc.h:42
int uordblks
total allocated space
Definition malloc.h:52
int fsmblks
space available in freed fastbin blocks
Definition malloc.h:50
int arena
non-mmapped space allocated from system
Definition malloc.h:38
int usmblks
maximum total allocated space
Definition malloc.h:48
int hblks
number of mmapped regions
Definition malloc.h:44
int ordblks
number of free chunks
Definition malloc.h:40
int fordblks
total free space
Definition malloc.h:54
int hblkhd
space in mmapped regions
Definition malloc.h:46
int keepcost
top-most, releasable (via malloc_trim) space
Definition malloc.h:56