aboutsummaryrefslogtreecommitdiff
path: root/lib/std/mem.zig
diff options
context:
space:
mode:
authorJacob Young <jacobly0@users.noreply.github.com>2024-08-17 01:15:04 -0400
committerGitHub <noreply@github.com>2024-08-17 01:15:04 -0400
commitbb70501060a8bfff25818cf1d80491d724f8a634 (patch)
tree546c8d93fcbdf4e2f3e2656d5d4f45bc79e9d483 /lib/std/mem.zig
parent90989be0e31a91335f8d1c1eafb84c3b34792a8c (diff)
parented19ecd115beedfbf496c6f20995e74fbcd8ccb4 (diff)
downloadzig-bb70501060a8bfff25818cf1d80491d724f8a634.tar.gz
zig-bb70501060a8bfff25818cf1d80491d724f8a634.zip
Merge pull request #21078 from jacobly0/new-dwarf
Dwarf: rework self-hosted debug info from scratch
Diffstat (limited to 'lib/std/mem.zig')
-rw-r--r--lib/std/mem.zig19
1 files changed, 14 insertions, 5 deletions
diff --git a/lib/std/mem.zig b/lib/std/mem.zig
index 685e36feae..c2c168a2ae 100644
--- a/lib/std/mem.zig
+++ b/lib/std/mem.zig
@@ -128,7 +128,7 @@ pub fn alignAllocLen(full_len: usize, alloc_len: usize, len_align: u29) usize {
assert(full_len >= alloc_len);
if (len_align == 0)
return alloc_len;
- const adjusted = alignBackwardAnyAlign(full_len, len_align);
+ const adjusted = alignBackwardAnyAlign(usize, full_len, len_align);
assert(adjusted >= alloc_len);
return adjusted;
}
@@ -4312,6 +4312,15 @@ test "sliceAsBytes preserves pointer attributes" {
try testing.expectEqual(in.alignment, out.alignment);
}
+/// Round an address down to the next (or current) aligned address.
+/// Unlike `alignForward`, `alignment` can be any positive number, not just a power of 2.
+pub fn alignForwardAnyAlign(comptime T: type, addr: T, alignment: T) T {
+ if (isValidAlignGeneric(T, alignment))
+ return alignForward(T, addr, alignment);
+ assert(alignment != 0);
+ return alignBackwardAnyAlign(T, addr + (alignment - 1), alignment);
+}
+
/// Round an address up to the next (or current) aligned address.
/// The alignment must be a power of 2 and greater than 0.
/// Asserts that rounding up the address does not cause integer overflow.
@@ -4433,11 +4442,11 @@ test alignForward {
/// Round an address down to the previous (or current) aligned address.
/// Unlike `alignBackward`, `alignment` can be any positive number, not just a power of 2.
-pub fn alignBackwardAnyAlign(i: usize, alignment: usize) usize {
- if (isValidAlign(alignment))
- return alignBackward(usize, i, alignment);
+pub fn alignBackwardAnyAlign(comptime T: type, addr: T, alignment: T) T {
+ if (isValidAlignGeneric(T, alignment))
+ return alignBackward(T, addr, alignment);
assert(alignment != 0);
- return i - @mod(i, alignment);
+ return addr - @mod(addr, alignment);
}
/// Round an address down to the previous (or current) aligned address.