Removing heap allocations, Porting to Unix
This commit is contained in:
@@ -1,27 +1,28 @@
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Memory
|
||||
//-----------------------------------------------------------------------------
|
||||
const SizeU os_page_size = 4096;
|
||||
const size_t os_page_size = 4096;
|
||||
|
||||
function OS_Memory
|
||||
os_reserve(SizeU size){
|
||||
os_reserve(size_t size){
|
||||
OS_Memory result = {};
|
||||
SizeU adjusted_size = align_up(size, os_page_size);
|
||||
size_t adjusted_size = align_up(size, os_page_size);
|
||||
result.data = (U8*)VirtualAlloc(0, adjusted_size, MEM_RESERVE, PAGE_READWRITE);
|
||||
assert_msg(result.data, "Failed to reserve virtual memory");
|
||||
assert_message(result.data, "Failed to reserve virtual memory");
|
||||
result.reserve = adjusted_size;
|
||||
return result;
|
||||
}
|
||||
|
||||
function B32
|
||||
os_commit(OS_Memory *m, SizeU size){
|
||||
SizeU commit = align_up(size, os_page_size);
|
||||
SizeU total_commit = m->commit + commit;
|
||||
os_commit(OS_Memory *m, size_t size){
|
||||
size_t commit = align_up(size, os_page_size);
|
||||
size_t total_commit = m->commit + commit;
|
||||
total_commit = clamp_top(total_commit, m->reserve);
|
||||
SizeU adjusted_commit = total_commit - m->commit;
|
||||
size_t adjusted_commit = total_commit - m->commit;
|
||||
if(adjusted_commit != 0){
|
||||
void *result = VirtualAlloc((U8*)m->data + m->commit, adjusted_commit, MEM_COMMIT, PAGE_READWRITE);
|
||||
assert_msg(result, "Failed to commit more memory");
|
||||
assert_message(result, "Failed to commit more memory");
|
||||
m->commit += adjusted_commit;
|
||||
return true;
|
||||
}
|
||||
@@ -31,7 +32,7 @@ os_commit(OS_Memory *m, SizeU size){
|
||||
function void
|
||||
os_release(OS_Memory *m){
|
||||
BOOL result = VirtualFree(m->data, 0, MEM_RELEASE);
|
||||
assert_msg(result != 0, "Failed to release OS_Memory");
|
||||
assert_message(result != 0, "Failed to release OS_Memory");
|
||||
if(result){
|
||||
m->data = 0;
|
||||
m->commit = 0;
|
||||
@@ -40,10 +41,10 @@ os_release(OS_Memory *m){
|
||||
}
|
||||
|
||||
function B32
|
||||
os_decommit_pos(OS_Memory *m, SizeU pos){
|
||||
SizeU aligned = align_down(pos, os_page_size);
|
||||
SizeU adjusted_pos = clamp_top(aligned, m->commit);
|
||||
SizeU size_to_decommit = m->commit - adjusted_pos;
|
||||
os_decommit_pos(OS_Memory *m, size_t pos){
|
||||
size_t aligned = align_down(pos, os_page_size);
|
||||
size_t adjusted_pos = clamp_top(aligned, m->commit);
|
||||
size_t size_to_decommit = m->commit - adjusted_pos;
|
||||
if(size_to_decommit){
|
||||
U8 *base_address = m->data + adjusted_pos;
|
||||
BOOL result = VirtualFree(base_address, size_to_decommit, MEM_DECOMMIT);
|
||||
@@ -247,3 +248,56 @@ os_list_dir(Allocator *a, String dir, U32 flags = LIST_NO_FLAGS){
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// OS Heap allocator
|
||||
//-----------------------------------------------------------------------------
|
||||
struct OS_Heap:Allocator{
|
||||
void *handle;
|
||||
};
|
||||
|
||||
function void *
|
||||
os_heap_allocator_proc(Allocator *a, Allocation_Kind kind, void *old_pointer, size_t size){
|
||||
OS_Heap *heap = (OS_Heap *)a;
|
||||
switch(kind){
|
||||
case Allocation_FreeAll:{
|
||||
invalid_codepath;
|
||||
return 0;
|
||||
}
|
||||
case Allocation_Destroy:{
|
||||
BOOL result = HeapDestroy(heap->handle);
|
||||
assert(result != 0);
|
||||
heap->handle = 0;
|
||||
heap->proc = 0;
|
||||
return 0;
|
||||
}
|
||||
case Allocation_Free:{
|
||||
BOOL result = HeapFree(heap->handle, 0, old_pointer);
|
||||
assert(result != 0);
|
||||
return 0;
|
||||
}
|
||||
case Allocation_Alloc:{
|
||||
void *result = HeapAlloc(heap->handle, 0, size);
|
||||
assert(result);
|
||||
return result;
|
||||
}
|
||||
case Allocation_Resize:{
|
||||
void *result = HeapReAlloc(heap->handle, 0, old_pointer, size);
|
||||
assert(result);
|
||||
return result;
|
||||
}
|
||||
default: invalid_codepath;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function OS_Heap // max_size == 0 == growing heap
|
||||
win32_os_heap_create(B32 multithreaded, size_t initial_size, size_t max_size, String debug_name){
|
||||
OS_Heap result = {};
|
||||
result.debug_name = debug_name;
|
||||
result.proc = os_heap_allocator_proc;
|
||||
result.kind = Allocator_OSHeap;
|
||||
result.handle = HeapCreate(multithreaded ? 0 : HEAP_NO_SERIALIZE, initial_size, max_size);
|
||||
assert(result.handle);
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user