| ... | ... | @@ -97,7 +97,7 @@ const CAllocator = struct { |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | fn alloc( |
| 100 | | _: *u1, |
| 100 | _: *c_void, |
| 101 | 101 | len: usize, |
| 102 | 102 | alignment: u29, |
| 103 | 103 | len_align: u29, |
| ... | ... | @@ -123,7 +123,7 @@ const CAllocator = struct { |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | fn resize( |
| 126 | | _: *u1, |
| 126 | _: *c_void, |
| 127 | 127 | buf: []u8, |
| 128 | 128 | buf_align: u29, |
| 129 | 129 | new_len: usize, |
| ... | ... | @@ -152,11 +152,10 @@ const CAllocator = struct { |
| 152 | 152 | /// Supports the full Allocator interface, including alignment, and exploiting |
| 153 | 153 | /// `malloc_usable_size` if available. For an allocator that directly calls |
| 154 | 154 | /// `malloc`/`free`, see `raw_c_allocator`. |
| 155 | | pub 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); |
| 155 | pub const c_allocator = Allocator{ |
| 156 | .ptr = undefined, |
| 157 | .allocFn = CAllocator.alloc, |
| 158 | .resizeFn = CAllocator.resize, |
| 160 | 159 | }; |
| 161 | 160 | |
| 162 | 161 | /// Asserts allocations are within `@alignOf(std.c.max_align_t)` and directly calls |
| ... | ... | @@ -164,15 +163,14 @@ pub const c_allocator = blk: { |
| 164 | 163 | /// This allocator is safe to use as the backing allocator with |
| 165 | 164 | /// `ArenaAllocator` for example and is more optimal in such a case |
| 166 | 165 | /// than `c_allocator`. |
| 167 | | pub 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); |
| 166 | pub const raw_c_allocator = Allocator{ |
| 167 | .ptr = undefined, |
| 168 | .allocFn = rawCAlloc, |
| 169 | .resizeFn = rawCResize, |
| 172 | 170 | }; |
| 173 | 171 | |
| 174 | 172 | fn rawCAlloc( |
| 175 | | _: *u1, |
| 173 | _: *c_void, |
| 176 | 174 | len: usize, |
| 177 | 175 | ptr_align: u29, |
| 178 | 176 | len_align: u29, |
| ... | ... | @@ -186,7 +184,7 @@ fn rawCAlloc( |
| 186 | 184 | } |
| 187 | 185 | |
| 188 | 186 | fn rawCResize( |
| 189 | | _: *u1, |
| 187 | _: *c_void, |
| 190 | 188 | buf: []u8, |
| 191 | 189 | old_align: u29, |
| 192 | 190 | new_len: usize, |
| ... | ... | @@ -208,19 +206,19 @@ fn rawCResize( |
| 208 | 206 | /// This allocator makes a syscall directly for every allocation and free. |
| 209 | 207 | /// Thread-safe and lock-free. |
| 210 | 208 | pub const page_allocator = if (builtin.target.isWasm()) |
| 211 | | blk: { |
| 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 | } |
| 214 | else if (builtin.target.os.tag == .freestanding) |
| 217 | 215 | root.os.heap.page_allocator |
| 218 | | else 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 | | }; |
| 216 | else |
| 217 | Allocator{ |
| 218 | .ptr = undefined, |
| 219 | .allocFn = PageAllocator.alloc, |
| 220 | .resizeFn = PageAllocator.resize, |
| 221 | }; |
| 224 | 222 | |
| 225 | 223 | /// Verifies that the adjusted length will still map to the full length |
| 226 | 224 | 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 { |
| 233 | 231 | pub var next_mmap_addr_hint: ?[*]align(mem.page_size) u8 = null; |
| 234 | 232 | |
| 235 | 233 | const 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 | 235 | _ = ra; |
| 238 | 236 | assert(n > 0); |
| 239 | 237 | const aligned_len = mem.alignForward(n, mem.page_size); |
| ... | ... | @@ -331,7 +329,7 @@ const PageAllocator = struct { |
| 331 | 329 | } |
| 332 | 330 | |
| 333 | 331 | fn resize( |
| 334 | | _: *u1, |
| 332 | _: *c_void, |
| 335 | 333 | buf_unaligned: []u8, |
| 336 | 334 | buf_align: u29, |
| 337 | 335 | new_size: usize, |
| ... | ... | @@ -487,7 +485,7 @@ const WasmPageAllocator = struct { |
| 487 | 485 | return mem.alignForward(memsize, mem.page_size) / mem.page_size; |
| 488 | 486 | } |
| 489 | 487 | |
| 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 | 489 | _ = ra; |
| 492 | 490 | const page_count = nPages(len); |
| 493 | 491 | const page_idx = try allocPages(page_count, alignment); |
| ... | ... | @@ -542,7 +540,7 @@ const WasmPageAllocator = struct { |
| 542 | 540 | } |
| 543 | 541 | |
| 544 | 542 | fn resize( |
| 545 | | _: *u1, |
| 543 | _: *c_void, |
| 546 | 544 | buf: []u8, |
| 547 | 545 | buf_align: u29, |
| 548 | 546 | new_len: usize, |