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
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
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) */
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in new issue
Block a user