authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-09-22 14:42:01+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-11-05 16:10:15+01:00
loge749ab1d63cfdacb3ac9e511d1ef52e77799c82b
tree9a19f4fdd079ae3a985c43c906b3c2897da1681a
parent53433cdea2cee73caea52a6baa10dc7bc7f6da4d

Fix typo, remove debug leftover, rename few fns


1 files changed, 14 insertions(+), 13 deletions(-)

lib/std/heap.zig+14-13
......@@ -48,15 +48,16 @@ const CAllocator = struct {
4848 pub const supports_malloc_size = false;
4949 };
5050
51 pub const supports_posix_memalign = false and @hasDecl(c, "posix_memalign");
51 pub const supports_posix_memalign = @hasDecl(c, "posix_memalign");
5252
53 fn get_header(ptr: [*]u8) *[*]u8 {
53 fn getHeader(ptr: [*]u8) *[*]u8 {
5454 return @intToPtr(*[*]u8, @ptrToInt(ptr) - @sizeOf(usize));
5555 }
5656
57 fn aligned_alloc(len: usize, alignment: usize) ?[*]u8 {
57 fn alignedAlloc(len: usize, alignment: usize) ?[*]u8 {
5858 if (supports_posix_memalign) {
59 // The minimum alignment supported posix_memalign is the pointer size
59 // The posix_memalign only accepts alignment values that are a
60 // multiple of the pointer size
6061 const eff_alignment = std.math.max(alignment, @sizeOf(usize));
6162
6263 var aligned_ptr: ?*c_void = undefined;
......@@ -73,26 +74,26 @@ const CAllocator = struct {
7374 const unaligned_addr = @ptrToInt(unaligned_ptr);
7475 const aligned_addr = mem.alignForward(unaligned_addr + @sizeOf(usize), alignment);
7576 var aligned_ptr = unaligned_ptr + (aligned_addr - unaligned_addr);
76 get_header(aligned_ptr).* = unaligned_ptr;
77 getHeader(aligned_ptr).* = unaligned_ptr;
7778
7879 return aligned_ptr;
7980 }
8081
81 fn aligned_free(ptr: [*]u8) void {
82 fn alignedFree(ptr: [*]u8) void {
8283 if (supports_posix_memalign) {
8384 return c.free(ptr);
8485 }
8586
86 const unaligned_ptr = get_header(ptr).*;
87 const unaligned_ptr = getHeader(ptr).*;
8788 c.free(unaligned_ptr);
8889 }
8990
90 fn aligned_alloc_size(ptr: [*]u8) usize {
91 fn alignedAllocSize(ptr: [*]u8) usize {
9192 if (supports_posix_memalign) {
9293 return malloc_size(ptr);
9394 }
9495
95 const unaligned_ptr = get_header(ptr).*;
96 const unaligned_ptr = getHeader(ptr).*;
9697 const delta = @ptrToInt(ptr) - @ptrToInt(unaligned_ptr);
9798 return malloc_size(unaligned_ptr) - delta;
9899 }
......@@ -107,13 +108,13 @@ const CAllocator = struct {
107108 assert(len > 0);
108109 assert(std.math.isPowerOfTwo(alignment));
109110
110 var ptr = aligned_alloc(len, alignment) orelse return error.OutOfMemory;
111 var ptr = alignedAlloc(len, alignment) orelse return error.OutOfMemory;
111112 if (len_align == 0) {
112113 return ptr[0..len];
113114 }
114115 const full_len = init: {
115116 if (supports_malloc_size) {
116 const s = aligned_alloc_size(ptr);
117 const s = alignedAllocSize(ptr);
117118 assert(s >= len);
118119 break :init s;
119120 }
......@@ -131,14 +132,14 @@ const CAllocator = struct {
131132 return_address: usize,
132133 ) Allocator.Error!usize {
133134 if (new_len == 0) {
134 aligned_free(buf.ptr);
135 alignedFree(buf.ptr);
135136 return 0;
136137 }
137138 if (new_len <= buf.len) {
138139 return mem.alignAllocLen(buf.len, new_len, len_align);
139140 }
140141 if (supports_malloc_size) {
141 const full_len = aligned_alloc_size(buf.ptr);
142 const full_len = alignedAllocSize(buf.ptr);
142143 if (new_len <= full_len) {
143144 return mem.alignAllocLen(full_len, new_len, len_align);
144145 }