4 * @brief Dynamic memory manager dedicated to kernel heap. Using implicit free list implementation.
5 * This is designed to be portable, so it can serve as syscalls to malloc/free in the c std lib.
9 * @copyright Copyright (c) Lunaixsky 2022
14 #include <lunaix/mm/dmm.h>
15 #include <lunaix/mm/page.h>
16 #include <lunaix/mm/vmm.h>
18 #include <lunaix/constants.h>
19 #include <lunaix/spike.h>
21 #define M_ALLOCATED 0x1
22 #define M_PREV_FREE 0x2
24 #define M_NOT_ALLOCATED 0x0
25 #define M_PREV_ALLOCATED 0x0
27 #define CHUNK_S(header) ((header) & ~0x3)
28 #define CHUNK_PF(header) ((header)&M_PREV_FREE)
29 #define CHUNK_A(header) ((header)&M_ALLOCATED)
31 #define PACK(size, flags) (((size) & ~0x3) | (flags))
33 #define SW(p, w) (*((uint32_t*)(p)) = w)
34 #define LW(p) (*((uint32_t*)(p)))
36 #define HPTR(bp) ((uint32_t*)(bp)-1)
37 #define BPTR(bp) ((uint8_t*)(bp) + WSIZE)
38 #define FPTR(hp, size) ((uint32_t*)(hp + size - WSIZE))
39 #define NEXT_CHK(hp) ((uint8_t*)(hp) + CHUNK_S(LW(hp)))
45 coalesce(uint8_t* chunk_ptr);
48 lx_grow_heap(heap_context_t* heap, size_t sz);
50 void place_chunk(uint8_t* ptr, size_t size);
53 dmm_init(heap_context_t* heap)
55 assert((uintptr_t)heap->start % BOUNDARY == 0);
57 heap->brk = heap->start;
59 vmm_alloc_page(heap->brk, PG_PREM_RW);
61 SW(heap->start, PACK(4, M_ALLOCATED));
62 SW(heap->start + WSIZE, PACK(0, M_ALLOCATED));
65 return lx_grow_heap(heap, HEAP_INIT_SIZE) != NULL;
69 lxsbrk(heap_context_t* heap, void* addr)
71 return lxbrk(heap, addr - heap->brk) != NULL;
75 lxbrk(heap_context_t* heap, size_t size)
81 // plus WSIZE is the overhead for epilogue marker
83 void* next = heap->brk + ROUNDUP((uintptr_t)size, WSIZE);
85 if ((uintptr_t)next >= K_STACK_START) {
89 // Check the invariant
90 assert(size % BOUNDARY == 0)
92 uintptr_t heap_top_pg = PG_ALIGN(heap->brk);
93 if (heap_top_pg != PG_ALIGN(next))
95 // if next do require new pages to be allocated
96 if (!vmm_alloc_pages((void*)(heap_top_pg + PG_SIZE), ROUNDUP(size, PG_SIZE), PG_PREM_RW)) {
102 void* old = heap->brk;
103 heap->brk = next - WSIZE;
108 lx_grow_heap(heap_context_t* heap, size_t sz) {
111 sz = ROUNDUP(sz, BOUNDARY);
112 if (!(start = lxbrk(heap, sz))) {
116 uint32_t old_marker = *((uint32_t*)start);
117 uint32_t free_hdr = PACK(sz, CHUNK_PF(old_marker));
119 SW(FPTR(start, sz), free_hdr);
120 SW(NEXT_CHK(start), PACK(0, M_ALLOCATED | M_PREV_FREE));
122 return coalesce(start);
126 lx_malloc(heap_context_t* heap, size_t size)
128 // Simplest first fit approach.
130 uint8_t* ptr = heap->start;
131 // round to largest 4B aligned value
132 // and space for header
133 size = ROUNDUP(size, BOUNDARY) + WSIZE;
134 while (ptr < (uint8_t*)heap->brk) {
135 uint32_t header = *((uint32_t*)ptr);
136 size_t chunk_size = CHUNK_S(header);
137 if (chunk_size >= size && !CHUNK_A(header)) {
139 place_chunk(ptr, size);
145 // if heap is full (seems to be!), then allocate more space (if it's okay...)
146 if ((ptr = lx_grow_heap(heap, size))) {
147 place_chunk(ptr, size);
151 // Well, we are officially OOM!
155 void place_chunk(uint8_t* ptr, size_t size) {
156 uint32_t header = *((uint32_t*)ptr);
157 size_t chunk_size = CHUNK_S(header);
158 *((uint32_t*)ptr) = PACK(size, CHUNK_PF(header) | M_ALLOCATED);
159 uint8_t* n_hdrptr = (uint8_t*)(ptr + size);
160 uint32_t diff = chunk_size - size;
162 // if the current free block is fully occupied
163 uint32_t n_hdr = LW(n_hdrptr);
164 // notify the next block about our avaliability
165 SW(n_hdrptr, n_hdr & ~0x2);
167 // if there is remaining free space left
168 uint32_t remainder_hdr =
169 PACK(diff, M_NOT_ALLOCATED | M_PREV_ALLOCATED);
170 SW(n_hdrptr, remainder_hdr);
171 SW(FPTR(n_hdrptr, diff), remainder_hdr);
184 uint8_t* chunk_ptr = (uint8_t*)ptr - WSIZE;
185 uint32_t hdr = LW(chunk_ptr);
186 size_t sz = CHUNK_S(hdr);
187 uint8_t* next_hdr = chunk_ptr + sz;
189 SW(chunk_ptr, hdr & ~M_ALLOCATED);
190 SW(FPTR(chunk_ptr, sz), hdr & ~M_ALLOCATED);
191 SW(next_hdr, LW(next_hdr) | M_PREV_FREE);
197 coalesce(uint8_t* chunk_ptr)
199 uint32_t hdr = LW(chunk_ptr);
200 uint32_t pf = CHUNK_PF(hdr);
201 uint32_t sz = CHUNK_S(hdr);
203 uint32_t n_hdr = LW(chunk_ptr + sz);
205 if (CHUNK_A(n_hdr) && pf) {
206 // case 1: prev is free
207 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
208 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
209 uint32_t new_hdr = PACK(prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
210 SW(chunk_ptr - prev_chunk_sz, new_hdr);
211 SW(FPTR(chunk_ptr, sz), new_hdr);
212 chunk_ptr -= prev_chunk_sz;
213 } else if (!CHUNK_A(n_hdr) && !pf) {
214 // case 2: next is free
215 size_t next_chunk_sz = CHUNK_S(n_hdr);
216 uint32_t new_hdr = PACK(next_chunk_sz + sz, pf);
217 SW(chunk_ptr, new_hdr);
218 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
219 } else if (!CHUNK_A(n_hdr) && pf) {
221 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
222 size_t next_chunk_sz = CHUNK_S(n_hdr);
223 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
225 PACK(next_chunk_sz + prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
226 SW(chunk_ptr - prev_chunk_sz, new_hdr);
227 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
228 chunk_ptr -= prev_chunk_sz;
231 // case 4: prev and next are not free