From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id D662445BE6; Fri, 1 Nov 2024 00:48:21 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 6420E40264; Fri, 1 Nov 2024 00:48:21 +0100 (CET) Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by mails.dpdk.org (Postfix) with ESMTP id 5677440041 for ; Fri, 1 Nov 2024 00:48:20 +0100 (CET) Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-71e5a62031aso1148363b3a.1 for ; Thu, 31 Oct 2024 16:48:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1730418499; x=1731023299; darn=dpdk.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=E4gk741eLjkeC6pL9DhvETN0kAME8Ule3jjeMj86SP8=; b=br2r+3MZiyXl1fHvxGwxwJ3C+lTaBricCNHo1dIVoRUNVJZyqRDTkL9Syj3rFN0uYt 8EDhH//SL/rLO+sUggHZi02UrxHaqfSRGo8UEJWSjrcU1hVu/gKWNI/XaQTFnf3Z5Glw tffceq4qBuRqRLRatELS5/QDEqZgqvABSJfMmkNNI+y3xJ3AD4T5B9U0PhHfXcVfPpyL WaMhWGbinbBZCQ4CuHfmILdmb9WJ+8W4jp3bx6tVHbjlk45ew7b/Vt9vBvorxPBh3nKn WFfZQRLTU0kDZ0/s6ml3x0KDGLsr1a/DknFPZZDvsCOfIaDpbLM/oQgCs85UrJVWDGjj 9iOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730418499; x=1731023299; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=E4gk741eLjkeC6pL9DhvETN0kAME8Ule3jjeMj86SP8=; b=pmzqjbzIOiiACl1OvGDocw+h/UAcyEbfGUGtsAp4WCAePP9PHSjeceVbmyvW9r/Us7 N18g5FlaZSEUWl4pb0ABwY+Mbrgijj02hTdWnUgbPOU1THrF0G4vc9CpQMgxfdQbMrwc ETVP6nUvFqgnQbJmOgxXQjrjuyDEy52XURJZwgRqN/jPiB7QP6Kcqk1T4Uo0c9xNt+TU t5VXA8wtI/kJRT24g/z+BYK+VcFeLv01VyJ+aJ3AGyUs+wexHXzxw4WNaNWer/AGSFng 13hhdal4sWowwwZ5cuuJf02sZcdUzgdnG/FnY1+kQSIRgVWm1ravbPPXplE6VV71mrp7 vveg== X-Gm-Message-State: AOJu0YynXzYr6pD0j4ZxlGdMbooGgXYCNKl3l37uRHrG7asmGi0VszTs 40B5rq7Qfsj93jfavR32Ro2Xc6tp3rHTd0PrgchPU5RXQSOZgnWq83qCfEhom3sAN34IuHvKcT+ E X-Google-Smtp-Source: AGHT+IEq6TEsDu/XAOtQbZffSThtUJ4IcoR5h7GBJx7rk5sfrz2phRHTwT7CRM2gMw7NZC6NFetHSQ== X-Received: by 2002:a05:6a20:d795:b0:1d7:84f:f6f9 with SMTP id adf61e73a8af0-1dba5620c8cmr1771419637.48.1730418498995; Thu, 31 Oct 2024 16:48:18 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7ee45a002d4sm1574191a12.70.2024.10.31.16.48.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Oct 2024 16:48:18 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [RFC] kvargs: add free attribute annotation Date: Thu, 31 Oct 2024 16:47:49 -0700 Message-ID: <20241031234809.213517-1-stephen@networkplumber.org> X-Mailer: git-send-email 2.45.2 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-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Pointers created from parsing kvargs must be freed via rte_kvargs_free. Use compiler attributes to catch misuse. Signed-off-by: Stephen Hemminger --- lib/kvargs/rte_kvargs.c | 10 ++++++++-- lib/kvargs/rte_kvargs.h | 40 ++++++++++++++++++++++++---------------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/lib/kvargs/rte_kvargs.c b/lib/kvargs/rte_kvargs.c index 1d355516bc..cdfb7318f1 100644 --- a/lib/kvargs/rte_kvargs.c +++ b/lib/kvargs/rte_kvargs.c @@ -249,6 +249,13 @@ rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key) return rte_kvargs_get_with_value(kvlist, key, NULL); } +/* Helper function to get a pointer with kvargs_free attribute */ +static __rte_dealloc_kvargs_free struct rte_kvargs * +rte_kvargs_alloc(void) +{ + return calloc(1, sizeof(struct rte_kvargs)); +} + /* * Parse the arguments "key=value,key=value,..." string and return * an allocated structure that contains a key/value list. Also @@ -259,10 +266,9 @@ rte_kvargs_parse(const char *args, const char * const valid_keys[]) { struct rte_kvargs *kvlist; - kvlist = malloc(sizeof(*kvlist)); + kvlist = rte_kvargs_alloc(); if (kvlist == NULL) return NULL; - memset(kvlist, 0, sizeof(*kvlist)); if (rte_kvargs_tokenize(kvlist, args) < 0) { rte_kvargs_free(kvlist); diff --git a/lib/kvargs/rte_kvargs.h b/lib/kvargs/rte_kvargs.h index 73fa1e621b..11a78a109b 100644 --- a/lib/kvargs/rte_kvargs.h +++ b/lib/kvargs/rte_kvargs.h @@ -21,6 +21,8 @@ * ethernet devices at initialization for arguments parsing. */ +#include + #ifdef __cplusplus extern "C" { #endif @@ -62,6 +64,22 @@ struct rte_kvargs { struct rte_kvargs_pair pairs[RTE_KVARGS_MAX]; /**< list of key/values */ }; +/** + * Functions that expect return value to be freed with rte_kvargs_free() + */ +#define __rte_dealloc_kvargs_free __rte_dealloc(rte_kvargs_free, 1) + +/** + * Free a rte_kvargs structure + * + * Free a rte_kvargs structure previously allocated with + * rte_kvargs_parse(). + * + * @param kvlist + * The rte_kvargs structure. No error if NULL. + */ +void rte_kvargs_free(struct rte_kvargs *kvlist); + /** * Allocate a rte_kvargs and store key/value associations from a string * @@ -80,8 +98,9 @@ struct rte_kvargs { * - A pointer to an allocated rte_kvargs structure on success * - NULL on error */ -struct rte_kvargs *rte_kvargs_parse(const char *args, - const char *const valid_keys[]); +struct rte_kvargs * +rte_kvargs_parse(const char *args, const char *const valid_keys[]) + __rte_malloc __rte_dealloc_kvargs_free; /** * Allocate a rte_kvargs and store key/value associations from a string. @@ -108,20 +127,9 @@ struct rte_kvargs *rte_kvargs_parse(const char *args, * - A pointer to an allocated rte_kvargs structure on success * - NULL on error */ -struct rte_kvargs *rte_kvargs_parse_delim(const char *args, - const char *const valid_keys[], - const char *valid_ends); - -/** - * Free a rte_kvargs structure - * - * Free a rte_kvargs structure previously allocated with - * rte_kvargs_parse(). - * - * @param kvlist - * The rte_kvargs structure. No error if NULL. - */ -void rte_kvargs_free(struct rte_kvargs *kvlist); +struct rte_kvargs * +rte_kvargs_parse_delim(const char *args, const char *const valid_keys[], const char *valid_ends) + __rte_malloc __rte_dealloc_kvargs_free; /** * Get the value associated with a given key. -- 2.45.2