diff options
author | Nicolas Williams <nico@cryptonector.com> | 2017-04-21 18:40:39 -0500 |
---|---|---|
committer | Nicolas Williams <nico@cryptonector.com> | 2017-04-21 18:41:06 -0500 |
commit | 0bd7614bd5be91d3209f8fd857637f7c5299849e (patch) | |
tree | 7307309ede24948fb85b96de510b0f8dcdf3d3bc | |
parent | 4b4cf789e00f1139940f5f7cd2f2ddafaff3d89e (diff) |
Always use jv_mem_*alloc()
-rw-r--r-- | src/builtin.c | 3 | ||||
-rw-r--r-- | src/compile.c | 6 | ||||
-rw-r--r-- | src/jv_aux.c | 4 | ||||
-rw-r--r-- | src/jv_print.c | 2 | ||||
-rw-r--r-- | src/linker.c | 5 | ||||
-rw-r--r-- | src/locfile.c | 2 | ||||
-rw-r--r-- | 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; i<n; i++) { entries[i].object = jv_array_get(jv_copy(objects), i); entries[i].key = jv_array_get(jv_copy(keys), i); diff --git a/src/jv_print.c b/src/jv_print.c index cb2824d2..0127f0ca 100644 --- a/src/jv_print.c +++ b/src/jv_print.c @@ -43,7 +43,7 @@ static void put_buf(const char *s, int len, FILE *fout, jv *strout, int is_tty) if (len == -1) len = strlen(s); wl = MultiByteToWideChar(CP_UTF8, 0, s, len, NULL, 0); - ws = malloc((wl + 1) * sizeof(*ws)); + ws = jv_mem_calloc((wl + 1), sizeof(*ws)); if (!ws) return; wl = MultiByteToWideChar(CP_UTF8, 0, s, len, ws, wl + 1); diff --git a/src/linker.c b/src/linker.c index ad869da3..6cf1c879 100644 --- a/src/linker.c +++ b/src/linker.c @@ -19,6 +19,7 @@ #include "parser.h" #include "util.h" #include "compile.h" +#include "jv_alloc.h" struct lib_loading_state { char **names; @@ -324,8 +325,8 @@ static int load_library(jq_state *jq, jv lib_path, int is_data, int raw, const c } } state_idx = lib_state->ct++; - 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; i<length; i++) { if (data[i] == '\n') l->nlines++; } - 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<length; i++) { @@ -139,7 +139,7 @@ jv jq_realpath(jv path) { path_max = PATH_MAX; #endif if (path_max > 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); |