Skip to content

Commit

Permalink
refs #159: fixed a memory leak problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kray-G committed Jan 25, 2021
1 parent d0a20cd commit 8246bb7
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 114 deletions.
2 changes: 2 additions & 0 deletions include/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ default: \
} \
/**/

kvec_init_t(double);
kvec_init_t(int);
kvec_init_t(uint32_t);
struct kx_object_;
Expand Down Expand Up @@ -962,6 +963,7 @@ typedef struct kx_context_ {

int block_index;
int spread_additional;
kvec_t(double) dvalues;
kvec_t(uint32_t) labels;
kvec_t(kx_regex_t) regex;
kvec_t(kx_module_t) module;
Expand Down
1 change: 1 addition & 0 deletions include/kxnative.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ kvec_init_t(kx_switch_t);

#define KNX_MAX_REG (64)
typedef struct kx_native_context_ {
kx_context_t *ctx;
struct sljit_compiler *C;
const char *func_name;
int local_vars;
Expand Down
13 changes: 11 additions & 2 deletions src/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ void init_allocation(kx_context_t *ctx)
for (int i = 0; i < KX_INIT_FRM_COUNT; ++i) {
kv_A(ctx->alloc.frm_dead, i) = (kx_frm_t *)kx_calloc(1, sizeof(kx_frm_t));
}
kv_shrinkto(ctx->alloc.frm_dead, KX_INIT_FRM_COUNT);
kv_resize(kx_fnc_t*, ctx->alloc.fnc_dead, KX_INIT_FNC_COUNT);
for (int i = 0; i < KX_INIT_FNC_COUNT; ++i) {
kv_A(ctx->alloc.fnc_dead, i) = (kx_fnc_t *)kx_calloc(1, sizeof(kx_fnc_t));
}
kv_shrinkto(ctx->alloc.fnc_dead, KX_INIT_FNC_COUNT);
}

kx_context_t *make_context(void)
Expand Down Expand Up @@ -472,11 +474,14 @@ static void gc_object_cleanup(kx_context_t *ctx)
}
kliter_t(fnc) *pfnc;
for (pfnc = kl_begin(ctx->alloc.fnc_alive); pfnc != kl_end(ctx->alloc.fnc_alive); pfnc = kl_next(pfnc)) {
kx_free(kl_val(pfnc));
kx_fnc_t *fnc = kl_val(pfnc);
kv_destroy(fnc->stack);
kx_free(fnc);
}
kliter_t(frm) *pfrm;
for (pfrm = kl_begin(ctx->alloc.frm_alive); pfrm != kl_end(ctx->alloc.frm_alive); pfrm = kl_next(pfrm)) {
kx_frm_t *frm = kl_val(pfrm);
kv_destroy(frm->varname);
kv_destroy(frm->v);
kx_free(frm);
}
Expand Down Expand Up @@ -514,11 +519,14 @@ static void gc_object_cleanup(kx_context_t *ctx)
}
l = kv_size(ctx->alloc.fnc_dead);
for (i = 0; i < l; ++i) {
kx_free(kv_A(ctx->alloc.fnc_dead, i));
kx_fnc_t *fnc = kv_A(ctx->alloc.fnc_dead, i);
kv_destroy(fnc->stack);
kx_free(fnc);
}
l = kv_size(ctx->alloc.frm_dead);
for (i = 0; i < l; ++i) {
kx_frm_t *frm = kv_A(ctx->alloc.frm_dead, i);
kv_destroy(frm->varname);
kv_destroy(frm->v);
kx_free(frm);
}
Expand Down Expand Up @@ -623,6 +631,7 @@ void context_cleanup(kx_context_t *ctx)
builtin_cleanup(ctx);
debuginfo_cleanup(ctx);
free_string(ctx);
kv_destroy(ctx->dvalues);
kv_destroy(ctx->labels);
kv_destroy(ctx->fixcode);
kv_destroy(ctx->regex);
Expand Down
2 changes: 2 additions & 0 deletions src/ast_analyzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,7 @@ LOOP_HEAD:;
if (node->local_vars < node->count_args) {
node->local_vars = node->count_args;
}
kv_destroy(*(actx->vars));
kx_free(actx->vars);
actx->vars = vars;
actx->anon_arg = anon_arg;
Expand Down Expand Up @@ -1368,6 +1369,7 @@ LOOP_HEAD:;
if (node->local_vars < node->count_args) {
node->local_vars = node->count_args;
}
kv_destroy(*(actx->vars));
kx_free(actx->vars);
actx->vars = vars;
actx->anon_arg = anon_arg;
Expand Down
2 changes: 2 additions & 0 deletions src/ast_gencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -2026,6 +2026,7 @@ LOOP_HEAD:;
ana->function = func;
ana->classname = classname;
ana->in_try = in_try;
kv_destroy(*(ana->finallies));
kx_free(ana->finallies);
ana->finallies = finallies;
kv_remove_last(ana->fidxlist);
Expand Down Expand Up @@ -2082,6 +2083,7 @@ LOOP_HEAD:;
ana->block = old;
ana->function = func;
ana->in_try = in_try;
kv_destroy(*(ana->finallies));
kx_free(ana->finallies);
ana->finallies = finallies;
kv_remove_last(ana->fidxlist);
Expand Down
12 changes: 9 additions & 3 deletions src/ast_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ static void nativejit_ast(kx_native_context_t *nctx, kx_object_t *node, int lval
kv_destroy(sw->expr_case_list);
kv_destroy(sw->ival_case_list);
kv_remove_last(nctx->switch_list);
break;
break;
}
case KXST_CASE: { /* lhs: cond */
int clen = kv_size(KXNBLK(nctx)->code);
Expand Down Expand Up @@ -1972,6 +1972,7 @@ static void nativejit_ast(kx_native_context_t *nctx, kx_object_t *node, int lval
nctx->block = gen_kxn_block(nctx);
nativejit_ast(nctx, node->rhs, 0);
do_native_finally_all(nctx);
kv_destroy(*(nctx->finallies));
kx_free(nctx->finallies);
nctx->finallies = finallies;
break;
Expand Down Expand Up @@ -2028,7 +2029,7 @@ void native_dump_temp(unsigned char *f, const char *name, int size)

kxn_func_t start_nativejit_ast(kx_context_t *ctx, kx_object_t *node, uint8_t *args, int argn)
{
kx_native_context_t nctx = {0};
kx_native_context_t nctx = { .ctx = ctx };
if (node->type != KXST_NATIVE) {
return (kxn_func_t){ .func = 0 };
}
Expand Down Expand Up @@ -2069,7 +2070,12 @@ kxn_func_t start_nativejit_ast(kx_context_t *ctx, kx_object_t *node, uint8_t *ar
}
}

sljit_free_compiler(nctx.C);
sljit_free_compiler(nctx.C);

int blocklen = kv_size(nctx.block_list);
for (int i = 0; i < blocklen; ++i) {
kv_destroy((kv_A(nctx.block_list, i)).code);
}
kv_destroy(nctx.block_list);
kv_destroy(nctx.continue_list);
kv_destroy(nctx.break_list);
Expand Down
2 changes: 2 additions & 0 deletions src/ast_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ void free_nodes(void)
obj = next;
}
kx_obj_mgr = NULL;
kv_destroy(ns_stack);
kv_init(ns_stack);
}

