| ... | ... | @@ -48,15 +48,16 @@ const CAllocator = struct { |
| 48 | 48 | pub const supports_malloc_size = false; |
| 49 | 49 | }; |
| 50 | 50 | |
| 51 | | pub const supports_posix_memalign = false and @hasDecl(c, "posix_memalign"); |
| 51 | pub const supports_posix_memalign = @hasDecl(c, "posix_memalign"); |
| 52 | 52 | |
| 53 | | fn get_header(ptr: [*]u8) *[*]u8 { |
| 53 | fn getHeader(ptr: [*]u8) *[*]u8 { |
| 54 | 54 | return @intToPtr(*[*]u8, @ptrToInt(ptr) - @sizeOf(usize)); |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | | fn aligned_alloc(len: usize, alignment: usize) ?[*]u8 { |
| 57 | fn alignedAlloc(len: usize, alignment: usize) ?[*]u8 { |
| 58 | 58 | 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 |
| 60 | 61 | const eff_alignment = std.math.max(alignment, @sizeOf(usize)); |
| 61 | 62 | |
| 62 | 63 | var aligned_ptr: ?*c_void = undefined; |
| ... | ... | @@ -73,26 +74,26 @@ const CAllocator = struct { |
| 73 | 74 | const unaligned_addr = @ptrToInt(unaligned_ptr); |
| 74 | 75 | const aligned_addr = mem.alignForward(unaligned_addr + @sizeOf(usize), alignment); |
| 75 | 76 | var aligned_ptr = unaligned_ptr + (aligned_addr - unaligned_addr); |
| 76 | | get_header(aligned_ptr).* = unaligned_ptr; |
| 77 | getHeader(aligned_ptr).* = unaligned_ptr; |
| 77 | 78 | |
| 78 | 79 | return aligned_ptr; |
| 79 | 80 | } |
| 80 | 81 | |
| 81 | | fn aligned_free(ptr: [*]u8) void { |
| 82 | fn alignedFree(ptr: [*]u8) void { |
| 82 | 83 | if (supports_posix_memalign) { |
| 83 | 84 | return c.free(ptr); |
| 84 | 85 | } |
| 85 | 86 | |
| 86 | | const unaligned_ptr = get_header(ptr).*; |
| 87 | const unaligned_ptr = getHeader(ptr).*; |
| 87 | 88 | c.free(unaligned_ptr); |
| 88 | 89 | } |
| 89 | 90 | |
| 90 | | fn aligned_alloc_size(ptr: [*]u8) usize { |
| 91 | fn alignedAllocSize(ptr: [*]u8) usize { |
| 91 | 92 | if (supports_posix_memalign) { |
| 92 | 93 | return malloc_size(ptr); |
| 93 | 94 | } |
| 94 | 95 | |
| 95 | | const unaligned_ptr = get_header(ptr).*; |
| 96 | const unaligned_ptr = getHeader(ptr).*; |
| 96 | 97 | const delta = @ptrToInt(ptr) - @ptrToInt(unaligned_ptr); |
| 97 | 98 | return malloc_size(unaligned_ptr) - delta; |
| 98 | 99 | } |
| ... | ... | @@ -107,13 +108,13 @@ const CAllocator = struct { |
| 107 | 108 | assert(len > 0); |
| 108 | 109 | assert(std.math.isPowerOfTwo(alignment)); |
| 109 | 110 | |
| 110 | | var ptr = aligned_alloc(len, alignment) orelse return error.OutOfMemory; |
| 111 | var ptr = alignedAlloc(len, alignment) orelse return error.OutOfMemory; |
| 111 | 112 | if (len_align == 0) { |
| 112 | 113 | return ptr[0..len]; |
| 113 | 114 | } |
| 114 | 115 | const full_len = init: { |
| 115 | 116 | if (supports_malloc_size) { |
| 116 | | const s = aligned_alloc_size(ptr); |
| 117 | const s = alignedAllocSize(ptr); |
| 117 | 118 | assert(s >= len); |
| 118 | 119 | break :init s; |
| 119 | 120 | } |
| ... | ... | @@ -131,14 +132,14 @@ const CAllocator = struct { |
| 131 | 132 | return_address: usize, |
| 132 | 133 | ) Allocator.Error!usize { |
| 133 | 134 | if (new_len == 0) { |
| 134 | | aligned_free(buf.ptr); |
| 135 | alignedFree(buf.ptr); |
| 135 | 136 | return 0; |
| 136 | 137 | } |
| 137 | 138 | if (new_len <= buf.len) { |
| 138 | 139 | return mem.alignAllocLen(buf.len, new_len, len_align); |
| 139 | 140 | } |
| 140 | 141 | if (supports_malloc_size) { |
| 141 | | const full_len = aligned_alloc_size(buf.ptr); |
| 142 | const full_len = alignedAllocSize(buf.ptr); |
| 142 | 143 | if (new_len <= full_len) { |
| 143 | 144 | return mem.alignAllocLen(full_len, new_len, len_align); |
| 144 | 145 | } |