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

+12 -12
View File
@@ -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;