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 static heap_context_t kheap;
67 kheap.start = KHEAP_START;
70 (void*)PROC_START; // 在新的布局中,堆结束的地方即为进程表开始的地方
72 for (size_t i = 0; i < KHEAP_SIZE_MB >> 2; i++) {
73 vmm_set_mapping(PD_REFERENCED,
74 (uintptr_t)kheap.start + (i << 22),
80 if (!dmm_init(&kheap)) {
84 SW(kheap.start, PACK(4, M_ALLOCATED));
85 SW(kheap.start + WSIZE, PACK(0, M_ALLOCATED));
88 return lx_grow_heap(&kheap, HEAP_INIT_SIZE) != NULL;
94 mutex_lock(&kheap.lock);
95 void* r = lx_malloc_internal(&kheap, size);
96 mutex_unlock(&kheap.lock);
102 lxcalloc(size_t n, size_t elem)
104 size_t pd = n * elem;
106 // overflow detection
107 if (pd < elem || pd < n) {
111 void* ptr = lxmalloc(pd);
116 return memset(ptr, 0, pd);
125 mutex_lock(&kheap.lock);
127 uint8_t* chunk_ptr = (uint8_t*)ptr - WSIZE;
128 uint32_t hdr = LW(chunk_ptr);
129 size_t sz = CHUNK_S(hdr);
130 uint8_t* next_hdr = chunk_ptr + sz;
132 // make sure the ptr we are 'bout to free makes sense
133 // the size trick is stolen from glibc's malloc/malloc.c:4437 ;P
135 assert_msg(((uintptr_t)ptr < (uintptr_t)(-sz)) && !((uintptr_t)ptr & 0x3),
136 "free(): invalid pointer");
138 assert_msg(sz > WSIZE, "free(): invalid size");
140 SW(chunk_ptr, hdr & ~M_ALLOCATED);
141 SW(FPTR(chunk_ptr, sz), hdr & ~M_ALLOCATED);
142 SW(next_hdr, LW(next_hdr) | M_PREV_FREE);
146 mutex_unlock(&kheap.lock);
150 lx_malloc_internal(heap_context_t* heap, size_t size)
152 // Simplest first fit approach.
158 uint8_t* ptr = heap->start;
159 // round to largest 4B aligned value
160 // and space for header
161 size = ROUNDUP(size + WSIZE, BOUNDARY);
162 while (ptr < (uint8_t*)heap->brk) {
163 uint32_t header = *((uint32_t*)ptr);
164 size_t chunk_size = CHUNK_S(header);
165 if (!chunk_size && CHUNK_A(header)) {
168 if (chunk_size >= size && !CHUNK_A(header)) {
170 place_chunk(ptr, size);
176 // if heap is full (seems to be!), then allocate more space (if it's
178 if ((ptr = lx_grow_heap(heap, size))) {
179 place_chunk(ptr, size);
183 // Well, we are officially OOM!
188 place_chunk(uint8_t* ptr, size_t size)
190 uint32_t header = *((uint32_t*)ptr);
191 size_t chunk_size = CHUNK_S(header);
192 *((uint32_t*)ptr) = PACK(size, CHUNK_PF(header) | M_ALLOCATED);
193 uint8_t* n_hdrptr = (uint8_t*)(ptr + size);
194 uint32_t diff = chunk_size - size;
197 // if the current free block is fully occupied
198 uint32_t n_hdr = LW(n_hdrptr);
199 // notify the next block about our avaliability
200 SW(n_hdrptr, n_hdr & ~0x2);
202 // if there is remaining free space left
203 uint32_t remainder_hdr = PACK(diff, M_NOT_ALLOCATED | M_PREV_ALLOCATED);
204 SW(n_hdrptr, remainder_hdr);
205 SW(FPTR(n_hdrptr, diff), remainder_hdr);
220 coalesce(uint8_t* chunk_ptr)
222 uint32_t hdr = LW(chunk_ptr);
223 uint32_t pf = CHUNK_PF(hdr);
224 uint32_t sz = CHUNK_S(hdr);
226 uint32_t n_hdr = LW(chunk_ptr + sz);
228 if (CHUNK_A(n_hdr) && pf) {
229 // case 1: prev is free
230 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
231 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
232 uint32_t new_hdr = PACK(prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
233 SW(chunk_ptr - prev_chunk_sz, new_hdr);
234 SW(FPTR(chunk_ptr, sz), new_hdr);
235 chunk_ptr -= prev_chunk_sz;
236 } else if (!CHUNK_A(n_hdr) && !pf) {
237 // case 2: next is free
238 size_t next_chunk_sz = CHUNK_S(n_hdr);
239 uint32_t new_hdr = PACK(next_chunk_sz + sz, pf);
240 SW(chunk_ptr, new_hdr);
241 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
242 } else if (!CHUNK_A(n_hdr) && pf) {
244 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
245 size_t next_chunk_sz = CHUNK_S(n_hdr);
246 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
248 PACK(next_chunk_sz + prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
249 SW(chunk_ptr - prev_chunk_sz, new_hdr);
250 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
251 chunk_ptr -= prev_chunk_sz;
254 // (fall through) case 4: prev and next are not free
259 lx_grow_heap(heap_context_t* heap, size_t sz)
263 // The "+ WSIZE" capture the overhead for epilogue marker
264 if (!(start = lxsbrk(heap, sz + WSIZE, 0))) {
267 sz = ROUNDUP(sz, BOUNDARY);
269 // minus the overhead for epilogue, keep the invariant.
272 uint32_t old_marker = *((uint32_t*)start);
273 uint32_t free_hdr = PACK(sz, CHUNK_PF(old_marker));
275 SW(FPTR(start, sz), free_hdr);
276 SW(NEXT_CHK(start), PACK(0, M_ALLOCATED | M_PREV_FREE));
278 return coalesce(start);