TinyBlocks_Object: contagem de referências comum aos valores no heap

- tb_object.h: cabeçalho {classe, ref} com tb_object_retain/release; cada
  tipo dá a sua classe (destroy).
- Desenho vetorial, JSON, imagem e Result usam o cabeçalho como 1o campo;
  os retain/release de cada um viram atalhos para o comum.
- As classes de valor desses kinds dividem o mesmo copy/release, pela
  visão TinyBlocks_Value.as.object.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-22 19:00:41 -03:00
1 parent 5ffc1741c2
commit 4f1f83e9c2
12 files changed
+138 -57

No files matched your search

+8 -10
View File
@@ -8,8 +8,8 @@
#include "tinyblocks/tinyblocks.h"
struct TinyBlocks_JsonValue {
TinyBlocks_Object obj; /* contagem de referencias (1o campo) */
TinyBlocks_JsonKind kind;
int32_t ref;
union {
bool boolean;
double number;
@@ -29,11 +29,15 @@ struct TinyBlocks_JsonValue {
/* ===
* Construtores / ciclo de vida
* === */
static void json_destroy(TinyBlocks_JsonValue* j);
static void json_object_destroy(TinyBlocks_Object* o) { json_destroy((TinyBlocks_JsonValue*)o); }
static const TinyBlocks_ObjectClass s_json_class = { "JSON", json_object_destroy };
static TinyBlocks_JsonValue* json_alloc(TinyBlocks_JsonKind kind) {
TinyBlocks_JsonValue* j = calloc(1, sizeof(TinyBlocks_JsonValue));
if (!j) return NULL;
tb_object_init(&j->obj, &s_json_class);
j->kind = kind;
j->ref = 1;
return j;
}
@@ -85,14 +89,8 @@ static void json_destroy(TinyBlocks_JsonValue* j) {
free(j);
}
void tb_json_retain(TinyBlocks_JsonValue* j) {
if (j) j->ref++;
}
void tb_json_release(TinyBlocks_JsonValue* j) {
if (!j) return;
if (--j->ref <= 0) json_destroy(j);
}
void tb_json_retain(TinyBlocks_JsonValue* j) { tb_object_retain(j); }
void tb_json_release(TinyBlocks_JsonValue* j) { tb_object_release(j); }
TinyBlocks_JsonValue* tb_json_clone(const TinyBlocks_JsonValue* j) {
if (!j) return NULL;