Skip to content

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

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

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 <[email protected]>
Assisted-by: Claude Sonnet 4.6 <[email protected]>
};

struct libnvme_path { /*!generate-accessors*/
struct libnvme_path { //!generate-accessors
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move these changes into a separate patch? Thanks!

And while at it an extra space wouldn't hurt // !generate-accessors

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. will do.

Comment thread libnvme/src/libnvmf.ld
libnvmf_discovery;
libnvmf_discovery_args_create;
libnvmf_discovery_args_free;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like an unnecessary empty line

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

}

ret = libnvmf_discovery_args_create(&args);
ret = libnvmf_discovery_args_new(&args);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we look into making this consistent through out the code base? Having two types of wording is not good IMO.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per your text message. I will have a separate patch to change all _create to _new.

@igaw
Copy link
Copy Markdown
Collaborator

igaw commented Apr 20, 2026

The general feature looks fine as far as I can tell. The Python code is slowly turning into a bit of a monster, but I don’t care too much about that. There may come a point where we need to start from scratch. Apparently, that’s just how things go. So the important part is getting the requirements right.

We’ve already added quite a few features beyond the initial simple design, and I think it really helps avoid writing annoying boilerplate code. In short, I’m fine with this, just please try to split the changes into smaller patches.

@martin-belanger
Copy link
Copy Markdown
Author

Closing this PR. Will submit a fresh one with incremental commits instead of everything in a single commit.

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.

2 participants