Message ID | 20211015162633.550651-1-maskray@google.com |
---|---|
State | New |
Headers | show |
Series | [v2] nss: Unnest nested function add_key | expand |
On 15/10/2021 13:26, Fangrui Song via Libc-alpha wrote: > This makes makedb.c compilable with Clang which does not support nested > functions. LGTM with the style fix below. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org> > --- > nss/makedb.c | 82 ++++++++++++++++++++++++++-------------------------- > 1 file changed, 41 insertions(+), 41 deletions(-) > > diff --git a/nss/makedb.c b/nss/makedb.c > index 30c3d0426e..853a7e8d37 100644 > --- a/nss/makedb.c > +++ b/nss/makedb.c > @@ -618,6 +618,45 @@ next_prime (size_t seed) > return seed; > } > > +static size_t max_chainlength; > +static char *wp; > +static size_t nhashentries; > +static bool copy_string; > + > +void add_key(const void *nodep, VISIT which, void *arg) > +{ > + if (which != leaf && which != postorder) > + return; > + > + const struct database *db = (const struct database *)arg; Space after cast. > + 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); > +} > > static void > compute_tables (void) > @@ -649,45 +688,6 @@ 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); > - } > - > copy_string = false; > nhashentries = nhashentries_min; > for (size_t cnt = 0; cnt < TEST_RANGE; ++cnt) > @@ -697,7 +697,7 @@ compute_tables (void) > max_chainlength = 0; > wp = db->keystrtab; > > - twalk (db->entries, add_key); > + twalk_r (db->entries, add_key, db); > > if (max_chainlength == 0) > { > @@ -724,7 +724,7 @@ compute_tables (void) > copy_string = true; > wp = db->keystrtab; > > - twalk (db->entries, add_key); > + twalk_r (db->entries, add_key, db); > > db->nhashentries = nhashentries_best; > nhashentries_total += nhashentries_best; >
diff --git a/nss/makedb.c b/nss/makedb.c index 30c3d0426e..853a7e8d37 100644 --- a/nss/makedb.c +++ b/nss/makedb.c @@ -618,6 +618,45 @@ next_prime (size_t seed) return seed; } +static size_t max_chainlength; +static char *wp; +static size_t nhashentries; +static bool copy_string; + +void add_key(const void *nodep, VISIT which, void *arg) +{ + if (which != leaf && which != postorder) + return; + + const struct database *db = (const struct database *)arg; + 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); +} static void compute_tables (void) @@ -649,45 +688,6 @@ 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); - } - copy_string = false; nhashentries = nhashentries_min; for (size_t cnt = 0; cnt < TEST_RANGE; ++cnt) @@ -697,7 +697,7 @@ compute_tables (void) max_chainlength = 0; wp = db->keystrtab; - twalk (db->entries, add_key); + twalk_r (db->entries, add_key, db); if (max_chainlength == 0) { @@ -724,7 +724,7 @@ compute_tables (void) copy_string = true; wp = db->keystrtab; - twalk (db->entries, add_key); + twalk_r (db->entries, add_key, db); db->nhashentries = nhashentries_best; nhashentries_total += nhashentries_best;