refactor debug.global_allocator into mem.FixedBufferAllocator

This commit is contained in:
Andrew Kelley
2017-12-11 17:27:31 -05:00
parent d8d379faf1
commit c4e7d05ce3
3 changed files with 52 additions and 40 deletions

View File

@@ -965,41 +965,6 @@ fn readILeb128(in_stream: &io.InStream) -> %i64 {
}
}
pub const global_allocator = &global_allocator_state;
var global_allocator_state = mem.Allocator {
.allocFn = globalAlloc,
.reallocFn = globalRealloc,
.freeFn = globalFree,
};
pub var global_allocator_mem: [100 * 1024]u8 = undefined;
pub var global_allocator_index: usize = 0;
error OutOfMemory;
fn globalAlloc(self: &mem.Allocator, n: usize, alignment: u29) -> %[]u8 {
const addr = @ptrToInt(&global_allocator_mem[global_allocator_index]);
const rem = @rem(addr, alignment);
const march_forward_bytes = if (rem == 0) 0 else (alignment - rem);
const adjusted_index = global_allocator_index + march_forward_bytes;
const end_index = adjusted_index + n;
if (end_index > global_allocator_mem.len) {
return error.OutOfMemory;
}
const result = global_allocator_mem[adjusted_index .. end_index];
global_allocator_index = end_index;
return result;
}
fn globalRealloc(self: &mem.Allocator, old_mem: []u8, new_size: usize, alignment: u29) -> %[]u8 {
if (new_size <= old_mem.len) {
return old_mem[0..new_size];
} else {
const result = %return globalAlloc(self, new_size, alignment);
@memcpy(result.ptr, old_mem.ptr, old_mem.len);
return result;
}
}
fn globalFree(self: &mem.Allocator, memory: []u8) { }
pub const global_allocator = &global_fixed_allocator.allocator;
var global_fixed_allocator = mem.FixedBufferAllocator.init(global_allocator_mem[0..]);
var global_allocator_mem: [100 * 1024]u8 = undefined;