| ... | @@ -8,6 +8,7 @@ const assert = std.debug.assert; | ... | @@ -8,6 +8,7 @@ const assert = std.debug.assert; |
| 8 | const math = std.math; | 8 | const math = std.math; |
| 9 | const mem = std.mem; | 9 | const mem = std.mem; |
| 10 | const Alignment = std.mem.Alignment; | 10 | const Alignment = std.mem.Alignment; |
| | 11 | const Slice = std.meta.Slice; |
| 11 | | 12 | |
| 12 | pub const Error = error{OutOfMemory}; | 13 | pub const Error = error{OutOfMemory}; |
| 13 | pub const Log2Align = math.Log2Int(usize); | 14 | pub const Log2Align = math.Log2Int(usize); |
| ... | @@ -305,25 +306,6 @@ pub fn allocBytesAligned( | ... | @@ -305,25 +306,6 @@ pub fn allocBytesAligned( |
| 305 | return @alignCast(byte_ptr); | 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 | /// Request to modify the size of an allocation. | 309 | /// Request to modify the size of an allocation. |
| 328 | /// | 310 | /// |
| 329 | /// It is guaranteed to not move the pointer, however the allocator | 311 | /// It is guaranteed to not move the pointer, however the allocator |
| ... | @@ -336,7 +318,7 @@ fn SliceType(comptime Pointer: type) type { | ... | @@ -336,7 +318,7 @@ fn SliceType(comptime Pointer: type) type { |
| 336 | pub fn resize(self: Allocator, allocation: anytype, new_len: usize) bool { | 318 | pub fn resize(self: Allocator, allocation: anytype, new_len: usize) bool { |
| 337 | const slice_info = @typeInfo(@TypeOf(allocation)).pointer; | 319 | const slice_info = @typeInfo(@TypeOf(allocation)).pointer; |
| 338 | if (slice_info.size != .slice) { | 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 | return resize(self, slice, new_len); | 322 | return resize(self, slice, new_len); |
| 341 | } | 323 | } |
| 342 | comptime assert(slice_info.size == .slice); | 324 | comptime assert(slice_info.size == .slice); |
| ... | @@ -377,10 +359,10 @@ pub fn resize(self: Allocator, allocation: anytype, new_len: usize) bool { | ... | @@ -377,10 +359,10 @@ pub fn resize(self: Allocator, allocation: anytype, new_len: usize) bool { |
| 377 | /// `new_len` may be zero, in which case the allocation is freed. | 359 | /// `new_len` may be zero, in which case the allocation is freed. |
| 378 | /// | 360 | /// |
| 379 | /// If the allocation's elements' type is zero bytes sized, `allocation.len` is set to `new_len`. | 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 | const slice_info = @typeInfo(@TypeOf(allocation)).pointer; | 363 | const slice_info = @typeInfo(@TypeOf(allocation)).pointer; |
| 382 | if (slice_info.size != .slice) { | 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 | return remap(self, slice, new_len); | 366 | return remap(self, slice, new_len); |
| 385 | } | 367 | } |
| 386 | comptime assert(slice_info.size == .slice); | 368 | comptime assert(slice_info.size == .slice); |
| ... | @@ -426,7 +408,7 @@ pub fn remap(self: Allocator, allocation: anytype, new_len: usize) ?SliceType(@T | ... | @@ -426,7 +408,7 @@ pub fn remap(self: Allocator, allocation: anytype, new_len: usize) ?SliceType(@T |
| 426 | /// do the realloc more efficiently than the caller | 408 | /// do the realloc more efficiently than the caller |
| 427 | /// * `resize` which returns `false` when the `Allocator` implementation cannot | 409 | /// * `resize` which returns `false` when the `Allocator` implementation cannot |
| 428 | /// change the size without relocating the allocation. | 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 | return self.reallocAdvanced(old_mem, new_n, @returnAddress()); | 412 | return self.reallocAdvanced(old_mem, new_n, @returnAddress()); |
| 431 | } | 413 | } |
| 432 | | 414 | |
| ... | @@ -435,10 +417,10 @@ pub fn reallocAdvanced( | ... | @@ -435,10 +417,10 @@ pub fn reallocAdvanced( |
| 435 | old_mem: anytype, | 417 | old_mem: anytype, |
| 436 | new_n: usize, | 418 | new_n: usize, |
| 437 | return_address: usize, | 419 | return_address: usize, |
| 438 | ) Error!SliceType(@TypeOf(old_mem)) { | 420 | ) Error!Slice(@TypeOf(old_mem)) { |
| 439 | const slice_info = @typeInfo(@TypeOf(old_mem)).pointer; | 421 | const slice_info = @typeInfo(@TypeOf(old_mem)).pointer; |
| 440 | if (slice_info.size != .slice) { | 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 | return reallocAdvanced(self, slice, new_n, return_address); | 424 | return reallocAdvanced(self, slice, new_n, return_address); |
| 443 | } | 425 | } |
| 444 | comptime assert(slice_info.size == .slice); | 426 | comptime assert(slice_info.size == .slice); |
| ... | @@ -477,7 +459,7 @@ pub fn reallocAdvanced( | ... | @@ -477,7 +459,7 @@ pub fn reallocAdvanced( |
| 477 | pub fn free(self: Allocator, memory: anytype) void { | 459 | pub fn free(self: Allocator, memory: anytype) void { |
| 478 | const slice_info = @typeInfo(@TypeOf(memory)).pointer; | 460 | const slice_info = @typeInfo(@TypeOf(memory)).pointer; |
| 479 | if (slice_info.size != .slice) { | 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 | return free(self, slice); | 463 | return free(self, slice); |
| 482 | } | 464 | } |
| 483 | comptime assert(slice_info.size == .slice); | 465 | comptime assert(slice_info.size == .slice); |