| ... | @@ -97,7 +97,7 @@ const CAllocator = struct { | ... | @@ -97,7 +97,7 @@ const CAllocator = struct { |
| 97 | } | 97 | } |
| 98 | | 98 | |
| 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 | } |
| 124 | | 124 | |
| 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 exploiting | 152 | /// Supports the full Allocator interface, including alignment, and exploiting |
| 153 | /// `malloc_usable_size` if available. For an allocator that directly calls | 153 | /// `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`. |
| 155 | pub const c_allocator = blk: { | 155 | pub const c_allocator = Allocator{ |
| 156 | // TODO: This is an ugly hack, it could be improved once https://github.com/ziglang/zig/issues/6706 is implemented | 156 | .ptr = undefined, |
| 157 | // allowing the use of `*void` but it would still be ugly | 157 | .allocFn = CAllocator.alloc, |
| 158 | var tmp: u1 = 0; | 158 | .resizeFn = CAllocator.resize, |
| 159 | break :blk Allocator.init(&tmp, CAllocator.alloc, CAllocator.resize); | | |
| 160 | }; | 159 | }; |
| 161 | | 160 | |
| 162 | /// Asserts allocations are within `@alignOf(std.c.max_align_t)` and directly calls | 161 | /// 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 with | 163 | /// This allocator is safe to use as the backing allocator with |
| 165 | /// `ArenaAllocator` for example and is more optimal in such a case | 164 | /// `ArenaAllocator` for example and is more optimal in such a case |
| 166 | /// than `c_allocator`. | 165 | /// than `c_allocator`. |
| 167 | pub const raw_c_allocator = blk: { | 166 | pub 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 implemented | 167 | .ptr = undefined, |
| 169 | // allowing the use of `*void` but it would still be ugly | 168 | .allocFn = rawCAlloc, |
| 170 | var tmp: u1 = 0; | 169 | .resizeFn = rawCResize, |
| 171 | break :blk Allocator.init(&tmp, rawCAlloc, rawCResize); | | |
| 172 | }; | 170 | }; |
| 173 | | 171 | |
| 174 | fn rawCAlloc( | 172 | fn 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 | } |
| 187 | | 185 | |
| 188 | fn rawCResize( | 186 | fn 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. |
| 210 | pub const page_allocator = if (builtin.target.isWasm()) | 208 | pub const page_allocator = if (builtin.target.isWasm()) |
| 211 | blk: { | 209 | Allocator{ |
| 212 | // TODO: This is an ugly hack, it could be improved once https://github.com/ziglang/zig/issues/6706 is implemented | 210 | .ptr = undefined, |
| 213 | // allowing the use of `*void` but it would still be ugly | 211 | .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) | 214 | else if (builtin.target.os.tag == .freestanding) |
| 217 | root.os.heap.page_allocator | 215 | root.os.heap.page_allocator |
| 218 | else blk: { | 216 | else |
| 219 | // TODO: This is an ugly hack, it could be improved once https://github.com/ziglang/zig/issues/6706 is implemented | 217 | Allocator{ |
| 220 | // allowing the use of `*void` but it would still be ugly | 218 | .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 | }; |
| 224 | | 222 | |
| 225 | /// Verifies that the adjusted length will still map to the full length | 223 | /// Verifies that the adjusted length will still map to the full length |
| 226 | pub fn alignPageAllocLen(full_len: usize, len: usize, len_align: u29) usize { | 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,7 +231,7 @@ pub fn alignPageAllocLen(full_len: usize, len: usize, len_align: u29) usize { |
| 233 | pub var next_mmap_addr_hint: ?[*]align(mem.page_size) u8 = null; | 231 | pub var next_mmap_addr_hint: ?[*]align(mem.page_size) u8 = null; |
| 234 | | 232 | |
| 235 | const PageAllocator = struct { | 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 | _ = 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 | } |
| 332 | | 330 | |
| 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 | } |
| 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 | _ = 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 | } |
| 543 | | 541 | |
| 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, |