Skip to content

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(&reg, 42, some_vtable);
    if (err)
        return;

    void* found = NULL;
    err = dtvtable_get(&reg, 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_numbers Parallel array of model identifiers where zero indicates an empty slot.

void** vtables Parallel array of vtable pointers aligned with the model number array.

int max_vtables Number 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* reg Registry to search.

int32_t model_number Model identifier to resolve.

void** vtable Output 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* reg Registry to modify.

int32_t model_number Model identifier to publish.

void* vtable Vtable pointer to associate with the model.

Return: dterr_t* NULL on success or an error describing failure.