void kx_make_native_mode(void)
Expand Down
4 changes: 3 additions & 1 deletion src/extlib/kxfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@ int File_static_load(int args, kx_frm_t *frmv, kx_frm_t *lexv, kx_context_t *ctx
if (!fi) {
KX_THROW_BLTIN_EXCEPTION("FileException", static_format("File open failed: %s", strerror(errno)));
}
return File_load_impl(args, ctx, fi, 1);
int r = File_load_impl(args, ctx, fi, 1);
free_fileinfo(fi);
return r;
}

/*
Expand Down
8 changes: 3 additions & 5 deletions src/extlib/kxregex.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ int Regex_reset(int args, kx_frm_t *frmv, kx_frm_t *lexv, kx_context_t *ctx)
}

KEX_SET_PROP_CSTR(obj, "source", str);
if (r->source) {
kx_free(r->source);
}
kx_free(r->source);
r->source = kx_calloc(strlen(str) + 2, sizeof(char));
strcpy(r->source, str);
r->start = 0;
Expand Down Expand Up @@ -235,7 +233,7 @@ int Regex_splitOf(int args, kx_frm_t *frmv, kx_frm_t *lexv, kx_context_t *ctx)
kx_obj_t *res = allocate_obj(ctx);
kstr_t *sv = allocate_str(ctx);

while (index < len) {
while (index < len) {
onig_region_clear(r->region);
int rx = onig_search(r->reg, str + index, end, str + index, end, r->region, ONIG_OPTION_NONE);
if (rx == ONIG_MISMATCH) {
Expand Down Expand Up @@ -327,7 +325,7 @@ int Regex_replaceOf(int args, kx_frm_t *frmv, kx_frm_t *lexv, kx_context_t *ctx)
const unsigned char *end = str + len;
kstr_t *sv = allocate_str(ctx);

while (index < len) {
while (index < len) {
onig_region_clear(r->region);
int rx = onig_search(r->reg, str + index, end, str + index, end, r->region, ONIG_OPTION_NONE);
if (rx == ONIG_MISMATCH) {
Expand Down
20 changes: 11 additions & 9 deletions src/extlib/kxstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <kxutf8.h>
#include <kxthread.h>

#define ISALNUM(n) ((-1 <= n && n <= 255) ? isalnum(n) : 0)

KX_DECL_MEM_ALLOCATORS();

int String_length(int args, kx_frm_t *frmv, kx_frm_t *lexv, kx_context_t *ctx)
Expand Down Expand Up @@ -460,7 +462,7 @@ int String_stem(int args, kx_frm_t *frmv, kx_frm_t *lexv, kx_context_t *ctx)
KX_ADJST_STACK();
pos = string_find_last_of_impl(ks_string(sv), ".");
if (pos >= 0) {
sv = ks_slice(sv, 0, pos);
ks_append_n(sv, str, pos);
}
push_sv(ctx->stack, sv);
return 0;
Expand Down Expand Up @@ -528,8 +530,7 @@ int String_parentPath(int args, kx_frm_t *frmv, kx_frm_t *lexv, kx_context_t *ct
int pos = string_find_last_of_impl(str, "\\/");
if (pos >= 0) {
KX_ADJST_STACK();
ks_append(sv, str);
sv = ks_slice(sv, 0, pos);
ks_append_n(sv, str, pos);
push_sv(ctx->stack, sv);
return 0;
}
Expand Down Expand Up @@ -664,11 +665,11 @@ int String_next(int args, kx_frm_t *frmv, kx_frm_t *lexv, kx_context_t *ctx)
}

int hchar = str[0];
int other = !isalnum(hchar);
int other = !ISALNUM(hchar);
int alnum = 0;
int len = strlen(str);
for (int i = (other ? 1 : 0); i < len; ++i) {
if (isalnum(str[i])) {
if (ISALNUM(str[i])) {
++alnum;
}
}
Expand Down Expand Up @@ -711,7 +712,7 @@ int String_next(int args, kx_frm_t *frmv, kx_frm_t *lexv, kx_context_t *ctx)
} else if (ch == 0xFF) {
bin[i] = 0;
prv = 1;
} else if (isalnum(ch)) {
} else if (ISALNUM(ch)) {
if (prv) {
bin[i] = ch + 1;
} else {
Expand Down Expand Up @@ -778,15 +779,16 @@ int String_toUpperLower(int args, kx_frm_t *frmv, kx_frm_t *lexv, kx_context_t *
while (i < s) {
p[i] = str[i];
++i;
}
}
while (i < e) {
p[i] = f(str[i]);
int v = str[i];
p[i] = (-1 <= v && v <= 255) ? f(v) : v;
++i;
}
while (i < len) {
p[i] = str[i];
++i;
}
}

kstr_t *sv = allocate_str(ctx);
ks_append(sv, p);
Expand Down
5 changes: 5 additions & 0 deletions src/extlib/kxsystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ static void system_finalize(void)
pthread_cond_destroy(&g_system_cond);
pthread_mutex_destroy(&g_system_mtx);
free_shared_string();

kh_destroy(value_map, g_value_map);
kh_destroy(mutex_map, g_mutex_map);
kh_destroy(named_mutex_map, g_named_mutex_map);
kh_destroy(cond_map, g_cond_map);
}

static inline kx_val_t mk_json_object(kx_context_t *ctx, json_object_t *j)
Expand Down
1 change: 1 addition & 0 deletions src/ir_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ void thread_free(void *pp)
kx_thread_pack_t *p = (kx_thread_pack_t *)pp;
if (p->r) {
pthread_join(p->r, NULL);
kx_free(p);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/kstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ ks_replace_char(kstr_t *self, char s, char d) {
void
ks_trim_left(kstr_t *self) {
int c;
while ((c = *self->data) && isspace(c)) {
while ((c = *self->data) && (-1 <= c && c <= 255) && isspace(c)) {
++self->data;
}
}
Expand All @@ -315,7 +315,7 @@ void
ks_trim_right(kstr_t *self) {
int c;
size_t i = ks_length(self) - 1;
while ((c = self->data[i]) && isspace(c)) {
while ((c = self->data[i]) && (-1 <= c && c <= 255) && isspace(c)) {
self->data[i--] = 0;
}
}
Expand Down
Loading

0 comments on commit 8246bb7

Please sign in to comment.