Skip to content

Commit

Permalink
Use contextless where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerBill committed Mar 1, 2022
1 parent 18607e5 commit 7e5342f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
14 changes: 7 additions & 7 deletions core/mem/virtual/virtual.odin
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ DEFAULT_PAGE_SIZE := uint(4096)

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)
}

commit :: proc(data: rawptr, size: uint) -> Allocator_Error {
commit :: proc "contextless" (data: rawptr, size: uint) -> Allocator_Error {
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
commit(raw_data(data), size) or_return
return
}

decommit :: proc(data: rawptr, size: uint) {
decommit :: proc "contextless" (data: rawptr, size: uint) {
_decommit(data, size)
}

release :: proc(data: rawptr, size: uint) {
release :: proc "contextless" (data: rawptr, size: uint) {
_release(data, size)
}

Expand All @@ -36,7 +36,7 @@ Protect_Flag :: enum u32 {
Protect_Flags :: distinct bit_set[Protect_Flag; u32]
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)
}

Expand Down Expand Up @@ -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) {
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)
ptr := uintptr(block.base[block.used:])
mask := alignment-1
Expand Down
10 changes: 5 additions & 5 deletions core/mem/virtual/virtual_linux.odin
Original file line number Diff line number Diff line change
Expand Up @@ -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))
result := mmap(nil, size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)
if result == MAP_FAILED {
Expand All @@ -67,22 +67,22 @@ _reserve :: proc(size: uint) -> (data: []byte, err: Allocator_Error) {
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)
if result != 0 {
// TODO(bill): Handle error value correctly
return .Out_Of_Memory
}
return nil
}
_decommit :: proc(data: rawptr, size: uint) {
_decommit :: proc "contextless" (data: rawptr, size: uint) {
mprotect(data, size, PROT_NONE)
madvise(data, size, MADV_FREE)
}
_release :: proc(data: rawptr, size: uint) {
_release :: proc "contextless" (data: rawptr, size: uint) {
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 = PROT_NONE
if .Read in flags { pflags |= PROT_READ }
Expand Down
6 changes: 3 additions & 3 deletions core/mem/virtual/virtual_platform.odin
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Platform_Memory_Block :: struct {
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_reserve = max(to_commit, to_reserve)

Expand All @@ -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 {
release(block, block.reserved)
}
Expand Down Expand Up @@ -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 {
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions core/mem/virtual/virtual_windows.odin
Original file line number Diff line number Diff line change
Expand Up @@ -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)
if result == nil {
err = .Out_Of_Memory
Expand All @@ -72,7 +72,7 @@ _reserve :: proc(size: uint) -> (data: []byte, err: Allocator_Error) {
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)
if result == nil {
switch err := GetLastError(); err {
Expand All @@ -85,13 +85,13 @@ _commit :: proc(data: rawptr, size: uint) -> Allocator_Error {
}
return nil
}
_decommit :: proc(data: rawptr, size: uint) {
_decommit :: proc "contextless" (data: rawptr, size: uint) {
VirtualFree(data, size, MEM_DECOMMIT)
}
_release :: proc(data: rawptr, size: uint) {
_release :: proc "contextless" (data: rawptr, size: uint) {
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 = PAGE_NOACCESS
switch flags {
Expand Down

0 comments on commit 7e5342f

Please sign in to comment.