Add wrappers for malloc/realloc/free. See #43.
This commit is contained in:
1 parent
75421cbfe3
commit
04daafbde3
15 files changed
+106
-51
No files matched your search
@@ -24,7 +24,7 @@ version.gen.h: VERSION
|
|||||||
sed 's/.*/#define JQ_VERSION "&"/' $^ > $@
|
sed 's/.*/#define JQ_VERSION "&"/' $^ > $@
|
||||||
main.c: version.gen.h
|
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: CFLAGS += -DJQ_DEBUG=1
|
||||||
jq_test: $(JQ_SRC) jq_test.c
|
jq_test: $(JQ_SRC) jq_test.c
|
||||||
|
|||||||
+6
-5
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "bytecode.h"
|
#include "bytecode.h"
|
||||||
#include "opcode.h"
|
#include "opcode.h"
|
||||||
|
#include "jv_alloc.h"
|
||||||
|
|
||||||
static int bytecode_operation_length(uint16_t* codeptr) {
|
static int bytecode_operation_length(uint16_t* codeptr) {
|
||||||
int length = opcode_describe(*codeptr)->length;
|
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) {
|
void symbol_table_free(struct symbol_table* syms) {
|
||||||
free(syms->cfunctions);
|
jv_mem_free(syms->cfunctions);
|
||||||
free(syms);
|
jv_mem_free(syms);
|
||||||
}
|
}
|
||||||
|
|
||||||
void bytecode_free(struct bytecode* bc) {
|
void bytecode_free(struct bytecode* bc) {
|
||||||
free(bc->code);
|
jv_mem_free(bc->code);
|
||||||
jv_free(bc->constants);
|
jv_free(bc->constants);
|
||||||
for (int i=0; i<bc->nsubfunctions; i++)
|
for (int i=0; i<bc->nsubfunctions; i++)
|
||||||
bytecode_free(bc->subfunctions[i]);
|
bytecode_free(bc->subfunctions[i]);
|
||||||
if (!bc->parent)
|
if (!bc->parent)
|
||||||
symbol_table_free(bc->globals);
|
symbol_table_free(bc->globals);
|
||||||
free(bc->subfunctions);
|
jv_mem_free(bc->subfunctions);
|
||||||
free(bc);
|
jv_mem_free(bc);
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#define _GNU_SOURCE // for strdup
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -5,6 +6,7 @@
|
|||||||
#include "compile.h"
|
#include "compile.h"
|
||||||
#include "bytecode.h"
|
#include "bytecode.h"
|
||||||
#include "locfile.h"
|
#include "locfile.h"
|
||||||
|
#include "jv_alloc.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
The intermediate representation for jq filters is as a sequence of
|
The intermediate representation for jq filters is as a sequence of
|
||||||
@@ -57,7 +59,7 @@ struct inst {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static inst* inst_new(opcode op) {
|
static inst* inst_new(opcode op) {
|
||||||
inst* i = malloc(sizeof(inst));
|
inst* i = jv_mem_alloc(sizeof(inst));
|
||||||
i->next = i->prev = 0;
|
i->next = i->prev = 0;
|
||||||
i->op = op;
|
i->op = op;
|
||||||
i->bytecode_pos = -1;
|
i->bytecode_pos = -1;
|
||||||
@@ -70,13 +72,13 @@ static inst* inst_new(opcode op) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void inst_free(struct inst* i) {
|
static void inst_free(struct inst* i) {
|
||||||
free(i->symbol);
|
jv_mem_free(i->symbol);
|
||||||
block_free(i->subfn);
|
block_free(i->subfn);
|
||||||
block_free(i->arglist);
|
block_free(i->arglist);
|
||||||
if (opcode_describe(i->op)->flags & OP_HAS_CONSTANT) {
|
if (opcode_describe(i->op)->flags & OP_HAS_CONSTANT) {
|
||||||
jv_free(i->imm.constant);
|
jv_free(i->imm.constant);
|
||||||
}
|
}
|
||||||
free(i);
|
jv_mem_free(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
static block inst_block(inst* 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) {
|
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) {
|
for (inst* curr = b.first; curr; curr = curr->next) {
|
||||||
if (curr->op == CLOSURE_CREATE) {
|
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;
|
bc->subfunctions[curr->imm.intval] = subfn;
|
||||||
subfn->globals = bc->globals;
|
subfn->globals = bc->globals;
|
||||||
subfn->parent = bc;
|
subfn->parent = bc;
|
||||||
@@ -568,7 +570,7 @@ static int compile(struct locfile* locations, struct bytecode* bc, block b) {
|
|||||||
bc->subfunctions = 0;
|
bc->subfunctions = 0;
|
||||||
}
|
}
|
||||||
bc->codelen = pos;
|
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;
|
bc->code = code;
|
||||||
pos = 0;
|
pos = 0;
|
||||||
jv constant_pool = jv_array();
|
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) {
|
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->parent = 0;
|
||||||
bc->nclosures = 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);
|
int ncfunc = count_cfunctions(b);
|
||||||
bc->globals->ncfunctions = 0;
|
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);
|
int nerrors = compile(locations, bc, b);
|
||||||
assert(bc->globals->ncfunctions == ncfunc);
|
assert(bc->globals->ncfunctions == ncfunc);
|
||||||
if (nerrors > 0) {
|
if (nerrors > 0) {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#include "forkable_stack.h"
|
#include "forkable_stack.h"
|
||||||
#include "frame_layout.h"
|
#include "frame_layout.h"
|
||||||
|
|
||||||
|
#include "jv_alloc.h"
|
||||||
#include "locfile.h"
|
#include "locfile.h"
|
||||||
#include "jv.h"
|
#include "jv.h"
|
||||||
#include "jv_aux.h"
|
#include "jv_aux.h"
|
||||||
@@ -33,7 +34,7 @@ int path_push(stackval sv, jv val) {
|
|||||||
if (pos == pathsize) {
|
if (pos == pathsize) {
|
||||||
int oldpathsize = pathsize;
|
int oldpathsize = pathsize;
|
||||||
pathsize = oldpathsize ? oldpathsize * 2 : 100;
|
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++) {
|
for (int i=oldpathsize; i<pathsize; i++) {
|
||||||
pathbuf[i] = jv_invalid();
|
pathbuf[i] = jv_invalid();
|
||||||
}
|
}
|
||||||
@@ -491,7 +492,7 @@ void jq_teardown() {
|
|||||||
for (int i=0; i<pathsize; i++) {
|
for (int i=0; i<pathsize; i++) {
|
||||||
jv_free(pathbuf[i]);
|
jv_free(pathbuf[i]);
|
||||||
}
|
}
|
||||||
free(pathbuf);
|
jv_mem_free(pathbuf);
|
||||||
pathbuf = 0;
|
pathbuf = 0;
|
||||||
pathsize = 0;
|
pathsize = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -1,9 +1,9 @@
|
|||||||
#ifndef FORKABLE_STACK_H
|
#ifndef FORKABLE_STACK_H
|
||||||
#define FORKABLE_STACK_H
|
#define FORKABLE_STACK_H
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "jv_alloc.h"
|
||||||
|
|
||||||
struct forkable_stack_header {
|
struct forkable_stack_header {
|
||||||
/* distance in bytes between this header and the header
|
/* distance in bytes between this header and the header
|
||||||
@@ -18,7 +18,7 @@ struct forkable_stack {
|
|||||||
char* stk;
|
char* stk;
|
||||||
|
|
||||||
// stk+length is just past end of allocated area
|
// stk+length is just past end of allocated area
|
||||||
// stk = malloc(length)
|
// stk = jv_mem_alloc(length)
|
||||||
int length;
|
int length;
|
||||||
|
|
||||||
// stk+pos is header of top object, or stk+length if empty
|
// 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) {
|
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->length = sz;
|
||||||
s->pos = s->length;
|
s->pos = s->length;
|
||||||
s->savedlimit = 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) {
|
static void forkable_stack_free(struct forkable_stack* s) {
|
||||||
assert(forkable_stack_empty(s));
|
assert(forkable_stack_empty(s));
|
||||||
assert(s->savedlimit == s->length);
|
assert(s->savedlimit == s->length);
|
||||||
free(s->stk);
|
jv_mem_free(s->stk);
|
||||||
s->stk = 0;
|
s->stk = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ static void* forkable_stack_push(struct forkable_stack* s, size_t sz_size) {
|
|||||||
if (curr - size < 0) {
|
if (curr - size < 0) {
|
||||||
int oldlen = s->length;
|
int oldlen = s->length;
|
||||||
s->length = (size + oldlen + 1024) * 2;
|
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;
|
int shift = s->length - oldlen;
|
||||||
memmove(s->stk + shift, s->stk, oldlen);
|
memmove(s->stk + shift, s->stk, oldlen);
|
||||||
s->pos += shift;
|
s->pos += shift;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#include "jv_alloc.h"
|
||||||
#include "jv.h"
|
#include "jv.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -86,7 +87,7 @@ jv jv_invalid_with_msg(jv err) {
|
|||||||
jv x;
|
jv x;
|
||||||
x.kind = JV_KIND_INVALID;
|
x.kind = JV_KIND_INVALID;
|
||||||
x.val.complex.i[0] = x.val.complex.i[1] = 0;
|
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;
|
x.val.complex.ptr = &i->refcnt;
|
||||||
i->refcnt.count = 1;
|
i->refcnt.count = 1;
|
||||||
i->errmsg = err;
|
i->errmsg = err;
|
||||||
@@ -113,7 +114,7 @@ int jv_invalid_has_msg(jv inv) {
|
|||||||
static void jvp_invalid_free(jv_complex* x) {
|
static void jvp_invalid_free(jv_complex* x) {
|
||||||
if (jvp_refcnt_dec(x)) {
|
if (jvp_refcnt_dec(x)) {
|
||||||
jv_free(((jvp_invalid*)x->ptr)->errmsg);
|
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) {
|
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->refcnt.count = 1;
|
||||||
a->length = 0;
|
a->length = 0;
|
||||||
a->alloc_length = size;
|
a->alloc_length = size;
|
||||||
@@ -175,7 +176,7 @@ static void jvp_array_free(jv_complex* a) {
|
|||||||
for (int i=0; i<array->length; i++) {
|
for (int i=0; i<array->length; i++) {
|
||||||
jv_free(array->elements[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) {
|
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->refcnt.count = 1;
|
||||||
s->alloc_length = size;
|
s->alloc_length = size;
|
||||||
return s;
|
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) {
|
static void jvp_string_free(jv_complex* s) {
|
||||||
if (jvp_refcnt_dec(s)) {
|
if (jvp_refcnt_dec(s)) {
|
||||||
jvp_string* str = jvp_string_ptr(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, ...) {
|
jv jv_string_fmt(const char* fmt, ...) {
|
||||||
int size = 1024;
|
int size = 1024;
|
||||||
while (1) {
|
while (1) {
|
||||||
char* buf = malloc(size);
|
char* buf = jv_mem_alloc(size);
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
int n = vsnprintf(buf, size, fmt, args);
|
int n = vsnprintf(buf, size, fmt, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
if (n < size) {
|
if (n < size) {
|
||||||
jv ret = jv_string_sized(buf, n);
|
jv ret = jv_string_sized(buf, n);
|
||||||
free(buf);
|
jv_mem_free(buf);
|
||||||
return ret;
|
return ret;
|
||||||
} else {
|
} else {
|
||||||
free(buf);
|
jv_mem_free(buf);
|
||||||
size = n * 2;
|
size = n * 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -609,9 +610,9 @@ static jv_complex jvp_object_new(int size) {
|
|||||||
|
|
||||||
// size must be a power of two
|
// size must be a power of two
|
||||||
assert(size > 0 && (size & (size - 1)) == 0);
|
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(struct object_slot) * size +
|
||||||
sizeof(int) * (size * 2));
|
sizeof(int) * (size * 2));
|
||||||
obj->refcnt.count = 1;
|
obj->refcnt.count = 1;
|
||||||
for (int i=0; i<size; i++) {
|
for (int i=0; i<size; i++) {
|
||||||
obj->elements[i].next = i - 1;
|
obj->elements[i].next = i - 1;
|
||||||
@@ -711,7 +712,7 @@ static void jvp_object_free(jv_complex* o) {
|
|||||||
jv_free(slot->value);
|
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;
|
new_slot->value = slot->value;
|
||||||
}
|
}
|
||||||
// references are transported, just drop the old table
|
// references are transported, just drop the old table
|
||||||
free(jvp_object_ptr(object));
|
jv_mem_free(jvp_object_ptr(object));
|
||||||
*object = new_object;
|
*object = new_object;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1037,4 +1038,4 @@ int jv_contains(jv a, jv b) {
|
|||||||
jv_free(a);
|
jv_free(a);
|
||||||
jv_free(b);
|
jv_free(b);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
+15
@@ -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
@@ -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
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "jv_aux.h"
|
#include "jv_aux.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include "jv_alloc.h"
|
||||||
|
|
||||||
jv jv_lookup(jv t, jv k) {
|
jv jv_lookup(jv t, jv k) {
|
||||||
jv v;
|
jv v;
|
||||||
@@ -78,7 +79,7 @@ static int string_cmp(const void* pa, const void* pb){
|
|||||||
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 = malloc(sizeof(jv) * nkeys);
|
jv* keys = jv_mem_alloc(sizeof(jv) * nkeys);
|
||||||
int kidx = 0;
|
int kidx = 0;
|
||||||
jv_object_foreach(i, x) {
|
jv_object_foreach(i, x) {
|
||||||
keys[kidx++] = jv_object_iter_key(x, i);
|
keys[kidx++] = jv_object_iter_key(x, i);
|
||||||
@@ -88,7 +89,7 @@ jv jv_keys(jv x) {
|
|||||||
for (int i = 0; i<nkeys; i++) {
|
for (int i = 0; i<nkeys; i++) {
|
||||||
answer = jv_array_append(answer, keys[i]);
|
answer = jv_array_append(answer, keys[i]);
|
||||||
}
|
}
|
||||||
free(keys);
|
jv_mem_free(keys);
|
||||||
jv_free(x);
|
jv_free(x);
|
||||||
return answer;
|
return answer;
|
||||||
} else if (jv_get_kind(x) == JV_KIND_ARRAY) {
|
} 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_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 = 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++) {
|
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);
|
||||||
@@ -221,7 +222,7 @@ jv jv_sort(jv objects, jv keys) {
|
|||||||
jv_free(entries[i].key);
|
jv_free(entries[i].key);
|
||||||
ret = jv_array_set(ret, i, entries[i].object);
|
ret = jv_array_set(ret, i, entries[i].object);
|
||||||
}
|
}
|
||||||
free(entries);
|
jv_mem_free(entries);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,6 +248,6 @@ jv jv_group(jv objects, jv keys) {
|
|||||||
}
|
}
|
||||||
jv_free(curr_key);
|
jv_free(curr_key);
|
||||||
ret = jv_array_append(ret, group);
|
ret = jv_array_append(ret, group);
|
||||||
free(entries);
|
jv_mem_free(entries);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -195,6 +195,11 @@
|
|||||||
|
|
||||||
#include "jv_dtoa.h"
|
#include "jv_dtoa.h"
|
||||||
|
|
||||||
|
#include "jv_alloc.h"
|
||||||
|
#define MALLOC jv_mem_alloc
|
||||||
|
#define FREE jv_mem_free
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef Long
|
#ifndef Long
|
||||||
#define Long long
|
#define Long long
|
||||||
@@ -572,7 +577,7 @@ void jvp_dtoa_context_free(struct dtoa_context* C) {
|
|||||||
while (C->freelist[k]) {
|
while (C->freelist[k]) {
|
||||||
Bigint* v = C->freelist[k];
|
Bigint* v = C->freelist[k];
|
||||||
C->freelist[k] = v->next;
|
C->freelist[k] = v->next;
|
||||||
free(v);
|
FREE(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-4
@@ -5,6 +5,7 @@
|
|||||||
#include "jv_dtoa.h"
|
#include "jv_dtoa.h"
|
||||||
#include "jv_parse.h"
|
#include "jv_parse.h"
|
||||||
#include "jv_unicode.h"
|
#include "jv_unicode.h"
|
||||||
|
#include "jv_alloc.h"
|
||||||
|
|
||||||
typedef const char* presult;
|
typedef const char* presult;
|
||||||
|
|
||||||
@@ -32,8 +33,8 @@ void jv_parser_free(struct jv_parser* p) {
|
|||||||
jv_free(p->next);
|
jv_free(p->next);
|
||||||
for (int i=0; i<p->stackpos; i++)
|
for (int i=0; i<p->stackpos; i++)
|
||||||
jv_free(p->stack[i]);
|
jv_free(p->stack[i]);
|
||||||
free(p->stack);
|
jv_mem_free(p->stack);
|
||||||
free(p->tokenbuf);
|
jv_mem_free(p->tokenbuf);
|
||||||
jvp_dtoa_context_free(&p->dtoa);
|
jvp_dtoa_context_free(&p->dtoa);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +49,7 @@ static void push(struct jv_parser* p, jv v) {
|
|||||||
assert(p->stackpos <= p->stacklen);
|
assert(p->stackpos <= p->stacklen);
|
||||||
if (p->stackpos == p->stacklen) {
|
if (p->stackpos == p->stacklen) {
|
||||||
p->stacklen = p->stacklen * 2 + 10;
|
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);
|
assert(p->stackpos < p->stacklen);
|
||||||
p->stack[p->stackpos++] = v;
|
p->stack[p->stackpos++] = v;
|
||||||
@@ -142,7 +143,7 @@ static void tokenadd(struct jv_parser* p, char c) {
|
|||||||
assert(p->tokenpos <= p->tokenlen);
|
assert(p->tokenpos <= p->tokenlen);
|
||||||
if (p->tokenpos == p->tokenlen) {
|
if (p->tokenpos == p->tokenlen) {
|
||||||
p->tokenlen = p->tokenlen*2 + 256;
|
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);
|
assert(p->tokenpos < p->tokenlen);
|
||||||
p->tokenbuf[p->tokenpos++] = c;
|
p->tokenbuf[p->tokenpos++] = c;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
%{
|
%{
|
||||||
|
#include "jv_alloc.h"
|
||||||
#include "compile.h"
|
#include "compile.h"
|
||||||
|
|
||||||
struct lexer_param;
|
struct lexer_param;
|
||||||
@@ -25,6 +26,7 @@ struct lexer_param;
|
|||||||
%}
|
%}
|
||||||
|
|
||||||
%option noyywrap nounput noinput nodefault
|
%option noyywrap nounput noinput nodefault
|
||||||
|
%option noyyalloc noyyrealloc noyyfree
|
||||||
%option reentrant
|
%option reentrant
|
||||||
%option extra-type="int"
|
%option extra-type="int"
|
||||||
%option bison-bridge bison-locations
|
%option bison-bridge bison-locations
|
||||||
@@ -153,3 +155,13 @@ static int enter(int c, int currstate, yyscan_t yyscanner) {
|
|||||||
yy_push_state(state, yyscanner);
|
yy_push_state(state, yyscanner);
|
||||||
return c;
|
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);
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include "jv_alloc.h"
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int start, end;
|
int start, end;
|
||||||
} location;
|
} location;
|
||||||
@@ -24,7 +25,7 @@ static void locfile_init(struct locfile* l, const char* data, int length) {
|
|||||||
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 = malloc(sizeof(int) * (l->nlines + 1));
|
l->linemap = jv_mem_alloc(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++) {
|
||||||
@@ -37,7 +38,7 @@ static void locfile_init(struct locfile* l, const char* data, int length) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void locfile_free(struct locfile* l) {
|
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) {
|
static int locfile_get_line(struct locfile* l, int pos) {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#define _POSIX_SOURCE
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -10,6 +11,7 @@
|
|||||||
#include "locfile.h"
|
#include "locfile.h"
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "execute.h"
|
#include "execute.h"
|
||||||
|
#include "jv_alloc.h"
|
||||||
#include "version.gen.h"
|
#include "version.gen.h"
|
||||||
|
|
||||||
static const char* progname;
|
static const char* progname;
|
||||||
@@ -135,7 +137,7 @@ int main(int argc, char* argv[]) {
|
|||||||
if (argc) progname = argv[0];
|
if (argc) progname = argv[0];
|
||||||
|
|
||||||
const char* program = 0;
|
const char* program = 0;
|
||||||
input_filenames = malloc(sizeof(const char*) * argc);
|
input_filenames = jv_mem_alloc(sizeof(const char*) * argc);
|
||||||
ninput_files = 0;
|
ninput_files = 0;
|
||||||
int further_args_are_files = 0;
|
int further_args_are_files = 0;
|
||||||
for (int i=1; i<argc; i++) {
|
for (int i=1; i<argc; i++) {
|
||||||
@@ -256,7 +258,7 @@ int main(int argc, char* argv[]) {
|
|||||||
process(slurped);
|
process(slurped);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(input_filenames);
|
jv_mem_free(input_filenames);
|
||||||
bytecode_free(bc);
|
bytecode_free(bc);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user