dtvtable
Lightweight registry for model-indexed vtable dispatch
This group of functions provides a small registry for mapping 32-bit model numbers to vtable pointers for dispatch. It is intended for systems that select behavior by model identifier at runtime without dynamic allocation. It is designed to keep ownership external and make registration explicit. The implementation favors simple linear scans over additional indexing structures.
Mini-guide
- Initialize the registry with parallel arrays sized by a fixed maximum capacity to define storage up front.
- Publish a mapping by calling the registration function, which inserts into the first empty slot.
- Re-registering an existing model with the same pointer succeeds without modifying the registry.
- Avoid concurrent access or add external synchronization because operations are not thread-safe.
- Treat all returned vtable pointers as borrowed and keep them valid for the required lifetime.
Example
#include <dtcore/dtvtable.h>
static int32_t models[8] = {0};
static void* vtables[8] = {0};
void example(void)
{
dtvtable_registry_t reg = {
.model_numbers = models,
.vtables = vtables,
.max_vtables = 8,
};
void* some_vtable = (void*)0x1234;
dterr_t* err = dtvtable_set(®, 42, some_vtable);
if (err)
return;
void* found = NULL;
err = dtvtable_get(®, 42, &found);
if (err)
return;
}
Data structures
dtvtable_registry_t
Holds the backing storage and capacity for the model-to-vtable registry.
Members:
int32_t* model_numbersParallel array of model identifiers where zero indicates an empty slot.
void** vtablesParallel array of vtable pointers aligned with the model number array.
int max_vtablesNumber of usable entries defining the registry capacity.
Macros
DTVTABLE_DISPATCH
DTVTABLE_DISPATCH(CLASS, NAME, ARGS, PARAMS, RET)
Defines a forwarding function that resolves a vtable by model number and dispatches a method call.
Functions
dtvtable_get
Looks up a vtable pointer associated with a model number in the registry.
Params:
dtvtable_registry_t* regRegistry to search.
int32_t model_numberModel identifier to resolve.
void** vtableOutput location for the found vtable pointer.
Return: dterr_t* NULL on success or an error describing failure.
dtvtable_set
Registers a model number to vtable pointer mapping in the registry.
Params:
dtvtable_registry_t* regRegistry to modify.
int32_t model_numberModel identifier to publish.
void* vtableVtable pointer to associate with the model.
Return: dterr_t* NULL on success or an error describing failure.