mirror of
https://github.com/Alvin-Zilverstand/bob16.git
synced 2026-12-02 06:21:19 +01:00
;l
This commit is contained in:
1 parent
6dc841f956
commit
3d06f94e48
46 files changed
+5830
-543
No files matched your search
+68
-6
@@ -4,6 +4,25 @@
|
||||
#define BOB64_HEAP_ALIGNMENT 16u
|
||||
#define BOB64_HEAP_MINIMUM_REGION 4096u
|
||||
|
||||
static u64 heap_lock(BOB64_HEAP *heap) {
|
||||
u64 flags=0;
|
||||
#ifdef BOB64_UEFI_ABI
|
||||
__asm__ volatile("pushfq; pop %0; cli":"=r"(flags)::"memory");
|
||||
#endif
|
||||
while(__atomic_exchange_n(&heap->Lock,1,__ATOMIC_ACQUIRE))
|
||||
__asm__ volatile("pause" ::: "memory");
|
||||
return flags;
|
||||
}
|
||||
|
||||
static void heap_unlock(BOB64_HEAP *heap,u64 flags) {
|
||||
__atomic_store_n(&heap->Lock,0,__ATOMIC_RELEASE);
|
||||
#ifdef BOB64_UEFI_ABI
|
||||
if(flags&(1ull<<9))__asm__ volatile("sti" ::: "memory");
|
||||
#else
|
||||
(void)flags;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int align_size(usize value,usize *aligned) {
|
||||
if(value>~(usize)0-(BOB64_HEAP_ALIGNMENT-1))return 0;
|
||||
*aligned=(value+(BOB64_HEAP_ALIGNMENT-1))&~(usize)(BOB64_HEAP_ALIGNMENT-1);
|
||||
@@ -25,10 +44,20 @@ int bob64_heap_init(BOB64_HEAP *heap,usize maximum_bytes,
|
||||
BOB64_HEAP_GROW grow,void *context) {
|
||||
if(!heap||!grow||maximum_bytes<BOB64_HEAP_MINIMUM_REGION)return -1;
|
||||
heap->First=0;heap->Last=0;heap->Grow=grow;heap->Context=context;
|
||||
heap->Shrink=0;
|
||||
heap->Base=0;heap->MappedBytes=0;heap->MaximumBytes=maximum_bytes;
|
||||
__atomic_store_n(&heap->Lock,0,__ATOMIC_RELAXED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void bob64_heap_set_shrink(BOB64_HEAP *heap,BOB64_HEAP_SHRINK shrink) {
|
||||
if(heap) {
|
||||
u64 flags=heap_lock(heap);
|
||||
heap->Shrink=shrink;
|
||||
heap_unlock(heap,flags);
|
||||
}
|
||||
}
|
||||
|
||||
static BOB64_HEAP_BLOCK *find_block(BOB64_HEAP *heap,usize size) {
|
||||
for(BOB64_HEAP_BLOCK *block=heap->First;block;block=block->Next)
|
||||
if(block->Magic==BOB64_HEAP_BLOCK_MAGIC&&block->Free&&block->Size>=size)
|
||||
@@ -61,11 +90,31 @@ static int grow_heap(BOB64_HEAP *heap) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void shrink_heap_tail(BOB64_HEAP *heap) {
|
||||
usize bytes,maximum;
|
||||
void *region;
|
||||
if(!heap||!heap->Shrink||!heap->Last||!heap->Last->Free||
|
||||
heap->MappedBytes<=BOB64_HEAP_MINIMUM_REGION)return;
|
||||
bytes=heap->Last->Size&~(usize)(BOB64_HEAP_MINIMUM_REGION-1);
|
||||
maximum=(heap->MappedBytes-BOB64_HEAP_MINIMUM_REGION)&
|
||||
~(usize)(BOB64_HEAP_MINIMUM_REGION-1);
|
||||
if(bytes>maximum)bytes=maximum;
|
||||
if(!bytes)return;
|
||||
region=(u8 *)heap->Base+heap->MappedBytes-bytes;
|
||||
if(heap->Shrink(heap->Context,region,bytes))return;
|
||||
heap->Last->Size-=bytes;
|
||||
heap->MappedBytes-=bytes;
|
||||
}
|
||||
|
||||
void *bob64_heap_alloc(BOB64_HEAP *heap,usize size) {
|
||||
usize aligned;
|
||||
if(!heap||!heap->Grow||!size||!align_size(size,&aligned))return 0;
|
||||
u64 flags=heap_lock(heap);
|
||||
BOB64_HEAP_BLOCK *block;
|
||||
while(!(block=find_block(heap,aligned)))if(grow_heap(heap))return 0;
|
||||
while(!(block=find_block(heap,aligned)))if(grow_heap(heap)) {
|
||||
heap_unlock(heap,flags);
|
||||
return 0;
|
||||
}
|
||||
if(block->Size>=sizeof(*block)+BOB64_HEAP_ALIGNMENT&&
|
||||
aligned<=block->Size-sizeof(*block)-BOB64_HEAP_ALIGNMENT) {
|
||||
BOB64_HEAP_BLOCK *remainder=(BOB64_HEAP_BLOCK *)((u8 *)(block+1)+aligned);
|
||||
@@ -78,6 +127,7 @@ void *bob64_heap_alloc(BOB64_HEAP *heap,usize size) {
|
||||
block->Next=remainder;block->Size=aligned;
|
||||
}
|
||||
block->Free=0;
|
||||
heap_unlock(heap,flags);
|
||||
return block+1;
|
||||
}
|
||||
|
||||
@@ -96,15 +146,20 @@ void *bob64_heap_calloc(BOB64_HEAP *heap,usize count,usize size) {
|
||||
int bob64_heap_free(BOB64_HEAP *heap,void *pointer) {
|
||||
uintptr_t address=(uintptr_t)pointer,base,end;
|
||||
BOB64_HEAP_BLOCK *block;
|
||||
if(!heap||!pointer||!heap->Base||(address&(BOB64_HEAP_ALIGNMENT-1)))return -1;
|
||||
if(!heap||!pointer||(address&(BOB64_HEAP_ALIGNMENT-1)))return -1;
|
||||
u64 flags=heap_lock(heap);
|
||||
if(!heap->Base) {heap_unlock(heap,flags);return -1;}
|
||||
base=(uintptr_t)heap->Base;
|
||||
if(heap->MappedBytes>~(uintptr_t)0-base)return -1;
|
||||
if(heap->MappedBytes>~(uintptr_t)0-base) {heap_unlock(heap,flags);return -1;}
|
||||
end=base+heap->MappedBytes;
|
||||
if(address<base+sizeof(*block)||address>=end)return -1;
|
||||
if(address<base+sizeof(*block)||address>=end) {heap_unlock(heap,flags);return -1;}
|
||||
block=(BOB64_HEAP_BLOCK *)pointer-1;
|
||||
if((uintptr_t)block<base||(uintptr_t)block+sizeof(*block)>end||
|
||||
block->Magic!=BOB64_HEAP_BLOCK_MAGIC||block->Free||
|
||||
block->Size>(usize)(end-(uintptr_t)(block+1)))return -1;
|
||||
block->Size>(usize)(end-(uintptr_t)(block+1))) {
|
||||
heap_unlock(heap,flags);
|
||||
return -1;
|
||||
}
|
||||
block->Free=1;
|
||||
if(block->Next&&block->Next->Magic==BOB64_HEAP_BLOCK_MAGIC&&block->Next->Free) {
|
||||
BOB64_HEAP_BLOCK *next=block->Next;
|
||||
@@ -120,9 +175,16 @@ int bob64_heap_free(BOB64_HEAP *heap,void *pointer) {
|
||||
else heap->Last=previous;
|
||||
block->Magic=0;
|
||||
}
|
||||
shrink_heap_tail(heap);
|
||||
heap_unlock(heap,flags);
|
||||
return 0;
|
||||
}
|
||||
|
||||
usize bob64_heap_mapped_bytes(const BOB64_HEAP *heap) {
|
||||
return heap?heap->MappedBytes:0;
|
||||
usize result;
|
||||
if(!heap)return 0;
|
||||
u64 flags=heap_lock((BOB64_HEAP *)heap);
|
||||
result=heap->MappedBytes;
|
||||
heap_unlock((BOB64_HEAP *)heap,flags);
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user