srctree

Jay Petacat parent a338c279 fd43baa9
byos: Ease `GeneralPurposeAllocator` integration

These changes enable me to use GeneralPurposeAllocator with my "BringYour Own OS" package. The previous checks for a freestanding target havebeen expanded to @hasDecl checks.

  • root.os.heap.page_allocator is used if it exists.
  • debug.isValidMemory only calls os.msync if it's supported.
inlinesplit
lib/std/debug.zig added: 22, removed: 19, total 3
@@ -667,20 +667,7 @@ pub const StackIterator = struct {
if (aligned_address == 0) return false;
const aligned_memory = @as([*]align(mem.page_size) u8, @ptrFromInt(aligned_address))[0..mem.page_size];
 
if (native_os != .windows) {
if (native_os != .wasi) {
os.msync(aligned_memory, os.MSF.ASYNC) catch |err| {
switch (err) {
os.MSyncError.UnmappedMemory => {
return false;
},
else => unreachable,
}
};
}
 
return true;
} else {
if (native_os == .windows) {
const w = os.windows;
var memory_info: w.MEMORY_BASIC_INFORMATION = undefined;
 
@@ -700,6 +687,20 @@ pub const StackIterator = struct {
return false;
}
 
return true;
} else if (@hasDecl(os.system, "msync") and native_os != .wasi) {
os.msync(aligned_memory, os.MSF.ASYNC) catch |err| {
switch (err) {
os.MSyncError.UnmappedMemory => {
return false;
},
else => unreachable,
}
};
 
return true;
} else {
// We are unable to determine validity of memory on this target.
return true;
}
}
 
lib/std/heap.zig added: 22, removed: 19, total 3
@@ -223,7 +223,11 @@ fn rawCFree(
 
/// This allocator makes a syscall directly for every allocation and free.
/// Thread-safe and lock-free.
pub const page_allocator = if (builtin.target.isWasm())
pub const page_allocator = if (@hasDecl(root, "os") and
@hasDecl(root.os, "heap") and
@hasDecl(root.os.heap, "page_allocator"))
root.os.heap.page_allocator
else if (builtin.target.isWasm())
Allocator{
.ptr = undefined,
.vtable = &WasmPageAllocator.vtable,
@@ -233,8 +237,6 @@ else if (builtin.target.os.tag == .plan9)
.ptr = undefined,
.vtable = &SbrkAllocator(std.os.plan9.sbrk).vtable,
}
else if (builtin.target.os.tag == .freestanding)
root.os.heap.page_allocator
else
Allocator{
.ptr = undefined,