diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-07-23 14:28:14 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-07-23 14:28:14 -0400 |
| commit | 10d2f08d376a302bd79e87d17c06922d3145a939 (patch) | |
| tree | 1251086911401d33cda814ad87d0fce50da70184 /std | |
| parent | d767fae47e89aef53505191cf11ce7e592a784b2 (diff) | |
| download | zig-10d2f08d376a302bd79e87d17c06922d3145a939.tar.gz zig-10d2f08d376a302bd79e87d17c06922d3145a939.zip | |
self-hosted: fix error messages not cleaning up correctly
Diffstat (limited to 'std')
| -rw-r--r-- | std/mem.zig | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/std/mem.zig b/std/mem.zig index 2a5b0366a9..43961a6d14 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -35,6 +35,7 @@ pub const Allocator = struct { freeFn: fn (self: *Allocator, old_mem: []u8) void, /// Call `destroy` with the result + /// TODO this is deprecated. use createOne instead pub fn create(self: *Allocator, init: var) Error!*@typeOf(init) { const T = @typeOf(init); if (@sizeOf(T) == 0) return &(T{}); @@ -44,6 +45,14 @@ pub const Allocator = struct { return ptr; } + /// Call `destroy` with the result. + /// Returns undefined memory. + pub fn createOne(self: *Allocator, comptime T: type) Error!*T { + if (@sizeOf(T) == 0) return &(T{}); + const slice = try self.alloc(T, 1); + return &slice[0]; + } + /// `ptr` should be the return value of `create` pub fn destroy(self: *Allocator, ptr: var) void { const non_const_ptr = @intToPtr([*]u8, @ptrToInt(ptr)); @@ -149,13 +158,12 @@ pub fn copyBackwards(comptime T: type, dest: []T, source: []const T) void { @setRuntimeSafety(false); assert(dest.len >= source.len); var i = source.len; - while(i > 0){ + while (i > 0) { i -= 1; dest[i] = source[i]; } } - pub fn set(comptime T: type, dest: []T, value: T) void { for (dest) |*d| d.* = value; |
