X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/48b4a227035048fdebcd32532deb7a857c6199ac..2e21eb2f39dd80aa166216381d5d402be943686e:/lunaix-os/kernel/mm/kalloc.c diff --git a/lunaix-os/kernel/mm/kalloc.c b/lunaix-os/kernel/mm/kalloc.c index 91905b4..4d9a5d1 100644 --- a/lunaix-os/kernel/mm/kalloc.c +++ b/lunaix-os/kernel/mm/kalloc.c @@ -57,27 +57,32 @@ lx_grow_heap(heap_context_t* heap, size_t sz); Note: the brk always point to the beginning of epilogue. */ +static heap_context_t kheap; + int kalloc_init() { - heap_context_t* kheap = &__current->mm.k_heap; - kheap->start = &__kernel_heap_start; - kheap->brk = NULL; - kheap->max_addr = (void*)KSTACK_START; + kheap.start = &__kernel_heap_start; + kheap.brk = NULL; + kheap.max_addr = (void*)KSTACK_START; - if (!dmm_init(kheap)) { + if (!dmm_init(&kheap)) { return 0; } - SW(kheap->start, PACK(4, M_ALLOCATED)); - SW(kheap->start + WSIZE, PACK(0, M_ALLOCATED)); - kheap->brk += WSIZE; + SW(kheap.start, PACK(4, M_ALLOCATED)); + SW(kheap.start + WSIZE, PACK(0, M_ALLOCATED)); + kheap.brk += WSIZE; - return lx_grow_heap(kheap, HEAP_INIT_SIZE) != NULL; + return lx_grow_heap(&kheap, HEAP_INIT_SIZE) != NULL; } void* lxmalloc(size_t size) { - return lx_malloc_internal(&__current->mm.k_heap, size); + mutex_lock(&kheap.lock); + void* r = lx_malloc_internal(&kheap, size); + mutex_unlock(&kheap.lock); + + return r; } void* @@ -102,6 +107,7 @@ lxfree(void* ptr) { if (!ptr) { return; } + mutex_lock(&kheap.lock); uint8_t* chunk_ptr = (uint8_t*)ptr - WSIZE; uint32_t hdr = LW(chunk_ptr); @@ -122,6 +128,8 @@ lxfree(void* ptr) { SW(next_hdr, LW(next_hdr) | M_PREV_FREE); coalesce(chunk_ptr); + + mutex_unlock(&kheap.lock); }