| author | |
| committer | |
| log | c4e7d05ce37d60141afab997f23df33f5d4a218b |
| tree | c7f2fe66a890665c2db006d50495fba7ebddc455 |
| parent | d8d379faf1f656743d118e5e5cfa3dba1e537d65 |
3 files changed, 52 insertions(+), 40 deletions(-)
src-self-hosted/main.zig+4-2| ... | ... | @@ -66,9 +66,11 @@ pub fn main2() -> %void { |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | |
| 69 | var fixed_buffer_mem: [100 * 1024]u8 = undefined; | |
| 70 | ||
| 69 | 71 | fn testCanonical(source: []const u8) { |
| 70 | const allocator = std.debug.global_allocator; | |
| 71 | std.debug.global_allocator_index = 0; | |
| 72 | var fixed_allocator = mem.FixedBufferAllocator.init(fixed_buffer_mem[0..]); | |
| 73 | const allocator = &fixed_allocator.allocator; | |
| 72 | 74 | |
| 73 | 75 | var tokenizer = Tokenizer.init(source); |
| 74 | 76 | var parser = Parser.init(&tokenizer, allocator, "(memory buffer)"); |
std/debug.zig+3-38| ... | ... | @@ -965,41 +965,6 @@ fn readILeb128(in_stream: &io.InStream) -> %i64 { |
| 965 | 965 | } |
| 966 | 966 | } |
| 967 | 967 | |
| 968 | pub const global_allocator = &global_allocator_state; | |
| 969 | ||
| 970 | var global_allocator_state = mem.Allocator { | |
| 971 | .allocFn = globalAlloc, | |
| 972 | .reallocFn = globalRealloc, | |
| 973 | .freeFn = globalFree, | |
| 974 | }; | |
| 975 | ||
| 976 | pub var global_allocator_mem: [100 * 1024]u8 = undefined; | |
| 977 | pub var global_allocator_index: usize = 0; | |
| 978 | ||
| 979 | error OutOfMemory; | |
| 980 | ||
| 981 | fn globalAlloc(self: &mem.Allocator, n: usize, alignment: u29) -> %[]u8 { | |
| 982 | const addr = @ptrToInt(&global_allocator_mem[global_allocator_index]); | |
| 983 | const rem = @rem(addr, alignment); | |
| 984 | const march_forward_bytes = if (rem == 0) 0 else (alignment - rem); | |
| 985 | const adjusted_index = global_allocator_index + march_forward_bytes; | |
| 986 | const end_index = adjusted_index + n; | |
| 987 | if (end_index > global_allocator_mem.len) { | |
| 988 | return error.OutOfMemory; | |
| 989 | } | |
| 990 | const result = global_allocator_mem[adjusted_index .. end_index]; | |
| 991 | global_allocator_index = end_index; | |
| 992 | return result; | |
| 993 | } | |
| 994 | ||
| 995 | fn globalRealloc(self: &mem.Allocator, old_mem: []u8, new_size: usize, alignment: u29) -> %[]u8 { | |
| 996 | if (new_size <= old_mem.len) { | |
| 997 | return old_mem[0..new_size]; | |
| 998 | } else { | |
| 999 | const result = %return globalAlloc(self, new_size, alignment); | |
| 1000 | @memcpy(result.ptr, old_mem.ptr, old_mem.len); | |
| 1001 | return result; | |
| 1002 | } | |
| 1003 | } | |
| 1004 | ||
| 1005 | fn globalFree(self: &mem.Allocator, memory: []u8) { } | |
| 968 | pub const global_allocator = &global_fixed_allocator.allocator; | |
| 969 | var global_fixed_allocator = mem.FixedBufferAllocator.init(global_allocator_mem[0..]); | |
| 970 | var global_allocator_mem: [100 * 1024]u8 = undefined; |
std/mem.zig+45| ... | ... | @@ -105,6 +105,51 @@ pub const Allocator = struct { |
| 105 | 105 | } |
| 106 | 106 | }; |
| 107 | 107 | |
| 108 | pub const FixedBufferAllocator = struct { | |
| 109 | allocator: Allocator, | |
| 110 | end_index: usize, | |
| 111 | buffer: []u8, | |
| 112 | ||
| 113 | pub fn init(buffer: []u8) -> FixedBufferAllocator { | |
| 114 | return FixedBufferAllocator { | |
| 115 | .allocator = Allocator { | |
| 116 | .allocFn = alloc, | |
| 117 | .reallocFn = realloc, | |
| 118 | .freeFn = free, | |
| 119 | }, | |
| 120 | .buffer = buffer, | |
| 121 | .end_index = 0, | |
| 122 | }; | |
| 123 | } | |
| 124 | ||
| 125 | fn alloc(allocator: &Allocator, n: usize, alignment: u29) -> %[]u8 { | |
| 126 | const self = @fieldParentPtr(FixedBufferAllocator, "allocator", allocator); | |
| 127 | const addr = @ptrToInt(&self.buffer[self.end_index]); | |
| 128 | const rem = @rem(addr, alignment); | |
| 129 | const march_forward_bytes = if (rem == 0) 0 else (alignment - rem); | |
| 130 | const adjusted_index = self.end_index + march_forward_bytes; | |
| 131 | const new_end_index = adjusted_index + n; | |
| 132 | if (new_end_index > self.buffer.len) { | |
| 133 | return error.OutOfMemory; | |
| 134 | } | |
| 135 | const result = self.buffer[adjusted_index .. new_end_index]; | |
| 136 | self.end_index = new_end_index; | |
| 137 | return result; | |
| 138 | } | |
| 139 | ||
| 140 | fn realloc(allocator: &Allocator, old_mem: []u8, new_size: usize, alignment: u29) -> %[]u8 { | |
| 141 | if (new_size <= old_mem.len) { | |
| 142 | return old_mem[0..new_size]; | |
| 143 | } else { | |
| 144 | const result = %return alloc(allocator, new_size, alignment); | |
| 145 | copy(u8, result, old_mem); | |
| 146 | return result; | |
| 147 | } | |
| 148 | } | |
| 149 | ||
| 150 | fn free(allocator: &Allocator, bytes: []u8) { } | |
| 151 | }; | |
| 152 | ||
| 108 | 153 | |
| 109 | 154 | /// Copy all of source into dest at position 0. |
| 110 | 155 | /// dest.len must be >= source.len. |