4 * @brief Implicit free list implementation of malloc family, for kernel use.
6 * This version of code is however the simplest and yet insecured, thread unsafe
7 * it just to demonstrate how the malloc/free works behind the curtain
11 * @copyright Copyright (c) 2022
14 #include <lunaix/mm/kalloc.h>
15 #include <lunaix/mm/dmm.h>
17 #include <lunaix/common.h>
18 #include <lunaix/spike.h>
20 #include <klibc/string.h>
24 extern uint8_t __kernel_heap_start;
27 lx_malloc_internal(heap_context_t* heap, size_t size);
30 place_chunk(uint8_t* ptr, size_t size);
33 lx_free_internal(void* ptr);
36 coalesce(uint8_t* chunk_ptr);
39 lx_grow_heap(heap_context_t* heap, size_t sz);
42 At the beginning, we allocate an empty page and put our initial marker
48 Then, expand the heap further, with HEAP_INIT_SIZE (evaluated to 4096, i.e., 1 pg size)
49 This will allocate as much pages and override old epilogue marker with a free region hdr
50 and put new epilogue marker. These are handled by lx_grow_heap which is internally used
51 by alloc to expand the heap at many moment when needed.
53 | 4/1 | 4096/0 | ....... | 4096/0 | 0/1 |
57 Note: the brk always point to the beginning of epilogue.
60 static heap_context_t kheap;
64 kheap.start = &__kernel_heap_start;
66 kheap.max_addr = (void*)KSTACK_START;
68 if (!dmm_init(&kheap)) {
72 SW(kheap.start, PACK(4, M_ALLOCATED));
73 SW(kheap.start + WSIZE, PACK(0, M_ALLOCATED));
76 return lx_grow_heap(&kheap, HEAP_INIT_SIZE) != NULL;
80 lxmalloc(size_t size) {
81 mutex_lock(&kheap.lock);
82 void* r = lx_malloc_internal(&kheap, size);
83 mutex_unlock(&kheap.lock);
89 lxcalloc(size_t n, size_t elem) {
93 if (pd < elem || pd < n) {
97 void* ptr = lxmalloc(pd);
102 return memset(ptr, 0, pd);
110 mutex_lock(&kheap.lock);
112 uint8_t* chunk_ptr = (uint8_t*)ptr - WSIZE;
113 uint32_t hdr = LW(chunk_ptr);
114 size_t sz = CHUNK_S(hdr);
115 uint8_t* next_hdr = chunk_ptr + sz;
117 // make sure the ptr we are 'bout to free makes sense
118 // the size trick is stolen from glibc's malloc/malloc.c:4437 ;P
120 assert_msg(((uintptr_t)ptr < (uintptr_t)(-sz)) && !((uintptr_t)ptr & 0x3),
121 "free(): invalid pointer");
123 assert_msg(sz > WSIZE,
124 "free(): invalid size");
126 SW(chunk_ptr, hdr & ~M_ALLOCATED);
127 SW(FPTR(chunk_ptr, sz), hdr & ~M_ALLOCATED);
128 SW(next_hdr, LW(next_hdr) | M_PREV_FREE);
132 mutex_unlock(&kheap.lock);
137 lx_malloc_internal(heap_context_t* heap, size_t size)
139 // Simplest first fit approach.
145 uint8_t* ptr = heap->start;
146 // round to largest 4B aligned value
147 // and space for header
148 size = ROUNDUP(size + WSIZE, BOUNDARY);
149 while (ptr < (uint8_t*)heap->brk) {
150 uint32_t header = *((uint32_t*)ptr);
151 size_t chunk_size = CHUNK_S(header);
152 if (!chunk_size && CHUNK_A(header)) {
155 if (chunk_size >= size && !CHUNK_A(header)) {
157 place_chunk(ptr, size);
163 // if heap is full (seems to be!), then allocate more space (if it's
165 if ((ptr = lx_grow_heap(heap, size))) {
166 place_chunk(ptr, size);
170 // Well, we are officially OOM!
175 place_chunk(uint8_t* ptr, size_t size)
177 uint32_t header = *((uint32_t*)ptr);
178 size_t chunk_size = CHUNK_S(header);
179 *((uint32_t*)ptr) = PACK(size, CHUNK_PF(header) | M_ALLOCATED);
180 uint8_t* n_hdrptr = (uint8_t*)(ptr + size);
181 uint32_t diff = chunk_size - size;
184 // if the current free block is fully occupied
185 uint32_t n_hdr = LW(n_hdrptr);
186 // notify the next block about our avaliability
187 SW(n_hdrptr, n_hdr & ~0x2);
189 // if there is remaining free space left
190 uint32_t remainder_hdr = PACK(diff, M_NOT_ALLOCATED | M_PREV_ALLOCATED);
191 SW(n_hdrptr, remainder_hdr);
192 SW(FPTR(n_hdrptr, diff), remainder_hdr);
207 coalesce(uint8_t* chunk_ptr)
209 uint32_t hdr = LW(chunk_ptr);
210 uint32_t pf = CHUNK_PF(hdr);
211 uint32_t sz = CHUNK_S(hdr);
213 uint32_t n_hdr = LW(chunk_ptr + sz);
215 if (CHUNK_A(n_hdr) && pf) {
216 // case 1: prev is free
217 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
218 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
219 uint32_t new_hdr = PACK(prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
220 SW(chunk_ptr - prev_chunk_sz, new_hdr);
221 SW(FPTR(chunk_ptr, sz), new_hdr);
222 chunk_ptr -= prev_chunk_sz;
223 } else if (!CHUNK_A(n_hdr) && !pf) {
224 // case 2: next is free
225 size_t next_chunk_sz = CHUNK_S(n_hdr);
226 uint32_t new_hdr = PACK(next_chunk_sz + sz, pf);
227 SW(chunk_ptr, new_hdr);
228 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
229 } else if (!CHUNK_A(n_hdr) && pf) {
231 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
232 size_t next_chunk_sz = CHUNK_S(n_hdr);
233 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
235 PACK(next_chunk_sz + prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
236 SW(chunk_ptr - prev_chunk_sz, new_hdr);
237 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
238 chunk_ptr -= prev_chunk_sz;
241 // (fall through) case 4: prev and next are not free
247 lx_grow_heap(heap_context_t* heap, size_t sz)
251 // The "+ WSIZE" capture the overhead for epilogue marker
252 if (!(start = lxsbrk(heap, sz + WSIZE))) {
255 sz = ROUNDUP(sz, BOUNDARY);
257 // minus the overhead for epilogue, keep the invariant.
260 uint32_t old_marker = *((uint32_t*)start);
261 uint32_t free_hdr = PACK(sz, CHUNK_PF(old_marker));
263 SW(FPTR(start, sz), free_hdr);
264 SW(NEXT_CHK(start), PACK(0, M_ALLOCATED | M_PREV_FREE));
266 return coalesce(start);