public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Ulf Hermann <ulf.hermann@qt.io>
To: <elfutils-devel@sourceware.org>
Subject: [PATCH] Add our own tdestroy if search.h exposes a node_t struct
Date: Thu, 04 May 2017 09:53:00 -0000	[thread overview]
Message-ID: <bd6a55d0-d440-66f0-f3c4-8b0d1f139cb6@qt.io> (raw)

tdestroy is not necessarily available from search.h, but we need it.
gnulib cannot help us here as it will detect search.h to be available
and functional in that case. However, some search.h expose a node_t
struct which can be used to implement tdestroy. If that is the case, add
an implementation to libgnu.a.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
---
 ChangeLog          |  5 +++++
 configure.ac       | 21 +++++++++++++++++++++
 libgnu/ChangeLog   |  5 +++++
 libgnu/Makefile.am |  6 +++++-
 libgnu/tdestroy.c  | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 83 insertions(+), 1 deletion(-)
 create mode 100644 libgnu/tdestroy.c

diff --git a/ChangeLog b/ChangeLog
index aa0759c..5a86247 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* configure.ac: Check for tdestroy and node_t. Declare tdestroy in
+	config.h if tdestroy is not available from search.h, but node_t is.
+
+2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* configure.ac: Check for unlocked I/O functions.
 
 2017-04-28  Ulf Hermann  <ulf.hermann@qt.io>
diff --git a/configure.ac b/configure.ac
index bfdc53f..88b9d0a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -610,6 +610,27 @@ AC_CHECK_DECLS([feof_unlocked, ferror_unlocked, fputc_unlocked, fputs_unlocked,
 		fwrite_unlocked, putc_unlocked, putchar_unlocked],
 		[], [], [[#include <stdio.h>]])
 
+AC_CHECK_DECLS([tdestroy], [], [],
+	       [#include <search.h>])
+if test "x$ac_cv_have_decl_tdestroy" != "xyes"; then
+	AC_CHECK_MEMBERS([node_t.key, node_t.rlink, node_t.llink],
+			 [have_node_t="yes"], [have_node_t="no"],
+			 [#define _SEARCH_PRIVATE
+			  #include <search.h>])
+	if test "x$have_node_t" = "xyes"; then
+		AC_DEFINE([USE_PRIVATE_TDESTROY], [1], [Implement tdestroy using private node_t from search.h])
+	fi
+fi
+AM_CONDITIONAL(USE_PRIVATE_TDESTROY, [test "x$have_node_t" = "xyes"])
+
+AH_VERBATIM([USE_PRIVATE_TDESTROY], [
+/* Declare tdestroy here if it is not available from a system header. */
+#undef USE_PRIVATE_TDESTROY
+#ifdef USE_PRIVATE_TDESTROY
+void tdestroy(void *root, void (*free_node)(void *nodep));
+#endif
+])
+
 dnl Check if we have <linux/bpf.h> for EM_BPF disassembly.
 AC_CHECK_HEADERS(linux/bpf.h)
 AM_CONDITIONAL(HAVE_LINUX_BPF_H, [test "x$ac_cv_header_linux_bpf_h" = "xyes"])
diff --git a/libgnu/ChangeLog b/libgnu/ChangeLog
index 3394de6..7b146b6 100644
--- a/libgnu/ChangeLog
+++ b/libgnu/ChangeLog
@@ -1,5 +1,10 @@
 2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* Makefile.am: Use our own implementation of tdestroy if we have to.
+	* tdestroy.c: New file.
+
+2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* Makefile.am: If GNU basename is unavailable add our own
 	implementation.
 	* basename-gnu.c: New file.
diff --git a/libgnu/Makefile.am b/libgnu/Makefile.am
index 32c9aa7..99abab0 100644
--- a/libgnu/Makefile.am
+++ b/libgnu/Makefile.am
@@ -36,7 +36,7 @@ MOSTLYCLEANFILES =
 MOSTLYCLEANDIRS =
 BUILT_SOURCES =
 EXTRA_DIST = endian.in.h byteswap.in.h sys_mman.win32.h mman_win32.c sysconf_win32.c ar.in.h features.in.h \
-             stdio_ext.in.h fts.in.h basename-gnu.c
+             stdio_ext.in.h fts.in.h basename-gnu.c tdestroy.c
 CLEANFILES =
 SUFFIXES =
 
@@ -108,3 +108,7 @@ endif
 if !HAVE_BASENAME
 libgnu_a_SOURCES += basename-gnu.c
 endif
+
+if USE_PRIVATE_TDESTROY
+libgnu_a_SOURCES += tdestroy.c
+endif
diff --git a/libgnu/tdestroy.c b/libgnu/tdestroy.c
new file mode 100644
index 0000000..d14b875
--- /dev/null
+++ b/libgnu/tdestroy.c
@@ -0,0 +1,47 @@
+/* tdestroy, on systems where node is exposed from search.h
+   Copyright (C) 2017 The Qt Company Ltd.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+     * the GNU Lesser General Public License as published by the Free
+       Software Foundation; either version 3 of the License, or (at
+       your option) any later version
+
+   or
+
+     * the GNU General Public License as published by the Free
+       Software Foundation; either version 2 of the License, or (at
+       your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include <stdlib.h>
+#define _SEARCH_PRIVATE
+#include <search.h>
+
+void
+tdestroy (void *vroot, void (*free_node)(void *nodep))
+{
+  if (vroot == NULL)
+    return;
+
+  node_t *root = (node_t *) vroot;
+  tdestroy (root->llink, free_node);
+  tdestroy (root->rlink, free_node);
+  free_node ((void *) root->key);
+  free (root);
+}
+
-- 
2.1.4

                 reply	other threads:[~2017-05-04  9:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bd6a55d0-d440-66f0-f3c4-8b0d1f139cb6@qt.io \
    --to=ulf.hermann@qt.io \
    --cc=elfutils-devel@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).