Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/config Introduce type-safe wrappers around the hash ...



details:   https://anonhg.NetBSD.org/src/rev/a23c3f6d9a7f
branches:  trunk
changeset: 778004:a23c3f6d9a7f
user:      dholland <dholland%NetBSD.org@localhost>
date:      Mon Mar 12 00:20:30 2012 +0000

description:
Introduce type-safe wrappers around the hash tables. Use them for a
selected set of tables affected by the next nvlist cleanup in the
works.

diffstat:

 usr.bin/config/defs.h      |  37 +++++++++++++++-------
 usr.bin/config/hash.c      |  74 +++++++++++++++++++++++++++++++++++++++++++++-
 usr.bin/config/lint.c      |  14 ++++----
 usr.bin/config/main.c      |  42 +++++++++++++-------------
 usr.bin/config/mkheaders.c |   8 ++--
 5 files changed, 130 insertions(+), 45 deletions(-)

diffs (truncated from 351 to 300 lines):

diff -r 4305e582a4c3 -r a23c3f6d9a7f usr.bin/config/defs.h
--- a/usr.bin/config/defs.h     Sun Mar 11 23:42:06 2012 +0000
+++ b/usr.bin/config/defs.h     Mon Mar 12 00:20:30 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: defs.h,v 1.40 2012/03/11 21:16:07 dholland Exp $       */
+/*     $NetBSD: defs.h,v 1.41 2012/03/12 00:20:30 dholland Exp $       */
 
 /*
  * Copyright (c) 1992, 1993
@@ -460,12 +460,12 @@
 struct hashtab *needcnttab;    /* retains names marked "needs-count" */
 struct hashtab *opttab;        /* table of configured options */
 struct hashtab *fsopttab;      /* table of configured file systems */
-struct hashtab *defopttab;     /* options that have been "defopt"'d */
-struct hashtab *defflagtab;    /* options that have been "defflag"'d */
-struct hashtab *defparamtab;   /* options that have been "defparam"'d */
-struct hashtab *defoptlint;    /* lint values for options */
-struct hashtab *deffstab;      /* defined file systems */
-struct hashtab *optfiletab;    /* "defopt"'d option .h files */
+struct nvhash *defopttab;      /* options that have been "defopt"'d */
+struct nvhash *defflagtab;     /* options that have been "defflag"'d */
+struct nvhash *defparamtab;    /* options that have been "defparam"'d */
+struct nvhash *defoptlint;     /* lint values for options */
+struct nvhash *deffstab;       /* defined file systems */
+struct nvhash *optfiletab;     /* "defopt"'d option .h files */
 struct hashtab *attrtab;       /* attributes (locators, etc.) */
 struct hashtab *bdevmtab;      /* block devm lookup */
 struct hashtab *cdevmtab;      /* character devm lookup */
@@ -529,6 +529,19 @@
 typedef int (*ht_callback)(const char *, void *, void *);
 int    ht_enumerate(struct hashtab *, ht_callback, void *);
 
+/* typed hash, named struct HT, whose type is string -> struct VT */
+#define DECLHASH(HT, VT) \
+       struct HT;                                                      \
+       struct HT *HT##_create(void);                                   \
+       int HT##_insert(struct HT *, const char *, struct VT *);        \
+       int HT##_replace(struct HT *, const char *, struct VT *);       \
+       int HT##_remove(struct HT *, const char *);                     \
+       struct VT *HT##_lookup(struct HT *, const char *);              \
+       int HT##_enumerate(struct HT *,                                 \
+                       int (*)(const char *, struct VT *, void *),     \
+                       void *)
+DECLHASH(nvhash, nvlist);
+
 /* lint.c */
 void   emit_instances(void);
 void   emit_options(void);
@@ -554,11 +567,11 @@
 const char *strtolower(const char *);
 
 /* tests on option types */
-#define OPT_FSOPT(n)   (ht_lookup(deffstab, (n)) != NULL)
-#define OPT_DEFOPT(n)  (ht_lookup(defopttab, (n)) != NULL)
-#define OPT_DEFFLAG(n) (ht_lookup(defflagtab, (n)) != NULL)
-#define OPT_DEFPARAM(n)        (ht_lookup(defparamtab, (n)) != NULL)
-#define OPT_OBSOLETE(n)        (ht_lookup(obsopttab, (n)) != NULL)
+#define OPT_FSOPT(n)   (nvhash_lookup(deffstab, (n)) != NULL)
+#define OPT_DEFOPT(n)  (nvhash_lookup(defopttab, (n)) != NULL)
+#define OPT_DEFFLAG(n) (nvhash_lookup(defflagtab, (n)) != NULL)
+#define OPT_DEFPARAM(n)        (nvhash_lookup(defparamtab, (n)) != NULL)
+#define OPT_OBSOLETE(n)        (nvhash_lookup(obsopttab, (n)) != NULL)
 #define DEFINED_OPTION(n) (find_declared_option((n)) != NULL)
 
 /* main.c */
