From ee5861a25c2fa9ba42e84223d894fffd0c3cc17d Mon Sep 17 00:00:00 2001 From: Minus Date: Tue, 17 Nov 2026 22:15:10 +0000 Subject: [PATCH] refcount: own constants, closure attrs and generators - bytecode constant pool: cat_bytecode_add_const retains, and cat_bytecode_function_free releases each constant, so a chunk owns its constants even when built from borrowed values (bc_run). The compiler drops its own reference via add_const_owned. - closures: function_destroy releases upvalues (if any) and the attrs table. - generators: add a destroy that frees the operand stack and releases the captured closure and arguments; generator_new retains them. Verified with AddressSanitizer (detect_leaks=0): suite 130 green, no ASan reports. --- src/cat/bytecode.c | 14 ++++++++------ src/cat/compiler.c | 24 ++++++++++++++++++------ src/cat/types/function.c | 6 ++++++ src/cat/types/generator.c | 20 ++++++++++++++++++++ 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/cat/bytecode.c b/src/cat/bytecode.c index 3a181a4..d52e0f9 100644 --- a/src/cat/bytecode.c +++ b/src/cat/bytecode.c @@ -55,6 +55,10 @@ int cat_bytecode_add_const(CAT_BYTECODE_Function *fn, CAT_VALUE_Value *v) fn->consts = ncp; fn->capconsts = nc; } + /* The pool owns a reference: this keeps constants alive as long as + * the chunk does, even when the caller built it from borrowed + * values (e.g. `bc_run` reading a table). */ + cat_value_retain(v); fn->consts[fn->nconsts].value = v; return fn->nconsts++; } @@ -115,12 +119,10 @@ void cat_bytecode_function_free(CAT_BYTECODE_Function *fn) if (fn == NULL) return; - /* Constant VALUES are intentionally not destroyed here. A chunk's - * constants may be shared (e.g. a chunk built from a catscript table - * via bc_run borrows the table's values), and destroying them would - * double-free. The pool itself is freed; leaked values are bounded - * by the number of compilations in a single run. */ - (void)i; + /* Release the constant values this chunk owned (it took a reference + * in cat_bytecode_add_const). The last holder frees them. */ + for (i = 0; i < fn->nconsts; i++) + cat_value_release(fn->consts[i].value); for (i = 0; i < fn->nprotos; i++) cat_bytecode_function_free(fn->protos[i]); free(fn->code); diff --git a/src/cat/compiler.c b/src/cat/compiler.c index dda64a2..69e3734 100644 --- a/src/cat/compiler.c +++ b/src/cat/compiler.c @@ -112,7 +112,19 @@ static FuncCtx *ctx(Compiler *c) static int add_string(Compiler *c, const char *s) { - return cat_bytecode_add_const(cur_fn(c), cat_types_ascii_text_new(s)); + CAT_VALUE_Value *v = cat_types_ascii_text_new(s); + int k = cat_bytecode_add_const(cur_fn(c), v); + cat_value_release(v); /* pool owns it now */ + return k; +} + +/* Add a freshly-created constant: the pool takes a reference, we drop + * ours, so the constant lives exactly as long as the chunk. */ +static int add_const_owned(Compiler *c, CAT_VALUE_Value *v) +{ + int k = cat_bytecode_add_const(cur_fn(c), v); + cat_value_release(v); + return k; } static CAT_BYTECODE_Function *cur_fn(Compiler *c) @@ -284,13 +296,13 @@ static void primary(Compiler *c) switch (t.kind) { case CAT_LEXER_TOK_INT: { - int k = cat_bytecode_add_const(cur_fn(c), cat_types_int_new(t.ival)); + int k = add_const_owned(c, cat_types_int_new(t.ival)); emit(c, CAT_BYTECODE_OP_CONST, k, 0, t.line); next(c); break; } case CAT_LEXER_TOK_FLOAT: { - int k = cat_bytecode_add_const(cur_fn(c), cat_types_float_new(t.fval)); + int k = add_const_owned(c, cat_types_float_new(t.fval)); emit(c, CAT_BYTECODE_OP_CONST, k, 0, t.line); next(c); break; @@ -353,7 +365,7 @@ static void primary(Compiler *c) c->lx.line = sl; emit(c, CAT_BYTECODE_OP_DUP, 0, 0, t.line); emit(c, CAT_BYTECODE_OP_CONST, - cat_bytecode_add_const(cur_fn(c), + add_const_owned(c, cat_types_int_new(posidx++)), 0, t.line); expr(c); @@ -363,7 +375,7 @@ static void primary(Compiler *c) } else { emit(c, CAT_BYTECODE_OP_DUP, 0, 0, t.line); emit(c, CAT_BYTECODE_OP_CONST, - cat_bytecode_add_const(cur_fn(c), + add_const_owned(c, cat_types_int_new(posidx++)), 0, t.line); expr(c); @@ -735,7 +747,7 @@ static void stat(Compiler *c) expr(c); /* step */ else emit(c, CAT_BYTECODE_OP_CONST, - cat_bytecode_add_const(cur_fn(c), cat_types_int_new(1)), + add_const_owned(c, cat_types_int_new(1)), 0, line); expect(c, CAT_LEXER_TOK_DO, "'do'"); slot = add_local(c, var); diff --git a/src/cat/types/function.c b/src/cat/types/function.c index 78e1e94..572b575 100644 --- a/src/cat/types/function.c +++ b/src/cat/types/function.c @@ -67,7 +67,13 @@ static int function_set_attribute(CAT_VALUE_Value *self, const char *name, static void function_destroy(CAT_VALUE_Value *self) { CAT_TYPES_FUNCTION_Closure *cl = (CAT_TYPES_FUNCTION_Closure *)self; + int i; + + for (i = 0; i < cl->nupvals; i++) + cat_value_release(cl->upvals[i]); free(cl->upvals); + if (cl->attrs != NULL) + cat_value_release(cl->attrs); /* proto is owned by its enclosing module/bytecode tree, unless this * closure built it at runtime (injected bytecode) */ if (cl->owns_proto && cl->proto != NULL) diff --git a/src/cat/types/generator.c b/src/cat/types/generator.c index a5377ed..9bfff29 100644 --- a/src/cat/types/generator.c +++ b/src/cat/types/generator.c @@ -31,11 +31,25 @@ static int generator_get_attribute(CAT_VALUE_Value *self, const char *name, return (cat_error_raise(err, "generator has no method '%s'", name), -1); } +static void generator_destroy(CAT_VALUE_Value *self) +{ + CAT_TYPES_GENERATOR_Generator *g = (CAT_TYPES_GENERATOR_Generator *)self; + int i; + + free(g->stack); + for (i = 0; i < g->nargs; i++) + cat_value_release(g->args[i]); + if (g->cl != NULL) + cat_value_release(&g->cl->base); + free(g); +} + static CAT_VALUE_Vtable g_generator_vtable = { .type_name = "generator", .kind = CAT_VALUE_KIND_GENERATOR, .next = { generator_next }, .get_attribute = { generator_get_attribute }, + .destroy = generator_destroy, }; CAT_VALUE_Vtable *cat_types_generator_vtable(void) @@ -77,6 +91,12 @@ CAT_VALUE_Value *cat_types_generator_new(CAT_VM_State *vm, for (i = 0; i < nargs && i < 256; i++) g->args[i] = args[i]; g->nargs = nargs; + /* Own references to the closure and the captured arguments: the + * generator keeps them alive across suspensions. */ + if (cl != NULL) + cat_value_retain(&cl->base); + for (i = 0; i < nargs && i < 256; i++) + cat_value_retain(g->args[i]); return &g->base; }