Browse Source

Add a minimum alignment on *nix for the compiler in heap_allocator_proc

gingerBill 4 years ago
parent
commit
d29a0c6680
1 changed files with 14 additions and 4 deletions
  1. 14 4
      src/common_memory.cpp

+ 14 - 4
src/common_memory.cpp

@@ -363,6 +363,9 @@ GB_ALLOCATOR_PROC(heap_allocator_proc) {
 			}
 			}
 			break;
 			break;
 		}
 		}
+		
+		alignment = gb_max(alignment, gb_align_of(max_align_t));
+		
 		if (old_memory == nullptr) {
 		if (old_memory == nullptr) {
 			ptr = aligned_alloc(alignment, (size + alignment - 1) & ~(alignment - 1));
 			ptr = aligned_alloc(alignment, (size + alignment - 1) & ~(alignment - 1));
 			gb_zero_size(ptr, size);
 			gb_zero_size(ptr, size);
@@ -375,12 +378,17 @@ GB_ALLOCATOR_PROC(heap_allocator_proc) {
 
 
 		ptr = aligned_alloc(alignment, (size + alignment - 1) & ~(alignment - 1));
 		ptr = aligned_alloc(alignment, (size + alignment - 1) & ~(alignment - 1));
 		gb_memmove(ptr, old_memory, old_size);
 		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;
 		break;
 #else
 #else
 	// TODO(bill): *nix version that's decent
 	// TODO(bill): *nix version that's decent
 	case gbAllocation_Alloc: {
 	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);
 		gb_zero_size(ptr, size);
 	} break;
 	} break;
 
 
@@ -396,6 +404,9 @@ GB_ALLOCATOR_PROC(heap_allocator_proc) {
 			free(old_memory);
 			free(old_memory);
 			break;
 			break;
 		}
 		}
+		
+		alignment = gb_max(alignment, gb_align_of(max_align_t));
+		
 		if (old_memory == nullptr) {
 		if (old_memory == nullptr) {
 			err = posix_memalign(&ptr, alignment, size);
 			err = posix_memalign(&ptr, alignment, size);
 			GB_ASSERT_MSG(err == 0, "posix_memalign err: %d", err);
 			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_ASSERT(ptr != nullptr);
 		gb_memmove(ptr, old_memory, old_size);
 		gb_memmove(ptr, old_memory, old_size);
 		free(old_memory);
 		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;
 	} break;
 #endif
 #endif