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.
This commit is contained in:
dholland 2012-03-12 00:20:30 +00:00
parent bed7cf5a54
commit a883398ef5
5 changed files with 130 additions and 45 deletions

View File

@ -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 *selecttab; /* selects things that are "optional foo" */
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 @@ const char *intern(const char *);
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 @@ void setupdirs(void);
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 */

View File

@ -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 @@ ht_enumerate(struct hashtab *ht, ht_callback cbfunc, void *arg)
}
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);

View File

@ -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 struct opt_type {
};
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 @@ do_emit_option(const char *name, void *value, void *v)
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 @@ do_emit_option(const char *name, void *value, void *v)
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");
}

View File

@ -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 @@ int yyparse(void);
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 @@ static void do_kill_orphans(struct devbase *, struct attr *,
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 @@ main(int argc, char **argv)
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 @@ deffilesystem(struct nvlist *fses, struct nvlist *deps)
* 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 @@ find_declared_option(const char *name)
{
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 @@ find_declared_option(const char *name)
* 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 @@ defopt(struct hashtab *ht, const char *fname, struct nvlist *opts,
* 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 @@ defopt(struct hashtab *ht, const char *fname, struct nvlist *opts,
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 @@ defopt(struct hashtab *ht, const char *fname, struct nvlist *opts,
*/
if (obs) {
nv->nv_flags |= NV_OBSOLETE;
(void)ht_insert(obsopttab, nv->nv_name, nv);
(void)nvhash_insert(obsopttab, nv->nv_name, nv);
}
/*
@ -764,8 +764,8 @@ defopt(struct hashtab *ht, const char *fname, struct nvlist *opts,
* Otherwise, append to the list of options already
* associated with this file.
*/
if ((oldnv = ht_lookup(optfiletab, name)) == NULL) {
(void)ht_insert(optfiletab, name, nv);
if ((oldnv = nvhash_lookup(optfiletab, name)) == NULL) {
(void)nvhash_insert(optfiletab, name, nv);
} else {
while (oldnv->nv_next != NULL)
oldnv = oldnv->nv_next;

View File

@ -1,4 +1,4 @@
/* $NetBSD: mkheaders.c,v 1.19 2012/03/11 21:16:08 dholland Exp $ */
/* $NetBSD: mkheaders.c,v 1.20 2012/03/12 00:20:30 dholland Exp $ */
/*
* Copyright (c) 1992, 1993
@ -61,7 +61,7 @@ static int emitcnt(struct nvlist *);
static int emitopts(void);
static int emittime(void);
static int herr(const char *, const char *, FILE *);
static int defopts_print(const char *, void *, void *);
static int defopts_print(const char *, struct nvlist *, void *);
static char *cntname(const char *);
/*
@ -221,7 +221,7 @@ fprintstr(FILE *fp, const char *str)
*/
static int
/*ARGSUSED*/
defopts_print(const char *name, void *value, void *arg)
defopts_print(const char *name, struct nvlist *value, void *arg)
{
char tfname[BUFSIZ];
struct nvlist *nv, *option;
@ -286,7 +286,7 @@ static int
emitopts(void)
{
return (ht_enumerate(optfiletab, defopts_print, NULL));
return (nvhash_enumerate(optfiletab, defopts_print, NULL));
}
/*