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/dmm.h>
15 #include <lunaix/mm/kalloc.h>
16 #include <lunaix/mm/vmm.h>
18 #include <lunaix/common.h>
19 #include <lunaix/spike.h>
21 #include <klibc/string.h>
25 extern uint8_t __kernel_heap_start;
28 lx_malloc_internal(heap_context_t* heap, size_t size);
31 place_chunk(uint8_t* ptr, size_t size);
34 lx_free_internal(void* ptr);
37 coalesce(uint8_t* chunk_ptr);
40 lx_grow_heap(heap_context_t* heap, size_t sz);
43 At the beginning, we allocate an empty page and put our initial marker
49 Then, expand the heap further, with HEAP_INIT_SIZE (evaluated to 4096, i.e.,
50 1 pg size) This will allocate as much pages and override old epilogue marker
51 with a free region hdr and put new epilogue marker. These are handled by
52 lx_grow_heap which is internally used by alloc to expand the heap at many
55 | 4/1 | 4096/0 | ....... | 4096/0 | 0/1 |
59 Note: the brk always point to the beginning of epilogue.
62 // FIXME: This should be per-process but not global!
63 static heap_context_t kheap;
65 #define KHEAP_SIZE_MB 256
70 kheap.start = &__kernel_heap_start;
72 kheap.max_addr = (void*)((uintptr_t)kheap.start + (KHEAP_SIZE_MB << 20));
74 for (size_t i = 0; i < KHEAP_SIZE_MB >> 2; i++) {
75 vmm_set_mapping(PD_REFERENCED,
76 (uintptr_t)kheap.start + (i << 22),
82 if (!dmm_init(&kheap)) {
86 SW(kheap.start, PACK(4, M_ALLOCATED));
87 SW(kheap.start + WSIZE, PACK(0, M_ALLOCATED));
90 return lx_grow_heap(&kheap, HEAP_INIT_SIZE) != NULL;
96 mutex_lock(&kheap.lock);
97 void* r = lx_malloc_internal(&kheap, size);
98 mutex_unlock(&kheap.lock);
104 lxcalloc(size_t n, size_t elem)
106 size_t pd = n * elem;
108 // overflow detection
109 if (pd < elem || pd < n) {
113 void* ptr = lxmalloc(pd);
118 return memset(ptr, 0, pd);
127 mutex_lock(&kheap.lock);
129 uint8_t* chunk_ptr = (uint8_t*)ptr - WSIZE;
130 uint32_t hdr = LW(chunk_ptr);
131 size_t sz = CHUNK_S(hdr);
132 uint8_t* next_hdr = chunk_ptr + sz;
134 // make sure the ptr we are 'bout to free makes sense
135 // the size trick is stolen from glibc's malloc/malloc.c:4437 ;P
137 assert_msg(((uintptr_t)ptr < (uintptr_t)(-sz)) && !((uintptr_t)ptr & 0x3),
138 "free(): invalid pointer");
140 assert_msg(sz > WSIZE, "free(): invalid size");
142 SW(chunk_ptr, hdr & ~M_ALLOCATED);
143 SW(FPTR(chunk_ptr, sz), hdr & ~M_ALLOCATED);
144 SW(next_hdr, LW(next_hdr) | M_PREV_FREE);
148 mutex_unlock(&kheap.lock);
152 lx_malloc_internal(heap_context_t* heap, size_t size)
154 // Simplest first fit approach.
160 uint8_t* ptr = heap->start;
161 // round to largest 4B aligned value
162 // and space for header
163 size = ROUNDUP(size + WSIZE, BOUNDARY);
164 while (ptr < (uint8_t*)heap->brk) {
165 uint32_t header = *((uint32_t*)ptr);
166 size_t chunk_size = CHUNK_S(header);
167 if (!chunk_size && CHUNK_A(header)) {
170 if (chunk_size >= size && !CHUNK_A(header)) {
172 place_chunk(ptr, size);
178 // if heap is full (seems to be!), then allocate more space (if it's
180 if ((ptr = lx_grow_heap(heap, size))) {
181 place_chunk(ptr, size);
185 // Well, we are officially OOM!
190 place_chunk(uint8_t* ptr, size_t size)
192 uint32_t header = *((uint32_t*)ptr);
193 size_t chunk_size = CHUNK_S(header);
194 *((uint32_t*)ptr) = PACK(size, CHUNK_PF(header) | M_ALLOCATED);
195 uint8_t* n_hdrptr = (uint8_t*)(ptr + size);
196 uint32_t diff = chunk_size - size;
199 // if the current free block is fully occupied
200 uint32_t n_hdr = LW(n_hdrptr);
201 // notify the next block about our avaliability
202 SW(n_hdrptr, n_hdr & ~0x2);
204 // if there is remaining free space left
205 uint32_t remainder_hdr = PACK(diff, M_NOT_ALLOCATED | M_PREV_ALLOCATED);
206 SW(n_hdrptr, remainder_hdr);
207 SW(FPTR(n_hdrptr, diff), remainder_hdr);
222 coalesce(uint8_t* chunk_ptr)
224 uint32_t hdr = LW(chunk_ptr);
225 uint32_t pf = CHUNK_PF(hdr);
226 uint32_t sz = CHUNK_S(hdr);
228 uint32_t n_hdr = LW(chunk_ptr + sz);
230 if (CHUNK_A(n_hdr) && pf) {
231 // case 1: prev is free
232 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
233 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
234 uint32_t new_hdr = PACK(prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
235 SW(chunk_ptr - prev_chunk_sz, new_hdr);
236 SW(FPTR(chunk_ptr, sz), new_hdr);
237 chunk_ptr -= prev_chunk_sz;
238 } else if (!CHUNK_A(n_hdr) && !pf) {
239 // case 2: next is free
240 size_t next_chunk_sz = CHUNK_S(n_hdr);
241 uint32_t new_hdr = PACK(next_chunk_sz + sz, pf);
242 SW(chunk_ptr, new_hdr);
243 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
244 } else if (!CHUNK_A(n_hdr) && pf) {
246 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
247 size_t next_chunk_sz = CHUNK_S(n_hdr);
248 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
250 PACK(next_chunk_sz + prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
251 SW(chunk_ptr - prev_chunk_sz, new_hdr);
252 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
253 chunk_ptr -= prev_chunk_sz;
256 // (fall through) case 4: prev and next are not free
261 lx_grow_heap(heap_context_t* heap, size_t sz)
265 // The "+ WSIZE" capture the overhead for epilogue marker
266 if (!(start = lxsbrk(heap, sz + WSIZE, 0))) {
269 sz = ROUNDUP(sz, BOUNDARY);
271 // minus the overhead for epilogue, keep the invariant.
274 uint32_t old_marker = *((uint32_t*)start);
275 uint32_t free_hdr = PACK(sz, CHUNK_PF(old_marker));
277 SW(FPTR(start, sz), free_hdr);
278 SW(NEXT_CHK(start), PACK(0, M_ALLOCATED | M_PREV_FREE));
280 return coalesce(start);