From 98d493504b9558ce786333c1907158493a612b4d Mon Sep 17 00:00:00 2001 From: root Date: Mon, 10 Apr 2017 20:48:56 +0100 Subject: [PATCH] Fix segfault with heap allocation --- build.sh | 8 +++++--- code/demo.odin | 13 ------------- src/gb/gb.h | 36 +++++++++++++++++++++--------------- src/ir.c | 2 +- 4 files changed, 27 insertions(+), 32 deletions(-) diff --git a/build.sh b/build.sh index cda80781c..d27e97c50 100755 --- a/build.sh +++ b/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 diff --git a/code/demo.odin b/code/demo.odin index 1aa76f03e..323754a23 100644 --- a/code/demo.odin +++ b/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 := "+ + * - /"; diff --git a/src/gb/gb.h b/src/gb/gb.h index 39291d043..d5259a008 100644 --- a/src/gb/gb.h +++ b/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 } diff --git a/src/ir.c b/src/ir.c index 946e219dd..331d73b6b 100644 --- a/src/ir.c +++ b/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;