4 * @brief Dynamic memory manager dedicated to kernel heap. It is not portable at
5 * this moment. Implicit free list implementation.
9 * @copyright Copyright (c) Lunaixsky 2022
13 // TODO: Make the dmm portable
15 #include <lunaix/mm/dmm.h>
16 #include <lunaix/mm/page.h>
17 #include <lunaix/mm/vmm.h>
19 #include <lunaix/constants.h>
20 #include <lunaix/spike.h>
22 #define M_ALLOCATED 0x1
23 #define M_PREV_FREE 0x2
25 #define M_NOT_ALLOCATED 0x0
26 #define M_PREV_ALLOCATED 0x0
28 #define CHUNK_S(header) ((header) & ~0x3)
29 #define CHUNK_PF(header) ((header)&M_PREV_FREE)
30 #define CHUNK_A(header) ((header)&M_ALLOCATED)
32 #define PACK(size, flags) (((size) & ~0x3) | (flags))
34 #define SW(p, w) (*((uint32_t*)(p)) = w)
35 #define LW(p) (*((uint32_t*)(p)))
37 #define HPTR(bp) ((uint32_t*)(bp)-1)
38 #define BPTR(bp) ((uint8_t*)(bp) + WSIZE)
39 #define FPTR(hp, size) ((uint32_t*)(hp + size - WSIZE))
40 #define NEXT_CHK(hp) ((uint8_t*)(hp) + CHUNK_S(LW(hp)))
45 extern uint8_t __kernel_heap_start;
47 void* current_heap_top = NULL;
50 coalesce(uint8_t* chunk_ptr);
53 lx_grow_heap(size_t sz);
55 void place_chunk(uint8_t* ptr, size_t size);
60 assert((uintptr_t)&__kernel_heap_start % BOUNDARY == 0);
62 current_heap_top = &__kernel_heap_start;
63 uint8_t* heap_start = &__kernel_heap_start;
65 vmm_alloc_page(current_heap_top, PG_PREM_RW);
67 SW(heap_start, PACK(4, M_ALLOCATED));
68 SW(heap_start + WSIZE, PACK(0, M_ALLOCATED));
69 current_heap_top += WSIZE;
71 return lx_grow_heap(HEAP_INIT_SIZE) != NULL;
77 return lxbrk(addr - current_heap_top) != NULL;
84 return current_heap_top;
87 // plus WSIZE is the overhead for epilogue marker
89 void* next = current_heap_top + ROUNDUP((uintptr_t)size, WSIZE);
91 if ((uintptr_t)next >= K_STACK_START) {
95 // Check the invariant
96 assert(size % BOUNDARY == 0)
98 uintptr_t heap_top_pg = PG_ALIGN(current_heap_top);
99 if (heap_top_pg != PG_ALIGN(next))
101 // if next do require new pages to be allocated
102 if (!vmm_alloc_pages((void*)(heap_top_pg + PG_SIZE), ROUNDUP(size, PG_SIZE), PG_PREM_RW)) {
108 void* old = current_heap_top;
109 current_heap_top = next - WSIZE;
114 lx_grow_heap(size_t sz) {
117 sz = ROUNDUP(sz, BOUNDARY);
118 if (!(start = lxbrk(sz))) {
122 uint32_t old_marker = *((uint32_t*)start);
123 uint32_t free_hdr = PACK(sz, CHUNK_PF(old_marker));
125 SW(FPTR(start, sz), free_hdr);
126 SW(NEXT_CHK(start), PACK(0, M_ALLOCATED | M_PREV_FREE));
128 return coalesce(start);
132 lx_malloc(size_t size)
134 // Simplest first fit approach.
136 uint8_t* ptr = &__kernel_heap_start;
137 // round to largest 4B aligned value
138 // and space for header
139 size = ROUNDUP(size, BOUNDARY) + WSIZE;
140 while (ptr < (uint8_t*)current_heap_top) {
141 uint32_t header = *((uint32_t*)ptr);
142 size_t chunk_size = CHUNK_S(header);
143 if (chunk_size >= size && !CHUNK_A(header)) {
145 place_chunk(ptr, size);
151 // if heap is full (seems to be!), then allocate more space (if it's okay...)
152 if ((ptr = lx_grow_heap(size))) {
153 place_chunk(ptr, size);
157 // Well, we are officially OOM!
161 void place_chunk(uint8_t* ptr, size_t size) {
162 uint32_t header = *((uint32_t*)ptr);
163 size_t chunk_size = CHUNK_S(header);
164 *((uint32_t*)ptr) = PACK(size, CHUNK_PF(header) | M_ALLOCATED);
165 uint8_t* n_hdrptr = (uint8_t*)(ptr + size);
166 uint32_t diff = chunk_size - size;
168 // if the current free block is fully occupied
169 uint32_t n_hdr = LW(n_hdrptr);
170 // notify the next block about our avaliability
171 SW(n_hdrptr, n_hdr & ~0x2);
173 // if there is remaining free space left
174 uint32_t remainder_hdr =
175 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 uint8_t* next_hdr = chunk_ptr + CHUNK_S(hdr);
194 SW(chunk_ptr, hdr & ~M_ALLOCATED);
195 SW(FPTR(chunk_ptr, CHUNK_S(hdr)), hdr & ~M_ALLOCATED);
196 SW(next_hdr, LW(next_hdr) | M_PREV_FREE);
202 coalesce(uint8_t* chunk_ptr)
204 uint32_t hdr = LW(chunk_ptr);
205 uint32_t pf = CHUNK_PF(hdr);
206 uint32_t sz = CHUNK_S(hdr);
208 uint32_t n_hdr = LW(chunk_ptr + sz);
210 if (CHUNK_A(n_hdr) && pf) {
211 // case 1: prev is free
212 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
213 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
214 uint32_t new_hdr = PACK(prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
215 SW(chunk_ptr - prev_chunk_sz, new_hdr);
216 SW(FPTR(chunk_ptr, sz), new_hdr);
217 chunk_ptr -= prev_chunk_sz;
218 } else if (!CHUNK_A(n_hdr) && !pf) {
219 // case 2: next is free
220 size_t next_chunk_sz = CHUNK_S(n_hdr);
221 uint32_t new_hdr = PACK(next_chunk_sz + sz, pf);
222 SW(chunk_ptr, new_hdr);
223 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
224 } else if (!CHUNK_A(n_hdr) && pf) {
226 uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
227 size_t next_chunk_sz = CHUNK_S(n_hdr);
228 size_t prev_chunk_sz = CHUNK_S(prev_ftr);
230 PACK(next_chunk_sz + prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
231 SW(chunk_ptr - prev_chunk_sz, new_hdr);
232 SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
233 chunk_ptr -= prev_chunk_sz;
236 // case 4: prev and next are not free