source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Reduce the amount of code by moving the three copies of the
@ 2015-10-13 15:53 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2015-10-13 15:53 UTC (permalink / raw)
  To: source

Log Message:
-----------
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.

Modified Files:
--------------
    mdocml:
        Makefile
        Makefile.depend
        mandocdb.c
        mansearch.c
        tag.c

Added Files:
-----------
    mdocml:
        mandoc_ohash.c
        mandoc_ohash.h

Revision Data
-------------
Index: Makefile.depend
===================================================================
RCS file: /home/cvs/mdocml/mdocml/Makefile.depend,v
retrieving revision 1.15
retrieving revision 1.16
diff -LMakefile.depend -LMakefile.depend -u -p -r1.15 -r1.16
--- Makefile.depend
+++ Makefile.depend
@@ -32,10 +32,11 @@ man_term.o: man_term.c config.h mandoc_a
 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 man
 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
Index: Makefile
===================================================================
RCS file: /home/cvs/mdocml/mdocml/Makefile,v
retrieving revision 1.467
retrieving revision 1.468
diff -LMakefile -LMakefile -u -p -r1.467 -r1.468
--- Makefile
+++ Makefile
@@ -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
--- /dev/null
+++ 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);
+}
--- /dev/null
+++ 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
Index: mandocdb.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mandocdb.c,v
retrieving revision 1.201
retrieving revision 1.202
diff -Lmandocdb.c -Lmandocdb.c -u -p -r1.201 -r1.202
--- mandocdb.c
+++ mandocdb.c
@@ -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 stru
 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;
@@ -2382,27 +2361,6 @@ prepare_statements:
 #endif
 
 	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
Index: tag.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/tag.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -Ltag.c -Ltag.c -u -p -r1.9 -r1.10
--- tag.c
+++ tag.c
@@ -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:
@@ -186,28 +172,4 @@ tag_signal(int signum)
 	kill(getpid(), 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);
 }
Index: mansearch.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mansearch.c,v
retrieving revision 1.59
retrieving revision 1.60
diff -Lmansearch.c -Lmansearch.c -u -p -r1.59 -r1.60
--- mansearch.c
+++ mansearch.c
@@ -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 ma
 				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.
@@ -852,25 +838,4 @@ exprfree(struct expr *p)
 		free(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);
 }
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-10-13 15:53 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-13 15:53 mdocml: Reduce the amount of code by moving the three copies of the schwarze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).