diff -r 4305e582a4c3 -r a23c3f6d9a7f usr.bin/config/hash.c
--- a/usr.bin/config/hash.c     Sun Mar 11 23:42:06 2012 +0000
+++ b/usr.bin/config/hash.c     Mon Mar 12 00:20:30 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: hash.c,v 1.6 2009/04/11 12:41:10 lukem Exp $   */
+/*     $NetBSD: hash.c,v 1.7 2012/03/12 00:20:30 dholland Exp $        */
 
 /*
  * Copyright (c) 1992, 1993
@@ -315,3 +315,75 @@
        }
        return rval;
 }
+
+/************************************************************/
+
+/*
+ * Type-safe wrappers.
+ */
+
+#define DEFHASH(HT, VT) \
+       struct HT {                                             \
+               struct hashtab imp;                             \
+       };                                                      \
+                                                               \
+       struct HT *                                             \
+       HT##_create(void)                                       \
+       {                                                       \
+               struct HT *tbl;                                 \
+                                                               \
+               tbl = ecalloc(1, sizeof(*tbl));                 \
+               ht_init(&tbl->imp, 8);                          \
+               return tbl;                                     \
+       }                                                       \
+                                                               \
+       int                                                     \
+       HT##_insert(struct HT *tbl, const char *name, struct VT *val) \
+       {                                                       \
+               return ht_insert(&tbl->imp, name, val);         \
+       }                                                       \
+                                                               \
+       int                                                     \
+       HT##_replace(struct HT *tbl, const char *name, struct VT *val) \
+       {                                                       \
+               return ht_replace(&tbl->imp, name, val);        \
+       }                                                       \
+                                                               \
+       int                                                     \
+       HT##_remove(struct HT *tbl, const char *name)           \
+       {                                                       \
+               return ht_remove(&tbl->imp, name);              \
+       }                                                       \
+                                                               \
+       struct VT *                                             \
+       HT##_lookup(struct HT *tbl, const char *name)           \
+       {                                                       \
+               return ht_lookup(&tbl->imp, name);              \
+       }                                                       \
+                                                               \
+       struct HT##_enumcontext {                               \
+               int (*func)(const char *, struct VT *, void *); \
+               void *userctx;                                  \
+       };                                                      \
+                                                               \
+       static int                                              \
+       HT##_enumerate_thunk(const char *name, void *value, void *voidctx) \
+       {                                                       \
+               struct HT##_enumcontext *ctx = voidctx;         \
+                                                               \
+               return ctx->func(name, value, ctx->userctx);    \
+       }                                                       \
+                                                               \
+       int                                                     \
+       HT##_enumerate(struct HT *tbl,                          \
+                     int (*func)(const char *, struct VT *, void *), \
+                     void *userctx)                            \
+       {                                                       \
+               struct HT##_enumcontext ctx;                    \
+                                                               \
+               ctx.func = func;                                \
+               ctx.userctx = userctx;                          \
+               return ht_enumerate(&tbl->imp, HT##_enumerate_thunk, &ctx); \
+       }
+
+DEFHASH(nvhash, nvlist);
diff -r 4305e582a4c3 -r a23c3f6d9a7f usr.bin/config/lint.c
--- a/usr.bin/config/lint.c     Sun Mar 11 23:42:06 2012 +0000
+++ b/usr.bin/config/lint.c     Mon Mar 12 00:20:30 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lint.c,v 1.10 2012/03/11 21:16:08 dholland Exp $       */
+/*     $NetBSD: lint.c,v 1.11 2012/03/12 00:20:30 dholland Exp $       */
 
 /*
  *  Copyright (c) 2007 The NetBSD Foundation.
@@ -62,7 +62,7 @@
 };
 
 static int
-do_emit_option(const char *name, void *value, void *v)
+do_emit_option(const char *name, struct nvlist *value, void *v)
 {
        struct nvlist *nv = value;
        const struct opt_type *ot = v;
@@ -75,7 +75,7 @@
 
        printf("%s\t%s", ot->ot_name, nv->nv_name);
        if (ot->ot_type == OT_PARAM) {
-               struct nvlist *nv2  = ht_lookup(defoptlint, nv->nv_name);
+               struct nvlist *nv2  = nvhash_lookup(defoptlint, nv->nv_name);
                if (nv2 == NULL)
                        nv2 = nv;
                printf("=\"%s\"", nv2->nv_str ? nv2->nv_str : "1");
@@ -87,14 +87,14 @@
        
 
 void
-emit_options()
+emit_options(void)
 {
 
-       (void)ht_enumerate(defflagtab, do_emit_option, &opt_types[0]);
+       (void)nvhash_enumerate(defflagtab, do_emit_option, &opt_types[0]);
        printf("\n");
-       (void)ht_enumerate(defparamtab, do_emit_option, &opt_types[1]);
+       (void)nvhash_enumerate(defparamtab, do_emit_option, &opt_types[1]);
        printf("\n");
-       (void)ht_enumerate(deffstab, do_emit_option, &opt_types[2]);
+       (void)nvhash_enumerate(deffstab, do_emit_option, &opt_types[2]);
        printf("\n");
 }
 
diff -r 4305e582a4c3 -r a23c3f6d9a7f usr.bin/config/main.c
--- a/usr.bin/config/main.c     Sun Mar 11 23:42:06 2012 +0000
+++ b/usr.bin/config/main.c     Mon Mar 12 00:20:30 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.45 2012/03/11 08:21:53 dholland Exp $       */
+/*     $NetBSD: main.c,v 1.46 2012/03/12 00:20:30 dholland Exp $       */
 
 /*
  * Copyright (c) 1992, 1993
@@ -90,7 +90,7 @@
 extern int yydebug;
 #endif
 
-static struct hashtab *obsopttab;
+static struct nvhash *obsopttab;
 static struct hashtab *mkopttab;
 static struct nvlist **nextopt;
 static struct nvlist **nextmkopt;
@@ -119,7 +119,7 @@
     struct devbase *, int);
 static int     kill_orphans_cb(const char *, void *, void *);
 static int     cfcrosscheck(struct config *, const char *, struct nvlist *);
-void   defopt(struct hashtab *ht, const char *fname,
+void   defopt(struct nvhash *ht, const char *fname,
             struct nvlist *opts, struct nvlist *deps, int obs);
 
 #define LOGCONFIG_LARGE "INCLUDE_CONFIG_FILE"
@@ -266,13 +266,13 @@
        opttab = ht_new();
        mkopttab = ht_new();
        fsopttab = ht_new();
-       deffstab = ht_new();
-       defopttab = ht_new();
-       defparamtab = ht_new();
-       defoptlint = ht_new();
-       defflagtab = ht_new();
-       optfiletab = ht_new();
-       obsopttab = ht_new();
+       deffstab = nvhash_create();
+       defopttab = nvhash_create();
+       defparamtab = nvhash_create();
+       defoptlint = nvhash_create();
+       defflagtab = nvhash_create();
+       optfiletab = nvhash_create();
+       obsopttab = nvhash_create();
        bdevmtab = ht_new();
        maxbdevm = 0;
        cdevmtab = ht_new();
@@ -627,7 +627,7 @@
                 * used in "file-system" directives in the config
                 * file.
                 */
-               if (ht_insert(deffstab, nv->nv_name, nv))
+               if (nvhash_insert(deffstab, nv->nv_name, nv))
                        panic("file system `%s' already in table?!",
                            nv->nv_name);
 
@@ -668,10 +668,10 @@
 {
        struct nvlist *option = NULL;
 
-       if ((option = ht_lookup(defopttab, name)) != NULL ||
-           (option = ht_lookup(defparamtab, name)) != NULL ||
-           (option = ht_lookup(defflagtab, name)) != NULL ||
-           (option = ht_lookup(deffstab, name)) != NULL) {
+       if ((option = nvhash_lookup(defopttab, name)) != NULL ||
+           (option = nvhash_lookup(defparamtab, name)) != NULL ||
+           (option = nvhash_lookup(defflagtab, name)) != NULL ||
+           (option = nvhash_lookup(deffstab, name)) != NULL) {
                return (option);
        }
 
@@ -686,7 +686,7 @@
  * record the option information in the specified table.
  */
 void
-defopt(struct hashtab *ht, const char *fname, struct nvlist *opts,
+defopt(struct nvhash *ht, const char *fname, struct nvlist *opts,
        struct nvlist *deps, int obs)
 {
        struct nvlist *nv, *nextnv, *oldnv;
@@ -710,7 +710,7 @@
                         * If an entry already exists, then we are about to
                         * complain, so no worry.
                         */
-                       (void) ht_insert(defoptlint, nextnv->nv_name,
+                       (void) nvhash_insert(defoptlint, nextnv->nv_name,
                            nv);
                        nv = nextnv;
                        nextnv = nextnv->nv_next;
@@ -723,7 +723,7 @@
                        return;
                }
 
-               if (ht_insert(ht, nv->nv_name, nv)) {
+               if (nvhash_insert(ht, nv->nv_name, nv)) {
                        cfgerror("file system or option `%s' already defined",
                            nv->nv_name);
                        return;
@@ -756,7 +756,7 @@
                 */
                if (obs) {
                        nv->nv_flags |= NV_OBSOLETE;
-                       (void)ht_insert(obsopttab, nv->nv_name, nv);



Home | Main Index | Thread Index | Old Index