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 {...@@ -97,7 +97,7 @@ const CAllocator = struct {
97 }97 }
9898
99 fn alloc(99 fn alloc(
100 _: *u1,100 _: *c_void,
101 len: usize,101 len: usize,
102 alignment: u29,102 alignment: u29,
103 len_align: u29,103 len_align: u29,
...@@ -123,7 +123,7 @@ const CAllocator = struct {...@@ -123,7 +123,7 @@ const CAllocator = struct {
123 }123 }
124124
125 fn resize(125 fn resize(
126 _: *u1,126 _: *c_void,
127 buf: []u8,127 buf: []u8,
128 buf_align: u29,128 buf_align: u29,
129 new_len: usize,129 new_len: usize,
...@@ -152,11 +152,10 @@ const CAllocator = struct {...@@ -152,11 +152,10 @@ const CAllocator = struct {
152/// Supports the full Allocator interface, including alignment, and exploiting152/// Supports the full Allocator interface, including alignment, and exploiting
153/// `malloc_usable_size` if available. For an allocator that directly calls153/// `malloc_usable_size` if available. For an allocator that directly calls
154/// `malloc`/`free`, see `raw_c_allocator`.154/// `malloc`/`free`, see `raw_c_allocator`.
155pub const c_allocator = blk: {155pub const c_allocator = Allocator{
156 // TODO: This is an ugly hack, it could be improved once https://github.com/ziglang/zig/issues/6706 is implemented156 .ptr = undefined,
157 // allowing the use of `*void` but it would still be ugly157 .allocFn = CAllocator.alloc,
158 var tmp: u1 = 0;158 .resizeFn = CAllocator.resize,
159 break :blk Allocator.init(&tmp, CAllocator.alloc, CAllocator.resize);
160};159};
161160
162/// Asserts allocations are within `@alignOf(std.c.max_align_t)` and directly calls161/// Asserts allocations are within `@alignOf(std.c.max_align_t)` and directly calls
...@@ -164,15 +163,14 @@ pub const c_allocator = blk: {...@@ -164,15 +163,14 @@ pub const c_allocator = blk: {
164/// This allocator is safe to use as the backing allocator with163/// This allocator is safe to use as the backing allocator with
165/// `ArenaAllocator` for example and is more optimal in such a case164/// `ArenaAllocator` for example and is more optimal in such a case
166/// than `c_allocator`.165/// than `c_allocator`.
167pub const raw_c_allocator = blk: {166pub const raw_c_allocator = Allocator{
168 // TODO: This is an ugly hack, it could be improved once https://github.com/ziglang/zig/issues/6706 is implemented167 .ptr = undefined,
169 // allowing the use of `*void` but it would still be ugly168 .allocFn = rawCAlloc,
170 var tmp: u1 = 0;169 .resizeFn = rawCResize,
171 break :blk Allocator.init(&tmp, rawCAlloc, rawCResize);
172};170};
173171
174fn rawCAlloc(172fn rawCAlloc(
175 _: *u1,173 _: *c_void,
176 len: usize,174 len: usize,
177 ptr_align: u29,175 ptr_align: u29,
178 len_align: u29,176 len_align: u29,
...@@ -186,7 +184,7 @@ fn rawCAlloc(...@@ -186,7 +184,7 @@ fn rawCAlloc(
186}184}
187185
188fn rawCResize(186fn rawCResize(
189 _: *u1,187 _: *c_void,
190 buf: []u8,188 buf: []u8,
191 old_align: u29,189 old_align: u29,
192 new_len: usize,190 new_len: usize,
...@@ -208,19 +206,19 @@ fn rawCResize(...@@ -208,19 +206,19 @@ fn rawCResize(
208/// This allocator makes a syscall directly for every allocation and free.206/// This allocator makes a syscall directly for every allocation and free.
209/// Thread-safe and lock-free.207/// Thread-safe and lock-free.
210pub const page_allocator = if (builtin.target.isWasm())208pub const page_allocator = if (builtin.target.isWasm())
211blk: {209 Allocator{
212 // TODO: This is an ugly hack, it could be improved once https://github.com/ziglang/zig/issues/6706 is implemented210 .ptr = undefined,
213 // allowing the use of `*void` but it would still be ugly211 .allocFn = WasmPageAllocator.alloc,
214 var tmp: u1 = 0;212 .resizeFn = WasmPageAllocator.resize,
215 break :blk Allocator.init(&tmp, WasmPageAllocator.alloc, WasmPageAllocator.resize);213 }
216} else if (builtin.target.os.tag == .freestanding)214else if (builtin.target.os.tag == .freestanding)
217 root.os.heap.page_allocator215 root.os.heap.page_allocator
218else blk: {216else
219 // TODO: This is an ugly hack, it could be improved once https://github.com/ziglang/zig/issues/6706 is implemented217 Allocator{
220 // allowing the use of `*void` but it would still be ugly218 .ptr = undefined,
221 var tmp: u1 = 0;219 .allocFn = PageAllocator.alloc,
222 break :blk Allocator.init(&tmp, PageAllocator.alloc, PageAllocator.resize);220 .resizeFn = PageAllocator.resize,
223};221 };
224222
225/// Verifies that the adjusted length will still map to the full length223/// Verifies that the adjusted length will still map to the full length
226pub fn alignPageAllocLen(full_len: usize, len: usize, len_align: u29) usize {224pub 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 {...@@ -233,7 +231,7 @@ pub fn alignPageAllocLen(full_len: usize, len: usize, len_align: u29) usize {
233pub var next_mmap_addr_hint: ?[*]align(mem.page_size) u8 = null;231pub var next_mmap_addr_hint: ?[*]align(mem.page_size) u8 = null;
234232
235const PageAllocator = struct {233const 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 {
237 _ = ra;235 _ = ra;
238 assert(n > 0);236 assert(n > 0);
239 const aligned_len = mem.alignForward(n, mem.page_size);237 const aligned_len = mem.alignForward(n, mem.page_size);
...@@ -331,7 +329,7 @@ const PageAllocator = struct {...@@ -331,7 +329,7 @@ const PageAllocator = struct {
331 }329 }
332330
333 fn resize(331 fn resize(
334 _: *u1,332 _: *c_void,
335 buf_unaligned: []u8,333 buf_unaligned: []u8,
336 buf_align: u29,334 buf_align: u29,
337 new_size: usize,335 new_size: usize,
...@@ -487,7 +485,7 @@ const WasmPageAllocator = struct {...@@ -487,7 +485,7 @@ const WasmPageAllocator = struct {
487 return mem.alignForward(memsize, mem.page_size) / mem.page_size;485 return mem.alignForward(memsize, mem.page_size) / mem.page_size;
488 }486 }
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 {
491 _ = ra;489 _ = ra;
492 const page_count = nPages(len);490 const page_count = nPages(len);
493 const page_idx = try allocPages(page_count, alignment);491 const page_idx = try allocPages(page_count, alignment);
...@@ -542,7 +540,7 @@ const WasmPageAllocator = struct {...@@ -542,7 +540,7 @@ const WasmPageAllocator = struct {
542 }540 }
543541
544 fn resize(542 fn resize(
545 _: *u1,543 _: *c_void,
546 buf: []u8,544 buf: []u8,
547 buf_align: u29,545 buf_align: u29,
548 new_len: usize,546 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 {...@@ -131,14 +131,13 @@ pub fn alignAllocLen(full_len: usize, alloc_len: usize, len_align: u29) usize {
131 return adjusted;131 return adjusted;
132}132}
133133
134const failAllocator = blk: {134const failAllocator = Allocator{
135 // TODO: This is an ugly hack, it could be improved once https://github.com/ziglang/zig/issues/6706 is implemented135 .ptr = undefined,
136 // allowing the use of `*void` but it would still be ugly136 .allocFn = failAllocatorAlloc,
137 var tmp: u1 = 0;137 .resizeFn = Allocator.NoResize(c_void).noResize,
138 break :blk Allocator.init(&tmp, failAllocatorAlloc, Allocator.NoResize(u1).noResize);
139};138};
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 {
142 _ = n;141 _ = n;
143 _ = alignment;142 _ = alignment;
144 _ = len_align;143 _ = len_align;