From: Thomas Haller <thaller@redhat.com>
To: NetFilter <netfilter-devel@vger.kernel.org>
Cc: Thomas Haller <thaller@redhat.com>
Subject: [PATCH nft 2/5] datatype: don't clone static name/desc strings for datatype
Date: Wed, 27 Sep 2023 21:57:25 +0200 [thread overview]
Message-ID: <20230927200143.3798124-3-thaller@redhat.com> (raw)
In-Reply-To: <20230927200143.3798124-1-thaller@redhat.com>
Avoid cloning static strings for "struct datatype". With
concat_type_alloc(), the name/desc are generated dynamically and need to
be allocated. However, datatype_clone() only reuses the original
name/desc strings. If those strings are static already, we don't need to
clone them.
Note that there are no other places that also want to change or set the
name/desc. If there were, they would need to handle the new fact that
the strings may or may not be dynamically allocated.
Signed-off-by: Thomas Haller <thaller@redhat.com>
---
include/datatype.h | 2 ++
src/datatype.c | 15 ++++++++++-----
2 files changed, 12 insertions(+), 5 deletions(-)
diff --git a/include/datatype.h b/include/datatype.h
index b53a739e1e6c..465ade290652 100644
--- a/include/datatype.h
+++ b/include/datatype.h
@@ -132,6 +132,7 @@ struct parse_ctx;
* @f_prefix: preferred representation for ranges is a prefix
* @f_alloc: whether the instance is dynamically allocated. If not, datatype_get() and
* datatype_free() are NOPs.
+ * @f_allocated_strings: whether @name and @desc are heap allocated or static.
* @name: type name
* @desc: type description
* @basetype: basetype for subtypes, determines type compatibility
@@ -153,6 +154,7 @@ struct datatype {
enum byteorder byteorder;
bool f_prefix:1;
bool f_alloc:1;
+ bool f_allocated_strings:1;
const char *name;
const char *desc;
diff --git a/src/datatype.c b/src/datatype.c
index 464eb49171c6..1c557a06c751 100644
--- a/src/datatype.c
+++ b/src/datatype.c
@@ -1244,8 +1244,10 @@ struct datatype *datatype_clone(const struct datatype *orig_dtype)
dtype = xzalloc(sizeof(*dtype));
*dtype = *orig_dtype;
- dtype->name = xstrdup(orig_dtype->name);
- dtype->desc = xstrdup(orig_dtype->desc);
+ if (orig_dtype->f_allocated_strings) {
+ dtype->name = xstrdup(orig_dtype->name);
+ dtype->desc = xstrdup(orig_dtype->desc);
+ }
dtype->f_alloc = true;
dtype->refcnt = 1;
@@ -1265,8 +1267,10 @@ void datatype_free(const struct datatype *ptr)
if (--dtype->refcnt > 0)
return;
- xfree(dtype->name);
- xfree(dtype->desc);
+ if (dtype->f_allocated_strings) {
+ xfree(dtype->name);
+ xfree(dtype->desc);
+ }
xfree(dtype);
}
@@ -1299,7 +1303,8 @@ const struct datatype *concat_type_alloc(uint32_t type)
dtype = datatype_alloc();
dtype->type = type;
dtype->size = size;
- dtype->subtypes = subtypes;
+ dtype->subtypes = subtypes;
+ dtype->f_allocated_strings = true;
dtype->name = xstrdup(name);
dtype->desc = xstrdup(desc);
dtype->parse = concat_type_parse;
--
2.41.0
next prev parent reply other threads:[~2023-09-27 20:02 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-27 19:57 [PATCH nft 0/5] more various cleanups related to struct datatype Thomas Haller
2023-09-27 19:57 ` [PATCH nft 1/5] datatype: make "flags" field of datatype struct simple booleans Thomas Haller
2024-08-19 22:41 ` Pablo Neira Ayuso
2023-09-27 19:57 ` Thomas Haller [this message]
2023-09-27 19:57 ` [PATCH nft 3/5] datatype: don't clone datatype in set_datatype_alloc() if byteorder already matches Thomas Haller
2023-09-27 19:57 ` [PATCH nft 4/5] datatype: extend set_datatype_alloc() to change size Thomas Haller
2023-09-27 19:57 ` [PATCH nft 5/5] datatype: use xmalloc() for allocating datatype in datatype_clone() Thomas Haller
2023-09-28 19:11 ` Pablo Neira Ayuso
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=20230927200143.3798124-3-thaller@redhat.com \
--to=thaller@redhat.com \
--cc=netfilter-devel@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).