From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: Bruce Richardson <bruce.richardson@intel.com>
Subject: [PATCH 27.03 1/6] eal: add macros for local variable-length structure buffers
Date: Mon, 16 Nov 2026 16:09:27 +0000 [thread overview]
Message-ID: <20261116160932.1006354-2-bruce.richardson@intel.com> (raw)
In-Reply-To: <20261116160932.1006354-1-bruce.richardson@intel.com>
Many drivers allocate small control-path messages, which consist of a
header structure followed by a variable-length array, using malloc and
free, which adds error handling and the risk of leaks on error paths.
Add RTE_DEFINE_FLEX and RTE_DEFINE_FLEX_BYTES macros to define such
buffers on the stack instead, (similar to equivalent macros in Linux
kernel). The buffer is a union of a byte array and the structure type,
giving correct alignment and type-safe access via a pointer, with the
byte array first so that zero-initialization covers the whole buffer.
Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
app/test/test_common.c | 54 ++++++++++++++++++++++++++++++++++++
lib/eal/include/rte_common.h | 46 ++++++++++++++++++++++++++++++
2 files changed, 100 insertions(+)
diff --git a/app/test/test_common.c b/app/test/test_common.c
index 290bf763ee..148d31aee5 100644
--- a/app/test/test_common.c
+++ b/app/test/test_common.c
@@ -796,6 +796,59 @@ test_fls(void)
return 0;
}
+struct flex_test_elem {
+ uint64_t a;
+ uint16_t b;
+};
+
+struct flex_test_hdr {
+ uint16_t num;
+ struct flex_test_elem elems[];
+};
+
+struct flex_test_hdr_one {
+ uint8_t num;
+ struct {
+ uint8_t len;
+ struct flex_test_elem elems[1];
+ } list;
+};
+
+static int
+test_define_flex(void)
+{
+ RTE_DEFINE_FLEX(struct flex_test_hdr, hdr, elems, 4);
+ RTE_DEFINE_FLEX(struct flex_test_hdr_one, hdr_one, list.elems, 3);
+ RTE_DEFINE_FLEX_BYTES(struct flex_test_hdr, hdr_bytes, 100);
+
+ RTE_TEST_ASSERT(sizeof(hdr_u) >= sizeof(*hdr) + 4 * sizeof(hdr->elems[0]),
+ "RTE_DEFINE_FLEX size, flexible array");
+ RTE_TEST_ASSERT(sizeof(hdr_one_u) >= sizeof(*hdr_one) + 3 * sizeof(hdr_one->list.elems[0]),
+ "RTE_DEFINE_FLEX size, nested one-element array");
+ RTE_TEST_ASSERT(sizeof(hdr_bytes_u) >= 100, "RTE_DEFINE_FLEX_BYTES size");
+
+ RTE_TEST_ASSERT((uintptr_t)hdr % alignof(struct flex_test_hdr) == 0,
+ "RTE_DEFINE_FLEX alignment");
+ RTE_TEST_ASSERT((uintptr_t)hdr_one % alignof(struct flex_test_hdr_one) == 0,
+ "RTE_DEFINE_FLEX alignment, nested array");
+ RTE_TEST_ASSERT((uintptr_t)hdr_bytes % alignof(struct flex_test_hdr) == 0,
+ "RTE_DEFINE_FLEX_BYTES alignment");
+
+ for (size_t i = 0; i < sizeof(hdr_u.raw); i++)
+ RTE_TEST_ASSERT(hdr_u.raw[i] == 0, "RTE_DEFINE_FLEX not zeroed");
+ for (size_t i = 0; i < sizeof(hdr_one_u.raw); i++)
+ RTE_TEST_ASSERT(hdr_one_u.raw[i] == 0, "RTE_DEFINE_FLEX not zeroed, nested array");
+ for (size_t i = 0; i < sizeof(hdr_bytes_u.raw); i++)
+ RTE_TEST_ASSERT(hdr_bytes_u.raw[i] == 0, "RTE_DEFINE_FLEX_BYTES not zeroed");
+
+ hdr->num = 4;
+ for (uint16_t i = 0; i < hdr->num; i++)
+ hdr->elems[i].b = i;
+ RTE_TEST_ASSERT_EQUAL(hdr->elems[3].b, 3, "RTE_DEFINE_FLEX element access");
+
+ return 0;
+}
+
static struct unit_test_suite common_test_suite = {
.suite_name = "common autotest",
.setup = NULL,
@@ -805,6 +858,7 @@ static struct unit_test_suite common_test_suite = {
TEST_CASE(test_ptr_align_edge_cases),
TEST_CASE(test_align),
TEST_CASE(test_macros),
+ TEST_CASE(test_define_flex),
TEST_CASE(test_misc),
TEST_CASE(test_bsf),
TEST_CASE(test_log2),
diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
index fed373f412..4f55b776f8 100644
--- a/lib/eal/include/rte_common.h
+++ b/lib/eal/include/rte_common.h
@@ -1114,6 +1114,52 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
*/
#define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
+/**
+ * Define a zero-initialized, local buffer of a given size in bytes,
+ * accessed via a pointer to a structure type.
+ *
+ * This is intended for structures ending in a variable-length array,
+ * e.g. messages with a header followed by a list of elements,
+ * allowing them to be placed on the stack rather than dynamically allocated.
+ * The buffer is suitably aligned for the structure type.
+ *
+ * @param type
+ * The type of the structure at the start of the buffer.
+ * @param name
+ * The name of the pointer variable to define, of type "type * const".
+ * @param size
+ * The size of the buffer in bytes, a compile-time constant
+ * which must be no smaller than the size of the structure type.
+ */
+#define RTE_DEFINE_FLEX_BYTES(type, name, size) \
+ static_assert((size) >= sizeof(type), "buffer smaller than " RTE_STR(type)); \
+ union { \
+ uint8_t raw[size]; /* first member, so "{0}" zeroes the whole buffer */ \
+ type obj; \
+ } name ## _u = {0}; \
+ type * const name = &name ## _u.obj
+
+/**
+ * Define a zero-initialized, local buffer for a structure ending in an array,
+ * with space for a given number of array elements beyond the size of the structure.
+ *
+ * The buffer is sized as "sizeof(type) + count * sizeof(element)", so for structures
+ * whose array is declared with a single element, e.g. "elem[1]",
+ * "count" elements are available in addition to that one.
+ *
+ * @param type
+ * The type of the structure.
+ * @param name
+ * The name of the pointer variable to define, of type "type * const".
+ * @param member
+ * The array member at the end of the structure.
+ * @param count
+ * The number of extra array elements to allow for, a compile-time constant.
+ */
+#define RTE_DEFINE_FLEX(type, name, member, count) \
+ RTE_DEFINE_FLEX_BYTES(type, name, \
+ sizeof(type) + RTE_SIZEOF_FIELD(type, member[0]) * (count))
+
#define _RTE_STR(x) #x
/** Take a macro value and get a string version of it */
#define RTE_STR(x) _RTE_STR(x)
--
2.55.0
next prev parent reply other threads:[~2026-11-16 16:09 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 16:09 [PATCH 27.03 0/6] eliminate unnecessary dynamic allocation Bruce Richardson
2026-11-16 16:09 ` Bruce Richardson [this message]
2026-11-16 16:09 ` [PATCH 27.03 2/6] net/iavf: use stack storage for virtchnl messages Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 3/6] net/ice: use stack storage for function-local buffers Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 4/6] net/idpf: use stack storage for virtchnl messages Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 5/6] net/cpfl: use stack storage for function-local buffers Bruce Richardson
2026-11-16 16:09 ` [PATCH 27.03 6/6] net/i40e: " Bruce Richardson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261116160932.1006354-2-bruce.richardson@intel.com \
--to=bruce.richardson@intel.com \
--cc=dev@dpdk.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox