aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-08-08 00:37:22 -0700
committerAndrew Kelley <andrew@ziglang.org>2020-08-08 00:39:46 -0700
commit9f5a7d5922f48170551e7f6938b6de9eadeba0ff (patch)
tree15cf8d5affeec60acbf0abab243e6aee6dc43d16
parentcd6cdd0a752ba3e134ba371246cc02b1d7faaa88 (diff)
downloadzig-9f5a7d5922f48170551e7f6938b6de9eadeba0ff.tar.gz
zig-9f5a7d5922f48170551e7f6938b6de9eadeba0ff.zip
utilize math.ceilPowerOfTwo
-rw-r--r--lib/std/heap/general_purpose_allocator.zig13
-rw-r--r--lib/std/math.zig4
2 files changed, 7 insertions, 10 deletions
diff --git a/lib/std/heap/general_purpose_allocator.zig b/lib/std/heap/general_purpose_allocator.zig
index 9abf9cf253..98f029579c 100644
--- a/lib/std/heap/general_purpose_allocator.zig
+++ b/lib/std/heap/general_purpose_allocator.zig
@@ -475,7 +475,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
if (aligned_size > largest_bucket_object_size) {
return self.resizeLarge(old_mem, old_align, new_size, len_align, ret_addr);
}
- const size_class_hint = up_to_nearest_power_of_2(usize, aligned_size);
+ const size_class_hint = math.ceilPowerOfTwoAssert(usize, aligned_size);
var bucket_index = math.log2(size_class_hint);
var size_class: usize = size_class_hint;
@@ -512,7 +512,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
return @as(usize, 0);
}
const new_aligned_size = math.max(new_size, old_align);
- const new_size_class = up_to_nearest_power_of_2(usize, new_aligned_size);
+ const new_size_class = math.ceilPowerOfTwoAssert(usize, new_aligned_size);
if (new_size_class <= size_class) {
return new_size;
}
@@ -553,7 +553,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
return slice;
} else {
- const new_size_class = up_to_nearest_power_of_2(usize, new_aligned_size);
+ const new_size_class = math.ceilPowerOfTwoAssert(usize, new_aligned_size);
const ptr = try self.allocSlot(new_size_class, ret_addr);
return ptr[0..len];
}
@@ -586,13 +586,6 @@ const TraceKind = enum {
free,
};
-fn up_to_nearest_power_of_2(comptime T: type, n: T) T {
- var power: T = 1;
- while (power < n)
- power *= 2;
- return power;
-}
-
fn hash_addr(addr: usize) u32 {
if (@sizeOf(usize) == @sizeOf(u32))
return addr;
diff --git a/lib/std/math.zig b/lib/std/math.zig
index 111a618cef..17237ea9f0 100644
--- a/lib/std/math.zig
+++ b/lib/std/math.zig
@@ -837,6 +837,10 @@ pub fn ceilPowerOfTwo(comptime T: type, value: T) (error{Overflow}!T) {
return @intCast(T, x);
}
+pub fn ceilPowerOfTwoAssert(comptime T: type, value: T) T {
+ return ceilPowerOfTwo(T, value) catch unreachable;
+}
+
test "math.ceilPowerOfTwoPromote" {
testCeilPowerOfTwoPromote();
comptime testCeilPowerOfTwoPromote();