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