public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Fangrui Song <maskray@google.com>
To: libc-alpha@sourceware.org
Subject: [PATCH] nss: Unnest nested function add_key
Date: Thu, 14 Oct 2021 22:32:46 -0700	[thread overview]
Message-ID: <20211015053246.363952-1-maskray@google.com> (raw)

Add one static variable `db`, but make makedb.c compilable with Clang.
---
 nss/makedb.c | 78 +++++++++++++++++++++++++++-------------------------
 1 file changed, 40 insertions(+), 38 deletions(-)

diff --git a/nss/makedb.c b/nss/makedb.c
index 30c3d0426e..cc09e822d8 100644
--- a/nss/makedb.c
+++ b/nss/makedb.c
@@ -618,6 +618,45 @@ next_prime (size_t seed)
   return seed;
 }
 
+static struct database *cur_db;
+static size_t max_chainlength;
+static char *wp;
+static size_t nhashentries;
+static bool copy_string;
+
+void add_key(const void *nodep, const VISIT which, const int depth)
+{
+  if (which != leaf && which != postorder)
+    return;
+
+  const struct dbentry *dbe = *(const struct dbentry **) nodep;
+
+  ptrdiff_t stridx;
+  if (copy_string)
+    {
+      stridx = wp - cur_db->keystrtab;
+      wp = stpcpy (wp, dbe->str) + 1;
+    }
+  else
+    stridx = 0;
+
+  size_t hidx = dbe->hashval % nhashentries;
+  size_t hval2 = 1 + dbe->hashval % (nhashentries - 2);
+  size_t chainlength = 0;
+
+  while (cur_db->hashtable[hidx] != ~((stridx_t) 0))
+    {
+      ++chainlength;
+      if ((hidx += hval2) >= nhashentries)
+	hidx -= nhashentries;
+    }
+
+  cur_db->hashtable[hidx] = ((cur_db->extra_string ? valstrlen : 0)
+			     + dbe->validx);
+  cur_db->keyidxtab[hidx] = stridx;
+
+  max_chainlength = MAX (max_chainlength, chainlength);
+}
 
 static void
 compute_tables (void)
@@ -649,44 +688,7 @@ compute_tables (void)
 	db->keyidxtab = db->hashtable + nhashentries_max;
 	db->keystrtab = (char *) (db->keyidxtab + nhashentries_max);
 
-	static size_t max_chainlength;
-	static char *wp;
-	static size_t nhashentries;
-	static bool copy_string;
-
-	void add_key(const void *nodep, const VISIT which, const int depth)
-	{
-	  if (which != leaf && which != postorder)
-	    return;
-
-	  const struct dbentry *dbe = *(const struct dbentry **) nodep;
-
-	  ptrdiff_t stridx;
-	  if (copy_string)
-	    {
-	      stridx = wp - db->keystrtab;
-	      wp = stpcpy (wp, dbe->str) + 1;
-	    }
-	  else
-	    stridx = 0;
-
-	  size_t hidx = dbe->hashval % nhashentries;
-	  size_t hval2 = 1 + dbe->hashval % (nhashentries - 2);
-	  size_t chainlength = 0;
-
-	  while (db->hashtable[hidx] != ~((stridx_t) 0))
-	    {
-	      ++chainlength;
-	      if ((hidx += hval2) >= nhashentries)
-		hidx -= nhashentries;
-	    }
-
-	  db->hashtable[hidx] = ((db->extra_string ? valstrlen : 0)
-				 + dbe->validx);
-	  db->keyidxtab[hidx] = stridx;
-
-	  max_chainlength = MAX (max_chainlength, chainlength);
-	}
+	cur_db = db;
 
 	copy_string = false;
 	nhashentries = nhashentries_min;
-- 
2.33.0.1079.g6e70778dc9-goog


             reply	other threads:[~2021-10-15  5:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-15  5:32 Fangrui Song [this message]
2021-10-15  7:18 ` Florian Weimer

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=20211015053246.363952-1-maskray@google.com \
    --to=maskray@google.com \
    --cc=libc-alpha@sourceware.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 read-only IMAP folder(s) and NNTP newsgroup(s).