authorgravatar for leecannon@leecannon.xyzLee Cannon <leecannon@leecannon.xyz> 2021-10-29 00:41:58+01:00
committergravatar for leecannon@leecannon.xyzLee Cannon <leecannon@leecannon.xyz> 2021-11-30 23:32:47+00:00
log47bc13bc597622fc8deffa1c1a45d47dac51eeb0
tree8cb5fd7f1a810c8a18b41110f5aa857fb21ff8be
parent85de022c5671d777f62ddff254a814dab05242fc
signaturelock-open Commit is signed but in an unrecognized format.

allocgate: dont use a dummy temporary for stateless allocators


2 files changed, 33 insertions(+), 36 deletions(-)

lib/std/heap.zig+28-30
......@@ -97,7 +97,7 @@ const CAllocator = struct {
9797 }
9898
9999 fn alloc(
100 _: *u1,
100 _: *c_void,
101101 len: usize,
102102 alignment: u29,
103103 len_align: u29,
......@@ -123,7 +123,7 @@ const CAllocator = struct {
123123 }
124124
125125 fn resize(
126 _: *u1,
126 _: *c_void,
127127 buf: []u8,
128128 buf_align: u29,
129129 new_len: usize,
......@@ -152,11 +152,10 @@ const CAllocator = struct {
152152/// Supports the full Allocator interface, including alignment, and exploiting
153153/// `malloc_usable_size` if available. For an allocator that directly calls
154154/// `malloc`/`free`, see `raw_c_allocator`.
155pub const c_allocator = blk: {
156 // TODO: This is an ugly hack, it could be improved once https://github.com/ziglang/zig/issues/6706 is implemented
157 // allowing the use of `*void` but it would still be ugly
158 var tmp: u1 = 0;
159 break :blk Allocator.init(&tmp, CAllocator.alloc, CAllocator.resize);
155pub const c_allocator = Allocator{
156 .ptr = undefined,
157 .allocFn = CAllocator.alloc,
158 .resizeFn = CAllocator.resize,
160159};
161160
162161/// Asserts allocations are within `@alignOf(std.c.max_align_t)` and directly calls
......@@ -164,15 +163,14 @@ pub const c_allocator = blk: {
164163/// This allocator is safe to use as the backing allocator with
165164/// `ArenaAllocator` for example and is more optimal in such a case
166165/// than `c_allocator`.
167pub const raw_c_allocator = blk: {
168 // TODO: This is an ugly hack, it could be improved once https://github.com/ziglang/zig/issues/6706 is implemented
169 // allowing the use of `*void` but it would still be ugly
170 var tmp: u1 = 0;
171 break :blk Allocator.init(&tmp, rawCAlloc, rawCResize);
166pub const raw_c_allocator = Allocator{
167 .ptr = undefined,
168 .allocFn = rawCAlloc,
169 .resizeFn = rawCResize,
172170};
173171
174172fn rawCAlloc(
175 _: *u1,
173 _: *c_void,
176174 len: usize,
177175 ptr_align: u29,
178176 len_align: u29,
......@@ -186,7 +184,7 @@ fn rawCAlloc(
186184}
187185
188186fn rawCResize(
189 _: *u1,
187 _: *c_void,
190188 buf: []u8,
191189 old_align: u29,
192190 new_len: usize,
......@@ -208,19 +206,19 @@ fn rawCResize(
208206/// This allocator makes a syscall directly for every allocation and free.
209207/// Thread-safe and lock-free.
210208pub const page_allocator = if (builtin.target.isWasm())
211blk: {
212 // TODO: This is an ugly hack, it could be improved once https://github.com/ziglang/zig/issues/6706 is implemented
213 // allowing the use of `*void` but it would still be ugly
214 var tmp: u1 = 0;
215 break :blk Allocator.init(&tmp, WasmPageAllocator.alloc, WasmPageAllocator.resize);
216} else if (builtin.target.os.tag == .freestanding)
209 Allocator{
210 .ptr = undefined,
211 .allocFn = WasmPageAllocator.alloc,
212 .resizeFn = WasmPageAllocator.resize,
213 }
214else if (builtin.target.os.tag == .freestanding)
217215 root.os.heap.page_allocator
218else blk: {
219 // TODO: This is an ugly hack, it could be improved once https://github.com/ziglang/zig/issues/6706 is implemented
220 // allowing the use of `*void` but it would still be ugly
221 var tmp: u1 = 0;
222 break :blk Allocator.init(&tmp, PageAllocator.alloc, PageAllocator.resize);
223};
216else
217 Allocator{
218 .ptr = undefined,
219 .allocFn = PageAllocator.alloc,
220 .resizeFn = PageAllocator.resize,
221 };
224222
225223/// Verifies that the adjusted length will still map to the full length
226224pub fn alignPageAllocLen(full_len: usize, len: usize, len_align: u29) usize {
......@@ -233,7 +231,7 @@ pub fn alignPageAllocLen(full_len: usize, len: usize, len_align: u29) usize {
233231pub var next_mmap_addr_hint: ?[*]align(mem.page_size) u8 = null;
234232
235233const PageAllocator = struct {
236 fn alloc(_: *u1, n: usize, alignment: u29, len_align: u29, ra: usize) error{OutOfMemory}![]u8 {
234 fn alloc(_: *c_void, n: usize, alignment: u29, len_align: u29, ra: usize) error{OutOfMemory}![]u8 {
237235 _ = ra;
238236 assert(n > 0);
239237 const aligned_len = mem.alignForward(n, mem.page_size);
......@@ -331,7 +329,7 @@ const PageAllocator = struct {
331329 }
332330
333331 fn resize(
334 _: *u1,
332 _: *c_void,
335333 buf_unaligned: []u8,
336334 buf_align: u29,
337335 new_size: usize,
......@@ -487,7 +485,7 @@ const WasmPageAllocator = struct {
487485 return mem.alignForward(memsize, mem.page_size) / mem.page_size;
488486 }
489487
490 fn alloc(_: *u1, len: usize, alignment: u29, len_align: u29, ra: usize) error{OutOfMemory}![]u8 {
488 fn alloc(_: *c_void, len: usize, alignment: u29, len_align: u29, ra: usize) error{OutOfMemory}![]u8 {
491489 _ = ra;
492490 const page_count = nPages(len);
493491 const page_idx = try allocPages(page_count, alignment);
......@@ -542,7 +540,7 @@ const WasmPageAllocator = struct {
542540 }
543541
544542 fn resize(
545 _: *u1,
543 _: *c_void,
546544 buf: []u8,
547545 buf_align: u29,
548546 new_len: usize,
lib/std/mem.zig+5-6
......@@ -131,14 +131,13 @@ pub fn alignAllocLen(full_len: usize, alloc_len: usize, len_align: u29) usize {
131131 return adjusted;
132132}
133133
134const failAllocator = blk: {
135 // TODO: This is an ugly hack, it could be improved once https://github.com/ziglang/zig/issues/6706 is implemented
136 // allowing the use of `*void` but it would still be ugly
137 var tmp: u1 = 0;
138 break :blk Allocator.init(&tmp, failAllocatorAlloc, Allocator.NoResize(u1).noResize);
134const failAllocator = Allocator{
135 .ptr = undefined,
136 .allocFn = failAllocatorAlloc,
137 .resizeFn = Allocator.NoResize(c_void).noResize,
139138};
140139
141fn failAllocatorAlloc(_: *u1, n: usize, alignment: u29, len_align: u29, ra: usize) Allocator.Error![]u8 {
140fn failAllocatorAlloc(_: *c_void, n: usize, alignment: u29, len_align: u29, ra: usize) Allocator.Error![]u8 {
142141 _ = n;
143142 _ = alignment;
144143 _ = len_align;