diff --git a/src/compiler/bigint.c b/src/compiler/bigint.c index 72089a625..6d6e9f3d4 100644 --- a/src/compiler/bigint.c +++ b/src/compiler/bigint.c @@ -54,7 +54,7 @@ char *i128_to_string(Int128 op, uint64_t base, bool is_signed) *(loc++) = digits[rem.low]; op = i128_udiv(op, base_div); } while (!i128_is_zero(op)); - char *res = malloc((size_t)(loc - buffer + 2)); + char *res = malloc_arena((size_t)(loc - buffer + 2)); char *c = res; if (add_minus) *(c++) = '-'; while (loc > buffer) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 90f74a290..bd4c3bc12 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -434,8 +434,8 @@ void compiler_compile(void) } unsigned cfiles = vec_size(active_target.csources); - CompileData *compile_data = malloc(sizeof(CompileData) * output_file_count); - const char **obj_files = malloc(sizeof(char*) * (output_file_count + cfiles)); + CompileData *compile_data = cmalloc(sizeof(CompileData) * output_file_count); + const char **obj_files = cmalloc(sizeof(char*) * (output_file_count + cfiles)); if (cfiles) { @@ -444,9 +444,8 @@ void compiler_compile(void) { char *filename = NULL; char *dir = NULL; - bool split_worked = filenamesplit(active_target.csources[i], &filename, &dir); + bool split_worked = filenamesplit(active_target.csources[i], &filename, NULL); assert(split_worked); - free(dir); size_t len = strlen(filename); // .c -> .o (quick hack to fix the name on linux) filename[len - 1] = 'o'; diff --git a/src/compiler/llvm_codegen.c b/src/compiler/llvm_codegen.c index f78228344..6fa712bc0 100644 --- a/src/compiler/llvm_codegen.c +++ b/src/compiler/llvm_codegen.c @@ -984,7 +984,7 @@ void *llvm_gen(Module *module) { if (!vec_size(module->contexts)) return NULL; assert(intrinsics_setup); - GenContext *gen_context = calloc(sizeof(GenContext), 1); + GenContext *gen_context = cmalloc(sizeof(GenContext)); gencontext_init(gen_context, module); gencontext_begin_module(gen_context); diff --git a/src/compiler/tb_codegen.c b/src/compiler/tb_codegen.c index ba0ede732..e2ba48f85 100644 --- a/src/compiler/tb_codegen.c +++ b/src/compiler/tb_codegen.c @@ -656,7 +656,7 @@ static void tinybackend_gen_context(GenContext *c) // Generate context per module (single threaded) void *tinybackend_gen(Module *module) { - GenContext *c = calloc(sizeof(GenContext), 1); + GenContext *c = ccalloc(sizeof(GenContext), 1); c->code_module = module; // TODO identify target architecture diff --git a/src/utils/dirent.h b/src/utils/dirent.h index a2e847a0f..f273c77cc 100644 --- a/src/utils/dirent.h +++ b/src/utils/dirent.h @@ -373,7 +373,7 @@ static _WDIR *_wopendir(const wchar_t *dirname) } /* Allocate new _WDIR structure */ - _WDIR *dirp = (_WDIR*) malloc(sizeof(struct _WDIR)); + _WDIR *dirp = (_WDIR*) cmalloc(sizeof(struct _WDIR)); if (!dirp) return NULL; @@ -397,7 +397,7 @@ static _WDIR *_wopendir(const wchar_t *dirname) #endif /* Allocate room for absolute directory name and search pattern */ - dirp->patt = (wchar_t*) malloc(sizeof(wchar_t) * n + 16); + dirp->patt = (wchar_t*) cmalloc(sizeof(wchar_t) * n + 16); if (dirp->patt == NULL) goto exit_closedir; @@ -640,7 +640,7 @@ static DIR *opendir(const char *dirname) } /* Allocate memory for DIR structure */ - struct DIR *dirp = (DIR*) malloc(sizeof(struct DIR)); + struct DIR *dirp = (DIR*) cmalloc(sizeof(struct DIR)); if (!dirp) return NULL; @@ -810,7 +810,7 @@ static int scandir( while (1) { /* Allocate room for a temporary directory entry */ if (!tmp) { - tmp = (struct dirent*) malloc(sizeof(struct dirent)); + tmp = (struct dirent*) cmalloc(sizeof(struct dirent)); if (!tmp) goto exit_failure; } diff --git a/src/utils/file_utils.c b/src/utils/file_utils.c index 9e664a5f0..2a0930714 100644 --- a/src/utils/file_utils.c +++ b/src/utils/file_utils.c @@ -107,21 +107,18 @@ bool filenamesplit(const char *path, char** filename_ptr, char** directory_ptr) if (file_len == 1 && path[0] == '.') return false; if (file_len == 2 && path[0] == '.' && path[1] == '.') return false; if (!file_len) return false; - *filename_ptr = strdup(&path[len - file_len]); + *filename_ptr = strcopy(&path[len - file_len], file_len); + if (!directory_ptr) return true; if (file_len < len) { size_t dir_len = len - file_len; - char *dir = malloc(dir_len + 1); - memcpy(dir, path, dir_len - 1); - dir[dir_len] = 0; - *directory_ptr = dir; + *directory_ptr = strcopy(path, dir_len - 1); } else { - char *dir = malloc(2); - dir[0] = '.'; - dir[1] = 0; - *directory_ptr = dir; + *directory_ptr = malloc_arena(2); + (*directory_ptr)[0] = '.'; + (*directory_ptr)[1] = 0; } return true; } @@ -156,7 +153,7 @@ char *read_file(const char *path, size_t *return_size) *return_size = file_size; rewind(file); - char *buffer = (char *)malloc(file_size + 1); + char *buffer = (char *)malloc_arena(file_size + 1); if (buffer == NULL) { error_exit("Not enough memory to read \"%s\".\n", path); @@ -332,7 +329,7 @@ void file_add_wildcard_files(const char ***files, const char *path, bool recursi char *realpath(const char *path, char *const resolved_path) { - char *result = NULL == resolved_path ? calloc(PATH_MAX + 1, 1) : resolved_path; + char *result = NULL == resolved_path ? ccalloc(PATH_MAX + 1, 1) : resolved_path; if (NULL == result) return NULL; if (!GetFullPathNameA(path, MAX_PATH, result, NULL)) { diff --git a/src/utils/find_msvc.c b/src/utils/find_msvc.c index bb1726b4d..1e9931212 100644 --- a/src/utils/find_msvc.c +++ b/src/utils/find_msvc.c @@ -33,9 +33,9 @@ WindowsLinkPathsUTF8 get_windows_link_paths() { // note: WideCharToMultiByte doesn't seem to do null termination. // I'm wary of manually adding a null terminator, so hopefully this is reliable. // This wouldn't be a problem if windows used UTF-8 like the rest of the world >:( - out.windows_sdk_um_library_path = (char*)calloc(MAX_PATH, 1); - out.windows_sdk_ucrt_library_path = (char*)calloc(MAX_PATH, 1); - out.vs_library_path = (char*)calloc(MAX_PATH, 1); + out.windows_sdk_um_library_path = (char*)ccalloc(MAX_PATH, 1); + out.windows_sdk_ucrt_library_path = (char*)ccalloc(MAX_PATH, 1); + out.vs_library_path = (char*)ccalloc(MAX_PATH, 1); int um_len = wcslen(paths.windows_sdk_um_library_path); WideCharToMultiByte( @@ -246,7 +246,7 @@ wchar_t* find_windows_kit_root_with_key(HKEY key, wchar_t* version) { if (rc != 0) return NULL; DWORD length = required_length + 2; // The +2 is for the maybe optional zero later on. Probably we are over-allocating. - wchar_t* value = (wchar_t*)malloc(length); + wchar_t* value = (wchar_t*)cmalloc(length); if (!value) return NULL; rc = RegQueryValueExW(key, version, NULL, NULL, (LPBYTE)value, &required_length); // We know that version is zero-terminated... @@ -423,7 +423,7 @@ bool find_visual_studio_2017_by_fighting_through_microsoft_craziness(Find_Result } size_t version_length = (size_t)(tools_file_size.QuadPart + 1); // Warning: This multiplication by 2 presumes there is no variable-length encoding in the wchars (wacky characters in the file could betray this expectation). - wchar_t* version = (wchar_t*)malloc(version_length * 2); + wchar_t* version = (wchar_t*)cmalloc(version_length * 2); wchar_t* read_result = fgetws(version, version_length, f); fclose(f); @@ -502,7 +502,7 @@ void find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result* res continue; } - wchar_t* buffer = (wchar_t*)malloc(cb_data); + wchar_t* buffer = (wchar_t*)cmalloc(cb_data); if (!buffer) return; rc = RegQueryValueExW(vs7_key, v, NULL, NULL, (LPBYTE)buffer, &cb_data); diff --git a/src/utils/lib.h b/src/utils/lib.h index 8e7498af5..110688ea9 100644 --- a/src/utils/lib.h +++ b/src/utils/lib.h @@ -35,6 +35,7 @@ void path_get_dir_and_filename_from_full(const char *full_path, char **filename, void file_find_top_dir(); void file_add_wildcard_files(const char ***files, const char *path, bool recursive, const char *suffix1, const char *suffix2); void *cmalloc(size_t size); +void *ccalloc(size_t size, size_t elements); void memory_init(void); void memory_release(); void *malloc_arena(size_t mem); diff --git a/src/utils/malloc.c b/src/utils/malloc.c index f8c559290..0bbe49b38 100644 --- a/src/utils/malloc.c +++ b/src/utils/malloc.c @@ -71,4 +71,11 @@ void *cmalloc(size_t size) void *ptr = malloc(size); if (!ptr) error_exit("Failed to malloc %d bytes.", size); return ptr; -} \ No newline at end of file +} + +void *ccalloc(size_t size, size_t elements) +{ + void *ptr = calloc(size, elements); + if (!ptr) error_exit("Failed to calloc %d bytes.", size * elements); + return ptr; +} diff --git a/src/utils/stringutils.c b/src/utils/stringutils.c index 6ce9df902..e9b731ce7 100644 --- a/src/utils/stringutils.c +++ b/src/utils/stringutils.c @@ -95,7 +95,7 @@ int vasnprintf(char **strp, const char *fmt, va_list args) va_copy(args_copy, args); int res = vsprintf(NULL, fmt, args); if (res < 0) goto END; - char *buf = calloc(res + 1, 1); + char *buf = ccalloc(res + 1, 1); if (NULL == buf) goto END; sprintf(buf, fmt, args_copy); // this can't fail, right? *strp = buf; @@ -109,7 +109,7 @@ int vasprintf(char** ret, const char* fmt, va_list args) { if (length < 0) { // check if _vsnprintf failed return -1; } - *ret = malloc(length + 1); + *ret = cmalloc(length + 1); if (!*ret) { // check if malloc failed return -1; } @@ -122,7 +122,7 @@ int vasprintf(char** ret, const char* fmt, va_list args) { char *strndup(const char *s, size_t n) { n = strnlen(s, n); - char *t = calloc(n + 1, 1); + char *t = ccalloc(n + 1, 1); if (NULL != t) { memcpy(t, s, n); diff --git a/src/utils/toml.c b/src/utils/toml.c index 3798c79d3..40d889186 100644 --- a/src/utils/toml.c +++ b/src/utils/toml.c @@ -7,6 +7,7 @@ #include #include #include "toml.h" +#include "lib.h" #if (defined(_MSC_VER) && _MSC_VER < 1800) || defined(_ADI_COMPILER) #define va_copy(a, b) ((a) = (b)) @@ -15,7 +16,7 @@ char *toml_strdup(TOML_CONST char *str) { size_t len = strlen(str) + 1; - void *new = malloc(len); + void *new = cmalloc(len); if (new == NULL) { return NULL; @@ -26,7 +27,7 @@ char *toml_strdup(TOML_CONST char *str) char *toml_strndup(TOML_CONST char *str, size_t n) { - char *result = malloc(n + 1); + char *result = cmalloc(n + 1); if (result == NULL) { return NULL; @@ -164,7 +165,7 @@ static inline size_t toml_roundup_pow_of_two_size_t(size_t x) TomlString *toml_string_new(TomlErr *error) { - TomlString *self = malloc(sizeof(TomlString)); + TomlString *self = cmalloc(sizeof(TomlString)); if (self == NULL) { toml_set_err_literal(error, TOML_ERR_NOMEM, "out of memory"); @@ -337,7 +338,7 @@ struct _TomlTable TomlTable *toml_table_new(TomlErr *err) { - TomlTable *self = malloc(sizeof(TomlTable)); + TomlTable *self = cmalloc(sizeof(TomlTable)); if (self == NULL) { toml_set_err_literal(err, TOML_ERR_NOMEM, "out of memory"); @@ -472,7 +473,7 @@ struct _TomlTableIter TomlTableIter *toml_table_iter_new(TomlTable *table, TomlErr *error) { - TomlTableIter *self = malloc(sizeof(TomlTableIter)); + TomlTableIter *self = cmalloc(sizeof(TomlTableIter)); if (self == NULL) { toml_set_err_literal(error, TOML_ERR_NOMEM, "out of memory"); @@ -513,7 +514,7 @@ void toml_table_iter_next(TomlTableIter *self) TomlArray *toml_array_new(TomlErr *error) { - TomlArray *self = malloc(sizeof(TomlArray)); + TomlArray *self = cmalloc(sizeof(TomlArray)); if (self == NULL) { toml_set_err_literal(error, TOML_ERR_NOMEM, "out of memory"); @@ -572,7 +573,7 @@ TomlValue *toml_value_new(TomlType type, TomlErr *error) { TomlErr err = TOML_ERR_INIT; - TomlValue *self = malloc(sizeof(TomlValue)); + TomlValue *self = cmalloc(sizeof(TomlValue)); if (self == NULL) { toml_set_err_literal(&err, TOML_ERR_NOMEM, "out of memory"); @@ -601,7 +602,7 @@ TomlValue *toml_value_new(TomlType type, TomlErr *error) self->value.boolean = TOML_FALSE; break; case TOML_DATETIME: - self->value.datetime = calloc(1, sizeof(TomlDateTime)); + self->value.datetime = ccalloc(1, sizeof(TomlDateTime)); if (self->value.datetime == NULL) { toml_set_err(error, TOML_ERR_NOMEM, "out of memory"); @@ -618,7 +619,7 @@ TomlValue *toml_value_new_string(TOML_CONST char *str, TomlErr *error) { TomlErr err = TOML_ERR_INIT; - TomlValue *self = malloc(sizeof(TomlValue)); + TomlValue *self = cmalloc(sizeof(TomlValue)); if (self == NULL) { toml_set_err_literal(&err, TOML_ERR_NOMEM, "out of memory"); @@ -644,7 +645,7 @@ TomlValue *toml_value_new_nstring(TOML_CONST char *str, size_t len, TomlErr *err { TomlErr err = TOML_ERR_INIT; - TomlValue *self = malloc(sizeof(TomlValue)); + TomlValue *self = cmalloc(sizeof(TomlValue)); if (self == NULL) { toml_set_err_literal(&err, TOML_ERR_NOMEM, "out of memory"); @@ -670,7 +671,7 @@ TomlValue *toml_value_new_table(TomlErr *error) { TomlErr err = TOML_ERR_INIT; - TomlValue *self = malloc(sizeof(TomlValue)); + TomlValue *self = cmalloc(sizeof(TomlValue)); if (self == NULL) { toml_set_err_literal(&err, TOML_ERR_NOMEM, "out of memory"); @@ -696,7 +697,7 @@ TomlValue *toml_value_new_array(TomlErr *error) { TomlErr err = TOML_ERR_INIT; - TomlValue *self = malloc(sizeof(TomlValue)); + TomlValue *self = cmalloc(sizeof(TomlValue)); if (self == NULL) { toml_set_err_literal(&err, TOML_ERR_NOMEM, "out of memory"); @@ -727,7 +728,7 @@ TomlValue *toml_value_new_integer(long integer, TomlErr *error) { TomlErr err = TOML_ERR_INIT; - TomlValue *self = malloc(sizeof(TomlValue)); + TomlValue *self = cmalloc(sizeof(TomlValue)); if (self == NULL) { toml_set_err_literal(&err, TOML_ERR_NOMEM, "out of memory"); @@ -746,7 +747,7 @@ TomlValue *toml_value_new_float(double float_, TomlErr *error) { TomlErr err = TOML_ERR_INIT; - TomlValue *self = malloc(sizeof(TomlValue)); + TomlValue *self = cmalloc(sizeof(TomlValue)); if (self == NULL) { toml_set_err_literal(&err, TOML_ERR_NOMEM, "out of memory"); @@ -770,7 +771,7 @@ TomlValue *toml_value_new_boolean(int boolean, TomlErr *error) { TomlErr err = TOML_ERR_INIT; - TomlValue *self = malloc(sizeof(TomlValue)); + TomlValue *self = cmalloc(sizeof(TomlValue)); if (self == NULL) { toml_set_err_literal(&err, TOML_ERR_NOMEM, "out of memory"); @@ -822,7 +823,7 @@ typedef struct _TomlParser TomlParser *toml_parser_new(TOML_CONST char *str, size_t len, TomlErr *error) { - TomlParser *self = malloc(sizeof(TomlParser)); + TomlParser *self = cmalloc(sizeof(TomlParser)); if (self == NULL) { toml_set_err_literal(error, TOML_OK, "out of memory"); diff --git a/src/utils/whereami.c b/src/utils/whereami.c index 517870f56..3964d80aa 100644 --- a/src/utils/whereami.c +++ b/src/utils/whereami.c @@ -214,6 +214,7 @@ static int get_executable_path_raw(char *out, int capacity, int *dirname_length) #include #include +#include "lib.h" static int get_executable_path_raw(char *out, int capacity, int *dirname_length) { @@ -228,7 +229,7 @@ static int get_executable_path_raw(char *out, int capacity, int *dirname_length) uint32_t size = (uint32_t) sizeof(buffer1); if (_NSGetExecutablePath(path, &size) == -1) { - path = (char *) malloc(size); + path = (char *) cmalloc(size); if (!_NSGetExecutablePath(path, &size)) { break;