ソースを参照

Fix segfault with heap allocation

root 8 年 前
コミット
98d493504b
4 ファイル変更27 行追加32 行削除
  1. 5 3
      build.sh
  2. 0 13
      code/demo.odin
  3. 21 15
      src/gb/gb.h
  4. 1 1
      src/ir.c

+ 5 - 3
build.sh

@@ -2,10 +2,10 @@
 
 release_mode=0
 
-warnings_to_disable="-Wno-attributes -Wno-implicit-function-declaration -Wno-incompatible-pointer-types -Wno-switch -Wno-pointer-sign -Wno-tautological-constant-out-of-range-compare -Wno-tautological-compare"
+warnings_to_disable="-std=c11 -Wno-switch -Wno-pointer-sign -Wno-tautological-constant-out-of-range-compare -Wno-tautological-compare -Wno-macro-redefined"
 libraries="-pthread -ldl -lm"
-other_args="-x c"
-compiler="gcc"
+other_args=""
+compiler="clang"
 
 if [ "$release_mode" -eq "0" ]; then
 	other_args="${other_args} -g -fno-inline-functions"
@@ -20,3 +20,5 @@ if [[ "$(uname)" == "Darwin" ]]; then
 fi
 
 ${compiler} src/main.c ${warnings_to_disable} ${libraries} ${other_args} -o odin
+
+./odin run code/demo.odin

+ 0 - 13
code/demo.odin

@@ -1,17 +1,4 @@
-#import "atomic.odin";
-#import "decimal.odin";
 #import "fmt.odin";
-#import "hash.odin";
-#import "math.odin";
-#import "mem.odin";
-#import "opengl.odin";
-#import "os.odin";
-#import "strconv.odin";
-#import "strings.odin";
-#import "sync.odin";
-#import "types.odin";
-#import "utf8.odin";
-#import "utf16.odin";
 
 main :: proc() {
 	immutable program := "+ + * - /";

+ 21 - 15
src/gb/gb.h

@@ -4858,8 +4858,8 @@ GB_ALLOCATOR_PROC(gb_heap_allocator_proc) {
 #elif defined(GB_SYSTEM_LINUX)
 	// TODO(bill): *nix version that's decent
 	case gbAllocation_Alloc: {
-		// ptr = aligned_alloc(alignment, size);
-		ptr = malloc(size+alignment);
+		ptr = aligned_alloc(alignment, size);
+		// ptr = malloc(size+alignment);
 
 		if (flags & gbAllocatorFlag_ClearToZero) {
 			gb_zero_size(ptr, size);
@@ -4871,8 +4871,8 @@ GB_ALLOCATOR_PROC(gb_heap_allocator_proc) {
 	} break;
 
 	case gbAllocation_Resize: {
-		ptr = realloc(old_memory, size);
-		// ptr = gb_default_resize_align(gb_heap_allocator(), old_memory, old_size, size, alignment);
+		// ptr = realloc(old_memory, size);
+		ptr = gb_default_resize_align(gb_heap_allocator(), old_memory, old_size, size, alignment);
 	} break;
 #else
 	// TODO(bill): *nix version that's decent
@@ -7615,12 +7615,14 @@ gbFileError gb_file_open_mode(gbFile *f, gbFileMode mode, char const *filename)
 }
 
 gbFileError gb_file_close(gbFile *f) {
-	if (!f) {
+	if (f == NULL) {
 		return gbFileError_Invalid;
 	}
 
 #if defined(GB_COMPILER_MSVC)
-	if (f->filename) gb_free(gb_heap_allocator(), cast(char *)f->filename);
+	if (f->filename != NULL) {
+		gb_free(gb_heap_allocator(), cast(char *)f->filename);
+	}
 #else
 	// TODO HACK(bill): Memory Leak!!!
 #endif
@@ -8035,19 +8037,23 @@ char *gb_path_get_full_name(gbAllocator a, char const *path) {
 	new_path[new_len] = 0;
 	return new_path;
 #else
-// TODO(bill): Make work on *nix, etc.
-	char* p = realpath(path, 0);
-	GB_ASSERT(p && "file does not exist");
+	char *p, *result, *fullpath = NULL;
+	isize len;
+	p = realpath(path, NULL);
+	fullpath = p;
+	if (p == NULL) {
+		// NOTE(bill): File does not exist
+		fullpath = cast(char *)path;
+	}
 
-	isize len = gb_strlen(p);
+	len = gb_strlen(fullpath);
 
-	// bill... gb_alloc_str_len refused to work for this...
-	char* ret = gb_alloc(a, sizeof(char) * len + 1);
-	gb_memmove(ret, p, len);
-	ret[len] = 0;
+	result = gb_alloc_array(a, char, len + 1);
+	gb_memmove(result, fullpath, len);
+	result[len] = 0;
 	free(p);
 
-	return ret;
+	return result;
 #endif
 }
 

+ 1 - 1
src/ir.c

@@ -1090,7 +1090,7 @@ irValue *ir_generate_array(irModule *m, Type *elem_type, i64 count, String prefi
 	isize name_len = prefix.len + 10;
 	token.string.text = gb_alloc_array(a, u8, name_len);
 	token.string.len = gb_snprintf(cast(char *)token.string.text, name_len,
-	                               "%.*s-%llx", LIT(prefix), id)-1;
+	                               "%.*s-%llx", LIT(prefix), cast(unsigned long long)id)-1;
 	Entity *e = make_entity_variable(a, NULL, token, make_type_array(a, elem_type, count), false);
 	irValue *value = ir_value_global(a, e, NULL);
 	value->Global.is_private = true;