diff mbox series

nss: Unnest nested function add_key

Message ID 20211015053246.363952-1-maskray@google.com
State Superseded
Headers show
Series nss: Unnest nested function add_key | expand

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Fāng-ruì Sòng Oct. 15, 2021, 5:32 a.m. UTC
Add one static variable `db`, but make makedb.c compilable with Clang.
---
 nss/makedb.c | 78 +++++++++++++++++++++++++++-------------------------
 1 file changed, 40 insertions(+), 38 deletions(-)

Comments

Florian Weimer Oct. 15, 2021, 7:18 a.m. UTC | #1
* Fangrui Song via Libc-alpha:

> Add one static variable `db`, but make makedb.c compilable with Clang.

“cur_db”?

Please use the twalk_r function, it was added for such situations.
diff mbox series

Patch

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;