Use contextless where possible

This commit is contained in:
gingerBill
2022-03-01 15:52:14 +00:00
parent 18607e53cb
commit 7e5342f41f
4 changed files with 20 additions and 20 deletions
+7 -7
View File
@@ -6,25 +6,25 @@ DEFAULT_PAGE_SIZE := uint(4096)
Allocator_Error :: mem.Allocator_Error Allocator_Error :: mem.Allocator_Error
reserve :: proc(size: uint) -> (data: []byte, err: Allocator_Error) { reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) {
return _reserve(size) return _reserve(size)
} }
commit :: proc(data: rawptr, size: uint) -> Allocator_Error { commit :: proc "contextless" (data: rawptr, size: uint) -> Allocator_Error {
return _commit(data, size) return _commit(data, size)
} }
reserve_and_commit :: proc(size: uint) -> (data: []byte, err: Allocator_Error) { reserve_and_commit :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) {
data = reserve(size) or_return data = reserve(size) or_return
commit(raw_data(data), size) or_return commit(raw_data(data), size) or_return
return return
} }
decommit :: proc(data: rawptr, size: uint) { decommit :: proc "contextless" (data: rawptr, size: uint) {
_decommit(data, size) _decommit(data, size)
} }
release :: proc(data: rawptr, size: uint) { release :: proc "contextless" (data: rawptr, size: uint) {
_release(data, size) _release(data, size)
} }
@@ -36,7 +36,7 @@ Protect_Flag :: enum u32 {
Protect_Flags :: distinct bit_set[Protect_Flag; u32] Protect_Flags :: distinct bit_set[Protect_Flag; u32]
Protect_No_Access :: Protect_Flags{} Protect_No_Access :: Protect_Flags{}
protect :: proc(data: rawptr, size: uint, flags: Protect_Flags) -> bool { protect :: proc "contextless" (data: rawptr, size: uint, flags: Protect_Flags) -> bool {
return _protect(data, size, flags) return _protect(data, size, flags)
} }
@@ -107,7 +107,7 @@ memory_block_alloc :: proc(committed, reserved: uint, flags: Memory_Block_Flags)
} }
alloc_from_memory_block :: proc(block: ^Memory_Block, min_size, alignment: int) -> (data: []byte, err: Allocator_Error) { alloc_from_memory_block :: proc(block: ^Memory_Block, min_size, alignment: int) -> (data: []byte, err: Allocator_Error) {
calc_alignment_offset :: proc(block: ^Memory_Block, alignment: uintptr) -> uint { calc_alignment_offset :: proc "contextless" (block: ^Memory_Block, alignment: uintptr) -> uint {
alignment_offset := uint(0) alignment_offset := uint(0)
ptr := uintptr(block.base[block.used:]) ptr := uintptr(block.base[block.used:])
mask := alignment-1 mask := alignment-1
+5 -5
View File
@@ -58,7 +58,7 @@ madvise :: proc "contextless" (addr: rawptr, length: uint, advice: c.int) -> c.i
} }
_reserve :: proc(size: uint) -> (data: []byte, err: Allocator_Error) { _reserve :: proc "contextless" (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 {
@@ -67,7 +67,7 @@ _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) -> Allocator_Error { _commit :: proc "contextless" (data: rawptr, size: uint) -> Allocator_Error {
result := mprotect(data, size, PROT_READ|PROT_WRITE) result := mprotect(data, size, PROT_READ|PROT_WRITE)
if result != 0 { if result != 0 {
// TODO(bill): Handle error value correctly // TODO(bill): Handle error value correctly
@@ -75,14 +75,14 @@ _commit :: proc(data: rawptr, size: uint) -> Allocator_Error {
} }
return nil return nil
} }
_decommit :: proc(data: rawptr, size: uint) { _decommit :: proc "contextless" (data: rawptr, size: uint) {
mprotect(data, size, PROT_NONE) mprotect(data, size, PROT_NONE)
madvise(data, size, MADV_FREE) madvise(data, size, MADV_FREE)
} }
_release :: proc(data: rawptr, size: uint) { _release :: proc "contextless" (data: rawptr, size: uint) {
munmap(data, size) munmap(data, size)
} }
_protect :: proc(data: rawptr, size: uint, flags: Protect_Flags) -> bool { _protect :: proc "contextless" (data: rawptr, size: uint, flags: Protect_Flags) -> bool {
pflags: c.int pflags: c.int
pflags = PROT_NONE pflags = PROT_NONE
if .Read in flags { pflags |= PROT_READ } if .Read in flags { pflags |= PROT_READ }
+3 -3
View File
@@ -10,7 +10,7 @@ Platform_Memory_Block :: struct {
prev, next: ^Platform_Memory_Block, prev, next: ^Platform_Memory_Block,
} }
platform_memory_alloc :: proc(to_commit, to_reserve: uint) -> (block: ^Platform_Memory_Block, err: Allocator_Error) { platform_memory_alloc :: proc "contextless" (to_commit, to_reserve: uint) -> (block: ^Platform_Memory_Block, err: Allocator_Error) {
to_commit, to_reserve := to_commit, to_reserve to_commit, to_reserve := to_commit, to_reserve
to_reserve = max(to_commit, to_reserve) to_reserve = max(to_commit, to_reserve)
@@ -27,7 +27,7 @@ platform_memory_alloc :: proc(to_commit, to_reserve: uint) -> (block: ^Platform_
} }
platform_memory_free :: proc(block: ^Platform_Memory_Block) { platform_memory_free :: proc "contextless" (block: ^Platform_Memory_Block) {
if block != nil { if block != nil {
release(block, block.reserved) release(block, block.reserved)
} }
@@ -55,7 +55,7 @@ platform_memory_init :: proc() {
} }
} }
platform_memory_commit :: proc(block: ^Platform_Memory_Block, to_commit: uint) -> (err: Allocator_Error) { platform_memory_commit :: proc "contextless" (block: ^Platform_Memory_Block, to_commit: uint) -> (err: Allocator_Error) {
if to_commit < block.committed { if to_commit < block.committed {
return nil return nil
} }
+5 -5
View File
@@ -62,7 +62,7 @@ foreign Kernel32 {
} }
_reserve :: proc(size: uint) -> (data: []byte, err: Allocator_Error) { _reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) {
result := VirtualAlloc(nil, size, MEM_RESERVE, PAGE_READWRITE) result := VirtualAlloc(nil, size, MEM_RESERVE, PAGE_READWRITE)
if result == nil { if result == nil {
err = .Out_Of_Memory err = .Out_Of_Memory
@@ -72,7 +72,7 @@ _reserve :: proc(size: uint) -> (data: []byte, err: Allocator_Error) {
return return
} }
_commit :: proc(data: rawptr, size: uint) -> Allocator_Error { _commit :: proc "contextless" (data: rawptr, size: uint) -> Allocator_Error {
result := VirtualAlloc(data, size, MEM_COMMIT, PAGE_READWRITE) result := VirtualAlloc(data, size, MEM_COMMIT, PAGE_READWRITE)
if result == nil { if result == nil {
switch err := GetLastError(); err { switch err := GetLastError(); err {
@@ -85,13 +85,13 @@ _commit :: proc(data: rawptr, size: uint) -> Allocator_Error {
} }
return nil return nil
} }
_decommit :: proc(data: rawptr, size: uint) { _decommit :: proc "contextless" (data: rawptr, size: uint) {
VirtualFree(data, size, MEM_DECOMMIT) VirtualFree(data, size, MEM_DECOMMIT)
} }
_release :: proc(data: rawptr, size: uint) { _release :: proc "contextless" (data: rawptr, size: uint) {
VirtualFree(data, 0, MEM_RELEASE) VirtualFree(data, 0, MEM_RELEASE)
} }
_protect :: proc(data: rawptr, size: uint, flags: Protect_Flags) -> bool { _protect :: proc "contextless" (data: rawptr, size: uint, flags: Protect_Flags) -> bool {
pflags: u32 pflags: u32
pflags = PAGE_NOACCESS pflags = PAGE_NOACCESS
switch flags { switch flags {