| ... | ... | @@ -124,6 +124,13 @@ const CAllocator = struct { |
| 124 | 124 | } |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | const vtable: Allocator.VTable = .{ |
| 128 | .alloc = alloc, |
| 129 | .resize = resize, |
| 130 | .remap = remap, |
| 131 | .free = free, |
| 132 | }; |
| 133 | |
| 127 | 134 | pub const supports_malloc_size = @TypeOf(malloc_size) != void; |
| 128 | 135 | pub const malloc_size = if (@TypeOf(c.malloc_size) != void) |
| 129 | 136 | c.malloc_size |
| ... | ... | @@ -139,7 +146,7 @@ const CAllocator = struct { |
| 139 | 146 | }; |
| 140 | 147 | |
| 141 | 148 | fn getHeader(ptr: [*]u8) *[*]u8 { |
| 142 | | return @as(*[*]u8, @ptrFromInt(@intFromPtr(ptr) - @sizeOf(usize))); |
| 149 | return @ptrCast(ptr - @sizeOf(usize)); |
| 143 | 150 | } |
| 144 | 151 | |
| 145 | 152 | fn alignedAlloc(len: usize, alignment: mem.Alignment) ?[*]u8 { |
| ... | ... | @@ -147,13 +154,13 @@ const CAllocator = struct { |
| 147 | 154 | if (supports_posix_memalign) { |
| 148 | 155 | // The posix_memalign only accepts alignment values that are a |
| 149 | 156 | // multiple of the pointer size |
| 150 | | const eff_alignment = @max(alignment_bytes, @sizeOf(usize)); |
| 157 | const effective_alignment = @max(alignment_bytes, @sizeOf(usize)); |
| 151 | 158 | |
| 152 | 159 | var aligned_ptr: ?*anyopaque = undefined; |
| 153 | | if (c.posix_memalign(&aligned_ptr, eff_alignment, len) != 0) |
| 160 | if (c.posix_memalign(&aligned_ptr, effective_alignment, len) != 0) |
| 154 | 161 | return null; |
| 155 | 162 | |
| 156 | | return @as([*]u8, @ptrCast(aligned_ptr)); |
| 163 | return @ptrCast(aligned_ptr); |
| 157 | 164 | } |
| 158 | 165 | |
| 159 | 166 | // Thin wrapper around regular malloc, overallocate to account for |
| ... | ... | @@ -219,6 +226,18 @@ const CAllocator = struct { |
| 219 | 226 | return false; |
| 220 | 227 | } |
| 221 | 228 | |
| 229 | fn remap( |
| 230 | context: *anyopaque, |
| 231 | memory: []u8, |
| 232 | alignment: mem.Alignment, |
| 233 | new_len: usize, |
| 234 | return_address: usize, |
| 235 | ) ?[*]u8 { |
| 236 | // realloc would potentially return a new allocation that does not |
| 237 | // respect the original alignment. |
| 238 | return if (resize(context, memory, alignment, new_len, return_address)) memory.ptr else null; |
| 239 | } |
| 240 | |
| 222 | 241 | fn free( |
| 223 | 242 | _: *anyopaque, |
| 224 | 243 | buf: []u8, |
| ... | ... | @@ -234,39 +253,36 @@ const CAllocator = struct { |
| 234 | 253 | /// Supports the full Allocator interface, including alignment, and exploiting |
| 235 | 254 | /// `malloc_usable_size` if available. For an allocator that directly calls |
| 236 | 255 | /// `malloc`/`free`, see `raw_c_allocator`. |
| 237 | | pub const c_allocator = Allocator{ |
| 256 | pub const c_allocator: Allocator = .{ |
| 238 | 257 | .ptr = undefined, |
| 239 | | .vtable = &c_allocator_vtable, |
| 240 | | }; |
| 241 | | const c_allocator_vtable = Allocator.VTable{ |
| 242 | | .alloc = CAllocator.alloc, |
| 243 | | .resize = CAllocator.resize, |
| 244 | | .free = CAllocator.free, |
| 258 | .vtable = &CAllocator.vtable, |
| 245 | 259 | }; |
| 246 | 260 | |
| 247 | | /// Asserts allocations are within `@alignOf(std.c.max_align_t)` and directly calls |
| 248 | | /// `malloc`/`free`. Does not attempt to utilize `malloc_usable_size`. |
| 261 | /// Asserts allocations are within `@alignOf(std.c.max_align_t)` and directly |
| 262 | /// calls `malloc`/`free`. Does not attempt to utilize `malloc_usable_size`. |
| 249 | 263 | /// This allocator is safe to use as the backing allocator with |
| 250 | | /// `ArenaAllocator` for example and is more optimal in such a case |
| 251 | | /// than `c_allocator`. |
| 252 | | pub const raw_c_allocator = Allocator{ |
| 264 | /// `ArenaAllocator` for example and is more optimal in such a case than |
| 265 | /// `c_allocator`. |
| 266 | pub const raw_c_allocator: Allocator = .{ |
| 253 | 267 | .ptr = undefined, |
| 254 | 268 | .vtable = &raw_c_allocator_vtable, |
| 255 | 269 | }; |
| 256 | | const raw_c_allocator_vtable = Allocator.VTable{ |
| 270 | const raw_c_allocator_vtable: Allocator.VTable = .{ |
| 257 | 271 | .alloc = rawCAlloc, |
| 258 | 272 | .resize = rawCResize, |
| 273 | .remap = rawCRemap, |
| 259 | 274 | .free = rawCFree, |
| 260 | 275 | }; |
| 261 | 276 | |
| 262 | 277 | fn rawCAlloc( |
| 263 | | _: *anyopaque, |
| 278 | context: *anyopaque, |
| 264 | 279 | len: usize, |
| 265 | 280 | alignment: mem.Alignment, |
| 266 | | ret_addr: usize, |
| 281 | return_address: usize, |
| 267 | 282 | ) ?[*]u8 { |
| 268 | | _ = ret_addr; |
| 269 | | assert(alignment.order(.le, comptime .fromByteUnits(@alignOf(std.c.max_align_t)))); |
| 283 | _ = context; |
| 284 | _ = return_address; |
| 285 | assert(alignment.compare(.lte, comptime .fromByteUnits(@alignOf(std.c.max_align_t)))); |
| 270 | 286 | // Note that this pointer cannot be aligncasted to max_align_t because if |
| 271 | 287 | // len is < max_align_t then the alignment can be smaller. For example, if |
| 272 | 288 | // max_align_t is 16, but the user requests 8 bytes, there is no built-in |
| ... | ... | @@ -277,35 +293,43 @@ fn rawCAlloc( |
| 277 | 293 | } |
| 278 | 294 | |
| 279 | 295 | fn rawCResize( |
| 280 | | _: *anyopaque, |
| 281 | | buf: []u8, |
| 296 | context: *anyopaque, |
| 297 | memory: []u8, |
| 282 | 298 | alignment: mem.Alignment, |
| 283 | 299 | new_len: usize, |
| 284 | | ret_addr: usize, |
| 300 | return_address: usize, |
| 285 | 301 | ) bool { |
| 302 | _ = context; |
| 303 | _ = memory; |
| 286 | 304 | _ = alignment; |
| 287 | | _ = ret_addr; |
| 288 | | |
| 289 | | if (new_len <= buf.len) |
| 290 | | return true; |
| 291 | | |
| 292 | | if (CAllocator.supports_malloc_size) { |
| 293 | | const full_len = CAllocator.malloc_size(buf.ptr); |
| 294 | | if (new_len <= full_len) return true; |
| 295 | | } |
| 296 | | |
| 305 | _ = new_len; |
| 306 | _ = return_address; |
| 297 | 307 | return false; |
| 298 | 308 | } |
| 299 | 309 | |
| 310 | fn rawCRemap( |
| 311 | context: *anyopaque, |
| 312 | memory: []u8, |
| 313 | alignment: mem.Alignment, |
| 314 | new_len: usize, |
| 315 | return_address: usize, |
| 316 | ) ?[*]u8 { |
| 317 | _ = context; |
| 318 | _ = alignment; |
| 319 | _ = return_address; |
| 320 | return @ptrCast(c.realloc(memory.ptr, new_len)); |
| 321 | } |
| 322 | |
| 300 | 323 | fn rawCFree( |
| 301 | | _: *anyopaque, |
| 302 | | buf: []u8, |
| 324 | context: *anyopaque, |
| 325 | memory: []u8, |
| 303 | 326 | alignment: mem.Alignment, |
| 304 | | ret_addr: usize, |
| 327 | return_address: usize, |
| 305 | 328 | ) void { |
| 329 | _ = context; |
| 306 | 330 | _ = alignment; |
| 307 | | _ = ret_addr; |
| 308 | | c.free(buf.ptr); |
| 331 | _ = return_address; |
| 332 | c.free(memory.ptr); |
| 309 | 333 | } |
| 310 | 334 | |
| 311 | 335 | /// On operating systems that support memory mapping, this allocator makes a |