From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 47F6A606A322 for ; Wed, 2 Dec 2026 10:18:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=IXB2n6Q8 Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id A9C4740647; Wed, 2 Dec 2026 11:18:41 +0100 (CET) Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) by mails.dpdk.org (Postfix) with ESMTPS id 5D0A7402D5 for ; Wed, 2 Dec 2026 11:18:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796206720; x=1827742720; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OxU3MvXRIRBLCgk5RG1VfPmnFxQymCg4HoEYd5MGSRU=; b=IXB2n6Q81giWwgijGYXZZ6pM8NL8PhC+gLfCU2l0dhjvzVv+u4cr/26/ hCuWE55uURplSV+GRxMe4b5wl70umSpWvuGJ1da75XLlzi9Z5cQiplSnR pcVHk8M7T9rMnuTwql9ch+8xSd25TonH8KDPCuUeH+e0qHo2pK4rwtHXQ han5wIkdgGyPeSVY7GixaVHERGwtJff2fmv1tUh0biY42hacwjpWq8jK8 P9rNWcjfFX5/WPRNy/I5khULgKrPImC7LIF4ZPd0MJSVmMB9G8lCAqNqH oq+Kz10ANXh3sML1naVc0rv/kTkWGfgs3eFB0XEU/s4QfKRdd9nqQWXet w==; X-CSE-ConnectionGUID: qMAmSrwkS2S89JxXc0+cRg== X-CSE-MsgGUID: g5FjvwaSSzSqs8egOAkZlQ== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="5127303" X-IronPort-AV: E=Sophos;i="6.27,246,1787036400"; d="scan'208";a="5127303" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2026 02:18:37 -0800 X-CSE-ConnectionGUID: cOBH7JxATKyy64BkvWJQyQ== X-CSE-MsgGUID: LR2nX6FlTDGnzryKhVO3tQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,246,1787036400"; d="scan'208";a="13853511" Received: from silpixa00401385.ir.intel.com ([10.20.224.226]) by orviesa009.jf.intel.com with ESMTP; 02 Dec 2026 02:18:37 -0800 From: Bruce Richardson To: dev@dpdk.org Cc: Bruce Richardson Subject: [PATCH v3 1/6] eal: add macros for local variable-length structure buffers Date: Wed, 2 Dec 2026 10:18:28 +0000 Message-ID: <20261202101833.2882373-2-bruce.richardson@intel.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261202101833.2882373-1-bruce.richardson@intel.com> References: <20261116160932.1006354-1-bruce.richardson@intel.com> <20261202101833.2882373-1-bruce.richardson@intel.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Archive: List-Post: List-Help: List-Subscribe: , X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 --- v3: add extra static_asserts to macros: - no size values that are not compile-time constants - no trailing arrays greater than 4k after the variable-length struct --- app/test/test_common.c | 54 ++++++++++++++++++++++++++++++++++++ lib/eal/include/rte_common.h | 50 +++++++++++++++++++++++++++++++++ 2 files changed, 104 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..71ed975065 100644 --- a/lib/eal/include/rte_common.h +++ b/lib/eal/include/rte_common.h @@ -1114,6 +1114,56 @@ __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, + * and no more than 4k bytes larger than it. + */ +#define RTE_DEFINE_FLEX_BYTES(type, name, size) \ + static_assert(__rte_constant(size), "buffer size must be a compile-time constant"); \ + static_assert((size) >= sizeof(type), "buffer smaller than " RTE_STR(type)); \ + static_assert((size) - sizeof(type) <= 4096, "buffer too large"); \ + 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) \ + static_assert(__rte_constant(count), "element count must be a compile-time constant"); \ + 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