| author | |
| committer | |
| log | fd43baa9ad37217a5715c1e3cfad2d2d78558d1f |
| tree | c0d75d8a8c92038f05ea78f961b5dd7b23e0efbc |
| parent | a338c279f82bfeb68e37b40cd4fc59557336b6ce |
These changes enable me to use `GeneralPurposeAllocator` with my "Bring
Your Own OS" package. The previous checks for a freestanding target have
been expanded to `@hasDecl` checks.
- `root.os.heap.page_allocator` is used if it exists.
- `debug.isValidMemory` only calls `os.msync` if it's supported.2 files changed, 20 insertions(+), 17 deletions(-)
lib/std/debug.zig+15-14| ... | ... | @@ -667,20 +667,7 @@ pub const StackIterator = struct { |
| 667 | 667 | if (aligned_address == 0) return false; |
| 668 | 668 | const aligned_memory = @as([*]align(mem.page_size) u8, @ptrFromInt(aligned_address))[0..mem.page_size]; |
| 669 | 669 | |
| 670 | if (native_os != .windows) { | |
| 671 | if (native_os != .wasi) { | |
| 672 | os.msync(aligned_memory, os.MSF.ASYNC) catch |err| { | |
| 673 | switch (err) { | |
| 674 | os.MSyncError.UnmappedMemory => { | |
| 675 | return false; | |
| 676 | }, | |
| 677 | else => unreachable, | |
| 678 | } | |
| 679 | }; | |
| 680 | } | |
| 681 | ||
| 682 | return true; | |
| 683 | } else { | |
| 670 | if (native_os == .windows) { | |
| 684 | 671 | const w = os.windows; |
| 685 | 672 | var memory_info: w.MEMORY_BASIC_INFORMATION = undefined; |
| 686 | 673 | |
| ... | ... | @@ -700,6 +687,20 @@ pub const StackIterator = struct { |
| 700 | 687 | return false; |
| 701 | 688 | } |
| 702 | 689 | |
| 690 | return true; | |
| 691 | } else if (@hasDecl(os.system, "msync") and native_os != .wasi) { | |
| 692 | os.msync(aligned_memory, os.MSF.ASYNC) catch |err| { | |
| 693 | switch (err) { | |
| 694 | os.MSyncError.UnmappedMemory => { | |
| 695 | return false; | |
| 696 | }, | |
| 697 | else => unreachable, | |
| 698 | } | |
| 699 | }; | |
| 700 | ||
| 701 | return true; | |
| 702 | } else { | |
| 703 | // We are unable to determine validity of memory on this target. | |
| 703 | 704 | return true; |
| 704 | 705 | } |
| 705 | 706 | } |
lib/std/heap.zig+5-3| ... | ... | @@ -223,7 +223,11 @@ fn rawCFree( |
| 223 | 223 | |
| 224 | 224 | /// This allocator makes a syscall directly for every allocation and free. |
| 225 | 225 | /// Thread-safe and lock-free. |
| 226 | pub const page_allocator = if (builtin.target.isWasm()) | |
| 226 | pub const page_allocator = if (@hasDecl(root, "os") and | |
| 227 | @hasDecl(root.os, "heap") and | |
| 228 | @hasDecl(root.os.heap, "page_allocator")) | |
| 229 | root.os.heap.page_allocator | |
| 230 | else if (builtin.target.isWasm()) | |
| 227 | 231 | Allocator{ |
| 228 | 232 | .ptr = undefined, |
| 229 | 233 | .vtable = &WasmPageAllocator.vtable, |
| ... | ... | @@ -233,8 +237,6 @@ else if (builtin.target.os.tag == .plan9) |
| 233 | 237 | .ptr = undefined, |
| 234 | 238 | .vtable = &SbrkAllocator(std.os.plan9.sbrk).vtable, |
| 235 | 239 | } |
| 236 | else if (builtin.target.os.tag == .freestanding) | |
| 237 | root.os.heap.page_allocator | |
| 238 | 240 | else |
| 239 | 241 | Allocator{ |
| 240 | 242 | .ptr = undefined, |