1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00

Add wrappers for malloc/realloc/free. See #43.

This commit is contained in:
Stephen Dolan
2012-12-18 16:52:47 +00:00
parent 75421cbfe3
commit 04daafbde3
15 changed files with 106 additions and 51 deletions

View File

@@ -24,7 +24,7 @@ version.gen.h: VERSION
sed 's/.*/#define JQ_VERSION "&"/' $^ > $@
main.c: version.gen.h
JQ_SRC=parser.gen.c lexer.gen.c opcode.c bytecode.c compile.c execute.c builtin.c jv.c jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c jv_aux.c
JQ_SRC=parser.gen.c lexer.gen.c opcode.c bytecode.c compile.c execute.c builtin.c jv.c jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c jv_aux.c jv_alloc.c
jq_test: CFLAGS += -DJQ_DEBUG=1
jq_test: $(JQ_SRC) jq_test.c

View File

@@ -4,6 +4,7 @@
#include "bytecode.h"
#include "opcode.h"
#include "jv_alloc.h"
static int bytecode_operation_length(uint16_t* codeptr) {
int length = opcode_describe(*codeptr)->length;
@@ -72,17 +73,17 @@ void dump_operation(struct bytecode* bc, uint16_t* codeptr) {
}
void symbol_table_free(struct symbol_table* syms) {
free(syms->cfunctions);
free(syms);
jv_mem_free(syms->cfunctions);
jv_mem_free(syms);
}
void bytecode_free(struct bytecode* bc) {
free(bc->code);
jv_mem_free(bc->code);
jv_free(bc->constants);
for (int i=0; i<bc->nsubfunctions; i++)
bytecode_free(bc->subfunctions[i]);
if (!bc->parent)
symbol_table_free(bc->globals);
free(bc->subfunctions);
free(bc);
jv_mem_free(bc->subfunctions);
jv_mem_free(bc);
}

View File

@@ -1,3 +1,4 @@
#define _GNU_SOURCE // for strdup
#include <assert.h>
#include <string.h>
#include <stdlib.h>
@@ -5,6 +6,7 @@
#include "compile.h"
#include "bytecode.h"
#include "locfile.h"
#include "jv_alloc.h"
/*
The intermediate representation for jq filters is as a sequence of
@@ -57,7 +59,7 @@ struct inst {
};
static inst* inst_new(opcode op) {
inst* i = malloc(sizeof(inst));
inst* i = jv_mem_alloc(sizeof(inst));
i->next = i->prev = 0;
i->op = op;
i->bytecode_pos = -1;
@@ -70,13 +72,13 @@ static inst* inst_new(opcode op) {
}
static void inst_free(struct inst* i) {
free(i->symbol);
jv_mem_free(i->symbol);
block_free(i->subfn);
block_free(i->arglist);
if (opcode_describe(i->op)->flags & OP_HAS_CONSTANT) {
jv_free(i->imm.constant);
}
free(i);
jv_mem_free(i);
}
static block inst_block(inst* i) {
@@ -546,10 +548,10 @@ static int compile(struct locfile* locations, struct bytecode* bc, block b) {
}
}
if (bc->nsubfunctions) {
bc->subfunctions = malloc(sizeof(struct bytecode*) * bc->nsubfunctions);
bc->subfunctions = jv_mem_alloc(sizeof(struct bytecode*) * bc->nsubfunctions);
for (inst* curr = b.first; curr; curr = curr->next) {
if (curr->op == CLOSURE_CREATE) {
struct bytecode* subfn = malloc(sizeof(struct bytecode));
struct bytecode* subfn = jv_mem_alloc(sizeof(struct bytecode));
bc->subfunctions[curr->imm.intval] = subfn;
subfn->globals = bc->globals;
subfn->parent = bc;
@@ -568,7 +570,7 @@ static int compile(struct locfile* locations, struct bytecode* bc, block b) {
bc->subfunctions = 0;
}
bc->codelen = pos;
uint16_t* code = malloc(sizeof(uint16_t) * bc->codelen);
uint16_t* code = jv_mem_alloc(sizeof(uint16_t) * bc->codelen);
bc->code = code;
pos = 0;
jv constant_pool = jv_array();
@@ -620,13 +622,13 @@ static int compile(struct locfile* locations, struct bytecode* bc, block b) {
}
int block_compile(block b, struct locfile* locations, struct bytecode** out) {
struct bytecode* bc = malloc(sizeof(struct bytecode));
struct bytecode* bc = jv_mem_alloc(sizeof(struct bytecode));
bc->parent = 0;
bc->nclosures = 0;
bc->globals = malloc(sizeof(struct symbol_table));
bc->globals = jv_mem_alloc(sizeof(struct symbol_table));
int ncfunc = count_cfunctions(b);
bc->globals->ncfunctions = 0;
bc->globals->cfunctions = malloc(sizeof(struct cfunction) * ncfunc);
bc->globals->cfunctions = jv_mem_alloc(sizeof(struct cfunction) * ncfunc);
int nerrors = compile(locations, bc, b);
assert(bc->globals->ncfunctions == ncfunc);
if (nerrors > 0) {

View File

@@ -11,6 +11,7 @@
#include "forkable_stack.h"
#include "frame_layout.h"
#include "jv_alloc.h"
#include "locfile.h"
#include "jv.h"
#include "jv_aux.h"
@@ -33,7 +34,7 @@ int path_push(stackval sv, jv val) {
if (pos == pathsize) {
int oldpathsize = pathsize;
pathsize = oldpathsize ? oldpathsize * 2 : 100;
pathbuf = realloc(pathbuf, sizeof(pathbuf[0]) * pathsize);
pathbuf = jv_mem_realloc(pathbuf, sizeof(pathbuf[0]) * pathsize);
for (int i=oldpathsize; i<pathsize; i++) {
pathbuf[i] = jv_invalid();
}
@@ -491,7 +492,7 @@ void jq_teardown() {
for (int i=0; i<pathsize; i++) {
jv_free(pathbuf[i]);
}
free(pathbuf);
jv_mem_free(pathbuf);
pathbuf = 0;
pathsize = 0;
}

View File

@@ -1,9 +1,9 @@
#ifndef FORKABLE_STACK_H
#define FORKABLE_STACK_H
#include <stdlib.h>
#include <stddef.h>
#include <assert.h>
#include <string.h>
#include "jv_alloc.h"
struct forkable_stack_header {
/* distance in bytes between this header and the header
@@ -18,7 +18,7 @@ struct forkable_stack {
char* stk;
// stk+length is just past end of allocated area
// stk = malloc(length)
// stk = jv_mem_alloc(length)
int length;
// stk+pos is header of top object, or stk+length if empty
@@ -40,7 +40,7 @@ static int forkable_stack_empty(struct forkable_stack* s) {
}
static void forkable_stack_init(struct forkable_stack* s, size_t sz) {
s->stk = malloc(sz);
s->stk = jv_mem_alloc(sz);
s->length = sz;
s->pos = s->length;
s->savedlimit = s->length;
@@ -50,7 +50,7 @@ static void forkable_stack_init(struct forkable_stack* s, size_t sz) {
static void forkable_stack_free(struct forkable_stack* s) {
assert(forkable_stack_empty(s));
assert(s->savedlimit == s->length);
free(s->stk);
jv_mem_free(s->stk);
s->stk = 0;
}
@@ -62,7 +62,7 @@ static void* forkable_stack_push(struct forkable_stack* s, size_t sz_size) {
if (curr - size < 0) {
int oldlen = s->length;
s->length = (size + oldlen + 1024) * 2;
s->stk = realloc(s->stk, s->length);
s->stk = jv_mem_realloc(s->stk, s->length);
int shift = s->length - oldlen;
memmove(s->stk + shift, s->stk, oldlen);
s->pos += shift;

25
jv.c
View File

@@ -6,6 +6,7 @@
#include <string.h>
#include <stdarg.h>
#include "jv_alloc.h"
#include "jv.h"
/*
@@ -86,7 +87,7 @@ jv jv_invalid_with_msg(jv err) {
jv x;
x.kind = JV_KIND_INVALID;
x.val.complex.i[0] = x.val.complex.i[1] = 0;
jvp_invalid* i = malloc(sizeof(jvp_invalid));
jvp_invalid* i = jv_mem_alloc(sizeof(jvp_invalid));
x.val.complex.ptr = &i->refcnt;
i->refcnt.count = 1;
i->errmsg = err;
@@ -113,7 +114,7 @@ int jv_invalid_has_msg(jv inv) {
static void jvp_invalid_free(jv_complex* x) {
if (jvp_refcnt_dec(x)) {
jv_free(((jvp_invalid*)x->ptr)->errmsg);
free(x->ptr);
jv_mem_free(x->ptr);
}
}
@@ -157,7 +158,7 @@ static jvp_array* jvp_array_ptr(jv_complex* a) {
}
static jvp_array* jvp_array_alloc(unsigned size) {
jvp_array* a = malloc(sizeof(jvp_array) + sizeof(jv) * size);
jvp_array* a = jv_mem_alloc(sizeof(jvp_array) + sizeof(jv) * size);
a->refcnt.count = 1;
a->length = 0;
a->alloc_length = size;
@@ -175,7 +176,7 @@ static void jvp_array_free(jv_complex* a) {
for (int i=0; i<array->length; i++) {
jv_free(array->elements[i]);
}
free(array);
jv_mem_free(array);
}
}
@@ -371,7 +372,7 @@ static jvp_string* jvp_string_ptr(jv_complex* a) {
}
static jvp_string* jvp_string_alloc(uint32_t size) {
jvp_string* s = malloc(sizeof(jvp_string) + size + 1);
jvp_string* s = jv_mem_alloc(sizeof(jvp_string) + size + 1);
s->refcnt.count = 1;
s->alloc_length = size;
return s;
@@ -389,7 +390,7 @@ static jv_complex jvp_string_new(const char* data, uint32_t length) {
static void jvp_string_free(jv_complex* s) {
if (jvp_refcnt_dec(s)) {
jvp_string* str = jvp_string_ptr(s);
free(str);
jv_mem_free(str);
}
}
@@ -569,17 +570,17 @@ jv jv_string_append_str(jv a, const char* str) {
jv jv_string_fmt(const char* fmt, ...) {
int size = 1024;
while (1) {
char* buf = malloc(size);
char* buf = jv_mem_alloc(size);
va_list args;
va_start(args, fmt);
int n = vsnprintf(buf, size, fmt, args);
va_end(args);
if (n < size) {
jv ret = jv_string_sized(buf, n);
free(buf);
jv_mem_free(buf);
return ret;
} else {
free(buf);
jv_mem_free(buf);
size = n * 2;
}
}
@@ -609,7 +610,7 @@ static jv_complex jvp_object_new(int size) {
// size must be a power of two
assert(size > 0 && (size & (size - 1)) == 0);
jvp_object* obj = malloc(sizeof(jvp_object) +
jvp_object* obj = jv_mem_alloc(sizeof(jvp_object) +
sizeof(struct object_slot) * size +
sizeof(int) * (size * 2));
obj->refcnt.count = 1;
@@ -711,7 +712,7 @@ static void jvp_object_free(jv_complex* o) {
jv_free(slot->value);
}
}
free(jvp_object_ptr(o));
jv_mem_free(jvp_object_ptr(o));
}
}
@@ -730,7 +731,7 @@ static void jvp_object_rehash(jv_complex* object) {
new_slot->value = slot->value;
}
// references are transported, just drop the old table
free(jvp_object_ptr(object));
jv_mem_free(jvp_object_ptr(object));
*object = new_object;
}

15
jv_alloc.c Normal file
View File

@@ -0,0 +1,15 @@
#include <stdlib.h>
#include "jv_alloc.h"
void* jv_mem_alloc(size_t sz) {
return malloc(sz);
}
void jv_mem_free(void* p) {
free(p);
}
void* jv_mem_realloc(void* p, size_t sz) {
return realloc(p, sz);
}

10
jv_alloc.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef JV_ALLOC_H
#define JV_ALLOC_H
#include <stddef.h>
void* jv_mem_alloc(size_t);
void jv_mem_free(void*);
__attribute__((warn_unused_result)) void* jv_mem_realloc(void*, size_t);
#endif

View File

@@ -1,6 +1,7 @@
#include "jv_aux.h"
#include <string.h>
#include <stdlib.h>
#include "jv_alloc.h"
jv jv_lookup(jv t, jv k) {
jv v;
@@ -78,7 +79,7 @@ static int string_cmp(const void* pa, const void* pb){
jv jv_keys(jv x) {
if (jv_get_kind(x) == JV_KIND_OBJECT) {
int nkeys = jv_object_length(jv_copy(x));
jv* keys = malloc(sizeof(jv) * nkeys);
jv* keys = jv_mem_alloc(sizeof(jv) * nkeys);
int kidx = 0;
jv_object_foreach(i, x) {
keys[kidx++] = jv_object_iter_key(x, i);
@@ -88,7 +89,7 @@ jv jv_keys(jv x) {
for (int i = 0; i<nkeys; i++) {
answer = jv_array_append(answer, keys[i]);
}
free(keys);
jv_mem_free(keys);
jv_free(x);
return answer;
} else if (jv_get_kind(x) == JV_KIND_ARRAY) {
@@ -199,7 +200,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 = malloc(sizeof(struct sort_entry) * n);
struct sort_entry* entries = jv_mem_alloc(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);
@@ -221,7 +222,7 @@ jv jv_sort(jv objects, jv keys) {
jv_free(entries[i].key);
ret = jv_array_set(ret, i, entries[i].object);
}
free(entries);
jv_mem_free(entries);
return ret;
}
@@ -247,6 +248,6 @@ jv jv_group(jv objects, jv keys) {
}
jv_free(curr_key);
ret = jv_array_append(ret, group);
free(entries);
jv_mem_free(entries);
return ret;
}

View File

@@ -195,6 +195,11 @@
#include "jv_dtoa.h"
#include "jv_alloc.h"
#define MALLOC jv_mem_alloc
#define FREE jv_mem_free
#ifndef Long
#define Long long
@@ -572,7 +577,7 @@ void jvp_dtoa_context_free(struct dtoa_context* C) {
while (C->freelist[k]) {
Bigint* v = C->freelist[k];
C->freelist[k] = v->next;
free(v);
FREE(v);
}
}
}

View File

@@ -5,6 +5,7 @@
#include "jv_dtoa.h"
#include "jv_parse.h"
#include "jv_unicode.h"
#include "jv_alloc.h"
typedef const char* presult;
@@ -32,8 +33,8 @@ void jv_parser_free(struct jv_parser* p) {
jv_free(p->next);
for (int i=0; i<p->stackpos; i++)
jv_free(p->stack[i]);
free(p->stack);
free(p->tokenbuf);
jv_mem_free(p->stack);
jv_mem_free(p->tokenbuf);
jvp_dtoa_context_free(&p->dtoa);
}
@@ -48,7 +49,7 @@ static void push(struct jv_parser* p, jv v) {
assert(p->stackpos <= p->stacklen);
if (p->stackpos == p->stacklen) {
p->stacklen = p->stacklen * 2 + 10;
p->stack = realloc(p->stack, p->stacklen * sizeof(jv));
p->stack = jv_mem_realloc(p->stack, p->stacklen * sizeof(jv));
}
assert(p->stackpos < p->stacklen);
p->stack[p->stackpos++] = v;
@@ -142,7 +143,7 @@ static void tokenadd(struct jv_parser* p, char c) {
assert(p->tokenpos <= p->tokenlen);
if (p->tokenpos == p->tokenlen) {
p->tokenlen = p->tokenlen*2 + 256;
p->tokenbuf = realloc(p->tokenbuf, p->tokenlen);
p->tokenbuf = jv_mem_realloc(p->tokenbuf, p->tokenlen);
}
assert(p->tokenpos < p->tokenlen);
p->tokenbuf[p->tokenpos++] = c;

12
lexer.l
View File

@@ -1,4 +1,5 @@
%{
#include "jv_alloc.h"
#include "compile.h"
struct lexer_param;
@@ -25,6 +26,7 @@ struct lexer_param;
%}
%option noyywrap nounput noinput nodefault
%option noyyalloc noyyrealloc noyyfree
%option reentrant
%option extra-type="int"
%option bison-bridge bison-locations
@@ -153,3 +155,13 @@ static int enter(int c, int currstate, yyscan_t yyscanner) {
yy_push_state(state, yyscanner);
return c;
}
void* yyalloc(size_t sz, void* extra) {
return jv_mem_alloc(sz);
}
void* yyrealloc(void* p, size_t sz, void* extra) {
return jv_mem_realloc(p, sz);
}
void yyfree(void* p, void* extra) {
jv_mem_free(p);
}

View File

@@ -4,6 +4,7 @@
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
#include "jv_alloc.h"
typedef struct {
int start, end;
} location;
@@ -24,7 +25,7 @@ static void locfile_init(struct locfile* l, const char* data, int length) {
for (int i=0; i<length; i++) {
if (data[i] == '\n') l->nlines++;
}
l->linemap = malloc(sizeof(int) * (l->nlines + 1));
l->linemap = jv_mem_alloc(sizeof(int) * (l->nlines + 1));
l->linemap[0] = 0;
int line = 1;
for (int i=0; i<length; i++) {
@@ -37,7 +38,7 @@ static void locfile_init(struct locfile* l, const char* data, int length) {
}
static void locfile_free(struct locfile* l) {
free(l->linemap);
jv_mem_free(l->linemap);
}
static int locfile_get_line(struct locfile* l, int pos) {

6
main.c
View File

@@ -1,3 +1,4 @@
#define _POSIX_SOURCE
#include <stdio.h>
#include <errno.h>
#include <string.h>
@@ -10,6 +11,7 @@
#include "locfile.h"
#include "parser.h"
#include "execute.h"
#include "jv_alloc.h"
#include "version.gen.h"
static const char* progname;
@@ -135,7 +137,7 @@ int main(int argc, char* argv[]) {
if (argc) progname = argv[0];
const char* program = 0;
input_filenames = malloc(sizeof(const char*) * argc);
input_filenames = jv_mem_alloc(sizeof(const char*) * argc);
ninput_files = 0;
int further_args_are_files = 0;
for (int i=1; i<argc; i++) {
@@ -256,7 +258,7 @@ int main(int argc, char* argv[]) {
process(slurped);
}
}
free(input_filenames);
jv_mem_free(input_filenames);
bytecode_free(bc);
return 0;
}

View File

@@ -2,6 +2,9 @@
#include <stdio.h>
#include <string.h>
#include "compile.h"
#include "jv_alloc.h"
#define YYMALLOC jv_mem_alloc
#define YYFREE jv_mem_free
struct lexer_param;