aboutsummaryrefslogtreecommitdiff
path: root/src/common_memory.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-09-08 14:53:42 +0100
committergingerBill <bill@gingerbill.org>2021-09-08 14:53:42 +0100
commitd29a0c6680d421a785ea8d00025060f2a8712d29 (patch)
treec36f7815eebdd6d955a41a9550637dd9156a5885 /src/common_memory.cpp
parentff40e47042f46ed9f74ee9c480a82448a3d7b8b1 (diff)
Add a minimum alignment on *nix for the compiler in heap_allocator_proc
Diffstat (limited to 'src/common_memory.cpp')
-rw-r--r--src/common_memory.cpp18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/common_memory.cpp b/src/common_memory.cpp
index 7b4c03a41..115c58942 100644
--- a/src/common_memory.cpp
+++ b/src/common_memory.cpp
@@ -363,6 +363,9 @@ GB_ALLOCATOR_PROC(heap_allocator_proc) {
}
break;
}
+
+ alignment = gb_max(alignment, gb_align_of(max_align_t));
+
if (old_memory == nullptr) {
ptr = aligned_alloc(alignment, (size + alignment - 1) & ~(alignment - 1));
gb_zero_size(ptr, size);
@@ -375,12 +378,17 @@ GB_ALLOCATOR_PROC(heap_allocator_proc) {
ptr = aligned_alloc(alignment, (size + alignment - 1) & ~(alignment - 1));
gb_memmove(ptr, old_memory, old_size);
- gb_zero_size(cast(u8 *)ptr + old_size, gb_max(size-old_size, 0));
+ free(old_memory);
+ gb_zero_size(cast(u8 *)ptr + old_size, gb_max(size-old_size, 0););
break;
#else
// TODO(bill): *nix version that's decent
case gbAllocation_Alloc: {
- int err = posix_memalign(&ptr, alignment, size);
+ int err = 0;
+ alignment = gb_max(alignment, gb_align_of(max_align_t));
+
+ err = posix_memalign(&ptr, alignment, size);
+ GB_ASSERT_MSG(err == 0, "posix_memalign err: %d", err);
gb_zero_size(ptr, size);
} break;
@@ -396,6 +404,9 @@ GB_ALLOCATOR_PROC(heap_allocator_proc) {
free(old_memory);
break;
}
+
+ alignment = gb_max(alignment, gb_align_of(max_align_t));
+
if (old_memory == nullptr) {
err = posix_memalign(&ptr, alignment, size);
GB_ASSERT_MSG(err == 0, "posix_memalign err: %d", err);
@@ -413,8 +424,7 @@ GB_ALLOCATOR_PROC(heap_allocator_proc) {
GB_ASSERT(ptr != nullptr);
gb_memmove(ptr, old_memory, old_size);
free(old_memory);
- isize n = gb_max(size-old_size, 0);
- gb_zero_size(cast(u8 *)ptr + old_size, n);
+ gb_zero_size(cast(u8 *)ptr + old_size, gb_max(size-old_size, 0));
} break;
#endif