Browse Source

Add some minor sanity checks to the compiler's `heap_allocator_proc` on Darwin

gingerBill 4 years ago
parent
commit
76419383a8
2 changed files with 23 additions and 12 deletions
  1. 15 8
      src/common_memory.cpp
  2. 8 4
      src/gb/gb.h

+ 15 - 8
src/common_memory.cpp

@@ -379,10 +379,10 @@ GB_ALLOCATOR_PROC(heap_allocator_proc) {
 		break;
 		break;
 #else
 #else
 	// TODO(bill): *nix version that's decent
 	// TODO(bill): *nix version that's decent
-	case gbAllocation_Alloc:
-		posix_memalign(&ptr, alignment, size);
+	case gbAllocation_Alloc: {
+		int err = posix_memalign(&ptr, alignment, size);
 		gb_zero_size(ptr, size);
 		gb_zero_size(ptr, size);
-		break;
+	} break;
 
 
 	case gbAllocation_Free:
 	case gbAllocation_Free:
 		if (old_memory != nullptr) {
 		if (old_memory != nullptr) {
@@ -390,13 +390,16 @@ GB_ALLOCATOR_PROC(heap_allocator_proc) {
 		}
 		}
 		break;
 		break;
 
 
-	case gbAllocation_Resize:
+	case gbAllocation_Resize: {
+		int err = 0;
 		if (size == 0) {
 		if (size == 0) {
 			free(old_memory);
 			free(old_memory);
 			break;
 			break;
 		}
 		}
 		if (old_memory == nullptr) {
 		if (old_memory == nullptr) {
-			posix_memalign(&ptr, alignment, size);
+			err = posix_memalign(&ptr, alignment, size);
+			GB_ASSERT_MSG(err == 0, "posix_memalign err: %d", err);
+			GB_ASSERT(ptr != nullptr);
 			gb_zero_size(ptr, size);
 			gb_zero_size(ptr, size);
 			break;
 			break;
 		}
 		}
@@ -405,10 +408,14 @@ GB_ALLOCATOR_PROC(heap_allocator_proc) {
 			break;
 			break;
 		}
 		}
 
 
-		posix_memalign(&ptr, alignment, size);
+		err = posix_memalign(&ptr, alignment, size);
+		GB_ASSERT_MSG(err == 0, "posix_memalign err: %d", err);
+		GB_ASSERT(ptr != nullptr);
 		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));
-		break;
+		free(old_memory);
+		isize n = gb_max(size-old_size, 0);
+		gb_zero_size(cast(u8 *)ptr + old_size, n);
+	} break;
 #endif
 #endif
 
 
 	case gbAllocation_FreeAll:
 	case gbAllocation_FreeAll:

+ 8 - 4
src/gb/gb.h

@@ -3123,18 +3123,22 @@ gb_inline void *gb_memset(void *dest, u8 c, isize n) {
 		return NULL;
 		return NULL;
 	}
 	}
 
 
-	if (n == 0)
+	if (n <= 0) {
 		return dest;
 		return dest;
+	}
 	s[0] = s[n-1] = c;
 	s[0] = s[n-1] = c;
-	if (n < 3)
+	if (n < 3) {
 		return dest;
 		return dest;
+	}
 	s[1] = s[n-2] = c;
 	s[1] = s[n-2] = c;
 	s[2] = s[n-3] = c;
 	s[2] = s[n-3] = c;
-	if (n < 7)
+	if (n < 7) {
 		return dest;
 		return dest;
+	}
 	s[3] = s[n-4] = c;
 	s[3] = s[n-4] = c;
-	if (n < 9)
+	if (n < 9) {
 		return dest;
 		return dest;
+	}
 
 
 	k = -cast(intptr)s & 3;
 	k = -cast(intptr)s & 3;
 	s += k;
 	s += k;