added adress_hint to virtual memory reserve function on all platforms

This commit is contained in:
Lukas Arens
2026-04-12 18:51:56 +03:00
parent 3cee8f615b
commit f2635bbb13
8 changed files with 18 additions and 18 deletions

View File

@@ -16,8 +16,8 @@ platform_memory_init :: proc "contextless" () {
Allocator_Error :: mem.Allocator_Error
@(require_results, no_sanitize_address)
reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) {
return _reserve(size)
reserve :: proc "contextless" (size: uint, address_hint := uintptr(0)) -> (data: []byte, err: Allocator_Error) {
return _reserve(size, address_hint)
}
@(no_sanitize_address)

View File

@@ -2,8 +2,8 @@ package mem_virtual
import "core:sys/posix"
_reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) {
result := posix.mmap(nil, size, {}, {.ANONYMOUS, .PRIVATE})
_reserve :: proc "contextless" (size: uint, address_hint := uintptr(0)) -> (data: []byte, err: Allocator_Error) {
result := posix.mmap(rawptr(address_hint), size, {}, {.ANONYMOUS, .PRIVATE})
if result == posix.MAP_FAILED {
assert_contextless(posix.errno() == .ENOMEM)
return nil, .Out_Of_Memory

View File

@@ -2,14 +2,14 @@ package mem_virtual
import "core:sys/posix"
_reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) {
_reserve :: proc "contextless" (size: uint, address_hint := uintptr(0)) -> (data: []byte, err: Allocator_Error) {
PROT_MAX :: proc "contextless" (flags: posix.Prot_Flags) -> posix.Prot_Flags {
_PROT_MAX_SHIFT :: 16
return transmute(posix.Prot_Flags)(transmute(i32)flags << _PROT_MAX_SHIFT)
}
result := posix.mmap(nil, size, PROT_MAX({.READ, .WRITE, .EXEC}), {.ANONYMOUS, .PRIVATE})
result := posix.mmap(rawptr(address_hint), size, PROT_MAX({.READ, .WRITE, .EXEC}), {.ANONYMOUS, .PRIVATE})
if result == posix.MAP_FAILED {
assert_contextless(posix.errno() == .ENOMEM)
return nil, .Out_Of_Memory

View File

@@ -4,8 +4,8 @@ package mem_virtual
import "core:sys/linux"
_reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) {
addr, errno := linux.mmap(0, size, {}, {.PRIVATE, .ANONYMOUS})
_reserve :: proc "contextless" (size: uint, address_hint := uintptr(0)) -> (data: []byte, err: Allocator_Error) {
addr, errno := linux.mmap(address_hint, size, {}, {.PRIVATE, .ANONYMOUS})
if errno == .ENOMEM {
return nil, .Out_Of_Memory
} else if errno == .EINVAL {
@@ -68,4 +68,4 @@ _map_file :: proc "contextless" (fd: uintptr, size: i64, flags: Map_File_Flags)
_unmap_file :: proc "contextless" (data: []byte) {
_release(raw_data(data), uint(len(data)))
}
}

View File

@@ -2,13 +2,13 @@ package mem_virtual
import "core:sys/posix"
_reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) {
_reserve :: proc "contextless" (size: uint, address_hint := uintptr(0)) -> (data: []byte, err: Allocator_Error) {
PROT_MPROTECT :: proc "contextless" (flags: posix.Prot_Flags) -> posix.Prot_Flags {
return transmute(posix.Prot_Flags)(transmute(i32)flags << 3)
}
result := posix.mmap(nil, size, PROT_MPROTECT({.READ, .WRITE, .EXEC}), {.ANONYMOUS, .PRIVATE})
result := posix.mmap(rawptr(address_hint), size, PROT_MPROTECT({.READ, .WRITE, .EXEC}), {.ANONYMOUS, .PRIVATE})
if result == posix.MAP_FAILED {
assert_contextless(posix.errno() == .ENOMEM)
return nil, .Out_Of_Memory

View File

@@ -2,8 +2,8 @@ package mem_virtual
import "core:sys/posix"
_reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) {
result := posix.mmap(nil, size, {}, {.ANONYMOUS, .PRIVATE})
_reserve :: proc "contextless" (size: uint, address_hint := uintptr(0)) -> (data: []byte, err: Allocator_Error) {
result := posix.mmap(rawptr(address_hint), size, {}, {.ANONYMOUS, .PRIVATE})
if result == posix.MAP_FAILED {
assert_contextless(posix.errno() == .ENOMEM)
return nil, .Out_Of_Memory

View File

@@ -7,7 +7,7 @@
#+build !windows
package mem_virtual
_reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) {
_reserve :: proc "contextless" (size: uint, address_hint := uintptr(0)) -> (data: []byte, err: Allocator_Error) {
return nil, nil
}
@@ -35,4 +35,4 @@ _map_file :: proc "contextless" (f: any, size: i64, flags: Map_File_Flags) -> (d
_unmap_file :: proc "contextless" (data: []byte) {
}
}

View File

@@ -87,8 +87,8 @@ foreign Kernel32 {
}
@(no_sanitize_address)
_reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) {
result := VirtualAlloc(nil, size, MEM_RESERVE, PAGE_READWRITE)
_reserve :: proc "contextless" (size: uint, address_hint := uintptr(0)) -> (data: []byte, err: Allocator_Error) {
result := VirtualAlloc(rawptr(address_hint), size, MEM_RESERVE, PAGE_READWRITE)
if result == nil {
err = .Out_Of_Memory
return
@@ -191,4 +191,4 @@ _map_file :: proc "contextless" (fd: uintptr, size: i64, flags: Map_File_Flags)
@(no_sanitize_address)
_unmap_file :: proc "contextless" (data: []byte) {
UnmapViewOfFile(raw_data(data))
}
}