mwrap (Perl version) user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: mwrap-perl@80x24.org
Subject: [PATCH] eliminate alloca(3) use
Date: Fri, 18 Nov 2022 07:58:17 +0000	[thread overview]
Message-ID: <20221118075817.1817328-1-e@80x24.org> (raw)

alloca(3) is difficult for the compiler and static checkers to
deal with, and we already reserve TSD space for storing path
names.
---
 Mwrap.xs     |  2 +-
 mwrap_core.h | 21 ++++++++++++---------
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/Mwrap.xs b/Mwrap.xs
index b515894..367a4c3 100644
--- a/Mwrap.xs
+++ b/Mwrap.xs
@@ -153,7 +153,7 @@ mwrap_get(loc)
 PREINIT:
 	STRLEN len;
 	const char *str;
-	struct src_loc *k = (void *)kbuf;
+	struct src_loc *k = &tsd.src_loc;
 	uintptr_t p;
 	struct cds_lfht_iter iter;
 	struct cds_lfht_node *cur;
diff --git a/mwrap_core.h b/mwrap_core.h
index 3abc3dd..09b7930 100644
--- a/mwrap_core.h
+++ b/mwrap_core.h
@@ -178,9 +178,12 @@ struct alloc_hdr {
 };
 
 /* $PATHNAME:$LINENO */
-static MWRAP_TSD char kbuf[
-	sizeof(struct src_loc) + PATH_MAX + sizeof(":") + UINT2STR_MAX
-];
+static MWRAP_TSD union {
+	char kbuf[
+		sizeof(struct src_loc) + PATH_MAX + sizeof(":") + UINT2STR_MAX
+	];
+	struct src_loc src_loc;
+} tsd;
 
 static struct alloc_hdr *ptr2hdr(void *p)
 {
@@ -290,7 +293,7 @@ static struct src_loc *assign_line(size_t size, const char *file, unsigned line)
 	len = strlen(file);
 	if (len > PATH_MAX)
 		len = PATH_MAX;
-	k = (void *)kbuf;
+	k = &tsd.src_loc;
 	k->total = size;
 	dst = mempcpy(k->k, file, len);
 	*dst++ = ':';
@@ -311,7 +314,6 @@ static struct src_loc *
 update_stats_rcu_lock(size_t *generation, size_t size, uintptr_t caller)
 {
 	struct src_loc *k, *ret = 0;
-	static const size_t xlen = sizeof(caller);
 	struct cds_lfht *t = CMM_LOAD_SHARED(totals);
 	const COP *cop = NULL;
 
@@ -326,11 +328,11 @@ update_stats_rcu_lock(size_t *generation, size_t size, uintptr_t caller)
 		ret = assign_line(size, OutCopFILE(cop), CopLINE(cop));
 #endif /* MWRAP_PERL */
 	if (!ret) {
-		k = alloca(sizeof(*k) + xlen);
+		k = &tsd.src_loc;
 		k->total = size;
-		memcpy(k->k, &caller, xlen);
+		memcpy(k->k, &caller, sizeof(caller));
 		k->capa = 0;
-		hash_loc(k, xlen);
+		hash_loc(k, sizeof(caller));
 		ret = totals_add_rcu(k);
 	}
 out:
@@ -672,7 +674,8 @@ static void dump_destructor(void)
 			*dump_path) {
 		char *end = strchr(dump_path, ',');
 		if (end) {
-			char *tmp = alloca(end - dump_path + 1);
+			char *tmp = tsd.kbuf;
+			assert((end - dump_path) < sizeof(tsd.kbuf));
 			end = mempcpy(tmp, dump_path, end - dump_path);
 			*end = 0;
 			dump_path = tmp;

                 reply	other threads:[~2022-11-18  7:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20221118075817.1817328-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=mwrap-perl@80x24.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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mwrap-perl.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).