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
@@ -70,6 +70,7 @@ SRCS := src/tinyblocks.c \
|
|||||||
src/tb_input.c \
|
src/tb_input.c \
|
||||||
src/tb_vars.c \
|
src/tb_vars.c \
|
||||||
src/tb_result.c \
|
src/tb_result.c \
|
||||||
|
src/tb_object.c \
|
||||||
src/tb_json_block.c \
|
src/tb_json_block.c \
|
||||||
src/tb_image.c \
|
src/tb_image.c \
|
||||||
src/tb_meta.c
|
src/tb_meta.c
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ typedef struct TinyBlocks_JsonValue TinyBlocks_JsonValue;
|
|||||||
typedef struct TinyBlocks_Image TinyBlocks_Image;
|
typedef struct TinyBlocks_Image TinyBlocks_Image;
|
||||||
typedef struct TinyBlocks_Result TinyBlocks_Result;
|
typedef struct TinyBlocks_Result TinyBlocks_Result;
|
||||||
|
|
||||||
|
#include "tb_object.h"
|
||||||
|
|
||||||
/* -- Kinds de bloco --- */
|
/* -- Kinds de bloco --- */
|
||||||
typedef enum TinyBlocks_BlockKind {
|
typedef enum TinyBlocks_BlockKind {
|
||||||
TB_KIND_BLOCK = 0, /* bloco genérico / definido pelo usuário */
|
TB_KIND_BLOCK = 0, /* bloco genérico / definido pelo usuário */
|
||||||
@@ -68,6 +70,7 @@ typedef struct TinyBlocks_Value {
|
|||||||
TinyBlocks_JsonValue* json; /* ref-counted (retain/release) */
|
TinyBlocks_JsonValue* json; /* ref-counted (retain/release) */
|
||||||
TinyBlocks_Image* image; /* ref-counted (retain/release) */
|
TinyBlocks_Image* image; /* ref-counted (retain/release) */
|
||||||
TinyBlocks_Result* result; /* ref-counted (retain/release) */
|
TinyBlocks_Result* result; /* ref-counted (retain/release) */
|
||||||
|
TinyBlocks_Object* object; /* visao comum dos ref-counted */
|
||||||
} as;
|
} as;
|
||||||
} TinyBlocks_Value;
|
} TinyBlocks_Value;
|
||||||
|
|
||||||
@@ -88,9 +91,9 @@ typedef enum TinyBlocks_VCmdKind {
|
|||||||
typedef struct { TinyBlocks_VCmdKind kind; float args[5]; } TinyBlocks_VCmd;
|
typedef struct { TinyBlocks_VCmdKind kind; float args[5]; } TinyBlocks_VCmd;
|
||||||
|
|
||||||
struct TinyBlocks_VectorData {
|
struct TinyBlocks_VectorData {
|
||||||
|
TinyBlocks_Object obj; /* contagem de referencias (DEVE ser o 1o campo) */
|
||||||
TinyBlocks_VCmd* cmds;
|
TinyBlocks_VCmd* cmds;
|
||||||
uint32_t count, capacity;
|
uint32_t count, capacity;
|
||||||
int32_t ref;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* TinyBlocks - tb_object.h
|
||||||
|
* Objeto com contagem de referencias: o "cabecalho" comum dos valores que
|
||||||
|
* vivem no heap e sao divididos entre blocos (desenho vetorial, JSON,
|
||||||
|
* imagem, resultado...). Cada tipo poe um TinyBlocks_Object como PRIMEIRO
|
||||||
|
* campo e da a sua classe (destroy); retain/release sao os mesmos para
|
||||||
|
* todos, e as classes de valor desses kinds tambem (tb_value_class.h).
|
||||||
|
*
|
||||||
|
* struct MeuTipo { TinyBlocks_Object obj; ...; };
|
||||||
|
* static void meu_destroy(TinyBlocks_Object* o) { ...; free(o); }
|
||||||
|
* static const TinyBlocks_ObjectClass s_cls = { "meu tipo", meu_destroy };
|
||||||
|
* tb_object_init(&m->obj, &s_cls); // ref = 1
|
||||||
|
*/
|
||||||
|
#ifndef TINYBLOCKS_TB_OBJECT_H
|
||||||
|
#define TINYBLOCKS_TB_OBJECT_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifndef TB_API
|
||||||
|
# ifdef _WIN32
|
||||||
|
# define TB_API __declspec(dllexport)
|
||||||
|
# else
|
||||||
|
# define TB_API __attribute__((visibility("default")))
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct TinyBlocks_Object TinyBlocks_Object;
|
||||||
|
typedef struct TinyBlocks_ObjectClass TinyBlocks_ObjectClass;
|
||||||
|
|
||||||
|
struct TinyBlocks_ObjectClass {
|
||||||
|
const char* name;
|
||||||
|
void (*destroy)(TinyBlocks_Object* o); /* ref chegou a 0: libera tudo */
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TinyBlocks_Object {
|
||||||
|
const TinyBlocks_ObjectClass* cls;
|
||||||
|
int32_t ref;
|
||||||
|
};
|
||||||
|
|
||||||
|
TB_API void tb_object_init(TinyBlocks_Object* o, const TinyBlocks_ObjectClass* cls);
|
||||||
|
TB_API void tb_object_retain(void* o); /* aceita NULL */
|
||||||
|
TB_API void tb_object_release(void* o); /* aceita NULL */
|
||||||
|
TB_API int32_t tb_object_refs(const void* o);
|
||||||
|
|
||||||
|
#endif /* TINYBLOCKS_TB_OBJECT_H */
|
||||||
@@ -19,9 +19,9 @@ typedef enum TinyBlocks_ResultTag {
|
|||||||
} TinyBlocks_ResultTag;
|
} TinyBlocks_ResultTag;
|
||||||
|
|
||||||
struct TinyBlocks_Result {
|
struct TinyBlocks_Result {
|
||||||
|
TinyBlocks_Object obj; /* contagem de referencias (1o campo) */
|
||||||
TinyBlocks_ResultTag tag; /* qual variante */
|
TinyBlocks_ResultTag tag; /* qual variante */
|
||||||
TinyBlocks_Value payload; /* Ok: o valor; Erro: a mensagem/dado */
|
TinyBlocks_Value payload; /* Ok: o valor; Erro: a mensagem/dado */
|
||||||
int32_t ref;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Criacao (ref = 1; o payload e clonado) */
|
/* Criacao (ref = 1; o payload e clonado) */
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
#define TINYBLOCKS_H
|
#define TINYBLOCKS_H
|
||||||
|
|
||||||
#include "tb_defs.h"
|
#include "tb_defs.h"
|
||||||
|
#include "tb_object.h"
|
||||||
#include "tb_block.h"
|
#include "tb_block.h"
|
||||||
#include "tb_type_block.h"
|
#include "tb_type_block.h"
|
||||||
#include "tb_library_block.h"
|
#include "tb_library_block.h"
|
||||||
|
|||||||
+12
-12
@@ -10,7 +10,7 @@
|
|||||||
#include "tinyblocks/tb_image.h"
|
#include "tinyblocks/tb_image.h"
|
||||||
|
|
||||||
struct TinyBlocks_Image {
|
struct TinyBlocks_Image {
|
||||||
int refs;
|
TinyBlocks_Object obj; /* contagem de referencias (1o campo) */
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
int channels; /* 1 = cinza, 3 = RGB, 4 = RGBA */
|
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 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) {
|
TinyBlocks_Image* tb_image_new(int w, int h, int channels) {
|
||||||
if (w <= 0 || h <= 0 || !valid_channels(channels)) return NULL;
|
if (w <= 0 || h <= 0 || !valid_channels(channels)) return NULL;
|
||||||
TinyBlocks_Image* img = (TinyBlocks_Image*)calloc(1, sizeof(*img));
|
TinyBlocks_Image* img = (TinyBlocks_Image*)calloc(1, sizeof(*img));
|
||||||
if (!img) return NULL;
|
if (!img) return NULL;
|
||||||
|
tb_object_init(&img->obj, &s_image_class);
|
||||||
size_t n = (size_t)w * (size_t)h * (size_t)channels;
|
size_t n = (size_t)w * (size_t)h * (size_t)channels;
|
||||||
img->pixels = (uint8_t*)calloc(n ? n : 1, 1);
|
img->pixels = (uint8_t*)calloc(n ? n : 1, 1);
|
||||||
if (!img->pixels) { free(img); return NULL; }
|
if (!img->pixels) { free(img); return NULL; }
|
||||||
img->refs = 1;
|
|
||||||
img->width = w;
|
img->width = w;
|
||||||
img->height = h;
|
img->height = h;
|
||||||
img->channels = channels;
|
img->channels = channels;
|
||||||
return img;
|
return img;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tb_image_retain(TinyBlocks_Image* img) {
|
void tb_image_retain(TinyBlocks_Image* img) { tb_object_retain(img); }
|
||||||
if (img) img->refs++;
|
void tb_image_release(TinyBlocks_Image* img) { tb_object_release(img); }
|
||||||
}
|
|
||||||
|
|
||||||
void tb_image_release(TinyBlocks_Image* img) {
|
|
||||||
if (!img) return;
|
|
||||||
if (--img->refs > 0) return;
|
|
||||||
free(img->pixels);
|
|
||||||
free(img);
|
|
||||||
}
|
|
||||||
|
|
||||||
TinyBlocks_Image* tb_image_clone(const TinyBlocks_Image* img) {
|
TinyBlocks_Image* tb_image_clone(const TinyBlocks_Image* img) {
|
||||||
if (!img) return NULL;
|
if (!img) return NULL;
|
||||||
|
|||||||
+8
-10
@@ -8,8 +8,8 @@
|
|||||||
#include "tinyblocks/tinyblocks.h"
|
#include "tinyblocks/tinyblocks.h"
|
||||||
|
|
||||||
struct TinyBlocks_JsonValue {
|
struct TinyBlocks_JsonValue {
|
||||||
|
TinyBlocks_Object obj; /* contagem de referencias (1o campo) */
|
||||||
TinyBlocks_JsonKind kind;
|
TinyBlocks_JsonKind kind;
|
||||||
int32_t ref;
|
|
||||||
union {
|
union {
|
||||||
bool boolean;
|
bool boolean;
|
||||||
double number;
|
double number;
|
||||||
@@ -29,11 +29,15 @@ struct TinyBlocks_JsonValue {
|
|||||||
/* ===
|
/* ===
|
||||||
* Construtores / ciclo de vida
|
* 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) {
|
static TinyBlocks_JsonValue* json_alloc(TinyBlocks_JsonKind kind) {
|
||||||
TinyBlocks_JsonValue* j = calloc(1, sizeof(TinyBlocks_JsonValue));
|
TinyBlocks_JsonValue* j = calloc(1, sizeof(TinyBlocks_JsonValue));
|
||||||
if (!j) return NULL;
|
if (!j) return NULL;
|
||||||
|
tb_object_init(&j->obj, &s_json_class);
|
||||||
j->kind = kind;
|
j->kind = kind;
|
||||||
j->ref = 1;
|
|
||||||
return j;
|
return j;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,14 +89,8 @@ static void json_destroy(TinyBlocks_JsonValue* j) {
|
|||||||
free(j);
|
free(j);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tb_json_retain(TinyBlocks_JsonValue* j) {
|
void tb_json_retain(TinyBlocks_JsonValue* j) { tb_object_retain(j); }
|
||||||
if (j) j->ref++;
|
void tb_json_release(TinyBlocks_JsonValue* j) { tb_object_release(j); }
|
||||||
}
|
|
||||||
|
|
||||||
void tb_json_release(TinyBlocks_JsonValue* j) {
|
|
||||||
if (!j) return;
|
|
||||||
if (--j->ref <= 0) json_destroy(j);
|
|
||||||
}
|
|
||||||
|
|
||||||
TinyBlocks_JsonValue* tb_json_clone(const TinyBlocks_JsonValue* j) {
|
TinyBlocks_JsonValue* tb_json_clone(const TinyBlocks_JsonValue* j) {
|
||||||
if (!j) return NULL;
|
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"
|
#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) {
|
static TinyBlocks_Result* result_new(TinyBlocks_ResultTag tag, TinyBlocks_Value v) {
|
||||||
TinyBlocks_Result* r = malloc(sizeof(*r));
|
TinyBlocks_Result* r = malloc(sizeof(*r));
|
||||||
if (!r) return NULL;
|
if (!r) return NULL;
|
||||||
|
tb_object_init(&r->obj, &s_result_class);
|
||||||
r->tag = tag;
|
r->tag = tag;
|
||||||
r->payload = tb_value_clone(v);
|
r->payload = tb_value_clone(v);
|
||||||
r->ref = 1;
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,13 +33,8 @@ TinyBlocks_Result* tb_result_err_text(const char* msg) {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tb_result_retain(TinyBlocks_Result* r) { if (r) r->ref++; }
|
void tb_result_retain(TinyBlocks_Result* r) { tb_object_retain(r); }
|
||||||
|
void tb_result_release(TinyBlocks_Result* r) { tb_object_release(r); }
|
||||||
void tb_result_release(TinyBlocks_Result* r) {
|
|
||||||
if (!r || --r->ref > 0) return;
|
|
||||||
tb_value_free(&r->payload);
|
|
||||||
free(r);
|
|
||||||
}
|
|
||||||
|
|
||||||
TinyBlocks_Value tb_value_result(TinyBlocks_Result* r) {
|
TinyBlocks_Value tb_value_result(TinyBlocks_Result* r) {
|
||||||
if (!r) return tb_value_none();
|
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 : "");
|
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 ---------------------------------------------------------- */
|
/* -- Classe: nada ---------------------------------------------------------- */
|
||||||
|
|
||||||
static size_t none_text(TinyBlocks_Value v, char* out, size_t cap) { (void)v; return put(out, cap, ""); }
|
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 ---------------------------------------------- */
|
/* -- 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)
|
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); }
|
{ 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_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 bool vec_eq(TinyBlocks_Value a, TinyBlocks_Value b) { return a.as.vector == b.as.vector; }
|
||||||
|
|
||||||
static const TinyBlocks_ValueClass s_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) --------------------------------------- */
|
/* -- 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 */
|
/* texto JSON sai sem aspas; o resto, como JSON */
|
||||||
static size_t json_text(TinyBlocks_Value v, char* out, size_t cap)
|
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 = {
|
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 -------------------------------------------------------- */
|
/* -- 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)
|
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)); }
|
{ 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_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 bool img_eq(TinyBlocks_Value a, TinyBlocks_Value b) { return a.as.image == b.as.image; }
|
||||||
|
|
||||||
static const TinyBlocks_ValueClass s_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) ----------------------------------------- */
|
/* -- 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)
|
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 = {
|
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 ----------------------------------------------------- */
|
/* -- Tabela e despacho ----------------------------------------------------- */
|
||||||
|
|||||||
+13
-11
@@ -7,25 +7,27 @@
|
|||||||
/* ===
|
/* ===
|
||||||
* TinyBlocks_VectorData - buffer de comandos ref-counted
|
* 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* tb_vector_data_create(void) {
|
||||||
TinyBlocks_VectorData* v = malloc(sizeof(TinyBlocks_VectorData));
|
TinyBlocks_VectorData* v = malloc(sizeof(TinyBlocks_VectorData));
|
||||||
if (!v) return NULL;
|
if (!v) return NULL;
|
||||||
|
tb_object_init(&v->obj, &s_vector_data_class);
|
||||||
v->capacity = 16;
|
v->capacity = 16;
|
||||||
v->count = 0;
|
v->count = 0;
|
||||||
v->ref = 1;
|
|
||||||
v->cmds = malloc(v->capacity * sizeof(TinyBlocks_VCmd));
|
v->cmds = malloc(v->capacity * sizeof(TinyBlocks_VCmd));
|
||||||
if (!v->cmds) { free(v); return NULL; }
|
if (!v->cmds) { free(v); return NULL; }
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tb_vector_data_retain(TinyBlocks_VectorData* v) {
|
void tb_vector_data_retain(TinyBlocks_VectorData* v) { tb_object_retain(v); }
|
||||||
if (v) v->ref++;
|
void tb_vector_data_release(TinyBlocks_VectorData* v) { tb_object_release(v); }
|
||||||
}
|
|
||||||
|
|
||||||
void tb_vector_data_release(TinyBlocks_VectorData* v) {
|
|
||||||
if (!v) return;
|
|
||||||
if (--v->ref <= 0) { free(v->cmds); free(v); }
|
|
||||||
}
|
|
||||||
|
|
||||||
TinyBlocks_VectorData* tb_vector_data_clone(const TinyBlocks_VectorData* src) {
|
TinyBlocks_VectorData* tb_vector_data_clone(const TinyBlocks_VectorData* src) {
|
||||||
if (!src) return NULL;
|
if (!src) return NULL;
|
||||||
@@ -82,7 +84,7 @@ TinyBlocks_Value tb_value_vector(TinyBlocks_VectorData* v) {
|
|||||||
TinyBlocks_Value val;
|
TinyBlocks_Value val;
|
||||||
val.kind = TB_VALUE_VECTOR;
|
val.kind = TB_VALUE_VECTOR;
|
||||||
val.as.vector = v;
|
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;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,6 +187,6 @@ void tb_vector_block_set_data(TinyBlocks_VectorBlock* b, TinyBlocks_VectorData*
|
|||||||
if (!b) return;
|
if (!b) return;
|
||||||
tb_vector_data_release(b->data);
|
tb_vector_data_release(b->data);
|
||||||
b->data = v;
|
b->data = v;
|
||||||
if (v) v->ref++;
|
tb_object_retain(v);
|
||||||
b->base.dirty = true;
|
b->base.dirty = true;
|
||||||
}
|
}
|
||||||
+2
-2
@@ -56,9 +56,9 @@ int main(void) {
|
|||||||
CHECK(tb_value_is_err(ve) && !tb_value_truthy(ve), "Erro é erro e falso");
|
CHECK(tb_value_is_err(ve) && !tb_value_truthy(ve), "Erro é erro e falso");
|
||||||
CHECK(text_is(ve, "Erro(não achei)") && !strcmp(tb_value_type_name(ve), "resultado (erro)"), "texto e nome do Erro");
|
CHECK(text_is(ve, "Erro(não achei)") && !strcmp(tb_value_type_name(ve), "resultado (erro)"), "texto e nome do Erro");
|
||||||
TinyBlocks_Value c = tb_value_clone(ve);
|
TinyBlocks_Value c = tb_value_clone(ve);
|
||||||
CHECK(c.as.result == ve.as.result && ve.as.result->ref == 2, "clone de resultado divide a referência");
|
CHECK(c.as.result == ve.as.result && tb_object_refs(ve.as.result) == 2, "clone de resultado divide a referência");
|
||||||
tb_value_free(&c);
|
tb_value_free(&c);
|
||||||
CHECK(ve.as.result->ref == 1, "free solta a referência");
|
CHECK(tb_object_refs(ve.as.result) == 1, "free solta a referência");
|
||||||
TinyBlocks_Result* er2 = tb_result_err_text("não achei");
|
TinyBlocks_Result* er2 = tb_result_err_text("não achei");
|
||||||
TinyBlocks_Value ve2 = tb_value_result(er2);
|
TinyBlocks_Value ve2 = tb_value_result(er2);
|
||||||
tb_result_release(er2);
|
tb_result_release(er2);
|
||||||
|
|||||||
Reference in new issue
Block a user