Skip to content

generate-accessors: add lifecycle, defaults, and annotation redesign#3283

Open
martin-belanger wants to merge 1 commit intolinux-nvme:masterfrom
martin-belanger:generate-lifecycle
Open

generate-accessors: add lifecycle, defaults, and annotation redesign#3283
martin-belanger wants to merge 1 commit intolinux-nvme:masterfrom
martin-belanger:generate-lifecycle

Conversation

@martin-belanger
Copy link
Copy Markdown

@martin-belanger martin-belanger commented Apr 16, 2026

Extend the accessor generator with three new capabilities.

  1. Lifecycle (constructor + destructor): annotate a struct's opening brace with //!generate-lifecycle to generate foo_new() and foo_free(). foo_new() allocates a zeroed instance with calloc() and returns -EINVAL / -ENOMEM on error. foo_free() frees all owned char* and char** members then frees the struct. Passing NULL to foo_free() is safe: destructors that dereference members guard with if (!p) return; those with no members to dereference rely on free(NULL) being a no-op. //!lifecycle:none on a member excludes it from the destructor. const char* members are never freed (assumed externally owned).

  2. Defaults (init function): annotate individual members with //!default:VALUE to generate foo_init_defaults(), which assigns each annotated field its compile-time default. Any valid C expression is accepted as the value. When combined with //!generate-lifecycle, foo_new() calls foo_init_defaults() after allocation. foo_init_defaults() is also useful standalone to re-initialise a struct without reallocating.

  3. Annotation style: drop support for the /*!annotation*/ block-comment style. Annotations now use only the // line-comment style. The parser was redesigned accordingly: after //, each !keyword token is treated as a command, so multiple annotations may share one comment:

  struct foo { //!generate-accessors !generate-lifecycle

private.h and private-fabrics.h are updated throughout to use the new style.

The generated .c files now include <errno.h> for EINVAL/ENOMEM.

Apply these features to struct libnvmf_discovery_args, replacing the manually-written libnvmf_discovery_args_create() and libnvmf_discovery_args_free() with generated equivalents. The constructor is renamed _new (consistent with the generated naming convention).

@martin-belanger martin-belanger changed the title generate-accessors: add lifecycle and defaults generation generate-accessors: add lifecycle, defaults, and annotation redesign Apr 16, 2026
Extend the accessor generator with three new capabilities.

1. Lifecycle (constructor + destructor): annotate a struct's opening brace
with //!generate-lifecycle to generate foo_new() and foo_free().
foo_new() allocates a zeroed instance with calloc() and returns
-EINVAL / -ENOMEM on error. foo_free() frees all owned char* and char**
members then frees the struct. Passing NULL to foo_free() is safe:
destructors that dereference members guard with if (!p) return; those
with no members to dereference rely on free(NULL) being a no-op.
//!lifecycle:none on a member excludes it from the destructor. const
char* members are never freed (assumed externally owned).

2. Defaults (init function): annotate individual members with
//!default:VALUE to generate foo_init_defaults(), which assigns each
annotated field its compile-time default. Any valid C expression is
accepted as the value. When combined with //!generate-lifecycle,
foo_new() calls foo_init_defaults() after allocation. foo_init_defaults()
is also useful standalone to re-initialise a struct without reallocating.

3. Annotation style: drop support for the /*!annotation*/ block-comment
style. Annotations now use only the // line-comment style. The parser
was redesigned accordingly: after //, each !keyword token is treated as
a command, so multiple annotations may share one comment:

  struct foo { //!generate-accessors !generate-lifecycle

private.h and private-fabrics.h are updated throughout to use the new
style.

The generated .c files now include <errno.h> for EINVAL/ENOMEM.

Apply these features to struct libnvmf_discovery_args, replacing the
manually-written libnvmf_discovery_args_create() and
libnvmf_discovery_args_free() with generated equivalents. The
constructor is renamed _new (consistent with the generated naming
convention).

Signed-off-by: Martin Belanger <martin.belanger@dell.com>
Assisted-by: Claude Sonnet 4.6 <noreply@anthropic.com>
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

Successfully merging this pull request may close these issues.

1 participant