diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-05-17 13:53:27 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-17 13:53:27 -0400 |
| commit | 16f100b82e4075a047f008c0de6c44fc418eb58e (patch) | |
| tree | 93555fbcba29bcd1120349a03ae6904321cb7718 /lib/std/mem.zig | |
| parent | 9a22c8b6ca98fd01795f8cd4f3e9d92311175f13 (diff) | |
| parent | b0968abccbfb4072528c3b5e039bc03b27af89a1 (diff) | |
| download | zig-16f100b82e4075a047f008c0de6c44fc418eb58e.tar.gz zig-16f100b82e4075a047f008c0de6c44fc418eb58e.zip | |
Merge pull request #5307 from ziglang/self-hosted-incremental-compilation
rework self-hosted compiler for incremental builds
Diffstat (limited to 'lib/std/mem.zig')
| -rw-r--r-- | lib/std/mem.zig | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 86fe85753a..cfd3fd38d8 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -279,6 +279,21 @@ pub const Allocator = struct { const shrink_result = self.shrinkFn(self, non_const_ptr[0..bytes_len], Slice.alignment, 0, 1); assert(shrink_result.len == 0); } + + /// Copies `m` to newly allocated memory. Caller owns the memory. + pub fn dupe(allocator: *Allocator, comptime T: type, m: []const T) ![]T { + const new_buf = try allocator.alloc(T, m.len); + copy(T, new_buf, m); + return new_buf; + } + + /// Copies `m` to newly allocated memory, with a null-terminated element. Caller owns the memory. + pub fn dupeZ(allocator: *Allocator, comptime T: type, m: []const T) ![:0]T { + const new_buf = try allocator.alloc(T, m.len + 1); + copy(T, new_buf, m); + new_buf[m.len] = 0; + return new_buf[0..m.len :0]; + } }; var failAllocator = Allocator { @@ -785,19 +800,14 @@ pub fn allEqual(comptime T: type, slice: []const T, scalar: T) bool { return true; } -/// Copies `m` to newly allocated memory. Caller owns the memory. +/// Deprecated, use `Allocator.dupe`. pub fn dupe(allocator: *Allocator, comptime T: type, m: []const T) ![]T { - const new_buf = try allocator.alloc(T, m.len); - copy(T, new_buf, m); - return new_buf; + return allocator.dupe(T, m); } -/// Copies `m` to newly allocated memory, with a null-terminated element. Caller owns the memory. +/// Deprecated, use `Allocator.dupeZ`. pub fn dupeZ(allocator: *Allocator, comptime T: type, m: []const T) ![:0]T { - const new_buf = try allocator.alloc(T, m.len + 1); - copy(T, new_buf, m); - new_buf[m.len] = 0; - return new_buf[0..m.len :0]; + return allocator.dupeZ(T, m); } /// Remove values from the beginning of a slice. @@ -2112,7 +2122,11 @@ pub fn alignBackwardGeneric(comptime T: type, addr: T, alignment: T) T { /// Given an address and an alignment, return true if the address is a multiple of the alignment /// The alignment must be a power of 2 and greater than 0. pub fn isAligned(addr: usize, alignment: usize) bool { - return alignBackward(addr, alignment) == addr; + return isAlignedGeneric(u64, addr, alignment); +} + +pub fn isAlignedGeneric(comptime T: type, addr: T, alignment: T) bool { + return alignBackwardGeneric(T, addr, alignment) == addr; } test "isAligned" { |
