refcount: containers own their values (table, list)

Wiring the reference count into the two value containers:

- cat_types_table_set retains the new value and releases the old one;
  table_destroy releases every stored value before freeing its own keys.
- cat_types_list_push / list_set_attribute retain; list_destroy releases.
- ffi library/function constructors set refcnt = 1 (they are heap-owned);
  selector singletons are immortal like empty/true/false.
- file / in-memory stream constructors set refcnt = 1.
- the stack-allocated closure in cat_vm_run is marked immortal so a
  stray release can never free stack storage.

Globals are a table, so they inherit the ownership rule for free.
Suite still 130 green.
This commit is contained in:
Minus committed 2026-11-17 22:14:03 +00:00
1 parent 22b8de3040
commit fddfa5dea2
6 files changed
+28 -6

No files matched your search

+3
View File
@@ -331,6 +331,7 @@ CAT_VALUE_Value *cat_types_ffi_load(const char *path)
return cat_types_empty();
}
l->base.vtable = &g_library_vtable;
l->base.refcnt = 1;
l->handle = h;
l->path = strdup(path);
return &l->base;
@@ -353,6 +354,7 @@ CAT_VALUE_Value *cat_types_ffi_bind(CAT_VALUE_Value *lib, const char *name,
if (f == NULL)
return cat_types_empty();
f->base.vtable = &g_function_vtable;
f->base.refcnt = 1;
f->sym = sym;
f->name = strdup(name);
f->sig = sig ? strdup(sig) : NULL;
@@ -374,6 +376,7 @@ CAT_VALUE_Value *cat_types_ffi_bind_addr(unsigned long long addr,
if (f == NULL)
return cat_types_empty();
f->base.vtable = &g_function_vtable;
f->base.refcnt = 1;
f->sym = (void *)(size_t)addr;
f->name = strdup(name ? name : "?");
f->sig = sig ? strdup(sig) : NULL;
+3
View File
@@ -297,6 +297,7 @@ CAT_VALUE_Value *cat_types_file_open(const char *path, const char *mode)
return cat_types_empty();
}
f->af.base.vtable = &g_file_vtable;
f->af.base.refcnt = 1;
f->af.mode = m;
f->af.closed = 0;
f->af.is_bytes = 0;
@@ -312,6 +313,7 @@ CAT_VALUE_Value *cat_types_file_stringio(const char *initial)
if (m == NULL)
return cat_types_empty();
m->af.base.vtable = &g_memfile_vtable;
m->af.base.refcnt = 1;
m->af.mode = CAT_TYPES_FILE_MODE_READ | CAT_TYPES_FILE_MODE_WRITE;
m->af.is_bytes = 1; /* in-memory */
m->cap = 128;
@@ -342,6 +344,7 @@ CAT_VALUE_Value *cat_types_file_bytesio(void)
if (m == NULL)
return cat_types_empty();
m->af.base.vtable = &g_memfile_vtable;
m->af.base.refcnt = 1;
m->af.mode = CAT_TYPES_FILE_MODE_READ | CAT_TYPES_FILE_MODE_WRITE;
m->cap = 128;
m->buf = malloc((size_t)m->cap);
+6
View File
@@ -57,6 +57,8 @@ static int list_set_attribute(CAT_VALUE_Value *self, const char *name,
if (idx < 0 || idx >= l->n)
return (cat_error_raise(err,
"list index %ld out of range", idx), -1);
cat_value_retain(value);
cat_value_release(l->items[idx]);
l->items[idx] = value;
return 0;
}
@@ -66,7 +68,10 @@ static int list_set_attribute(CAT_VALUE_Value *self, const char *name,
static void list_destroy(CAT_VALUE_Value *self)
{
CAT_TYPES_LIST_List *l = (CAT_TYPES_LIST_List *)self;
int i;
for (i = 0; i < l->n; i++)
cat_value_release(l->items[i]);
free(l->items);
free(l);
}
@@ -115,6 +120,7 @@ int cat_types_list_push(CAT_VALUE_Value *list, CAT_VALUE_Value *v)
l->cap = nc;
}
l->items[l->n++] = v;
cat_value_retain(v);
return 0;
}
+4 -1
View File
@@ -102,8 +102,11 @@ CAT_VALUE_Value *cat_types_selector_lookup(const char *name)
return NULL;
/* accept both `_dot_` and `.` spellings of a selector */
for (i = 0; i < (int)(sizeof(g_sel) / sizeof(g_sel[0])); i++) {
if (g_sel[i].base.vtable == NULL)
if (g_sel[i].base.vtable == NULL) {
g_sel[i].base.vtable = &g_selector_vtable;
/* static singleton: immortal, never freed */
g_sel[i].base.refcnt = -1;
}
}
strncpy(key, name, sizeof(key) - 1);
key[sizeof(key) - 1] = '\0';
+9 -5
View File
@@ -73,12 +73,13 @@ static void table_destroy(CAT_VALUE_Value *self)
{
CAT_TYPES_TABLE_Table *t = (CAT_TYPES_TABLE_Table *)self;
int i;
/* Values are NOT destroyed: the same value object may be referenced
* from several tables (globals, constants, locals), so a recursive
* destroy would double-free. We only release the table's own keys
* and storage. */
for (i = 0; i < t->npairs; i++)
/* Release the values we own, then our own keys and storage. Values
* are shared, so we drop exactly one reference each; the last holder
* frees them. */
for (i = 0; i < t->npairs; i++) {
cat_value_release(t->pairs[i].value);
free(t->pairs[i].key);
}
free(t->pairs);
free(t);
}
@@ -125,6 +126,8 @@ int cat_types_table_set(CAT_VALUE_Value *tbl, const char *key,
(void)err;
if (idx >= 0) {
cat_value_retain(value);
cat_value_release(t->pairs[idx].value);
t->pairs[idx].value = value;
return 0;
}
@@ -138,6 +141,7 @@ int cat_types_table_set(CAT_VALUE_Value *tbl, const char *key,
}
t->pairs[t->npairs].key = strdup(key);
t->pairs[t->npairs].value = value;
cat_value_retain(value);
t->npairs++;
return 0;
}
+3
View File
@@ -587,6 +587,9 @@ int cat_vm_run(CAT_VM_State *vm, CAT_BYTECODE_Function *fn, CAT_VALUE_Value **ou
memset(&cl, 0, sizeof(cl));
cl.base.vtable = cat_types_function_vtable();
cl.proto = fn;
/* This closure lives on the C stack: mark it immortal so a stray
* release can never try to free it. */
cat_value_make_immortal(&cl.base);
return cat_vm_exec_closure(vm, &cl, NULL, 0, out);
}