Stop debug allocator ever panicking (#492)

This commit is contained in:
Marc Tiehuis 2017-09-20 01:47:41 +12:00 committed by Andrew Kelley
parent 751ab72a82
commit c4a54377e3

View File

@ -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;