authorgravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2020-03-10 12:06:10-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-10 15:03:59-04:00
log90c232bbe8e53c09df14536def54b33d92ddd3c5
tree199c5412f08071a8afea4a87cea0eec78686b23b
parent9c4dc7b1bb79b2e1cf5a88d99b9e187640426769

add allocSentinel function


1 files changed, 14 insertions(+), 0 deletions(-)

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