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 {...@@ -475,7 +475,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
475 if (aligned_size > largest_bucket_object_size) {475 if (aligned_size > largest_bucket_object_size) {
476 return self.resizeLarge(old_mem, old_align, new_size, len_align, ret_addr);476 return self.resizeLarge(old_mem, old_align, new_size, len_align, ret_addr);
477 }477 }
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
480 var bucket_index = math.log2(size_class_hint);480 var bucket_index = math.log2(size_class_hint);
481 var size_class: usize = size_class_hint;481 var size_class: usize = size_class_hint;
...@@ -512,7 +512,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {...@@ -512,7 +512,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
512 return @as(usize, 0);512 return @as(usize, 0);
513 }513 }
514 const new_aligned_size = math.max(new_size, old_align);514 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);
516 if (new_size_class <= size_class) {516 if (new_size_class <= size_class) {
517 return new_size;517 return new_size;
518 }518 }
...@@ -553,7 +553,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {...@@ -553,7 +553,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
553553
554 return slice;554 return slice;
555 } else {555 } 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);
557 const ptr = try self.allocSlot(new_size_class, ret_addr);557 const ptr = try self.allocSlot(new_size_class, ret_addr);
558 return ptr[0..len];558 return ptr[0..len];
559 }559 }
...@@ -586,13 +586,6 @@ const TraceKind = enum {...@@ -586,13 +586,6 @@ const TraceKind = enum {
586 free,586 free,
587};587};
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
596fn hash_addr(addr: usize) u32 {589fn hash_addr(addr: usize) u32 {
597 if (@sizeOf(usize) == @sizeOf(u32))590 if (@sizeOf(usize) == @sizeOf(u32))
598 return addr;591 return addr;
lib/std/math.zig+4
...@@ -837,6 +837,10 @@ pub fn ceilPowerOfTwo(comptime T: type, value: T) (error{Overflow}!T) {...@@ -837,6 +837,10 @@ pub fn ceilPowerOfTwo(comptime T: type, value: T) (error{Overflow}!T) {
837 return @intCast(T, x);837 return @intCast(T, x);
838}838}
839839
840pub fn ceilPowerOfTwoAssert(comptime T: type, value: T) T {
841 return ceilPowerOfTwo(T, value) catch unreachable;
842}
843
840test "math.ceilPowerOfTwoPromote" {844test "math.ceilPowerOfTwoPromote" {
841 testCeilPowerOfTwoPromote();845 testCeilPowerOfTwoPromote();
842 comptime testCeilPowerOfTwoPromote();846 comptime testCeilPowerOfTwoPromote();