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:
1 parent
5ffc1741c2
commit
4f1f83e9c2
12 files changed
+138
-57
No files matched your search
+12
-12
@@ -10,7 +10,7 @@
|
||||
#include "tinyblocks/tb_image.h"
|
||||
|
||||
struct TinyBlocks_Image {
|
||||
int refs;
|
||||
TinyBlocks_Object obj; /* contagem de referencias (1o campo) */
|
||||
int width;
|
||||
int height;
|
||||
int channels; /* 1 = cinza, 3 = RGB, 4 = RGBA */
|
||||
@@ -19,30 +19,30 @@ struct TinyBlocks_Image {
|
||||
|
||||
static int valid_channels(int c) { return c == 1 || c == 3 || c == 4; }
|
||||
|
||||
static void image_destroy(TinyBlocks_Object* o) {
|
||||
TinyBlocks_Image* img = (TinyBlocks_Image*)o;
|
||||
free(img->pixels);
|
||||
free(img);
|
||||
}
|
||||
|
||||
static const TinyBlocks_ObjectClass s_image_class = { "imagem", image_destroy };
|
||||
|
||||
TinyBlocks_Image* tb_image_new(int w, int h, int channels) {
|
||||
if (w <= 0 || h <= 0 || !valid_channels(channels)) return NULL;
|
||||
TinyBlocks_Image* img = (TinyBlocks_Image*)calloc(1, sizeof(*img));
|
||||
if (!img) return NULL;
|
||||
tb_object_init(&img->obj, &s_image_class);
|
||||
size_t n = (size_t)w * (size_t)h * (size_t)channels;
|
||||
img->pixels = (uint8_t*)calloc(n ? n : 1, 1);
|
||||
if (!img->pixels) { free(img); return NULL; }
|
||||
img->refs = 1;
|
||||
img->width = w;
|
||||
img->height = h;
|
||||
img->channels = channels;
|
||||
return img;
|
||||
}
|
||||
|
||||
void tb_image_retain(TinyBlocks_Image* img) {
|
||||
if (img) img->refs++;
|
||||
}
|
||||
|
||||
void tb_image_release(TinyBlocks_Image* img) {
|
||||
if (!img) return;
|
||||
if (--img->refs > 0) return;
|
||||
free(img->pixels);
|
||||
free(img);
|
||||
}
|
||||
void tb_image_retain(TinyBlocks_Image* img) { tb_object_retain(img); }
|
||||
void tb_image_release(TinyBlocks_Image* img) { tb_object_release(img); }
|
||||
|
||||
TinyBlocks_Image* tb_image_clone(const TinyBlocks_Image* img) {
|
||||
if (!img) return NULL;
|
||||
|
||||
+8
-10
@@ -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;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* TinyBlocks - tb_object.c
|
||||
* Contagem de referencias comum (ver tb_object.h).
|
||||
*/
|
||||
#include "tinyblocks/tinyblocks.h"
|
||||
|
||||
void tb_object_init(TinyBlocks_Object* o, const TinyBlocks_ObjectClass* cls)
|
||||
{
|
||||
o->cls = cls;
|
||||
o->ref = 1;
|
||||
}
|
||||
|
||||
void tb_object_retain(void* p)
|
||||
{
|
||||
TinyBlocks_Object* o = p;
|
||||
if (o) o->ref++;
|
||||
}
|
||||
|
||||
void tb_object_release(void* p)
|
||||
{
|
||||
TinyBlocks_Object* o = p;
|
||||
if (!o || --o->ref > 0) return;
|
||||
if (o->cls && o->cls->destroy) o->cls->destroy(o);
|
||||
}
|
||||
|
||||
int32_t tb_object_refs(const void* p)
|
||||
{
|
||||
const TinyBlocks_Object* o = p;
|
||||
return o ? o->ref : 0;
|
||||
}
|
||||
+11
-8
@@ -6,12 +6,20 @@
|
||||
|
||||
#include "tinyblocks/tinyblocks.h"
|
||||
|
||||
static void result_destroy(TinyBlocks_Object* o) {
|
||||
TinyBlocks_Result* r = (TinyBlocks_Result*)o;
|
||||
tb_value_free(&r->payload);
|
||||
free(r);
|
||||
}
|
||||
|
||||
static const TinyBlocks_ObjectClass s_result_class = { "resultado", result_destroy };
|
||||
|
||||
static TinyBlocks_Result* result_new(TinyBlocks_ResultTag tag, TinyBlocks_Value v) {
|
||||
TinyBlocks_Result* r = malloc(sizeof(*r));
|
||||
if (!r) return NULL;
|
||||
tb_object_init(&r->obj, &s_result_class);
|
||||
r->tag = tag;
|
||||
r->payload = tb_value_clone(v);
|
||||
r->ref = 1;
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -25,13 +33,8 @@ TinyBlocks_Result* tb_result_err_text(const char* msg) {
|
||||
return r;
|
||||
}
|
||||
|
||||
void tb_result_retain(TinyBlocks_Result* r) { if (r) r->ref++; }
|
||||
|
||||
void tb_result_release(TinyBlocks_Result* r) {
|
||||
if (!r || --r->ref > 0) return;
|
||||
tb_value_free(&r->payload);
|
||||
free(r);
|
||||
}
|
||||
void tb_result_retain(TinyBlocks_Result* r) { tb_object_retain(r); }
|
||||
void tb_result_release(TinyBlocks_Result* r) { tb_object_release(r); }
|
||||
|
||||
TinyBlocks_Value tb_value_result(TinyBlocks_Result* r) {
|
||||
if (!r) return tb_value_none();
|
||||
|
||||
+10
-12
@@ -89,6 +89,12 @@ static size_t put(char* out, size_t cap, const char* s)
|
||||
return (size_t)snprintf(out, cap, "%s", s ? s : "");
|
||||
}
|
||||
|
||||
/* -- Objetos com contagem de referencias (vetor, JSON, imagem, resultado):
|
||||
copy/release iguais para todos, pelo cabecalho TinyBlocks_Object -- */
|
||||
|
||||
static TinyBlocks_Value obj_copy(TinyBlocks_Value v) { tb_object_retain(v.as.object); return v; }
|
||||
static void obj_release(TinyBlocks_Value* v) { tb_object_release(v->as.object); v->as.object = NULL; }
|
||||
|
||||
/* -- Classe: nada ---------------------------------------------------------- */
|
||||
|
||||
static size_t none_text(TinyBlocks_Value v, char* out, size_t cap) { (void)v; return put(out, cap, ""); }
|
||||
@@ -159,21 +165,17 @@ static const TinyBlocks_ValueClass s_block = {
|
||||
|
||||
/* -- Classe: desenho vetorial ---------------------------------------------- */
|
||||
|
||||
static TinyBlocks_Value vec_copy(TinyBlocks_Value v) { if (v.as.vector) tb_vector_data_retain(v.as.vector); return v; }
|
||||
static void vec_release(TinyBlocks_Value* v) { tb_vector_data_release(v->as.vector); v->as.vector = NULL; }
|
||||
static size_t vec_text(TinyBlocks_Value v, char* out, size_t cap)
|
||||
{ return (size_t)snprintf(out, cap, "[desenho com %u comandos]", v.as.vector ? v.as.vector->count : 0u); }
|
||||
static bool vec_truthy(TinyBlocks_Value v) { return v.as.vector && v.as.vector->count > 0; }
|
||||
static bool vec_eq(TinyBlocks_Value a, TinyBlocks_Value b) { return a.as.vector == b.as.vector; }
|
||||
|
||||
static const TinyBlocks_ValueClass s_vector = {
|
||||
TB_VALUE_VECTOR, "desenho vetorial", vec_copy, vec_release, vec_text, NULL, vec_truthy, vec_eq, NULL,
|
||||
TB_VALUE_VECTOR, "desenho vetorial", obj_copy, obj_release, vec_text, NULL, vec_truthy, vec_eq, NULL,
|
||||
};
|
||||
|
||||
/* -- Classe: JSON (listas e objetos) --------------------------------------- */
|
||||
|
||||
static TinyBlocks_Value json_copy(TinyBlocks_Value v) { if (v.as.json) tb_json_retain(v.as.json); return v; }
|
||||
static void json_release(TinyBlocks_Value* v) { tb_json_release(v->as.json); v->as.json = NULL; }
|
||||
|
||||
/* texto JSON sai sem aspas; o resto, como JSON */
|
||||
static size_t json_text(TinyBlocks_Value v, char* out, size_t cap)
|
||||
@@ -233,26 +235,22 @@ static const char* json_type_name(TinyBlocks_Value v)
|
||||
}
|
||||
|
||||
static const TinyBlocks_ValueClass s_json = {
|
||||
TB_VALUE_JSON, "JSON", json_copy, json_release, json_text, json_num, json_truthy, json_eq, json_type_name,
|
||||
TB_VALUE_JSON, "JSON", obj_copy, obj_release, json_text, json_num, json_truthy, json_eq, json_type_name,
|
||||
};
|
||||
|
||||
/* -- Classe: imagem -------------------------------------------------------- */
|
||||
|
||||
static TinyBlocks_Value img_copy(TinyBlocks_Value v) { if (v.as.image) tb_image_retain(v.as.image); return v; }
|
||||
static void img_release(TinyBlocks_Value* v) { tb_image_release(v->as.image); v->as.image = NULL; }
|
||||
static size_t img_text(TinyBlocks_Value v, char* out, size_t cap)
|
||||
{ return (size_t)snprintf(out, cap, "[imagem %dx%d]", tb_image_width(v.as.image), tb_image_height(v.as.image)); }
|
||||
static bool img_truthy(TinyBlocks_Value v) { return v.as.image != NULL; }
|
||||
static bool img_eq(TinyBlocks_Value a, TinyBlocks_Value b) { return a.as.image == b.as.image; }
|
||||
|
||||
static const TinyBlocks_ValueClass s_image = {
|
||||
TB_VALUE_IMAGE, "imagem", img_copy, img_release, img_text, NULL, img_truthy, img_eq, NULL,
|
||||
TB_VALUE_IMAGE, "imagem", obj_copy, obj_release, img_text, NULL, img_truthy, img_eq, NULL,
|
||||
};
|
||||
|
||||
/* -- Classe: resultado (Ok | Erro) ----------------------------------------- */
|
||||
|
||||
static TinyBlocks_Value res_copy(TinyBlocks_Value v) { tb_result_retain(v.as.result); return v; }
|
||||
static void res_release(TinyBlocks_Value* v) { tb_result_release(v->as.result); v->as.result = NULL; }
|
||||
|
||||
static size_t res_text(TinyBlocks_Value v, char* out, size_t cap)
|
||||
{
|
||||
@@ -278,7 +276,7 @@ static const char* res_type_name(TinyBlocks_Value v)
|
||||
}
|
||||
|
||||
static const TinyBlocks_ValueClass s_result = {
|
||||
TB_VALUE_RESULT, "resultado", res_copy, res_release, res_text, NULL, res_truthy, res_eq, res_type_name,
|
||||
TB_VALUE_RESULT, "resultado", obj_copy, obj_release, res_text, NULL, res_truthy, res_eq, res_type_name,
|
||||
};
|
||||
|
||||
/* -- Tabela e despacho ----------------------------------------------------- */
|
||||
|
||||
+13
-11
@@ -7,25 +7,27 @@
|
||||
/* ===
|
||||
* TinyBlocks_VectorData - buffer de comandos ref-counted
|
||||
* === */
|
||||
static void vector_data_destroy(TinyBlocks_Object* o) {
|
||||
TinyBlocks_VectorData* v = (TinyBlocks_VectorData*)o;
|
||||
free(v->cmds);
|
||||
free(v);
|
||||
}
|
||||
|
||||
static const TinyBlocks_ObjectClass s_vector_data_class = { "desenho vetorial", vector_data_destroy };
|
||||
|
||||
TinyBlocks_VectorData* tb_vector_data_create(void) {
|
||||
TinyBlocks_VectorData* v = malloc(sizeof(TinyBlocks_VectorData));
|
||||
if (!v) return NULL;
|
||||
tb_object_init(&v->obj, &s_vector_data_class);
|
||||
v->capacity = 16;
|
||||
v->count = 0;
|
||||
v->ref = 1;
|
||||
v->cmds = malloc(v->capacity * sizeof(TinyBlocks_VCmd));
|
||||
if (!v->cmds) { free(v); return NULL; }
|
||||
return v;
|
||||
}
|
||||
|
||||
void tb_vector_data_retain(TinyBlocks_VectorData* v) {
|
||||
if (v) v->ref++;
|
||||
}
|
||||
|
||||
void tb_vector_data_release(TinyBlocks_VectorData* v) {
|
||||
if (!v) return;
|
||||
if (--v->ref <= 0) { free(v->cmds); free(v); }
|
||||
}
|
||||
void tb_vector_data_retain(TinyBlocks_VectorData* v) { tb_object_retain(v); }
|
||||
void tb_vector_data_release(TinyBlocks_VectorData* v) { tb_object_release(v); }
|
||||
|
||||
TinyBlocks_VectorData* tb_vector_data_clone(const TinyBlocks_VectorData* src) {
|
||||
if (!src) return NULL;
|
||||
@@ -82,7 +84,7 @@ TinyBlocks_Value tb_value_vector(TinyBlocks_VectorData* v) {
|
||||
TinyBlocks_Value val;
|
||||
val.kind = TB_VALUE_VECTOR;
|
||||
val.as.vector = v;
|
||||
if (v) v->ref++; /* tb_value retém a referência */
|
||||
tb_object_retain(v); /* tb_value retém a referência */
|
||||
return val;
|
||||
}
|
||||
|
||||
@@ -185,6 +187,6 @@ void tb_vector_block_set_data(TinyBlocks_VectorBlock* b, TinyBlocks_VectorData*
|
||||
if (!b) return;
|
||||
tb_vector_data_release(b->data);
|
||||
b->data = v;
|
||||
if (v) v->ref++;
|
||||
tb_object_retain(v);
|
||||
b->base.dirty = true;
|
||||
}
|
||||
Reference in new issue
Block a user