Skip to content

Commit 3fdc002

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 3fdc002

17 files changed

Lines changed: 835 additions & 154 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 new instance.
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() - Set fields to their defaults.
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/accessors.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ long libnvme_fabrics_config_get_tls_key_id(
236236
const struct libnvme_fabrics_config *p);
237237

238238
/**
239-
* libnvme_fabrics_config_set_tls_configured_key_id() - Set tls_configured_key_id.
239+
* libnvme_fabrics_config_set_tls_configured_key_id() - Setter.
240240
* @p: The &struct libnvme_fabrics_config instance to update.
241241
* @tls_configured_key_id: Value to assign to the tls_configured_key_id field.
242242
*/
@@ -245,7 +245,7 @@ void libnvme_fabrics_config_set_tls_configured_key_id(
245245
long tls_configured_key_id);
246246

247247
/**
248-
* libnvme_fabrics_config_get_tls_configured_key_id() - Get tls_configured_key_id.
248+
* libnvme_fabrics_config_get_tls_configured_key_id() - Getter.
249249
* @p: The &struct libnvme_fabrics_config instance to query.
250250
*
251251
* Return: The value of the tls_configured_key_id field.

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

0 commit comments

Comments
 (0)