mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-08 23:04:24 +00:00
std.heap: do not excessively call mmap, and munmap in direct allocator
This commit is contained in:
parent
8afa1e800b
commit
3c13aa178b
1 changed files with 5 additions and 1 deletions
|
|
@ -59,6 +59,8 @@ pub const DirectAllocator = struct {
|
||||||
|
|
||||||
fn alloc(allocator: *Allocator, n: usize, alignment: u29) error{OutOfMemory}![]u8 {
|
fn alloc(allocator: *Allocator, n: usize, alignment: u29) error{OutOfMemory}![]u8 {
|
||||||
const self = @fieldParentPtr(DirectAllocator, "allocator", allocator);
|
const self = @fieldParentPtr(DirectAllocator, "allocator", allocator);
|
||||||
|
if (n == 0)
|
||||||
|
return (([*]u8)(undefined))[0..0];
|
||||||
|
|
||||||
switch (builtin.os) {
|
switch (builtin.os) {
|
||||||
Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => {
|
Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => {
|
||||||
|
|
@ -140,7 +142,9 @@ pub const DirectAllocator = struct {
|
||||||
}
|
}
|
||||||
const result = try alloc(allocator, new_size, new_align);
|
const result = try alloc(allocator, new_size, new_align);
|
||||||
@memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len));
|
@memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len));
|
||||||
_ = os.posix.munmap(@ptrToInt(old_mem.ptr), old_mem.len);
|
if (old_mem.len > 0) {
|
||||||
|
_ = os.posix.munmap(@ptrToInt(old_mem.ptr), old_mem.len);
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
Os.windows => {
|
Os.windows => {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue