| ... | ... | @@ -8,6 +8,7 @@ const assert = std.debug.assert; |
| 8 | 8 | const math = std.math; |
| 9 | 9 | const mem = std.mem; |
| 10 | 10 | const Alignment = std.mem.Alignment; |
| 11 | const Slice = std.meta.Slice; |
| 11 | 12 | |
| 12 | 13 | pub const Error = error{OutOfMemory}; |
| 13 | 14 | pub const Log2Align = math.Log2Int(usize); |
| ... | ... | @@ -305,25 +306,6 @@ pub fn allocBytesAligned( |
| 305 | 306 | return @alignCast(byte_ptr); |
| 306 | 307 | } |
| 307 | 308 | |
| 308 | | fn SliceType(comptime Pointer: type) type { |
| 309 | | const info = @typeInfo(Pointer).pointer; |
| 310 | | switch (info.size) { |
| 311 | | .slice => return Pointer, |
| 312 | | .one => { |
| 313 | | const child_info = @typeInfo(info.child); |
| 314 | | comptime assert(child_info == .array); |
| 315 | | const sentinel_ptr: ?*const child_info.array.child = @ptrCast(@alignCast(child_info.array.sentinel_ptr)); |
| 316 | | return @Pointer( |
| 317 | | .slice, |
| 318 | | info.attrs, |
| 319 | | child_info.array.child, |
| 320 | | if (sentinel_ptr) |ptr| ptr.* else null, |
| 321 | | ); |
| 322 | | }, |
| 323 | | else => unreachable, |
| 324 | | } |
| 325 | | } |
| 326 | | |
| 327 | 309 | /// Request to modify the size of an allocation. |
| 328 | 310 | /// |
| 329 | 311 | /// It is guaranteed to not move the pointer, however the allocator |
| ... | ... | @@ -336,7 +318,7 @@ fn SliceType(comptime Pointer: type) type { |
| 336 | 318 | pub fn resize(self: Allocator, allocation: anytype, new_len: usize) bool { |
| 337 | 319 | const slice_info = @typeInfo(@TypeOf(allocation)).pointer; |
| 338 | 320 | if (slice_info.size != .slice) { |
| 339 | | const slice: SliceType(@TypeOf(allocation)) = allocation; // coerce *[len]T to []T |
| 321 | const slice: Slice(@TypeOf(allocation)) = allocation; // coerce *[len]T to []T |
| 340 | 322 | return resize(self, slice, new_len); |
| 341 | 323 | } |
| 342 | 324 | comptime assert(slice_info.size == .slice); |
| ... | ... | @@ -377,10 +359,10 @@ pub fn resize(self: Allocator, allocation: anytype, new_len: usize) bool { |
| 377 | 359 | /// `new_len` may be zero, in which case the allocation is freed. |
| 378 | 360 | /// |
| 379 | 361 | /// If the allocation's elements' type is zero bytes sized, `allocation.len` is set to `new_len`. |
| 380 | | pub fn remap(self: Allocator, allocation: anytype, new_len: usize) ?SliceType(@TypeOf(allocation)) { |
| 362 | pub fn remap(self: Allocator, allocation: anytype, new_len: usize) ?Slice(@TypeOf(allocation)) { |
| 381 | 363 | const slice_info = @typeInfo(@TypeOf(allocation)).pointer; |
| 382 | 364 | if (slice_info.size != .slice) { |
| 383 | | const slice: SliceType(@TypeOf(allocation)) = allocation; // coerce *[len]T to []T |
| 365 | const slice: Slice(@TypeOf(allocation)) = allocation; // coerce *[len]T to []T |
| 384 | 366 | return remap(self, slice, new_len); |
| 385 | 367 | } |
| 386 | 368 | comptime assert(slice_info.size == .slice); |
| ... | ... | @@ -426,7 +408,7 @@ pub fn remap(self: Allocator, allocation: anytype, new_len: usize) ?SliceType(@T |
| 426 | 408 | /// do the realloc more efficiently than the caller |
| 427 | 409 | /// * `resize` which returns `false` when the `Allocator` implementation cannot |
| 428 | 410 | /// change the size without relocating the allocation. |
| 429 | | pub fn realloc(self: Allocator, old_mem: anytype, new_n: usize) Error!SliceType(@TypeOf(old_mem)) { |
| 411 | pub fn realloc(self: Allocator, old_mem: anytype, new_n: usize) Error!Slice(@TypeOf(old_mem)) { |
| 430 | 412 | return self.reallocAdvanced(old_mem, new_n, @returnAddress()); |
| 431 | 413 | } |
| 432 | 414 | |
| ... | ... | @@ -435,10 +417,10 @@ pub fn reallocAdvanced( |
| 435 | 417 | old_mem: anytype, |
| 436 | 418 | new_n: usize, |
| 437 | 419 | return_address: usize, |
| 438 | | ) Error!SliceType(@TypeOf(old_mem)) { |
| 420 | ) Error!Slice(@TypeOf(old_mem)) { |
| 439 | 421 | const slice_info = @typeInfo(@TypeOf(old_mem)).pointer; |
| 440 | 422 | if (slice_info.size != .slice) { |
| 441 | | const slice: SliceType(@TypeOf(old_mem)) = old_mem; // coerce *[len]T to []T |
| 423 | const slice: Slice(@TypeOf(old_mem)) = old_mem; // coerce *[len]T to []T |
| 442 | 424 | return reallocAdvanced(self, slice, new_n, return_address); |
| 443 | 425 | } |
| 444 | 426 | comptime assert(slice_info.size == .slice); |
| ... | ... | @@ -477,7 +459,7 @@ pub fn reallocAdvanced( |
| 477 | 459 | pub fn free(self: Allocator, memory: anytype) void { |
| 478 | 460 | const slice_info = @typeInfo(@TypeOf(memory)).pointer; |
| 479 | 461 | if (slice_info.size != .slice) { |
| 480 | | const slice: SliceType(@TypeOf(memory)) = memory; // coerce *[len]T to []T |
| 462 | const slice: Slice(@TypeOf(memory)) = memory; // coerce *[len]T to []T |
| 481 | 463 | return free(self, slice); |
| 482 | 464 | } |
| 483 | 465 | comptime assert(slice_info.size == .slice); |