Add error value to commit

This commit is contained in:
gingerBill
2021-10-05 17:11:12 +01:00
parent 16ca677c1f
commit 455d64fbd4
4 changed files with 29 additions and 10 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ static_arena_init :: proc(arena: ^Static_Arena, reserved: uint, commit_size: uin
committed = min(committed, reserved) committed = min(committed, reserved)
ptr := raw_data(data) ptr := raw_data(data)
commit(ptr, uint(committed)) commit(ptr, uint(committed)) or_return
arena.block = memory_block_alloc(commit_size, reserved, {}) or_return arena.block = memory_block_alloc(commit_size, reserved, {}) or_return
arena.total_used = 0 arena.total_used = 0
+4 -4
View File
@@ -10,13 +10,13 @@ reserve :: proc(size: uint) -> (data: []byte, err: Allocator_Error) {
return _reserve(size) return _reserve(size)
} }
commit :: proc(data: rawptr, size: uint) { commit :: proc(data: rawptr, size: uint) -> Allocator_Error {
_commit(data, size) return _commit(data, size)
} }
reserve_and_commit :: proc(size: uint) -> (data: []byte, err: Allocator_Error) { reserve_and_commit :: proc(size: uint) -> (data: []byte, err: Allocator_Error) {
data = reserve(size) or_return data = reserve(size) or_return
commit(raw_data(data), size) commit(raw_data(data), size) or_return
return return
} }
@@ -82,7 +82,7 @@ memory_block_alloc :: proc(committed, reserved: uint, flags: Memory_Block_Flags)
pmblock := platform_memory_alloc(0, total_size) or_return pmblock := platform_memory_alloc(0, total_size) or_return
pmblock.block.base = ([^]byte)(uintptr(pmblock) + base_offset) pmblock.block.base = ([^]byte)(uintptr(pmblock) + base_offset)
commit(pmblock.block.base, committed) commit(pmblock.block.base, committed) or_return
// Should be zeroed // Should be zeroed
assert(pmblock.block.used == 0) assert(pmblock.block.used == 0)
assert(pmblock.block.prev == nil) assert(pmblock.block.prev == nil)
+8 -3
View File
@@ -67,7 +67,7 @@ madvise :: proc "contextless" (addr: rawptr, length: uint, advice: c.int) -> c.i
_reserve :: proc(size: uint) -> (data: []byte, err: Allocator_Error) { _reserve :: proc(size: uint) -> (data: []byte, err: Allocator_Error) {
MAP_FAILED := rawptr(uintptr(~0)) MAP_FAILED := rawptr(~uintptr(0))
result := mmap(nil, size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) result := mmap(nil, size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)
if result == MAP_FAILED { if result == MAP_FAILED {
return nil, .Out_Of_Memory return nil, .Out_Of_Memory
@@ -75,8 +75,13 @@ _reserve :: proc(size: uint) -> (data: []byte, err: Allocator_Error) {
return ([^]byte)(result)[:size], nil return ([^]byte)(result)[:size], nil
} }
_commit :: proc(data: rawptr, size: uint) { _commit :: proc(data: rawptr, size: uint) -> Allocator_Error {
mprotect(data, size, PROT_READ|PROT_WRITE) result := mprotect(data, size, PROT_READ|PROT_WRITE)
if result != 0 {
// TODO(bill): Handle error value correctly
return .Out_Of_Memory
}
return nil
} }
_decommit :: proc(data: rawptr, size: uint) { _decommit :: proc(data: rawptr, size: uint) {
mprotect(data, size, PROT_NONE) mprotect(data, size, PROT_NONE)
+16 -2
View File
@@ -50,11 +50,15 @@ PAGE_WRITECOPY :: 0x08
PAGE_TARGETS_INVALID :: 0x40000000 PAGE_TARGETS_INVALID :: 0x40000000
PAGE_TARGETS_NO_UPDATE :: 0x40000000 PAGE_TARGETS_NO_UPDATE :: 0x40000000
ERROR_INVALID_ADDRESS :: 487
@(default_calling_convention="stdcall")
foreign Kernel32 { foreign Kernel32 {
GetSystemInfo :: proc(lpSystemInfo: LPSYSTEM_INFO) --- GetSystemInfo :: proc(lpSystemInfo: LPSYSTEM_INFO) ---
VirtualAlloc :: proc(lpAddress: rawptr, dwSize: uint, flAllocationType: u32, flProtect: u32) -> rawptr --- VirtualAlloc :: proc(lpAddress: rawptr, dwSize: uint, flAllocationType: u32, flProtect: u32) -> rawptr ---
VirtualFree :: proc(lpAddress: rawptr, dwSize: uint, dwFreeType: u32) -> b32 --- VirtualFree :: proc(lpAddress: rawptr, dwSize: uint, dwFreeType: u32) -> b32 ---
VirtualProtect :: proc(lpAddress: rawptr, dwSize: uint, flNewProtect: u32, lpflOldProtect: ^u32) -> b32 --- VirtualProtect :: proc(lpAddress: rawptr, dwSize: uint, flNewProtect: u32, lpflOldProtect: ^u32) -> b32 ---
GetLastError :: proc() -> u32 ---
} }
@@ -68,8 +72,18 @@ _reserve :: proc(size: uint) -> (data: []byte, err: Allocator_Error) {
return return
} }
_commit :: proc(data: rawptr, size: uint) { _commit :: proc(data: rawptr, size: uint) -> Allocator_Error {
VirtualAlloc(data, size, MEM_COMMIT, PAGE_READWRITE) result := VirtualAlloc(data, size, MEM_COMMIT, PAGE_READWRITE)
if result == nil {
switch err := GetLastError(); err {
case ERROR_INVALID_ADDRESS:
return .Out_Of_Memory
}
// TODO(bill): Handle errors correctly
return .Invalid_Argument
}
return nil
} }
_decommit :: proc(data: rawptr, size: uint) { _decommit :: proc(data: rawptr, size: uint) {
VirtualFree(data, size, MEM_DECOMMIT) VirtualFree(data, size, MEM_DECOMMIT)