Go to the source code of this file.
◆ _MEM_POOL_ALIGN4
#define _MEM_POOL_ALIGN4 |
( |
|
__name, |
|
|
|
__sz |
|
) |
| __attribute__((aligned(4))) u8 __name[__sz] = {0} |
◆ MEM_Allocator
◆ MEM_Alloc()
Allocate a block of memory from the pool with at least 'size' bytes.
- Parameters
-
ma | Pointer to the memory allocator. |
size | Size of the block to allocate. |
- Returns
- Pointer to the allocated memory or NULL if no block is available.
Definition at line 52 of file allocator.c.
53{
55 return NULL;
56
57
59
62 while (current_blk)
63 {
64 if (current_blk->
free && current_blk->
size >= size)
65 {
66
67
68
69 if (current_blk->
size >= size + min_split_size)
70 {
71
75 .free = true,
76 .next = current_blk->
next,
77 };
78
79
80 current_blk->
size = size;
81 current_blk->
free =
false;
82 current_blk->
next = new_blk;
83 }
84 else
85 current_blk->
free =
false;
86
89 }
90
91 current_blk = current_blk->
next;
92 }
93
94
95 return NULL;
96}
_PRIVATE MEM_Allocator sAllocator
◆ MEM_Free()
Free a previously allocated block.
- Parameters
-
ma | Pointer to the memory allocator. |
ptr | Pointer to the block to free. |
Definition at line 98 of file allocator.c.
99{
101 return;
102
103
107
108
110 {
113 }
114
115
116
118 while (current_blk && current_blk != blk)
119 {
120 prev = current_blk;
121 current_blk = current_blk->
next;
122 }
123
124 if (prev && prev->free)
125 {
127 prev->next = blk->
next;
128 }
129}
◆ MEM_Init()
Creates a new allocator from the given memory pool.
- Parameters
-
pool | Pointer to the memory pool. |
pool_size | Size of the memory pool in bytes. |
- Returns
- Pointer to the new allocator or NULL on error.
Definition at line 31 of file allocator.c.
32{
34 return NULL;
35
39 .pool_used = 0,
41 };
42
46
48}
◆ MEM_Realloc()
Reallocate a previously allocated block to a new size.
- Parameters
-
ma | Pointer to the memory allocator. |
ptr | Pointer to the block to reallocate. If NULL, acts like MEM_Alloc(). |
new_size | New size of the block. If 0, acts like MEM_Free(); if less than the current size, shrinks the block; if greater, allocates a new block, copies the data, and frees the old block. |
- Returns
- Pointer to the reallocated block or NULL on error.
Definition at line 131 of file allocator.c.
132{
134 return NULL;
135
136 if (!new_size)
137 {
138
140 return NULL;
141 }
142
143 if (!ptr)
145
146
148
149
151 if (new_size <= blk->size)
152 {
153
154
156 if (blk->
size >= new_size + min_split_thresh)
157 {
158
162 .free = true,
164 };
165
166 blk->
size = new_size;
168 }
169
170 return ptr;
171 }
172 else
173 {
174
176 if (!new_ptr)
177 return NULL;
178
179 memcpy(new_ptr, ptr, blk->
size);
181 return new_ptr;
182 }
183}
void MEM_Free(MEM_Allocator *ma, void *ptr)
Free a previously allocated block.
void * MEM_Alloc(MEM_Allocator *ma, u32 size)
Allocate a block of memory from the pool with at least 'size' bytes.