diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-12-11 17:27:31 -0500 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-12-11 17:27:31 -0500 |
| commit | c4e7d05ce37d60141afab997f23df33f5d4a218b (patch) | |
| tree | c7f2fe66a890665c2db006d50495fba7ebddc455 /std/debug.zig | |
| parent | d8d379faf1f656743d118e5e5cfa3dba1e537d65 (diff) | |
| download | zig-c4e7d05ce37d60141afab997f23df33f5d4a218b.tar.gz zig-c4e7d05ce37d60141afab997f23df33f5d4a218b.zip | |
refactor debug.global_allocator into mem.FixedBufferAllocator
Diffstat (limited to 'std/debug.zig')
| -rw-r--r-- | std/debug.zig | 41 |
1 files changed, 3 insertions, 38 deletions
diff --git a/std/debug.zig b/std/debug.zig index 3e7a38d043..25d6c84e0b 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -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; |
