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.
This commit is contained in:
Minus committed 2026-11-17 22:15:10 +00:00
1 parent fddfa5dea2
commit ee5861a25c
4 files changed
+52 -12

No files matched your search

+8 -6
View File
@@ -55,6 +55,10 @@ int cat_bytecode_add_const(CAT_BYTECODE_Function *fn, CAT_VALUE_Value *v)
fn->consts = ncp; fn->consts = ncp;
fn->capconsts = nc; 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; fn->consts[fn->nconsts].value = v;
return fn->nconsts++; return fn->nconsts++;
} }
@@ -115,12 +119,10 @@ void cat_bytecode_function_free(CAT_BYTECODE_Function *fn)
if (fn == NULL) if (fn == NULL)
return; return;
/* Constant VALUES are intentionally not destroyed here. A chunk's /* Release the constant values this chunk owned (it took a reference
* constants may be shared (e.g. a chunk built from a catscript table * in cat_bytecode_add_const). The last holder frees them. */
* via bc_run borrows the table's values), and destroying them would for (i = 0; i < fn->nconsts; i++)
* double-free. The pool itself is freed; leaked values are bounded cat_value_release(fn->consts[i].value);
* by the number of compilations in a single run. */
(void)i;
for (i = 0; i < fn->nprotos; i++) for (i = 0; i < fn->nprotos; i++)
cat_bytecode_function_free(fn->protos[i]); cat_bytecode_function_free(fn->protos[i]);
free(fn->code); free(fn->code);
+18 -6
View File
@@ -112,7 +112,19 @@ static FuncCtx *ctx(Compiler *c)
static int add_string(Compiler *c, const char *s) 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) static CAT_BYTECODE_Function *cur_fn(Compiler *c)
@@ -284,13 +296,13 @@ static void primary(Compiler *c)
switch (t.kind) { switch (t.kind) {
case CAT_LEXER_TOK_INT: { 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); emit(c, CAT_BYTECODE_OP_CONST, k, 0, t.line);
next(c); next(c);
break; break;
} }
case CAT_LEXER_TOK_FLOAT: { 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); emit(c, CAT_BYTECODE_OP_CONST, k, 0, t.line);
next(c); next(c);
break; break;
@@ -353,7 +365,7 @@ static void primary(Compiler *c)
c->lx.line = sl; c->lx.line = sl;
emit(c, CAT_BYTECODE_OP_DUP, 0, 0, t.line); emit(c, CAT_BYTECODE_OP_DUP, 0, 0, t.line);
emit(c, CAT_BYTECODE_OP_CONST, emit(c, CAT_BYTECODE_OP_CONST,
cat_bytecode_add_const(cur_fn(c), add_const_owned(c,
cat_types_int_new(posidx++)), cat_types_int_new(posidx++)),
0, t.line); 0, t.line);
expr(c); expr(c);
@@ -363,7 +375,7 @@ static void primary(Compiler *c)
} else { } else {
emit(c, CAT_BYTECODE_OP_DUP, 0, 0, t.line); emit(c, CAT_BYTECODE_OP_DUP, 0, 0, t.line);
emit(c, CAT_BYTECODE_OP_CONST, emit(c, CAT_BYTECODE_OP_CONST,
cat_bytecode_add_const(cur_fn(c), add_const_owned(c,
cat_types_int_new(posidx++)), cat_types_int_new(posidx++)),
0, t.line); 0, t.line);
expr(c); expr(c);
@@ -735,7 +747,7 @@ static void stat(Compiler *c)
expr(c); /* step */ expr(c); /* step */
else else
emit(c, CAT_BYTECODE_OP_CONST, 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); 0, line);
expect(c, CAT_LEXER_TOK_DO, "'do'"); expect(c, CAT_LEXER_TOK_DO, "'do'");
slot = add_local(c, var); slot = add_local(c, var);
+6
View File
@@ -67,7 +67,13 @@ static int function_set_attribute(CAT_VALUE_Value *self, const char *name,
static void function_destroy(CAT_VALUE_Value *self) static void function_destroy(CAT_VALUE_Value *self)
{ {
CAT_TYPES_FUNCTION_Closure *cl = (CAT_TYPES_FUNCTION_Closure *)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); free(cl->upvals);
if (cl->attrs != NULL)
cat_value_release(cl->attrs);
/* proto is owned by its enclosing module/bytecode tree, unless this /* proto is owned by its enclosing module/bytecode tree, unless this
* closure built it at runtime (injected bytecode) */ * closure built it at runtime (injected bytecode) */
if (cl->owns_proto && cl->proto != NULL) if (cl->owns_proto && cl->proto != NULL)
+20
View File
@@ -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); 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 = { static CAT_VALUE_Vtable g_generator_vtable = {
.type_name = "generator", .type_name = "generator",
.kind = CAT_VALUE_KIND_GENERATOR, .kind = CAT_VALUE_KIND_GENERATOR,
.next = { generator_next }, .next = { generator_next },
.get_attribute = { generator_get_attribute }, .get_attribute = { generator_get_attribute },
.destroy = generator_destroy,
}; };
CAT_VALUE_Vtable *cat_types_generator_vtable(void) 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++) for (i = 0; i < nargs && i < 256; i++)
g->args[i] = args[i]; g->args[i] = args[i];
g->nargs = nargs; 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; return &g->base;
} }