Fix segfault with heap allocation

This commit is contained in:
root
2017-04-10 20:48:56 +01:00
parent 3a3202fbc6
commit 98d493504b
4 changed files with 27 additions and 32 deletions
+5 -3
View File
@@ -2,10 +2,10 @@
release_mode=0 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" libraries="-pthread -ldl -lm"
other_args="-x c" other_args=""
compiler="gcc" compiler="clang"
if [ "$release_mode" -eq "0" ]; then if [ "$release_mode" -eq "0" ]; then
other_args="${other_args} -g -fno-inline-functions" other_args="${other_args} -g -fno-inline-functions"
@@ -20,3 +20,5 @@ if [[ "$(uname)" == "Darwin" ]]; then
fi fi
${compiler} src/main.c ${warnings_to_disable} ${libraries} ${other_args} -o odin ${compiler} src/main.c ${warnings_to_disable} ${libraries} ${other_args} -o odin
./odin run code/demo.odin
-13
View File
@@ -1,17 +1,4 @@
#import "atomic.odin";
#import "decimal.odin";
#import "fmt.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() { main :: proc() {
immutable program := "+ + * - /"; immutable program := "+ + * - /";
+21 -15
View File
@@ -4858,8 +4858,8 @@ GB_ALLOCATOR_PROC(gb_heap_allocator_proc) {
#elif defined(GB_SYSTEM_LINUX) #elif defined(GB_SYSTEM_LINUX)
// TODO(bill): *nix version that's decent // TODO(bill): *nix version that's decent
case gbAllocation_Alloc: { case gbAllocation_Alloc: {
// ptr = aligned_alloc(alignment, size); ptr = aligned_alloc(alignment, size);
ptr = malloc(size+alignment); // ptr = malloc(size+alignment);
if (flags & gbAllocatorFlag_ClearToZero) { if (flags & gbAllocatorFlag_ClearToZero) {
gb_zero_size(ptr, size); gb_zero_size(ptr, size);
@@ -4871,8 +4871,8 @@ GB_ALLOCATOR_PROC(gb_heap_allocator_proc) {
} break; } break;
case gbAllocation_Resize: { case gbAllocation_Resize: {
ptr = realloc(old_memory, size); // ptr = realloc(old_memory, size);
// ptr = gb_default_resize_align(gb_heap_allocator(), old_memory, old_size, size, alignment); ptr = gb_default_resize_align(gb_heap_allocator(), old_memory, old_size, size, alignment);
} break; } break;
#else #else
// TODO(bill): *nix version that's decent // 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) { gbFileError gb_file_close(gbFile *f) {
if (!f) { if (f == NULL) {
return gbFileError_Invalid; return gbFileError_Invalid;
} }
#if defined(GB_COMPILER_MSVC) #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 #else
// TODO HACK(bill): Memory Leak!!! // TODO HACK(bill): Memory Leak!!!
#endif #endif
@@ -8035,19 +8037,23 @@ char *gb_path_get_full_name(gbAllocator a, char const *path) {
new_path[new_len] = 0; new_path[new_len] = 0;
return new_path; return new_path;
#else #else
// TODO(bill): Make work on *nix, etc. char *p, *result, *fullpath = NULL;
char* p = realpath(path, 0); isize len;
GB_ASSERT(p && "file does not exist"); 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... result = gb_alloc_array(a, char, len + 1);
char* ret = gb_alloc(a, sizeof(char) * len + 1); gb_memmove(result, fullpath, len);
gb_memmove(ret, p, len); result[len] = 0;
ret[len] = 0;
free(p); free(p);
return ret; return result;
#endif #endif
} }
+1 -1
View File
@@ -1090,7 +1090,7 @@ irValue *ir_generate_array(irModule *m, Type *elem_type, i64 count, String prefi
isize name_len = prefix.len + 10; isize name_len = prefix.len + 10;
token.string.text = gb_alloc_array(a, u8, name_len); token.string.text = gb_alloc_array(a, u8, name_len);
token.string.len = gb_snprintf(cast(char *)token.string.text, 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); Entity *e = make_entity_variable(a, NULL, token, make_type_array(a, elem_type, count), false);
irValue *value = ir_value_global(a, e, NULL); irValue *value = ir_value_global(a, e, NULL);
value->Global.is_private = true; value->Global.is_private = true;