@@ -13,6 +13,8 @@ static uint64_t allocated = 0;
1313
1414static uint32_t low_mem_cur = LOW_HEAP_START ;
1515
16+ static spinlock_t allocator_lock = 0 ;
17+
1618volatile struct limine_memmap_request memory_map_request = {
1719 .id = LIMINE_MEMMAP_REQUEST ,
1820 .revision = 0 ,
@@ -29,6 +31,8 @@ void init_heap() {
2931 uint64_t best_len = 0 ;
3032 uint64_t best_addr = 0 ;
3133
34+ init_spinlock (& allocator_lock );
35+
3236 for (uint64_t i = 0 ; i < memory_map_request .response -> entry_count ; ++ i ) {
3337 struct limine_memmap_entry * entry = memory_map_request .response -> entries [i ];
3438 if (entry -> type == LIMINE_MEMMAP_USABLE && entry -> length > best_len ) {
@@ -54,29 +58,35 @@ void print_heapinfo() {
5458}
5559
5660void * kmalloc (uint64_t size ) {
61+ lock_spinlock (& allocator_lock );
5762 void * p = ta_alloc (size );
5863 allocated += ta_usable_size ((void * )p );
64+ unlock_spinlock (& allocator_lock );
5965 return p ;
6066}
6167
6268void kfree (const void * ptr ) {
6369 if (!ptr ) {
6470 return ;
6571 }
72+ lock_spinlock (& allocator_lock );
6673 uintptr_t a = (uintptr_t )ptr ;
6774 if (a >= LOW_HEAP_START && a < LOW_HEAP_MAX ) {
6875 preboot_fail ("kfree: tried to free low heap memory - use kfree_low instead!" );
6976 }
7077 allocated -= ta_usable_size ((void * )ptr );
7178 ta_free ((void * )ptr );
79+ unlock_spinlock (& allocator_lock );
7280}
7381
7482uint32_t kmalloc_low (uint32_t size ) {
83+ lock_spinlock (& allocator_lock );
7584 uint32_t ret = low_mem_cur ;
7685 if (ret + size >= LOW_HEAP_MAX ) {
7786 preboot_fail ("kmalloc_low exhausted" );
7887 }
7988 low_mem_cur += size ;
89+ unlock_spinlock (& allocator_lock );
8090 return ret ;
8191}
8292
0 commit comments