LittleKernel学习笔记(5)
heap_init
static inline void HEAP_INIT(void) {
    /* start the heap off with some spare memory in the page allocator */
    size_t len;
    void *ptr = page_first_alloc(&len);
    miniheap_init(ptr, len);
}
1 void miniheap_init(void *ptr, size_t len) { 2 LTRACEF("ptr %p, len %zu\n", ptr, len); 3 4 // create a mutex 5 mutex_init(&theheap.lock); 6 7 // initialize the free list 8 list_initialize(&theheap.free_list); 9 10 // align the buffer to a ptr 11 if (((uintptr_t)ptr % sizeof(uintptr_t)) > 0) { 12 uintptr_t aligned_ptr = ROUNDUP((uintptr_t)ptr, sizeof(uintptr_t)); 13 14 DEBUG_ASSERT((aligned_ptr - (uintptr_t)ptr) < len); 15 16 len -= aligned_ptr - (uintptr_t)ptr; 17 ptr = (void *)aligned_ptr; 18 19 LTRACEF("(aligned) ptr %p, len %zu\n", ptr, len); 20 } 21 22 // set the heap range 23 theheap.base = ptr; 24 theheap.len = len; 25 theheap.remaining = 0; // will get set by heap_insert_free_chunk() 26 theheap.low_watermark = 0; 27 28 // if passed a default range, use it 29 if (len > 0) 30 heap_insert_free_chunk(heap_create_free_chunk(ptr, len, true)); 31 }
 
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号