4 * @brief Dynamic memory manager dedicated to kernel heap. Using implicit free
5 * list implementation. This is designed to be portable, so it can serve as
6 * syscalls to malloc/free in the c std lib.
8 * This version of code is however the simplest and yet insecured,
9 * it just to demonstrate how the malloc/free works behind the stage
14 * @copyright Copyright (c) Lunaixsky 2022
18 #include <lunaix/mm/dmm.h>
19 #include <lunaix/mm/page.h>
20 #include <lunaix/mm/vmm.h>
22 #include <lunaix/constants.h>
23 #include <lunaix/spike.h>
25 #define M_ALLOCATED 0x1
26 #define M_PREV_FREE 0x2
28 #define M_NOT_ALLOCATED 0x0
29 #define M_PREV_ALLOCATED 0x0
31 #define CHUNK_S(header) ((header) & ~0x3)
32 #define CHUNK_PF(header) ((header)&M_PREV_FREE)
33 #define CHUNK_A(header) ((header)&M_ALLOCATED)
35 #define PACK(size, flags) (((size) & ~0x3) | (flags))
37 #define SW(p, w) (*((uint32_t*)(p)) = w)
38 #define LW(p) (*((uint32_t*)(p)))
40 #define HPTR(bp) ((uint32_t*)(bp)-1)
41 #define BPTR(bp) ((uint8_t*)(bp) + WSIZE)
42 #define FPTR(hp, size) ((uint32_t*)(hp + size - WSIZE))
43 #define NEXT_CHK(hp) ((uint8_t*)(hp) + CHUNK_S(LW(hp)))
49 coalesce(uint8_t* chunk_ptr);
52 lx_grow_heap(heap_context_t* heap, size_t sz);
55 place_chunk(uint8_t* ptr, size_t size);
58 dmm_init(heap_context_t* heap)
60 assert((uintptr_t)heap->start % BOUNDARY == 0);
62 heap->brk = heap->start;
64 vmm_alloc_page(heap->brk, PG_PREM_RW);
66 SW(heap->start, PACK(4, M_ALLOCATED));
67 SW(heap->start + WSIZE, PACK(0, M_ALLOCATED));
70 return lx_grow_heap(heap, HEAP_INIT_SIZE) != NULL;
74 lxsbrk(heap_context_t* heap, void* addr)
76 return lxbrk(heap, addr - heap->brk) != NULL;
80 lxbrk(heap_context_t* heap, size_t size)
86 // The upper bound of our next brk of heap given the size.
87 // This will be used to calculate the page we need to allocate.
88 // The "+ WSIZE" capture the overhead for epilogue marker
89 void* next = heap->brk + ROUNDUP(size + WSIZE, WSIZE);
91 if ((uintptr_t)next >= K_STACK_START) {
95 uintptr_t heap_top_pg = PG_ALIGN(heap->brk);
96 if (heap_top_pg != PG_ALIGN(next)) {
97 // if next do require new pages to be allocated
98 if (!vmm_alloc_pages((void*)(heap_top_pg + PG_SIZE),
99 ROUNDUP(size, PG_SIZE),
105 void* old = heap->brk;
111 lx_grow_heap(heap_context_t* heap, size_t sz)
115 if (!(start = lxbrk(heap, sz))) {
118 sz = ROUNDUP(sz, BOUNDARY);
120 uint32_t old_marker = *((uint32_t*)start);
121 uint32_t free_hdr = PACK(sz, CHUNK_PF(old_marker));
123 SW(FPTR(start, sz), free_hdr);
124 SW(NEXT_CHK(start), PACK(0, M_ALLOCATED | M_PREV_FREE));
126 return coalesce(start);
130 lx_malloc(heap_context_t* heap, size_t size)
132 // Simplest first fit approach.
134 uint8_t* ptr = heap->start;
135 // round to largest 4B aligned value
136 // and space for header
137 size = ROUNDUP(size, BOUNDARY) + WSIZE;
138 while (ptr < (uint8_t*)heap->brk) {
139 uint32_t header = *((uint32_t*)ptr);
140 size_t chunk_size = CHUNK_S(header);
141 if (chunk_size >= size && !CHUNK_A(header)) {
143 place_chunk(ptr, size);
149 // if heap is full (seems to be!), then allocate more space (if it's
151 if ((ptr = lx_grow_heap(heap, size))) {
152 place_chunk(ptr, size);
156 // Well, we are officially OOM!
161 place_chunk(uint8_t* ptr, size_t size)
163 uint32_t header = *((uint32_t*)ptr);
164 size_t chunk_size = CHUNK_S(header);
165 *((uint32_t*)ptr) = PACK(size, CHUNK_PF(header) | M_ALLOCATED);
166 uint8_t* n_hdrptr = (uint8_t*)(ptr + size);
167 uint32_t diff = chunk_size - size;
169 // if the current free block is fully occupied
170 uint32_t n_hdr = LW(n_hdrptr);
171 // notify the next block about our avaliability
172 SW(n_hdrptr, n_hdr & ~0x2);
174 // if there is remaining free space left
175 uint32_t remainder_hdr = PACK(diff, M_NOT_ALLOCATED | M_PREV_ALLOCATED);
176 SW(n_hdrptr, remainder_hdr);
177 SW(FPTR(n_hdrptr, diff), remainder_hdr);
190 uint8_t* chunk_ptr = (uint8_t*)ptr - WSIZE;
191 uint32_t hdr = LW(chunk_ptr);
192 size_t sz = CHUNK_S(hdr);
193 uint8_t* next_hdr = chunk_ptr + sz;
195 // make sure the ptr we are 'bout to free makes sense
196 // the size trick comes from:
197 // https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/malloc.c;h=1a1ac1d8f05b6f9bf295d7fdd0f12c2e4650a33c;hb=HEAD#l4437
198 assert_msg(((uintptr_t)ptr < (uintptr_t)(-sz)) && !((uintptr_t)ptr & ~0x3),
199 "free(): invalid pointer");
200 assert_msg(sz > WSIZE && (sz & ~0x3),
201 "free(): invalid size");
203 SW(chunk_ptr, hdr & ~M_ALLOCATED);
204 SW(FPTR(chunk_ptr, sz), hdr & ~M_ALLOCATED);
205 SW(next_hdr, LW(next_hdr) | M_PREV_FREE);
211 coalesce(uint8_t* chunk_ptr)
213 uint32_t hdr = LW(chunk_ptr);
214 uint32_t pf = CHUNK_PF(hdr);
215 uint32_t sz = CHUNK_S(hdr);
217 uint32_t n_hdr = LW(chunk_ptr + sz);
219 if (CHUNK_A(n_hdr) && pf) {
220 // case 1: prev is free
221 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
222 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
223 uint32_t new_hdr = PACK(prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
224 SW(chunk_ptr - prev_chunk_sz, new_hdr);
225 SW(FPTR(chunk_ptr, sz), new_hdr);
226 chunk_ptr -= prev_chunk_sz;
227 } else if (!CHUNK_A(n_hdr) && !pf) {
228 // case 2: next is free
229 size_t next_chunk_sz = CHUNK_S(n_hdr);
230 uint32_t new_hdr = PACK(next_chunk_sz + sz, pf);
231 SW(chunk_ptr, new_hdr);
232 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
233 } else if (!CHUNK_A(n_hdr) && pf) {
235 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
236 size_t next_chunk_sz = CHUNK_S(n_hdr);
237 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
239 PACK(next_chunk_sz + prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
240 SW(chunk_ptr - prev_chunk_sz, new_hdr);
241 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
242 chunk_ptr -= prev_chunk_sz;
245 // case 4: prev and next are not free