#define _POSIX_C_SOURCE 200809L #include #include #include #include #include "tinyblocks/tinyblocks.h" /* -- Alocador padrão --- */ static void* default_alloc(size_t size, void* ud) { (void)ud; return malloc(size); } static void default_free (void* ptr, void* ud) { (void)ud; free(ptr); } /* -- Inicialização --- */ bool tinyblocks_init(TinyBlocks** out_rt, const TinyBlocks_InitOptions* opts) { if (!out_rt) return false; TinyBlocks_AllocFn alloc_fn = opts && opts->alloc ? opts->alloc : default_alloc; TinyBlocks_FreeFn free_fn = opts && opts->free_fn ? opts->free_fn : default_free; void* alloc_ud = opts ? opts->alloc_userdata : NULL; TinyBlocks* rt = alloc_fn(sizeof(TinyBlocks), alloc_ud); if (!rt) return false; memset(rt, 0, sizeof(TinyBlocks)); rt->alloc = alloc_fn; rt->free = free_fn; rt->alloc_userdata = alloc_ud; rt->next_id = 1; uint32_t bcap = (opts && opts->initial_block_cap) ? opts->initial_block_cap : 64; uint32_t tcap = (opts && opts->initial_type_cap) ? opts->initial_type_cap : 32; uint32_t lcap = (opts && opts->initial_library_cap) ? opts->initial_library_cap : 16; rt->blocks = alloc_fn(bcap * sizeof(TinyBlocks_Block*), alloc_ud); rt->types = alloc_fn(tcap * sizeof(TinyBlocks_TypeBlock*), alloc_ud); rt->libraries = alloc_fn(lcap * sizeof(TinyBlocks_LibraryBlock*), alloc_ud); rt->dl_handles= alloc_fn(lcap * sizeof(void*), alloc_ud); if (!rt->blocks || !rt->types || !rt->libraries || !rt->dl_handles) { if (rt->blocks) free_fn(rt->blocks, alloc_ud); if (rt->types) free_fn(rt->types, alloc_ud); if (rt->libraries) free_fn(rt->libraries, alloc_ud); if (rt->dl_handles)free_fn(rt->dl_handles,alloc_ud); free_fn(rt, alloc_ud); return false; } rt->block_capacity = bcap; rt->type_capacity = tcap; rt->library_capacity = lcap; rt->dl_handle_capacity = lcap; *out_rt = rt; return true; } /* -- Desinicialização --- */ void tinyblocks_deinit(TinyBlocks** rt_ptr) { if (!rt_ptr || !*rt_ptr) return; TinyBlocks* rt = *rt_ptr; /* Fase 1: destruir instâncias (KB_KIND_BLOCK) primeiro. Elas referenciam TypeBlocks via ->type; devem ser liberadas antes das libraries que contêm esses TypeBlocks. */ { uint32_t i = rt->block_count; while (i > 0) { TinyBlocks_Block* b = rt->blocks[i - 1]; if (b->kind == TB_KIND_BLOCK) { tb_block_destroy(b, rt); /* remove de rt->blocks e libera */ i = rt->block_count; /* reinicia: o array mudou de tamanho */ } else { --i; } } } /* Fase 2: descarregar libraries (invoca on_unload e destrói TypeBlocks). */ while (rt->library_count > 0) { TinyBlocks_LibraryBlock* lib = rt->libraries[rt->library_count - 1]; if (lib->on_unload) lib->on_unload(lib, rt); tb_library_block_destroy(lib, rt); } /* Fase 3: TypeBlocks e primitivos órfãos (não pertencentes a nenhuma library). */ while (rt->block_count > 0) { TinyBlocks_Block* b = rt->blocks[rt->block_count - 1]; tb_block_destroy(b, rt); } #ifdef __linux__ /* Fecha dl handles (apenas no Linux por enquanto) */ for (uint32_t i = 0; i < rt->dl_handle_count; ++i) { extern int dlclose(void*); dlclose(rt->dl_handles[i]); } #endif TinyBlocks_FreeFn free_fn = rt->free; void* alloc_ud = rt->alloc_userdata; free_fn(rt->blocks, alloc_ud); free_fn(rt->types, alloc_ud); free_fn(rt->libraries, alloc_ud); free_fn(rt->dl_handles, alloc_ud); free_fn(rt, alloc_ud); *rt_ptr = NULL; } /* -- Alocação --- */ void* tb_alloc(TinyBlocks* rt, size_t size) { void* p = rt->alloc(size, rt->alloc_userdata); if (p) memset(p, 0, size); return p; } void tb_free(TinyBlocks* rt, void* ptr) { if (ptr) rt->free(ptr, rt->alloc_userdata); } char* tb_strdup(TinyBlocks* rt, const char* str) { if (!str) return NULL; size_t n = strlen(str) + 1; char* p = rt->alloc(n, rt->alloc_userdata); if (p) memcpy(p, str, n); return p; } /* -- Geração de ID --- */ uint64_t tb_rt_next_id(TinyBlocks* rt) { return rt->next_id++; } /* -- Registro de blocos --- */ static bool grow_array(TinyBlocks* rt, void*** arr, uint32_t* cap, uint32_t count) { if (count < *cap) return true; uint32_t new_cap = (*cap) * 2; void** new_arr = rt->alloc(new_cap * sizeof(void*), rt->alloc_userdata); if (!new_arr) return false; memcpy(new_arr, *arr, count * sizeof(void*)); rt->free(*arr, rt->alloc_userdata); *arr = new_arr; *cap = new_cap; return true; } bool tb_rt_register_block(TinyBlocks* rt, TinyBlocks_Block* block) { if (!grow_array(rt, (void***)&rt->blocks, &rt->block_capacity, rt->block_count)) return false; rt->blocks[rt->block_count++] = block; return true; } void tb_rt_unregister_block(TinyBlocks* rt, TinyBlocks_Block* block) { for (uint32_t i = 0; i < rt->block_count; ++i) { if (rt->blocks[i] == block) { rt->blocks[i] = rt->blocks[--rt->block_count]; return; } } } bool tb_rt_register_type(TinyBlocks* rt, TinyBlocks_TypeBlock* type) { if (!grow_array(rt, (void***)&rt->types, &rt->type_capacity, rt->type_count)) return false; rt->types[rt->type_count++] = type; return true; } void tb_rt_unregister_type(TinyBlocks* rt, TinyBlocks_TypeBlock* type) { for (uint32_t i = 0; i < rt->type_count; ++i) { if (rt->types[i] == type) { rt->types[i] = rt->types[--rt->type_count]; return; } } } bool tb_rt_register_library(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { if (!grow_array(rt, (void***)&rt->libraries, &rt->library_capacity, rt->library_count)) return false; rt->libraries[rt->library_count++] = lib; return true; } void tb_rt_unregister_library(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { for (uint32_t i = 0; i < rt->library_count; ++i) { if (rt->libraries[i] == lib) { rt->libraries[i] = rt->libraries[--rt->library_count]; return; } } } /* -- Busca --- */ TinyBlocks_Block* tb_rt_find_block(TinyBlocks* rt, uint64_t id) { for (uint32_t i = 0; i < rt->block_count; ++i) if (rt->blocks[i]->id == id) return rt->blocks[i]; return NULL; } TinyBlocks_TypeBlock* tb_rt_find_type(TinyBlocks* rt, const char* name) { for (uint32_t i = 0; i < rt->type_count; ++i) if (rt->types[i]->type_name && strcmp(rt->types[i]->type_name, name) == 0) return rt->types[i]; return NULL; } TinyBlocks_LibraryBlock* tb_rt_find_library(TinyBlocks* rt, const char* name) { for (uint32_t i = 0; i < rt->library_count; ++i) if (rt->libraries[i]->base.name && strcmp(rt->libraries[i]->base.name, name) == 0) return rt->libraries[i]; return NULL; } /* -- Enumeracao publica (esconde o layout de TinyBlocks) --- */ uint32_t tb_rt_type_count(const TinyBlocks* rt) { return rt ? rt->type_count : 0; } TinyBlocks_TypeBlock* tb_rt_type_at(const TinyBlocks* rt, uint32_t i) { return (rt && i < rt->type_count) ? rt->types[i] : NULL; } uint32_t tb_rt_library_count(const TinyBlocks* rt) { return rt ? rt->library_count : 0; } TinyBlocks_LibraryBlock* tb_rt_library_at(const TinyBlocks* rt, uint32_t i) { return (rt && i < rt->library_count) ? rt->libraries[i] : NULL; } /* -- Erros --- */ const char* tinyblocks_last_error(TinyBlocks* rt) { return rt->error; } void tinyblocks_clear_error(TinyBlocks* rt) { rt->error[0] = '\0'; } const char* tinyblocks_caught_error(TinyBlocks* rt) { return rt->caught; } void tinyblocks_set_caught_error(TinyBlocks* rt, const char* msg) { snprintf(rt->caught, sizeof(rt->caught), "%s", msg ? msg : ""); } void tinyblocks_set_error(TinyBlocks* rt, const char* fmt, ...) { va_list ap; va_start(ap, fmt); vsnprintf(rt->error, sizeof(rt->error), fmt, ap); va_end(ap); }