diff --git a/Makefile b/Makefile index 8f668dc..1a22997 100644 --- a/Makefile +++ b/Makefile @@ -70,6 +70,7 @@ SRCS := src/tinyblocks.c \ src/tb_input.c \ src/tb_vars.c \ src/tb_result.c \ + src/tb_object.c \ src/tb_json_block.c \ src/tb_image.c \ src/tb_meta.c diff --git a/include/tinyblocks/tb_defs.h b/include/tinyblocks/tb_defs.h index d6fa55b..c6a4e4d 100644 --- a/include/tinyblocks/tb_defs.h +++ b/include/tinyblocks/tb_defs.h @@ -31,6 +31,8 @@ typedef struct TinyBlocks_JsonValue TinyBlocks_JsonValue; typedef struct TinyBlocks_Image TinyBlocks_Image; typedef struct TinyBlocks_Result TinyBlocks_Result; +#include "tb_object.h" + /* -- Kinds de bloco --- */ typedef enum TinyBlocks_BlockKind { 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_Image* image; /* ref-counted (retain/release) */ TinyBlocks_Result* result; /* ref-counted (retain/release) */ + TinyBlocks_Object* object; /* visao comum dos ref-counted */ } as; } TinyBlocks_Value; @@ -88,9 +91,9 @@ typedef enum TinyBlocks_VCmdKind { typedef struct { TinyBlocks_VCmdKind kind; float args[5]; } TinyBlocks_VCmd; struct TinyBlocks_VectorData { + TinyBlocks_Object obj; /* contagem de referencias (DEVE ser o 1o campo) */ TinyBlocks_VCmd* cmds; uint32_t count, capacity; - int32_t ref; }; diff --git a/include/tinyblocks/tb_object.h b/include/tinyblocks/tb_object.h new file mode 100644 index 0000000..3205504 --- /dev/null +++ b/include/tinyblocks/tb_object.h @@ -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 + +#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 */ diff --git a/include/tinyblocks/tb_result.h b/include/tinyblocks/tb_result.h index f23b2f6..bc9d193 100644 --- a/include/tinyblocks/tb_result.h +++ b/include/tinyblocks/tb_result.h @@ -19,9 +19,9 @@ typedef enum TinyBlocks_ResultTag { } TinyBlocks_ResultTag; struct TinyBlocks_Result { + TinyBlocks_Object obj; /* contagem de referencias (1o campo) */ TinyBlocks_ResultTag tag; /* qual variante */ TinyBlocks_Value payload; /* Ok: o valor; Erro: a mensagem/dado */ - int32_t ref; }; /* Criacao (ref = 1; o payload e clonado) */ diff --git a/include/tinyblocks/tinyblocks.h b/include/tinyblocks/tinyblocks.h index d71e8a3..2d10be9 100644 --- a/include/tinyblocks/tinyblocks.h +++ b/include/tinyblocks/tinyblocks.h @@ -27,6 +27,7 @@ #define TINYBLOCKS_H #include "tb_defs.h" +#include "tb_object.h" #include "tb_block.h" #include "tb_type_block.h" #include "tb_library_block.h" diff --git a/src/tb_image.c b/src/tb_image.c index c3f6ea3..2c75607 100644 --- a/src/tb_image.c +++ b/src/tb_image.c @@ -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; diff --git a/src/tb_json_block.c b/src/tb_json_block.c index a0ed6b3..b7e719f 100644 --- a/src/tb_json_block.c +++ b/src/tb_json_block.c @@ -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; diff --git a/src/tb_object.c b/src/tb_object.c new file mode 100644 index 0000000..68955f3 --- /dev/null +++ b/src/tb_object.c @@ -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; +} diff --git a/src/tb_result.c b/src/tb_result.c index e87c64a..9278afa 100644 --- a/src/tb_result.c +++ b/src/tb_result.c @@ -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(); diff --git a/src/tb_value.c b/src/tb_value.c index 5173c8d..7fc04de 100644 --- a/src/tb_value.c +++ b/src/tb_value.c @@ -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 ----------------------------------------------------- */ diff --git a/src/tb_vector_block.c b/src/tb_vector_block.c index bf3b912..547c045 100644 --- a/src/tb_vector_block.c +++ b/src/tb_vector_block.c @@ -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; } diff --git a/tests/value_test.c b/tests/value_test.c index b3ef7cd..c8b2a00 100644 --- a/tests/value_test.c +++ b/tests/value_test.c @@ -56,9 +56,9 @@ int main(void) { 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"); 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); - 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_Value ve2 = tb_value_result(er2); tb_result_release(er2);