Skip to content

Commit 8c4a0bf

Browse files
author
Martin Belanger
committed
generate-accessors: add lifecycle, defaults, and annotation redesign
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]>
1 parent c0631f3 commit 8c4a0bf

16 files changed

Lines changed: 812 additions & 148 deletions

libnvme/examples/discover-loop.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ int main()
9494
goto free_fctx;
9595
}
9696

97-
ret = libnvmf_discovery_args_create(&args);
97+
ret = libnvmf_discovery_args_new(&args);
9898
if (!ret) {
9999
libnvmf_discovery_args_set_max_retries(args, 4);
100100
ret = libnvmf_get_discovery_log(c, args, &log);

libnvme/libnvme/nvme.i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ struct libnvmf_context {};
829829
discover_err = 1;
830830
return NULL;
831831
}
832-
discover_err = libnvmf_discovery_args_create(&args);
832+
discover_err = libnvmf_discovery_args_new(&args);
833833
if (discover_err)
834834
return NULL;
835835
libnvmf_discovery_args_set_lsp(args, lsp);

libnvme/src/accessors-fabrics.ld

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010

1111
LIBNVMF_ACCESSORS_3 {
1212
global:
13+
libnvmf_discovery_args_free;
1314
libnvmf_discovery_args_get_lsp;
1415
libnvmf_discovery_args_get_max_retries;
16+
libnvmf_discovery_args_init_defaults;
17+
libnvmf_discovery_args_new;
1518
libnvmf_discovery_args_set_lsp;
1619
libnvmf_discovery_args_set_max_retries;
1720
libnvmf_uri_get_fragment;

libnvme/src/libnvmf.ld

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ LIBNVMF_3 {
2424
libnvmf_ctrl_get_fabrics_config;
2525
libnvmf_disconnect_ctrl;
2626
libnvmf_discovery;
27-
libnvmf_discovery_args_create;
28-
libnvmf_discovery_args_free;
27+
2928
libnvmf_discovery_config_file;
3029
libnvmf_discovery_config_json;
3130
libnvmf_discovery_nbft;

libnvme/src/nvme/accessors-fabrics.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* To update run: meson compile -C [BUILD-DIR] update-accessors
1818
* Or: make update-accessors
1919
*/
20+
#include <errno.h>
2021
#include <stdlib.h>
2122
#include <string.h>
2223
#include "accessors-fabrics.h"
@@ -28,6 +29,31 @@
2829
* Accessors for: struct libnvmf_discovery_args
2930
****************************************************************************/
3031

32+
__public int libnvmf_discovery_args_new(struct libnvmf_discovery_args **pp)
33+
{
34+
if (!pp)
35+
return -EINVAL;
36+
*pp = calloc(1, sizeof(struct libnvmf_discovery_args));
37+
if (!*pp)
38+
return -ENOMEM;
39+
libnvmf_discovery_args_init_defaults(*pp);
40+
return 0;
41+
}
42+
43+
__public void libnvmf_discovery_args_free(struct libnvmf_discovery_args *p)
44+
{
45+
free(p);
46+
}
47+
48+
__public void libnvmf_discovery_args_init_defaults(
49+
struct libnvmf_discovery_args *p)
50+
{
51+
if (!p)
52+
return;
53+
p->max_retries = 6;
54+
p->lsp = NVMF_LOG_DISC_LSP_NONE;
55+
}
56+
3157
__public void libnvmf_discovery_args_set_max_retries(
3258
struct libnvmf_discovery_args *p,
3359
int max_retries)

libnvme/src/nvme/accessors-fabrics.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,36 @@ struct libnvmf_uri;
3535
* Accessors for: struct libnvmf_discovery_args
3636
****************************************************************************/
3737

38+
/**
39+
* libnvmf_discovery_args_new() - Allocate and initialise a libnvmf_discovery_args object.
40+
* @pp: On success, *pp is set to the newly allocated object.
41+
*
42+
* Allocates a zeroed &struct libnvmf_discovery_args on the heap.
43+
* The caller must release it with libnvmf_discovery_args_free().
44+
*
45+
* Return: 0 on success, -EINVAL if @pp is NULL,
46+
* -ENOMEM if allocation fails.
47+
*/
48+
int libnvmf_discovery_args_new(struct libnvmf_discovery_args **pp);
49+
50+
/**
51+
* libnvmf_discovery_args_free() - Release a libnvmf_discovery_args object.
52+
* @p: Object previously returned by libnvmf_discovery_args_new().
53+
* A NULL pointer is silently ignored.
54+
*/
55+
void libnvmf_discovery_args_free(struct libnvmf_discovery_args *p);
56+
57+
/**
58+
* libnvmf_discovery_args_init_defaults() - Apply default values to a libnvmf_discovery_args instance.
59+
* @p: The &struct libnvmf_discovery_args instance to initialise.
60+
*
61+
* Sets each field that carries a default annotation to its
62+
* compile-time default value. Called automatically by
63+
* libnvmf_discovery_args_new() but may also be called directly to reset an
64+
* instance to its defaults without reallocating it.
65+
*/
66+
void libnvmf_discovery_args_init_defaults(struct libnvmf_discovery_args *p);
67+
3868
/**
3969
* libnvmf_discovery_args_set_max_retries() - Set max_retries.
4070
* @p: The &struct libnvmf_discovery_args instance to update.

libnvme/src/nvme/accessors.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* To update run: meson compile -C [BUILD-DIR] update-accessors
1818
* Or: make update-accessors
1919
*/
20+
#include <errno.h>
2021
#include <stdlib.h>
2122
#include <string.h>
2223
#include "accessors.h"

libnvme/src/nvme/fabrics.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,28 +1494,6 @@ static void sanitize_discovery_log_entry(struct libnvme_global_ctx *ctx,
14941494
}
14951495
}
14961496

1497-
__public int libnvmf_discovery_args_create(struct libnvmf_discovery_args **argsp)
1498-
{
1499-
struct libnvmf_discovery_args *args;
1500-
1501-
if (!argsp)
1502-
return -EINVAL;
1503-
1504-
args = calloc(1, sizeof(*args));
1505-
if (!args)
1506-
return -ENOMEM;
1507-
1508-
args->max_retries = 6;
1509-
args->lsp = NVMF_LOG_DISC_LSP_NONE;
1510-
1511-
*argsp = args;
1512-
return 0;
1513-
}
1514-
1515-
__public void libnvmf_discovery_args_free(struct libnvmf_discovery_args *args)
1516-
{
1517-
free(args);
1518-
}
15191497

15201498
__public int libnvmf_get_discovery_log(libnvme_ctrl_t ctrl,
15211499
const struct libnvmf_discovery_args *args,

libnvme/src/nvme/fabrics.h

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ int libnvmf_connect_ctrl(libnvme_ctrl_t c);
157157
/*
158158
* struct libnvmf_discovery_args - Opaque arguments for libnvmf_get_discovery_log()
159159
*
160-
* Allocate with libnvmf_discovery_args_create() and release with
160+
* Allocate with libnvmf_discovery_args_new() and release with
161161
* libnvmf_discovery_args_free(). Use the setter/getter accessors to configure
162162
* fields; do not access members directly.
163163
*/
@@ -168,23 +168,6 @@ struct libnvmf_discovery_args;
168168
*/
169169
struct libnvmf_uri;
170170

171-
/**
172-
* libnvmf_discovery_args_create() - Allocate a discovery args object
173-
* @argsp: On success, set to the newly allocated object
174-
*
175-
* Allocates and initialises a &struct libnvmf_discovery_args with sensible
176-
* defaults. The caller must release it with libnvmf_discovery_args_free().
177-
*
178-
* Return: 0 on success, or a negative error code on failure.
179-
*/
180-
int libnvmf_discovery_args_create(struct libnvmf_discovery_args **argsp);
181-
182-
/**
183-
* libnvmf_discovery_args_free() - Release a discovery args object
184-
* @args: Object previously returned by libnvmf_discovery_args_create()
185-
*/
186-
void libnvmf_discovery_args_free(struct libnvmf_discovery_args *args);
187-
188171
/**
189172
* libnvmf_get_discovery_log() - Fetch the NVMe-oF discovery log page
190173
* @ctrl: Discovery controller

libnvme/src/nvme/private-fabrics.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
/* SPDX-License-Identifier: LGPL-2.1-or-later */
22
/*
3+
* NVMe-oF private struct definitions.
4+
*
35
* This file is part of libnvme.
46
* Copyright (c) 2026, Dell Technologies Inc. or its subsidiaries.
57
*
68
* Authors: Martin Belanger <[email protected]>
9+
*
10+
* Structs in this file are NVMe-oF-specific (fabrics layer). They are kept
11+
* separate from private.h so that PCIe-only builds can exclude this entire
12+
* file and its generated accessors (accessors-fabrics.{h,c}) along with the
13+
* rest of the fabrics layer.
714
*/
15+
816
#pragma once
917

1018
#include <nvme/fabrics.h>
@@ -69,18 +77,9 @@ struct libnvmf_context {
6977
};
7078

7179

72-
/**
73-
* NVMe-oF private struct definitions.
74-
*
75-
* Structs in this file are NVMe-oF-specific (fabrics layer). They are kept
76-
* separate from private.h so that PCIe-only builds can exclude this entire
77-
* file and its generated accessors (accessors-fabrics.{h,c}) along with the
78-
* rest of the fabrics layer.
79-
*/
80-
81-
struct libnvmf_discovery_args { /*!generate-accessors*/
82-
int max_retries;
83-
__u8 lsp;
80+
struct libnvmf_discovery_args { //!generate-accessors !generate-lifecycle
81+
int max_retries; //!default:6
82+
__u8 lsp; //!default:NVMF_LOG_DISC_LSP_NONE
8483
};
8584

8685
/**

0 commit comments

Comments
 (0)