Skip to content

Provide macro to declare pre-inited tables #24

Description

@RicBent

Hi, thanks for your work! Really appreciate the clean api and efficiency it brings to the (vers)table.

In my use case, I have many tables as global state. As they are static and spread across multiple TUs, all of them previously needed a <module>_init() function that just calls vt_init for all contained tables.

To remove this need, I simply added this macro:

#define VT_INITED {.metadata = (uint16_t*)&vt_empty_placeholder_metadatum}

For better compatibility with pre-C99 and pre-C++20 it could also be defined as:

#define VT_INITED {0, 0x0000000000000000ull, NULL, (uint16_t*)&vt_empty_placeholder_metadatum}
#define VT_INITED_WITH_CTX(ctx) {0, 0x0000000000000000ull, NULL, (uint16_t*)&vt_empty_placeholder_metadatum, ctx}

This allows simply defining global tables like this without the need for any runtime initializer:

static my_table_t s_table = VT_INITED;

One thing I am unsure about with this design: Destructors still need to be called manually. Not avoidable, but this may be missed by users as you can easily forget about that if you do not ever call vt_init manually.

I still think this would be pretty useful addition. What do you think?

Best regards

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions