Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support structs with flexible array members? #29

Open
intelfx opened this issue Dec 24, 2023 · 0 comments
Open

Support structs with flexible array members? #29

intelfx opened this issue Dec 24, 2023 · 0 comments

Comments

@intelfx
Copy link

intelfx commented Dec 24, 2023

I'd like to extend libcsptr (specifically, the shared_ptr/unique_ptr macros) to handle C99 structs with flexible array members.

I. e.

struct Foo
{
    size_t size;
    char data[];
}

struct Foo *foop = shared_flex(struct Foo, ...);

Easy enough on the surface (add a member + length parameters and calculate the allocation size with a few offsetof and sizeof calls) and so far I came up with this implementation:

#define smart_flex(Kind, Type, Member, Length, ...)                         \
    ({                                                                      \
        struct s_tmp {                                                      \
            CSPTR_SENTINEL_DEC                                              \
            __typeof__(Type) value;                                         \
            f_destructor dtor;                                              \
            struct {                                                        \
                const void *ptr;                                            \
                size_t size;                                                \
            } meta;                                                         \
        } args = {                                                          \
            CSPTR_SENTINEL                                                  \
            __VA_ARGS__                                                     \
        };                                                                  \
        const size_t alloc_size =                                           \
            offsetof (Type, Member)                                         \
            + sizeof (((Type *)0)->Member[0]) * (Length);                   \
        void *var = smalloc(alloc_size, 0, Kind, ARGS_);                    \
        if (var != NULL)                                                    \
            memcpy(var, &args.value, sizeof (Type));                        \
        var;                                                                \
    })

However, including a struct with a flexible array member as a non-last member of another struct is a (now-deprecated) gcc extension.

Any ideas on how to do this properly and preserve the ergonomics of accepting an optional destructor + metadata at the end of the smart_flex() call?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant