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;
68 kheap.start = KHEAP_START;
71 (void*)PROC_START; // 在新的布局中,堆结束的地方即为进程表开始的地方
73 for (size_t i = 0; i < KHEAP_SIZE_MB >> 2; i++) {
74 vmm_set_mapping(PD_REFERENCED,
75 (uintptr_t)kheap.start + (i << 22),
81 if (!dmm_init(&kheap)) {
85 SW(kheap.start, PACK(4, M_ALLOCATED));
86 SW(kheap.start + WSIZE, PACK(0, M_ALLOCATED));
89 return lx_grow_heap(&kheap, HEAP_INIT_SIZE) != NULL;
95 mutex_lock(&kheap.lock);
96 void* r = lx_malloc_internal(&kheap, size);
97 mutex_unlock(&kheap.lock);
103 lxcalloc(size_t n, size_t elem)
105 size_t pd = n * elem;
107 // overflow detection
108 if (pd < elem || pd < n) {
112 void* ptr = lxmalloc(pd);
117 return memset(ptr, 0, pd);
126 mutex_lock(&kheap.lock);
128 uint8_t* chunk_ptr = (uint8_t*)ptr - WSIZE;
129 uint32_t hdr = LW(chunk_ptr);
130 size_t sz = CHUNK_S(hdr);
131 uint8_t* next_hdr = chunk_ptr + sz;
133 // make sure the ptr we are 'bout to free makes sense
134 // the size trick is stolen from glibc's malloc/malloc.c:4437 ;P
136 assert_msg(((uintptr_t)ptr < (uintptr_t)(-sz)) && !((uintptr_t)ptr & 0x3),
137 "free(): invalid pointer");
139 assert_msg(sz > WSIZE, "free(): invalid size");
141 SW(chunk_ptr, hdr & ~M_ALLOCATED);
142 SW(FPTR(chunk_ptr, sz), hdr & ~M_ALLOCATED);
143 SW(next_hdr, LW(next_hdr) | M_PREV_FREE);
147 mutex_unlock(&kheap.lock);
151 lx_malloc_internal(heap_context_t* heap, size_t size)
153 // Simplest first fit approach.
159 uint8_t* ptr = heap->start;
160 // round to largest 4B aligned value
161 // and space for header
162 size = ROUNDUP(size + WSIZE, BOUNDARY);
163 while (ptr < (uint8_t*)heap->brk) {
164 uint32_t header = *((uint32_t*)ptr);
165 size_t chunk_size = CHUNK_S(header);
166 if (!chunk_size && CHUNK_A(header)) {
169 if (chunk_size >= size && !CHUNK_A(header)) {
171 place_chunk(ptr, size);
177 // if heap is full (seems to be!), then allocate more space (if it's
179 if ((ptr = lx_grow_heap(heap, size))) {
180 place_chunk(ptr, size);
184 // Well, we are officially OOM!
189 place_chunk(uint8_t* ptr, size_t size)
191 uint32_t header = *((uint32_t*)ptr);
192 size_t chunk_size = CHUNK_S(header);
193 *((uint32_t*)ptr) = PACK(size, CHUNK_PF(header) | M_ALLOCATED);
194 uint8_t* n_hdrptr = (uint8_t*)(ptr + size);
195 uint32_t diff = chunk_size - size;
198 // if the current free block is fully occupied
199 uint32_t n_hdr = LW(n_hdrptr);
200 // notify the next block about our avaliability
201 SW(n_hdrptr, n_hdr & ~0x2);
203 // if there is remaining free space left
204 uint32_t remainder_hdr = PACK(diff, M_NOT_ALLOCATED | M_PREV_ALLOCATED);
205 SW(n_hdrptr, remainder_hdr);
206 SW(FPTR(n_hdrptr, diff), remainder_hdr);
221 coalesce(uint8_t* chunk_ptr)
223 uint32_t hdr = LW(chunk_ptr);
224 uint32_t pf = CHUNK_PF(hdr);
225 uint32_t sz = CHUNK_S(hdr);
227 uint32_t n_hdr = LW(chunk_ptr + sz);
229 if (CHUNK_A(n_hdr) && pf) {
230 // case 1: prev is free
231 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
232 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
233 uint32_t new_hdr = PACK(prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
234 SW(chunk_ptr - prev_chunk_sz, new_hdr);
235 SW(FPTR(chunk_ptr, sz), new_hdr);
236 chunk_ptr -= prev_chunk_sz;
237 } else if (!CHUNK_A(n_hdr) && !pf) {
238 // case 2: next is free
239 size_t next_chunk_sz = CHUNK_S(n_hdr);
240 uint32_t new_hdr = PACK(next_chunk_sz + sz, pf);
241 SW(chunk_ptr, new_hdr);
242 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
243 } else if (!CHUNK_A(n_hdr) && pf) {
245 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
246 size_t next_chunk_sz = CHUNK_S(n_hdr);
247 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
249 PACK(next_chunk_sz + prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
250 SW(chunk_ptr - prev_chunk_sz, new_hdr);
251 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
252 chunk_ptr -= prev_chunk_sz;
255 // (fall through) case 4: prev and next are not free
260 lx_grow_heap(heap_context_t* heap, size_t sz)
264 // The "+ WSIZE" capture the overhead for epilogue marker
265 if (!(start = lxsbrk(heap, sz + WSIZE, 0))) {
268 sz = ROUNDUP(sz, BOUNDARY);
270 // minus the overhead for epilogue, keep the invariant.
273 uint32_t old_marker = *((uint32_t*)start);
274 uint32_t free_hdr = PACK(sz, CHUNK_PF(old_marker));
276 SW(FPTR(start, sz), free_hdr);
277 SW(NEXT_CHK(start), PACK(0, M_ALLOCATED | M_PREV_FREE));
279 return coalesce(start);