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