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;
26 // FIXME: This should go to PCB once we're started to support multitasking
27 static heap_context_t __kalloc_kheap;
30 lx_malloc_internal(heap_context_t* heap, size_t size);
33 place_chunk(uint8_t* ptr, size_t size);
36 lx_free_internal(void* ptr);
39 coalesce(uint8_t* chunk_ptr);
42 lx_grow_heap(heap_context_t* heap, size_t sz);
45 At the beginning, we allocate an empty page and put our initial marker
51 Then, expand the heap further, with HEAP_INIT_SIZE (evaluated to 4096, i.e., 1 pg size)
52 This will allocate as much pages and override old epilogue marker with a free region hdr
53 and put new epilogue marker. These are handled by lx_grow_heap which is internally used
54 by alloc to expand the heap at many moment when needed.
56 | 4/1 | 4096/0 | ....... | 4096/0 | 0/1 |
60 Note: the brk always point to the beginning of epilogue.
65 __kalloc_kheap.start = &__kernel_heap_start;
66 __kalloc_kheap.brk = NULL;
67 __kalloc_kheap.max_addr = (void*)K_STACK_START;
69 if (!dmm_init(&__kalloc_kheap)) {
73 SW(__kalloc_kheap.start, PACK(4, M_ALLOCATED));
74 SW(__kalloc_kheap.start + WSIZE, PACK(0, M_ALLOCATED));
75 __kalloc_kheap.brk += WSIZE;
77 return lx_grow_heap(&__kalloc_kheap, HEAP_INIT_SIZE) != NULL;
81 lxmalloc(size_t size) {
82 return lx_malloc_internal(&__kalloc_kheap, size);
86 lxcalloc(size_t n, size_t elem) {
90 if (pd < elem || pd < n) {
94 void* ptr = lxmalloc(pd);
99 return memset(ptr, 0, pd);
108 uint8_t* chunk_ptr = (uint8_t*)ptr - WSIZE;
109 uint32_t hdr = LW(chunk_ptr);
110 size_t sz = CHUNK_S(hdr);
111 uint8_t* next_hdr = chunk_ptr + sz;
113 // make sure the ptr we are 'bout to free makes sense
114 // the size trick is stolen from glibc's malloc/malloc.c:4437 ;P
116 assert_msg(((uintptr_t)ptr < (uintptr_t)(-sz)) && !((uintptr_t)ptr & 0x3),
117 "free(): invalid pointer");
119 assert_msg(sz > WSIZE,
120 "free(): invalid size");
122 SW(chunk_ptr, hdr & ~M_ALLOCATED);
123 SW(FPTR(chunk_ptr, sz), hdr & ~M_ALLOCATED);
124 SW(next_hdr, LW(next_hdr) | M_PREV_FREE);
131 lx_malloc_internal(heap_context_t* heap, size_t size)
133 // Simplest first fit approach.
139 uint8_t* ptr = heap->start;
140 // round to largest 4B aligned value
141 // and space for header
142 size = ROUNDUP(size + WSIZE, BOUNDARY);
143 while (ptr < (uint8_t*)heap->brk) {
144 uint32_t header = *((uint32_t*)ptr);
145 size_t chunk_size = CHUNK_S(header);
146 if (!chunk_size && CHUNK_A(header)) {
149 if (chunk_size >= size && !CHUNK_A(header)) {
151 place_chunk(ptr, size);
157 // if heap is full (seems to be!), then allocate more space (if it's
159 if ((ptr = lx_grow_heap(heap, size))) {
160 place_chunk(ptr, size);
164 // Well, we are officially OOM!
169 place_chunk(uint8_t* ptr, size_t size)
171 uint32_t header = *((uint32_t*)ptr);
172 size_t chunk_size = CHUNK_S(header);
173 *((uint32_t*)ptr) = PACK(size, CHUNK_PF(header) | M_ALLOCATED);
174 uint8_t* n_hdrptr = (uint8_t*)(ptr + size);
175 uint32_t diff = chunk_size - size;
178 // if the current free block is fully occupied
179 uint32_t n_hdr = LW(n_hdrptr);
180 // notify the next block about our avaliability
181 SW(n_hdrptr, n_hdr & ~0x2);
183 // if there is remaining free space left
184 uint32_t remainder_hdr = PACK(diff, M_NOT_ALLOCATED | M_PREV_ALLOCATED);
185 SW(n_hdrptr, remainder_hdr);
186 SW(FPTR(n_hdrptr, diff), remainder_hdr);
201 coalesce(uint8_t* chunk_ptr)
203 uint32_t hdr = LW(chunk_ptr);
204 uint32_t pf = CHUNK_PF(hdr);
205 uint32_t sz = CHUNK_S(hdr);
207 uint32_t n_hdr = LW(chunk_ptr + sz);
209 if (CHUNK_A(n_hdr) && pf) {
210 // case 1: prev is free
211 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
212 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
213 uint32_t new_hdr = PACK(prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
214 SW(chunk_ptr - prev_chunk_sz, new_hdr);
215 SW(FPTR(chunk_ptr, sz), new_hdr);
216 chunk_ptr -= prev_chunk_sz;
217 } else if (!CHUNK_A(n_hdr) && !pf) {
218 // case 2: next is free
219 size_t next_chunk_sz = CHUNK_S(n_hdr);
220 uint32_t new_hdr = PACK(next_chunk_sz + sz, pf);
221 SW(chunk_ptr, new_hdr);
222 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
223 } else if (!CHUNK_A(n_hdr) && pf) {
225 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
226 size_t next_chunk_sz = CHUNK_S(n_hdr);
227 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
229 PACK(next_chunk_sz + prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
230 SW(chunk_ptr - prev_chunk_sz, new_hdr);
231 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
232 chunk_ptr -= prev_chunk_sz;
235 // (fall through) case 4: prev and next are not free
241 lx_grow_heap(heap_context_t* heap, size_t sz)
245 // The "+ WSIZE" capture the overhead for epilogue marker
246 if (!(start = lxbrk(heap, sz + WSIZE))) {
249 sz = ROUNDUP(sz, BOUNDARY);
251 // minus the overhead for epilogue, keep the invariant.
254 uint32_t old_marker = *((uint32_t*)start);
255 uint32_t free_hdr = PACK(sz, CHUNK_PF(old_marker));
257 SW(FPTR(start, sz), free_hdr);
258 SW(NEXT_CHK(start), PACK(0, M_ALLOCATED | M_PREV_FREE));
260 return coalesce(start);