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.
62 heap_context_t* kheap = &__current->mm.k_heap;
63 kheap->start = &__kernel_heap_start;
65 kheap->max_addr = (void*)KSTACK_START;
67 if (!dmm_init(kheap)) {
71 SW(kheap->start, PACK(4, M_ALLOCATED));
72 SW(kheap->start + WSIZE, PACK(0, M_ALLOCATED));
75 return lx_grow_heap(kheap, HEAP_INIT_SIZE) != NULL;
79 lxmalloc(size_t size) {
80 return lx_malloc_internal(&__current->mm.k_heap, size);
84 lxcalloc(size_t n, size_t elem) {
88 if (pd < elem || pd < n) {
92 void* ptr = lxmalloc(pd);
97 return memset(ptr, 0, pd);
106 uint8_t* chunk_ptr = (uint8_t*)ptr - WSIZE;
107 uint32_t hdr = LW(chunk_ptr);
108 size_t sz = CHUNK_S(hdr);
109 uint8_t* next_hdr = chunk_ptr + sz;
111 // make sure the ptr we are 'bout to free makes sense
112 // the size trick is stolen from glibc's malloc/malloc.c:4437 ;P
114 assert_msg(((uintptr_t)ptr < (uintptr_t)(-sz)) && !((uintptr_t)ptr & 0x3),
115 "free(): invalid pointer");
117 assert_msg(sz > WSIZE,
118 "free(): invalid size");
120 SW(chunk_ptr, hdr & ~M_ALLOCATED);
121 SW(FPTR(chunk_ptr, sz), hdr & ~M_ALLOCATED);
122 SW(next_hdr, LW(next_hdr) | M_PREV_FREE);
129 lx_malloc_internal(heap_context_t* heap, size_t size)
131 // Simplest first fit approach.
137 uint8_t* ptr = heap->start;
138 // round to largest 4B aligned value
139 // and space for header
140 size = ROUNDUP(size + WSIZE, BOUNDARY);
141 while (ptr < (uint8_t*)heap->brk) {
142 uint32_t header = *((uint32_t*)ptr);
143 size_t chunk_size = CHUNK_S(header);
144 if (!chunk_size && CHUNK_A(header)) {
147 if (chunk_size >= size && !CHUNK_A(header)) {
149 place_chunk(ptr, size);
155 // if heap is full (seems to be!), then allocate more space (if it's
157 if ((ptr = lx_grow_heap(heap, size))) {
158 place_chunk(ptr, size);
162 // Well, we are officially OOM!
167 place_chunk(uint8_t* ptr, size_t size)
169 uint32_t header = *((uint32_t*)ptr);
170 size_t chunk_size = CHUNK_S(header);
171 *((uint32_t*)ptr) = PACK(size, CHUNK_PF(header) | M_ALLOCATED);
172 uint8_t* n_hdrptr = (uint8_t*)(ptr + size);
173 uint32_t diff = chunk_size - size;
176 // if the current free block is fully occupied
177 uint32_t n_hdr = LW(n_hdrptr);
178 // notify the next block about our avaliability
179 SW(n_hdrptr, n_hdr & ~0x2);
181 // if there is remaining free space left
182 uint32_t remainder_hdr = PACK(diff, M_NOT_ALLOCATED | M_PREV_ALLOCATED);
183 SW(n_hdrptr, remainder_hdr);
184 SW(FPTR(n_hdrptr, diff), remainder_hdr);
199 coalesce(uint8_t* chunk_ptr)
201 uint32_t hdr = LW(chunk_ptr);
202 uint32_t pf = CHUNK_PF(hdr);
203 uint32_t sz = CHUNK_S(hdr);
205 uint32_t n_hdr = LW(chunk_ptr + sz);
207 if (CHUNK_A(n_hdr) && pf) {
208 // case 1: prev is free
209 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
210 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
211 uint32_t new_hdr = PACK(prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
212 SW(chunk_ptr - prev_chunk_sz, new_hdr);
213 SW(FPTR(chunk_ptr, sz), new_hdr);
214 chunk_ptr -= prev_chunk_sz;
215 } else if (!CHUNK_A(n_hdr) && !pf) {
216 // case 2: next is free
217 size_t next_chunk_sz = CHUNK_S(n_hdr);
218 uint32_t new_hdr = PACK(next_chunk_sz + sz, pf);
219 SW(chunk_ptr, new_hdr);
220 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
221 } else if (!CHUNK_A(n_hdr) && pf) {
223 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
224 size_t next_chunk_sz = CHUNK_S(n_hdr);
225 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
227 PACK(next_chunk_sz + prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
228 SW(chunk_ptr - prev_chunk_sz, new_hdr);
229 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
230 chunk_ptr -= prev_chunk_sz;
233 // (fall through) case 4: prev and next are not free
239 lx_grow_heap(heap_context_t* heap, size_t sz)
243 // The "+ WSIZE" capture the overhead for epilogue marker
244 if (!(start = lxbrk(heap, sz + WSIZE))) {
247 sz = ROUNDUP(sz, BOUNDARY);
249 // minus the overhead for epilogue, keep the invariant.
252 uint32_t old_marker = *((uint32_t*)start);
253 uint32_t free_hdr = PACK(sz, CHUNK_PF(old_marker));
255 SW(FPTR(start, sz), free_hdr);
256 SW(NEXT_CHK(start), PACK(0, M_ALLOCATED | M_PREV_FREE));
258 return coalesce(start);