aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2015-10-13 15:53:05 +0000
committerIngo Schwarze <schwarze@openbsd.org>2015-10-13 15:53:05 +0000
commite47784200392e2dea53b3decd3ceb23e1e2a0ca6 (patch)
tree06f0453944a7435752f1560277357ba9f8bec536
parent899e2d9f117239c813531c03cd4eb41d120bce26 (diff)
downloadmandoc-e47784200392e2dea53b3decd3ceb23e1e2a0ca6.tar.gz
mandoc-e47784200392e2dea53b3decd3ceb23e1e2a0ca6.tar.zst
mandoc-e47784200392e2dea53b3decd3ceb23e1e2a0ca6.zip
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.
-rw-r--r--Makefile5
-rw-r--r--Makefile.depend7
-rw-r--r--mandoc_ohash.c62
-rw-r--r--mandoc_ohash.h27
-rw-r--r--mandocdb.c62
-rw-r--r--mansearch.c41
-rw-r--r--tag.c44
7 files changed, 113 insertions, 135 deletions
diff --git a/Makefile b/Makefile
index d26058da..c101c6ff 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.467 2015/10/11 21:12:54 schwarze Exp $
+# $Id: Makefile,v 1.468 2015/10/13 15:53:05 schwarze Exp $
#
# Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
# Copyright (c) 2011, 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
@@ -76,6 +76,7 @@ SRCS = att.c \
man_validate.c \
mandoc.c \
mandoc_aux.c \
+ mandoc_ohash.c \
mandocdb.c \
manpage.c \
manpath.c \
@@ -151,6 +152,7 @@ DISTFILES = INSTALL \
mandoc_headers.3 \
mandoc_html.3 \
mandoc_malloc.3 \
+ mandoc_ohash.h \
mansearch.3 \
mansearch.h \
mchars_alloc.3 \
@@ -198,6 +200,7 @@ LIBMANDOC_OBJS = $(LIBMAN_OBJS) \
chars.o \
mandoc.o \
mandoc_aux.o \
+ mandoc_ohash.o \
msec.o \
preconv.o \
read.o
diff --git a/Makefile.depend b/Makefile.depend
index 914d3908..abac604e 100644
--- a/Makefile.depend
+++ b/Makefile.depend
@@ -32,10 +32,11 @@ man_term.o: man_term.c config.h mandoc_aux.h mandoc.h roff.h man.h out.h term.h
man_validate.o: man_validate.c config.h mandoc_aux.h mandoc.h roff.h man.h libmandoc.h roff_int.h libman.h
mandoc.o: mandoc.c config.h mandoc.h mandoc_aux.h libmandoc.h
mandoc_aux.o: mandoc_aux.c config.h mandoc.h mandoc_aux.h
-mandocdb.o: mandocdb.c config.h compat_fts.h compat_ohash.h mandoc_aux.h mandoc.h roff.h mdoc.h man.h manconf.h mansearch.h
+mandoc_ohash.o: mandoc_ohash.c mandoc_aux.h mandoc_ohash.h compat_ohash.h
+mandocdb.o: mandocdb.c config.h compat_fts.h mandoc_aux.h mandoc_ohash.h compat_ohash.h mandoc.h roff.h mdoc.h man.h manconf.h mansearch.h
manpage.o: manpage.c config.h manconf.h mansearch.h
manpath.o: manpath.c config.h mandoc_aux.h manconf.h
-mansearch.o: mansearch.c config.h compat_ohash.h mandoc.h mandoc_aux.h manconf.h mansearch.h
+mansearch.o: mansearch.c config.h mandoc.h mandoc_aux.h mandoc_ohash.h compat_ohash.h manconf.h mansearch.h
mansearch_const.o: mansearch_const.c config.h mansearch.h
mdoc.o: mdoc.c config.h mandoc_aux.h mandoc.h roff.h mdoc.h libmandoc.h roff_int.h libmdoc.h
mdoc_argv.o: mdoc_argv.c config.h mandoc_aux.h mandoc.h roff.h mdoc.h libmandoc.h libmdoc.h
@@ -52,7 +53,7 @@ read.o: read.c config.h mandoc_aux.h mandoc.h roff.h mdoc.h man.h libmandoc.h ro
roff.o: roff.c config.h mandoc.h mandoc_aux.h roff.h libmandoc.h roff_int.h libroff.h predefs.in
soelim.o: soelim.c config.h compat_stringlist.h
st.o: st.c config.h roff.h mdoc.h libmdoc.h st.in
-tag.o: tag.c config.h compat_ohash.h mandoc_aux.h tag.h
+tag.o: tag.c config.h mandoc_aux.h mandoc_ohash.h compat_ohash.h tag.h
tbl.o: tbl.c config.h mandoc.h mandoc_aux.h libmandoc.h libroff.h
tbl_data.o: tbl_data.c config.h mandoc.h mandoc_aux.h libmandoc.h libroff.h
tbl_html.o: tbl_html.c config.h mandoc.h out.h html.h
diff --git a/mandoc_ohash.c b/mandoc_ohash.c
new file mode 100644
index 00000000..a75599ef
--- /dev/null
+++ b/mandoc_ohash.c
@@ -0,0 +1,62 @@
+/* $Id: mandoc_ohash.c,v 1.1 2015/10/13 15:53:05 schwarze Exp $ */
+/*
+ * Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
+ *
+ * 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 <sys/types.h>
+#include <stddef.h>
+#include <stdlib.h>
+
+#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/mandoc_ohash.h b/mandoc_ohash.h
new file mode 100644
index 00000000..7e24517c
--- /dev/null
+++ b/mandoc_ohash.h
@@ -0,0 +1,27 @@
+/* $Id: mandoc_ohash.h,v 1.1 2015/10/13 15:53:05 schwarze Exp $ */
+/*
+ * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
+ *
+ * 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.
+ */
+#if HAVE_OHASH
+#include <ohash.h>
+#else
+#include "compat_ohash.h"
+#endif
+
+__BEGIN_DECLS
+
+void mandoc_ohash_init(struct ohash *, unsigned int, ptrdiff_t);
+
+__END_DECLS
diff --git a/mandocdb.c b/mandocdb.c
index 8ac06391..228e2a0d 100644
--- a/mandocdb.c
+++ b/mandocdb.c
@@ -1,4 +1,4 @@
-/* $Id: mandocdb.c,v 1.201 2015/10/12 21:17:15 schwarze Exp $ */
+/* $Id: mandocdb.c,v 1.202 2015/10/13 15:53:05 schwarze Exp $ */
/*
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011-2015 Ingo Schwarze <schwarze@openbsd.org>
@@ -40,14 +40,10 @@
#include <string.h>
#include <unistd.h>
-#if HAVE_OHASH
-#include <ohash.h>
-#else
-#include "compat_ohash.h"
-#endif
#include <sqlite3.h>
#include "mandoc_aux.h"
+#include "mandoc_ohash.h"
#include "mandoc.h"
#include "roff.h"
#include "mdoc.h"
@@ -146,9 +142,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 *);
@@ -342,7 +335,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;
@@ -351,14 +343,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 +422,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) {
@@ -509,8 +493,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))
@@ -1099,7 +1085,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;
@@ -1108,12 +1093,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");
@@ -1126,8 +1105,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;
@@ -2384,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/mansearch.c b/mansearch.c
index def52984..41fdb499 100644
--- a/mansearch.c
+++ b/mansearch.c
@@ -1,4 +1,4 @@
-/* $Id: mansearch.c,v 1.59 2015/10/11 21:12:55 schwarze Exp $ */
+/* $Id: mansearch.c,v 1.60 2015/10/13 15:53:05 schwarze Exp $ */
/*
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
@@ -35,11 +35,6 @@
#include <string.h>
#include <unistd.h>
-#if HAVE_OHASH
-#include <ohash.h>
-#else
-#include "compat_ohash.h"
-#endif
#include <sqlite3.h>
#ifndef SQLITE_DETERMINISTIC
#define SQLITE_DETERMINISTIC 0
@@ -47,6 +42,7 @@
#include "mandoc.h"
#include "mandoc_aux.h"
+#include "mandoc_ohash.h"
#include "manconf.h"
#include "mansearch.h"
@@ -93,9 +89,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 *);
@@ -173,7 +166,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;
@@ -184,11 +176,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;
@@ -284,8 +271,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.
@@ -853,24 +839,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/tag.c b/tag.c
index ca78803c..5766d540 100644
--- a/tag.c
+++ b/tag.c
@@ -1,4 +1,4 @@
-/* $Id: tag.c,v 1.9 2015/10/11 22:00:52 schwarze Exp $ */
+/* $Id: tag.c,v 1.10 2015/10/13 15:53:05 schwarze Exp $ */
/*
* Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
*
@@ -26,13 +26,8 @@
#include <string.h>
#include <unistd.h>
-#if HAVE_OHASH
-#include <ohash.h>
-#else
-#include "compat_ohash.h"
-#endif
-
#include "mandoc_aux.h"
+#include "mandoc_ohash.h"
#include "tag.h"
struct tag_entry {
@@ -42,9 +37,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;
@@ -58,7 +50,6 @@ static struct tag_files tag_files;
struct tag_files *
tag_init(void)
{
- struct ohash_info tag_info;
int ofd;
ofd = -1;
@@ -91,12 +82,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:
@@ -187,27 +173,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);
-}