From 04b721d53530c1de825f7d1df93f2e23916e092d Mon Sep 17 00:00:00 2001 From: MovingtoMars Date: Thu, 28 Jan 2016 13:18:29 +1300 Subject: [PATCH] malloc fails on proper errors --- std/mem.zig | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/std/mem.zig b/std/mem.zig index d19dad9f9e83c7a49ea3b7d6005fa781f16541c7..dae3a091b1de49facb1694179c5cbaf3ed32f66e 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -1,5 +1,6 @@ import "syscall.zig"; import "std.zig"; +import "errno.zig"; pub fn malloc(bytes: isize) -> ?&u8 { if (bytes > 4096) { @@ -7,13 +8,28 @@ pub fn malloc(bytes: isize) -> ?&u8 { return null; } - const result = mmap(0, 4096, MMAP_PROT_READ|MMAP_PROT_WRITE, MMAP_MAP_ANON|MMAP_MAP_SHARED, -1, 0); + const result = mmap(isize(0), 4096, MMAP_PROT_READ|MMAP_PROT_WRITE, MMAP_MAP_ANON|MMAP_MAP_SHARED, -1, 0); - if (result == -1) { - return null; + const failed: bool = switch (-result) { + 0 => true, + EINVAL => true, + EACCES => true, + EAGAIN => true, + EBADF => true, + EMFILE => true, + ENODEV => true, + ENOMEM => true, + EOPNOTSUPP => true, + ENXIO => true, + EOVERFLOW => true, + else => false, + }; + + if (failed) { + null + } else { + (&u8)(result) } - - (&u8)(result) } pub fn free(ptr: &u8) { -- 2.54.0