From: schwarze Date: Tue, 13 Oct 2015 15:50:15 +0000 (+0000) Subject: Reduce the amount of code by moving the three copies of the ohash X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=c4b66caec184046e9873cf909bbbccfc9e0cdebd;p=openbsd Reduce the amount of code by moving the three copies of the ohash callback functions into one common place, preparing for the use of ohash for some additional purposes. No functional change. --- diff --git a/usr.bin/mandoc/Makefile b/usr.bin/mandoc/Makefile index 89aa7917aa5..d2e41d637de 100644 --- a/usr.bin/mandoc/Makefile +++ b/usr.bin/mandoc/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.97 2015/07/19 05:59:07 schwarze Exp $ +# $OpenBSD: Makefile,v 1.98 2015/10/13 15:50:15 schwarze Exp $ .include @@ -6,7 +6,7 @@ CFLAGS += -W -Wall -Wstrict-prototypes -Wno-unused-parameter DPADD += ${LIBUTIL} LDADD += -lsqlite3 -lutil -lz -SRCS= mandoc.c mandoc_aux.c preconv.c read.c \ +SRCS= mandoc.c mandoc_aux.c mandoc_ohash.c preconv.c read.c \ roff.c tbl.c tbl_opts.c tbl_layout.c tbl_data.c eqn.c SRCS+= mdoc_macro.c mdoc.c mdoc_hash.c \ mdoc_argv.c mdoc_validate.c att.c msec.c st.c @@ -46,12 +46,13 @@ LIBMDOC_OBJS = mdoc_argv.o mdoc_hash.o mdoc_macro.o mdoc_validate.o \ LIBMAN_OBJS = man.o man_hash.o man_macro.o man_validate.o LIBROFF_OBJS = roff.o eqn.o tbl.o tbl_data.o tbl_layout.o tbl_opts.o LIBMANDOC_OBJS = ${LIBMDOC_OBJS} ${LIBMAN_OBJS} ${LIBROFF_OBJS} \ - mandoc.o mandoc_aux.o chars.o msec.o preconv.o read.o + mandoc.o mandoc_aux.o mandoc_ohash.o \ + chars.o msec.o preconv.o read.o HTML_OBJS = html.o mdoc_html.o man_html.o tbl_html.o eqn_html.o out.o CGI_OBJS = ${LIBMANDOC_OBJS} ${HTML_OBJS} \ mansearch.o mansearch_const.o cgi.o -cgi.o: main.h mandoc.h mandoc_aux.h manconf.h mansearch.h cgi.h +cgi.o: main.h mandoc.h mandoc_aux.h mandoc_ohash.h manconf.h mansearch.h cgi.h man.cgi: ${CGI_OBJS} ${CC} ${LDFLAGS} ${STATIC} -o ${.TARGET} ${CGI_OBJS} ${LDADD} diff --git a/usr.bin/mandoc/mandoc_ohash.c b/usr.bin/mandoc/mandoc_ohash.c new file mode 100644 index 00000000000..464652acc59 --- /dev/null +++ b/usr.bin/mandoc/mandoc_ohash.c @@ -0,0 +1,62 @@ +/* $OpenBSD: mandoc_ohash.c,v 1.1 2015/10/13 15:50:15 schwarze Exp $ */ +/* + * Copyright (c) 2014, 2015 Ingo Schwarze + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#include +#include +#include + +#include "mandoc_aux.h" +#include "mandoc_ohash.h" + +static void *hash_alloc(size_t, void *); +static void *hash_calloc(size_t, size_t, void *); +static void hash_free(void *, void *); + + +void +mandoc_ohash_init(struct ohash *h, unsigned int sz, ptrdiff_t ko) +{ + struct ohash_info info; + + info.alloc = hash_alloc; + info.calloc = hash_calloc; + info.free = hash_free; + info.data = NULL; + info.key_offset = ko; + + ohash_init(h, sz, &info); +} + +static void * +hash_alloc(size_t sz, void *arg) +{ + + return mandoc_malloc(sz); +} + +static void * +hash_calloc(size_t n, size_t sz, void *arg) +{ + + return mandoc_calloc(n, sz); +} + +static void +hash_free(void *p, void *arg) +{ + + free(p); +} diff --git a/usr.bin/mandoc/mandoc_ohash.h b/usr.bin/mandoc/mandoc_ohash.h new file mode 100644 index 00000000000..6879f0d83ea --- /dev/null +++ b/usr.bin/mandoc/mandoc_ohash.h @@ -0,0 +1,23 @@ +/* $OpenBSD: mandoc_ohash.h,v 1.1 2015/10/13 15:50:15 schwarze Exp $ */ +/* + * Copyright (c) 2015 Ingo Schwarze + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#include + +__BEGIN_DECLS + +void mandoc_ohash_init(struct ohash *, unsigned int, ptrdiff_t); + +__END_DECLS diff --git a/usr.bin/mandoc/mandocdb.c b/usr.bin/mandoc/mandocdb.c index 072ced11844..9c0bd93122c 100644 --- a/usr.bin/mandoc/mandocdb.c +++ b/usr.bin/mandoc/mandocdb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mandocdb.c,v 1.155 2015/10/12 22:30:27 schwarze Exp $ */ +/* $OpenBSD: mandocdb.c,v 1.156 2015/10/13 15:50:15 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2011-2015 Ingo Schwarze @@ -34,10 +34,10 @@ #include #include -#include #include #include "mandoc_aux.h" +#include "mandoc_ohash.h" #include "mandoc.h" #include "roff.h" #include "mdoc.h" @@ -136,9 +136,6 @@ static void dbadd_mlink_name(const struct mlink *mlink); static int dbopen(int); static void dbprune(void); static void filescan(const char *); -static void *hash_alloc(size_t, void *); -static void hash_free(void *, void *); -static void *hash_calloc(size_t, size_t, void *); static void mlink_add(struct mlink *, const struct stat *); static void mlink_check(struct mpage *, struct mlink *); static void mlink_free(struct mlink *); @@ -332,7 +329,6 @@ int mandocdb(int argc, char *argv[]) { struct manconf conf; - struct ohash_info mpages_info, mlinks_info; struct mparse *mp; const char *path_arg; size_t j, sz; @@ -346,14 +342,6 @@ mandocdb(int argc, char *argv[]) memset(&conf, 0, sizeof(conf)); memset(stmts, 0, STMT__MAX * sizeof(sqlite3_stmt *)); - mpages_info.alloc = mlinks_info.alloc = hash_alloc; - mpages_info.calloc = mlinks_info.calloc = hash_calloc; - mpages_info.free = mlinks_info.free = hash_free; - mpages_info.data = mlinks_info.data = NULL; - - mpages_info.key_offset = offsetof(struct mpage, inodev); - mlinks_info.key_offset = offsetof(struct mlink, file); - /* * We accept a few different invocations. * The CHECKOP macro makes sure that invocation styles don't @@ -438,8 +426,8 @@ mandocdb(int argc, char *argv[]) mchars = mchars_alloc(); mp = mparse_alloc(mparse_options, MANDOCLEVEL_BADARG, NULL, mchars, NULL); - ohash_init(&mpages, 6, &mpages_info); - ohash_init(&mlinks, 6, &mlinks_info); + mandoc_ohash_init(&mpages, 6, offsetof(struct mpage, inodev)); + mandoc_ohash_init(&mlinks, 6, offsetof(struct mlink, file)); if (OP_UPDATE == op || OP_DELETE == op || OP_TEST == op) { @@ -515,8 +503,10 @@ mandocdb(int argc, char *argv[]) continue; if (j) { - ohash_init(&mpages, 6, &mpages_info); - ohash_init(&mlinks, 6, &mlinks_info); + mandoc_ohash_init(&mpages, 6, + offsetof(struct mpage, inodev)); + mandoc_ohash_init(&mlinks, 6, + offsetof(struct mlink, file)); } if ( ! set_basedir(conf.manpath.paths[j], argc > 0)) @@ -1097,7 +1087,6 @@ static void mpages_merge(struct mparse *mp) { char any[] = "any"; - struct ohash_info str_info; struct mpage *mpage, *mpage_dest; struct mlink *mlink, *mlink_dest; struct roff_man *man; @@ -1106,12 +1095,6 @@ mpages_merge(struct mparse *mp) int fd; unsigned int pslot; - str_info.alloc = hash_alloc; - str_info.calloc = hash_calloc; - str_info.free = hash_free; - str_info.data = NULL; - str_info.key_offset = offsetof(struct str, key); - if ( ! nodb) SQL_EXEC("BEGIN TRANSACTION"); @@ -1124,8 +1107,8 @@ mpages_merge(struct mparse *mp) } name_mask = NAME_MASK; - ohash_init(&names, 4, &str_info); - ohash_init(&strings, 6, &str_info); + mandoc_ohash_init(&names, 4, offsetof(struct str, key)); + mandoc_ohash_init(&strings, 6, offsetof(struct str, key)); mparse_reset(mp); man = NULL; sodest = NULL; @@ -2380,27 +2363,6 @@ prepare_statements: return 1; } -static void * -hash_calloc(size_t n, size_t sz, void *arg) -{ - - return mandoc_calloc(n, sz); -} - -static void * -hash_alloc(size_t sz, void *arg) -{ - - return mandoc_malloc(sz); -} - -static void -hash_free(void *p, void *arg) -{ - - free(p); -} - static int set_basedir(const char *targetdir, int report_baddir) { diff --git a/usr.bin/mandoc/mansearch.c b/usr.bin/mandoc/mansearch.c index d4c9754fc3c..0d197057272 100644 --- a/usr.bin/mandoc/mansearch.c +++ b/usr.bin/mandoc/mansearch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mansearch.c,v 1.46 2015/10/11 21:06:59 schwarze Exp $ */ +/* $OpenBSD: mansearch.c,v 1.47 2015/10/13 15:50:15 schwarze Exp $ */ /* * Copyright (c) 2012 Kristaps Dzonsons * Copyright (c) 2013, 2014, 2015 Ingo Schwarze @@ -34,11 +34,11 @@ #include #include -#include #include #include "mandoc.h" #include "mandoc_aux.h" +#include "mandoc_ohash.h" #include "manconf.h" #include "mansearch.h" @@ -85,9 +85,6 @@ static void buildnames(const struct mansearch *, const char *, int form); static char *buildoutput(sqlite3 *, sqlite3_stmt *, uint64_t, uint64_t); -static void *hash_alloc(size_t, void *); -static void hash_free(void *, void *); -static void *hash_calloc(size_t, size_t, void *); static struct expr *exprcomp(const struct mansearch *, int, char *[]); static void exprfree(struct expr *); @@ -165,7 +162,6 @@ mansearch(const struct mansearch *search, sqlite3 *db; sqlite3_stmt *s, *s2; struct match *mp; - struct ohash_info info; struct ohash htab; unsigned int idx; size_t i, j, cur, maxres; @@ -176,11 +172,6 @@ mansearch(const struct mansearch *search, return 0; } - info.calloc = hash_calloc; - info.alloc = hash_alloc; - info.free = hash_free; - info.key_offset = offsetof(struct match, pageid); - cur = maxres = 0; *res = NULL; @@ -276,8 +267,7 @@ mansearch(const struct mansearch *search, SQL_BIND_INT64(db, s, j, ep->bits); } - memset(&htab, 0, sizeof(struct ohash)); - ohash_init(&htab, 4, &info); + mandoc_ohash_init(&htab, 4, offsetof(struct match, pageid)); /* * Hash each entry on its [unique] document identifier. @@ -845,24 +835,3 @@ exprfree(struct expr *p) p = pp; } } - -static void * -hash_calloc(size_t nmemb, size_t sz, void *arg) -{ - - return mandoc_calloc(nmemb, sz); -} - -static void * -hash_alloc(size_t sz, void *arg) -{ - - return mandoc_malloc(sz); -} - -static void -hash_free(void *p, void *arg) -{ - - free(p); -} diff --git a/usr.bin/mandoc/tag.c b/usr.bin/mandoc/tag.c index 3c97d06963c..46c5406b384 100644 --- a/usr.bin/mandoc/tag.c +++ b/usr.bin/mandoc/tag.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tag.c,v 1.9 2015/10/11 21:59:48 schwarze Exp $ */ +/* $OpenBSD: tag.c,v 1.10 2015/10/13 15:50:15 schwarze Exp $ */ /* * Copyright (c) 2015 Ingo Schwarze * @@ -24,9 +24,8 @@ #include #include -#include - #include "mandoc_aux.h" +#include "mandoc_ohash.h" #include "tag.h" struct tag_entry { @@ -36,9 +35,6 @@ struct tag_entry { }; static void tag_signal(int); -static void *tag_alloc(size_t, void *); -static void tag_free(void *, void *); -static void *tag_calloc(size_t, size_t, void *); static struct ohash tag_data; static struct tag_files tag_files; @@ -52,7 +48,6 @@ static struct tag_files tag_files; struct tag_files * tag_init(void) { - struct ohash_info tag_info; int ofd; ofd = -1; @@ -85,12 +80,7 @@ tag_init(void) * where various marked-up terms are documented. */ - tag_info.alloc = tag_alloc; - tag_info.calloc = tag_calloc; - tag_info.free = tag_free; - tag_info.key_offset = offsetof(struct tag_entry, s); - tag_info.data = NULL; - ohash_init(&tag_data, 4, &tag_info); + mandoc_ohash_init(&tag_data, 4, offsetof(struct tag_entry, s)); return &tag_files; fail: @@ -181,27 +171,3 @@ tag_signal(int signum) /* NOTREACHED */ _exit(1); } - -/* - * Memory management callback functions for ohash. - */ -static void * -tag_alloc(size_t sz, void *arg) -{ - - return mandoc_malloc(sz); -} - -static void * -tag_calloc(size_t nmemb, size_t sz, void *arg) -{ - - return mandoc_calloc(nmemb, sz); -} - -static void -tag_free(void *p, void *arg) -{ - - free(p); -}