Always use jv_mem_*alloc()
This commit is contained in:
1 parent
4b4cf789e0
commit
0bd7614bd5
7 files changed
+13
-11
No files matched your search
+2
-1
@@ -41,6 +41,7 @@ void *alloca (size_t);
|
|||||||
#include "linker.h"
|
#include "linker.h"
|
||||||
#include "locfile.h"
|
#include "locfile.h"
|
||||||
#include "jv_unicode.h"
|
#include "jv_unicode.h"
|
||||||
|
#include "jv_alloc.h"
|
||||||
|
|
||||||
|
|
||||||
static jv type_error(jv bad, const char* msg) {
|
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);
|
const unsigned char* data = (const unsigned char*)jv_string_value(input);
|
||||||
int len = jv_string_length_bytes(jv_copy(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
|
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));
|
memset(result, 0, decoded_len * sizeof(char));
|
||||||
uint32_t ri = 0;
|
uint32_t ri = 0;
|
||||||
int input_bytes_read=0;
|
int input_bytes_read=0;
|
||||||
|
|||||||
+3
-3
@@ -1296,7 +1296,7 @@ static int compile(struct bytecode* bc, block b, struct locfile* lf, jv args, jv
|
|||||||
bc->codelen = pos;
|
bc->codelen = pos;
|
||||||
bc->debuginfo = jv_object_set(bc->debuginfo, jv_string("locals"), localnames);
|
bc->debuginfo = jv_object_set(bc->debuginfo, jv_string("locals"), localnames);
|
||||||
if (bc->nsubfunctions) {
|
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) {
|
for (inst* curr = b.first; curr; curr = curr->next) {
|
||||||
if (curr->op == CLOSURE_CREATE) {
|
if (curr->op == CLOSURE_CREATE) {
|
||||||
struct bytecode* subfn = jv_mem_alloc(sizeof(struct bytecode));
|
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 {
|
} else {
|
||||||
bc->subfunctions = 0;
|
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;
|
bc->code = code;
|
||||||
pos = 0;
|
pos = 0;
|
||||||
jv constant_pool = jv_array();
|
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));
|
bc->globals = jv_mem_alloc(sizeof(struct symbol_table));
|
||||||
int ncfunc = count_cfunctions(b);
|
int ncfunc = count_cfunctions(b);
|
||||||
bc->globals->ncfunctions = 0;
|
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->globals->cfunc_names = jv_array();
|
||||||
bc->debuginfo = jv_object_set(jv_object(), jv_string("name"), jv_null());
|
bc->debuginfo = jv_object_set(jv_object(), jv_string("name"), jv_null());
|
||||||
jv env = jv_invalid();
|
jv env = jv_invalid();
|
||||||
|
|||||||
+2
-2
@@ -465,7 +465,7 @@ jv jv_keys_unsorted(jv x) {
|
|||||||
jv jv_keys(jv x) {
|
jv jv_keys(jv x) {
|
||||||
if (jv_get_kind(x) == JV_KIND_OBJECT) {
|
if (jv_get_kind(x) == JV_KIND_OBJECT) {
|
||||||
int nkeys = jv_object_length(jv_copy(x));
|
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;
|
int kidx = 0;
|
||||||
jv_object_foreach(x, key, value) {
|
jv_object_foreach(x, key, value) {
|
||||||
keys[kidx++] = key;
|
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_get_kind(keys) == JV_KIND_ARRAY);
|
||||||
assert(jv_array_length(jv_copy(objects)) == jv_array_length(jv_copy(keys)));
|
assert(jv_array_length(jv_copy(objects)) == jv_array_length(jv_copy(keys)));
|
||||||
int n = jv_array_length(jv_copy(objects));
|
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++) {
|
for (int i=0; i<n; i++) {
|
||||||
entries[i].object = jv_array_get(jv_copy(objects), i);
|
entries[i].object = jv_array_get(jv_copy(objects), i);
|
||||||
entries[i].key = jv_array_get(jv_copy(keys), i);
|
entries[i].key = jv_array_get(jv_copy(keys), i);
|
||||||
|
|||||||
+1
-1
@@ -43,7 +43,7 @@ static void put_buf(const char *s, int len, FILE *fout, jv *strout, int is_tty)
|
|||||||
if (len == -1)
|
if (len == -1)
|
||||||
len = strlen(s);
|
len = strlen(s);
|
||||||
wl = MultiByteToWideChar(CP_UTF8, 0, s, len, NULL, 0);
|
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)
|
if (!ws)
|
||||||
return;
|
return;
|
||||||
wl = MultiByteToWideChar(CP_UTF8, 0, s, len, ws, wl + 1);
|
wl = MultiByteToWideChar(CP_UTF8, 0, s, len, ws, wl + 1);
|
||||||
|
|||||||
+3
-2
@@ -19,6 +19,7 @@
|
|||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "compile.h"
|
#include "compile.h"
|
||||||
|
#include "jv_alloc.h"
|
||||||
|
|
||||||
struct lib_loading_state {
|
struct lib_loading_state {
|
||||||
char **names;
|
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++;
|
state_idx = lib_state->ct++;
|
||||||
lib_state->names = realloc(lib_state->names, lib_state->ct * sizeof(const char *));
|
lib_state->names = jv_mem_realloc(lib_state->names, lib_state->ct * sizeof(const char *));
|
||||||
lib_state->defs = realloc(lib_state->defs, lib_state->ct * sizeof(block));
|
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->names[state_idx] = strdup(jv_string_value(lib_path));
|
||||||
lib_state->defs[state_idx] = program;
|
lib_state->defs[state_idx] = program;
|
||||||
*out_block = program;
|
*out_block = program;
|
||||||
|
|||||||
+1
-1
@@ -21,7 +21,7 @@ struct locfile* locfile_init(jq_state *jq, const char *fname, const char* data,
|
|||||||
for (int i=0; i<length; i++) {
|
for (int i=0; i<length; i++) {
|
||||||
if (data[i] == '\n') l->nlines++;
|
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;
|
l->linemap[0] = 0;
|
||||||
int line = 1;
|
int line = 1;
|
||||||
for (int i=0; i<length; i++) {
|
for (int i=0; i<length; i++) {
|
||||||
|
|||||||
+1
-1
@@ -139,7 +139,7 @@ jv jq_realpath(jv path) {
|
|||||||
path_max = PATH_MAX;
|
path_max = PATH_MAX;
|
||||||
#endif
|
#endif
|
||||||
if (path_max > 0) {
|
if (path_max > 0) {
|
||||||
buf = malloc(sizeof(char) * path_max);
|
buf = jv_mem_alloc(path_max);
|
||||||
}
|
}
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
char *tmp = _fullpath(buf, jv_string_value(path), path_max);
|
char *tmp = _fullpath(buf, jv_string_value(path), path_max);
|
||||||
|
|||||||
Reference in new issue
Block a user