Skip to content

Commit

Permalink
Always use jv_mem_*alloc()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicowilliams committed Apr 21, 2017
1 parent 4b4cf78 commit 0bd7614
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/jv_aux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/jv_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/linker.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "parser.h"
#include "util.h"
#include "compile.h"
#include "jv_alloc.h"

struct lib_loading_state {
char **names;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/locfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
2 changes: 1 addition & 1 deletion src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 0bd7614

Please sign in to comment.