zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 90c232bbe8e53c09df14536def54b33d92ddd3c5 (tree)
parent 9c4dc7b1bb79b2e1cf5a88d99b9e187640426769
Author: Jonathan Marler <jonathan.j.marler@hp.com>
Date:   Tue, 10 Mar 2020 12:06:10 -0600

add allocSentinel function

Diffstat:
Mlib/std/mem.zig | 14++++++++++++++
1 file changed, 14 insertions(+), 0 deletions(-)

diff --git a/lib/std/mem.zig b/lib/std/mem.zig @@ -105,6 +105,20 @@ pub const Allocator = struct { return self.alignedAlloc(T, null, n); } + /// Allocates an array of `n + 1` items of type `T` and sets the first `n` + /// items to `undefined` and the last item to `sentinel`. Depending on the + /// Allocator implementation, it may be required to call `free` once the + /// memory is no longer needed, to avoid a resource leak. If the + /// `Allocator` implementation is unknown, then correct code will + /// call `free` when done. + /// + /// For allocating a single item, see `create`. + pub fn allocSentinel(self: *Allocator, comptime Elem: type, n: usize, comptime sentinel: Elem) Error![:sentinel]Elem { + var ptr = try self.alloc(Elem, n + 1); + ptr[n] = sentinel; + return ptr[0 .. n :sentinel]; + } + pub fn alignedAlloc( self: *Allocator, comptime T: type,