36 lines
917 B
C
36 lines
917 B
C
function S32 os_main();
|
|
const SizeU page_size = 4096;
|
|
|
|
function SizeU
|
|
get_align_offset(SizeU size, SizeU align){
|
|
SizeU mask = align - 1;
|
|
SizeU val = size & mask;
|
|
if(val){
|
|
val = align - val;
|
|
}
|
|
return val;
|
|
}
|
|
|
|
function SizeU
|
|
align_up(SizeU size, SizeU align){
|
|
SizeU result = size + get_align_offset(size, align);
|
|
return result;
|
|
}
|
|
|
|
function OS_Memory
|
|
os_reserve(SizeU size){
|
|
OS_Memory result = {0};
|
|
SizeU adjusted_size = align_up(size, page_size);
|
|
result.data = VirtualAlloc(0, adjusted_size, MEM_RESERVE, PAGE_READWRITE);
|
|
assert_msg(result.data, "Failed to reserve virtual memory");
|
|
result.reserve = adjusted_size;
|
|
return result;
|
|
}
|
|
|
|
function void
|
|
os_commit(OS_Memory *m, SizeU size){
|
|
SizeU commit = align_up(size, page_size);
|
|
void *p = VirtualAlloc((U8 *)m->data + m->commit, commit, MEM_COMMIT, PAGE_READWRITE);
|
|
assert_msg(p, "Failed to commit more memory");
|
|
m->commit += commit;
|
|
} |