crt: Implement tdestroy

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
Signed-off-by: LIU Hao <lh_mouse@126.com>
This commit is contained in:
Yonggang Luo 2022-10-23 21:47:10 +08:00 committed by LIU Hao
parent c0d9ec0f81
commit 3510dea359
4 changed files with 45 additions and 1 deletions

View File

@ -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 \

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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