Signed-off-by: Yonggang Luo <[email protected]>
---
 mingw-w64-crt/Makefile.am           |  2 +-
 mingw-w64-crt/misc/tdestroy.c       | 24 ++++++++++++++++++++++++
 mingw-w64-crt/testcases/t_tsearch.c | 16 ++++++++++++++++
 mingw-w64-headers/crt/search.h      |  4 ++++
 4 files changed, 45 insertions(+), 1 deletion(-)
 create mode 100644 mingw-w64-crt/misc/tdestroy.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index fc49e3286..4bc09516d 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -557,7 +557,7 @@ src_libmingwex=\
   misc/mingw_wcstold.c \
   misc/mkstemp.c         misc/sleep.c           \
   misc/strnlen.c         misc/strsafe.c         \
-  misc/strtoimax.c       misc/strtoumax.c        misc/tdelete.c               
misc/tfind.c           \
+  misc/strtoimax.c       misc/strtoumax.c           misc/tdelete.c          
misc/tdestroy.c              misc/tfind.c           \
   misc/tsearch.c         misc/twalk.c           \
   misc/wcsnlen.c         misc/wcstof.c          \
   misc/wcstoimax.c       misc/wcstold.c             misc/wcstoumax.c        
misc/wctob.c                 misc/wctrans.c         \
diff --git a/mingw-w64-crt/misc/tdestroy.c b/mingw-w64-crt/misc/tdestroy.c
new file mode 100644
index 000000000..7054af7e9
--- /dev/null
+++ b/mingw-w64-crt/misc/tdestroy.c
@@ -0,0 +1,24 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#include <assert.h>
+#define _SEARCH_PRIVATE
+#include <stdlib.h>
+#include <search.h>
+
+
+/* destroy tree recursively and call free_node on each node key */
+void tdestroy(void *root, void (*free_node)(void *))
+{
+  node_t *p = (node_t *)root;
+  if (!p)
+    return;
+
+  tdestroy(p->llink , free_node);
+  tdestroy(p->rlink, free_node);
+  free_node((void*)p->key);
+  free(p);
+}
diff --git a/mingw-w64-crt/testcases/t_tsearch.c 
b/mingw-w64-crt/testcases/t_tsearch.c
index c4d8258e9..d9f692243 100644
--- a/mingw-w64-crt/testcases/t_tsearch.c
+++ b/mingw-w64-crt/testcases/t_tsearch.c
@@ -31,6 +31,11 @@ static int node_any (const void *a, const void *b)
  return 0;
 }
 
+static void
+noop_free (void *arg)
+{
+}
+
 void print_node (const void *ptr, VISIT order, int level)
 {
  const char *s = *(const char **) ptr;
@@ -71,6 +76,17 @@ int main (int argc, char **argv)
  printf("---------- tree after deletion of all nodes using tdelete() only:\n");
  twalk(root, print_node);
  printf("----------\n");
+
+ tsearch("ggg", &root, node_cmp);
+ tsearch("hhh", &root, node_cmp);
+ tsearch("iii", &root, node_cmp);
+ printf("---------- tree after insertion of 3 new nodes:\n");
+ twalk(root, print_node);
+ printf("----------\n");
+ tdestroy(root, noop_free);
+ printf("---------- tree after deletion of all nodes using tdeosty() only:\n");
+ twalk(root, print_node);
+ printf("----------\n");
  printf("Passed\n");
  return 0;
 }
diff --git a/mingw-w64-headers/crt/search.h b/mingw-w64-headers/crt/search.h
index 144b0f4ec..90af34889 100644
--- a/mingw-w64-headers/crt/search.h
+++ b/mingw-w64-headers/crt/search.h
@@ -62,6 +62,10 @@ void * __cdecl tfind (const void *, void * const *, int 
(*)(const void *, const
 void * __cdecl tsearch (const void *, void **, int (*)(const void *, const 
void *)) __MINGW_ATTRIB_NONNULL (2) __MINGW_ATTRIB_NONNULL (3);
 void __cdecl twalk (const void *, void (*)(const void *, VISIT, int));
 
+#ifdef _GNU_SOURCE
+void __cdecl tdestroy(void *, void (*)(void *))  __MINGW_ATTRIB_NONNULL (2);
+#endif
+
 #ifdef __cplusplus
 }
 #endif
-- 
2.36.1.windows.1



_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to