Commit c4a54377e3

Marc Tiehuis <marctiehuis@gmail.com>
2017-09-19 15:47:41
Stop debug allocator ever panicking (#492)
1 parent 751ab72
Changed files (1)
std/debug.zig
@@ -957,12 +957,17 @@ pub var global_allocator = mem.Allocator {
 var some_mem: [100 * 1024]u8 = undefined;
 var some_mem_index: usize = 0;
 
+error OutOfMemory;
+
 fn globalAlloc(self: &mem.Allocator, n: usize, alignment: usize) -> %[]u8 {
     const addr = @ptrToInt(&some_mem[some_mem_index]);
     const rem = @rem(addr, alignment);
     const march_forward_bytes = if (rem == 0) 0 else (alignment - rem);
     const adjusted_index = some_mem_index + march_forward_bytes;
     const end_index = adjusted_index + n;
+    if (end_index > some_mem.len) {
+        return error.OutOfMemory;
+    }
     const result = some_mem[adjusted_index .. end_index];
     some_mem_index = end_index;
     return result;