authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-08 00:37:22-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-08 00:39:46-07:00
log9f5a7d5922f48170551e7f6938b6de9eadeba0ff
tree15cf8d5affeec60acbf0abab243e6aee6dc43d16
parentcd6cdd0a752ba3e134ba371246cc02b1d7faaa88

utilize math.ceilPowerOfTwo


2 files changed, 7 insertions(+), 10 deletions(-)

lib/std/heap/general_purpose_allocator.zig+3-10
......@@ -475,7 +475,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
475475 if (aligned_size > largest_bucket_object_size) {
476476 return self.resizeLarge(old_mem, old_align, new_size, len_align, ret_addr);
477477 }
478 const size_class_hint = up_to_nearest_power_of_2(usize, aligned_size);
478 const size_class_hint = math.ceilPowerOfTwoAssert(usize, aligned_size);
479479
480480 var bucket_index = math.log2(size_class_hint);
481481 var size_class: usize = size_class_hint;
......@@ -512,7 +512,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
512512 return @as(usize, 0);
513513 }
514514 const new_aligned_size = math.max(new_size, old_align);
515 const new_size_class = up_to_nearest_power_of_2(usize, new_aligned_size);
515 const new_size_class = math.ceilPowerOfTwoAssert(usize, new_aligned_size);
516516 if (new_size_class <= size_class) {
517517 return new_size;
518518 }
......@@ -553,7 +553,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
553553
554554 return slice;
555555 } else {
556 const new_size_class = up_to_nearest_power_of_2(usize, new_aligned_size);
556 const new_size_class = math.ceilPowerOfTwoAssert(usize, new_aligned_size);
557557 const ptr = try self.allocSlot(new_size_class, ret_addr);
558558 return ptr[0..len];
559559 }
......@@ -586,13 +586,6 @@ const TraceKind = enum {
586586 free,
587587};
588588
589fn up_to_nearest_power_of_2(comptime T: type, n: T) T {
590 var power: T = 1;
591 while (power < n)
592 power *= 2;
593 return power;
594}
595
596589fn hash_addr(addr: usize) u32 {
597590 if (@sizeOf(usize) == @sizeOf(u32))
598591 return addr;
lib/std/math.zig+4
......@@ -837,6 +837,10 @@ pub fn ceilPowerOfTwo(comptime T: type, value: T) (error{Overflow}!T) {
837837 return @intCast(T, x);
838838}
839839
840pub fn ceilPowerOfTwoAssert(comptime T: type, value: T) T {
841 return ceilPowerOfTwo(T, value) catch unreachable;
842}
843
840844test "math.ceilPowerOfTwoPromote" {
841845 testCeilPowerOfTwoPromote();
842846 comptime testCeilPowerOfTwoPromote();