From 0bd7614bd5be91d3209f8fd857637f7c5299849e Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Fri, 21 Apr 2017 18:40:39 -0500 Subject: Always use jv_mem_*alloc() --- src/builtin.c | 3 ++- src/compile.c | 6 +++--- src/jv_aux.c | 4 ++-- src/jv_print.c | 2 +- src/linker.c | 5 +++-- src/locfile.c | 2 +- src/util.c | 2 +- 7 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/builtin.c b/src/builtin.c index 2973c4fd..cfdd4fef 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -41,6 +41,7 @@ void *alloca (size_t); #include "linker.h" #include "locfile.h" #include "jv_unicode.h" +#include "jv_alloc.h" static jv type_error(jv bad, const char* msg) { @@ -645,7 +646,7 @@ static jv f_format(jq_state *jq, jv input, jv fmt) { const unsigned char* data = (const unsigned char*)jv_string_value(input); int len = jv_string_length_bytes(jv_copy(input)); size_t decoded_len = (3 * len) / 4; // 3 usable bytes for every 4 bytes of input - char *result = malloc(decoded_len * sizeof(char)); + char *result = jv_mem_calloc(decoded_len, sizeof(char)); memset(result, 0, decoded_len * sizeof(char)); uint32_t ri = 0; int input_bytes_read=0; diff --git a/src/compile.c b/src/compile.c index 7960cfe6..afd42aed 100644 --- a/src/compile.c +++ b/src/compile.c @@ -1296,7 +1296,7 @@ static int compile(struct bytecode* bc, block b, struct locfile* lf, jv args, jv bc->codelen = pos; bc->debuginfo = jv_object_set(bc->debuginfo, jv_string("locals"), localnames); if (bc->nsubfunctions) { - bc->subfunctions = jv_mem_alloc(sizeof(struct bytecode*) * bc->nsubfunctions); + bc->subfunctions = jv_mem_calloc(sizeof(struct bytecode*), bc->nsubfunctions); for (inst* curr = b.first; curr; curr = curr->next) { if (curr->op == CLOSURE_CREATE) { struct bytecode* subfn = jv_mem_alloc(sizeof(struct bytecode)); @@ -1321,7 +1321,7 @@ static int compile(struct bytecode* bc, block b, struct locfile* lf, jv args, jv } else { bc->subfunctions = 0; } - uint16_t* code = jv_mem_alloc(sizeof(uint16_t) * bc->codelen); + uint16_t* code = jv_mem_calloc(sizeof(uint16_t), bc->codelen); bc->code = code; pos = 0; jv constant_pool = jv_array(); @@ -1386,7 +1386,7 @@ int block_compile(block b, struct bytecode** out, struct locfile* lf, jv args) { bc->globals = jv_mem_alloc(sizeof(struct symbol_table)); int ncfunc = count_cfunctions(b); bc->globals->ncfunctions = 0; - bc->globals->cfunctions = jv_mem_alloc(sizeof(struct cfunction) * ncfunc); + bc->globals->cfunctions = jv_mem_calloc(sizeof(struct cfunction), ncfunc); bc->globals->cfunc_names = jv_array(); bc->debuginfo = jv_object_set(jv_object(), jv_string("name"), jv_null()); jv env = jv_invalid(); diff --git a/src/jv_aux.c b/src/jv_aux.c index db2e0ef8..129cd043 100644 --- a/src/jv_aux.c +++ b/src/jv_aux.c @@ -465,7 +465,7 @@ jv jv_keys_unsorted(jv x) { jv jv_keys(jv x) { if (jv_get_kind(x) == JV_KIND_OBJECT) { int nkeys = jv_object_length(jv_copy(x)); - jv* keys = jv_mem_alloc(sizeof(jv) * nkeys); + jv* keys = jv_mem_calloc(sizeof(jv), nkeys); int kidx = 0; jv_object_foreach(x, key, value) { keys[kidx++] = key; @@ -587,7 +587,7 @@ static struct sort_entry* sort_items(jv objects, jv keys) { assert(jv_get_kind(keys) == JV_KIND_ARRAY); assert(jv_array_length(jv_copy(objects)) == jv_array_length(jv_copy(keys))); int n = jv_array_length(jv_copy(objects)); - struct sort_entry* entries = jv_mem_alloc(sizeof(struct sort_entry) * n); + struct sort_entry* entries = jv_mem_calloc(sizeof(struct sort_entry), n); for (int i=0; ict++; - lib_state->names = realloc(lib_state->names, lib_state->ct * sizeof(const char *)); - lib_state->defs = realloc(lib_state->defs, lib_state->ct * sizeof(block)); + lib_state->names = jv_mem_realloc(lib_state->names, lib_state->ct * sizeof(const char *)); + lib_state->defs = jv_mem_realloc(lib_state->defs, lib_state->ct * sizeof(block)); lib_state->names[state_idx] = strdup(jv_string_value(lib_path)); lib_state->defs[state_idx] = program; *out_block = program; diff --git a/src/locfile.c b/src/locfile.c index 97bb3482..90772a3f 100644 --- a/src/locfile.c +++ b/src/locfile.c @@ -21,7 +21,7 @@ struct locfile* locfile_init(jq_state *jq, const char *fname, const char* data, for (int i=0; inlines++; } - l->linemap = jv_mem_alloc(sizeof(int) * (l->nlines + 1)); + l->linemap = jv_mem_calloc(sizeof(int), (l->nlines + 1)); l->linemap[0] = 0; int line = 1; for (int i=0; i 0) { - buf = malloc(sizeof(char) * path_max); + buf = jv_mem_alloc(path_max); } #ifdef WIN32 char *tmp = _fullpath(buf, jv_string_value(path), path_max); -- cgit v1.2.3