From a3dc9fafd0f064d33d8b70a8292656232431cfab Mon Sep 17 00:00:00 2001 From: David McMackins II Date: Wed, 30 Dec 2015 16:45:36 -0600 Subject: Add ability to free referenced data (stdlib free only) --- Makefile.am | 2 +- configure.ac | 2 +- pfxtree.c | 11 ++++++++++- pfxtree.h | 15 +++++++++++++-- pfxtree.pc | 2 +- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Makefile.am b/Makefile.am index 0c318b3..2c42d6c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -7,7 +7,7 @@ include_HEADERS = pfxtree.h lib_LTLIBRARIES = libpfxtree.la libpfxtree_la_SOURCES = pfxtree.c -libpfxtree_la_LDFLAGS = -version-info 1:0:1 +libpfxtree_la_LDFLAGS = -version-info 2:0:2 libpfxtree_la_CFLAGS = -Wall -Wextra -Wunreachable-code -ftrapv -std=c89 AM_CFLAGS = $(DEPS_CFLAGS) diff --git a/configure.ac b/configure.ac index 333178c..e4536a1 100644 --- a/configure.ac +++ b/configure.ac @@ -1,5 +1,5 @@ AC_PREREQ([2.60]) -AC_INIT([libpfxtree],[0.1.0],[support@delwink.com]) +AC_INIT([libpfxtree],[0.2.0],[support@delwink.com]) AC_CONFIG_SRCDIR([pfxtree.c]) AC_CONFIG_AUX_DIR([build-aux]) diff --git a/pfxtree.c b/pfxtree.c index f58d1ae..fa9a0e7 100644 --- a/pfxtree.c +++ b/pfxtree.c @@ -44,12 +44,21 @@ pt_new () void pt_free (PrefixTree *self) +{ + pt_deep_free (self, false); +} + +void +pt_deep_free (PrefixTree *self, bool free_data) { PrefixTree *child, *next = NULL; for (child = self->children; child != NULL; child = next) { + if (free_data && 'p' == child->type && child->data.p != NULL) + free (child->data.p); + next = child->next; - pt_free (child); + pt_deep_free (child, free_data); } free (self); diff --git a/pfxtree.h b/pfxtree.h index 45c16fa..4c2987d 100644 --- a/pfxtree.h +++ b/pfxtree.h @@ -17,8 +17,8 @@ /** * @file pfxtree.h - * @version 0.1 - * @date 10/04/2015 + * @version 0.2 + * @date 12/30/2015 * @author David McMackins II * @brief Delwink prefix tree (trie) library */ @@ -26,6 +26,8 @@ #ifndef DELWINK_PFXTREE_H #define DELWINK_PFXTREE_H +#include + #ifdef __cplusplus # define __BEGIN_DECLS extern "C" { # define __END_DECLS } @@ -77,6 +79,15 @@ pt_new (void); void pt_free (PrefixTree *self); +/** + * @brief Frees an allocated prefix tree (and its child nodes) and optionally + * its referenced data. + * @param self The tree to free. + * @param free_data Whether to free memory pointed to by the data (if pointer). + */ +void +pt_deep_free (PrefixTree *self, bool free_data); + /** * @brief Adds a new word to a prefix tree with integer data. * @param self The tree to which to add the word. diff --git a/pfxtree.pc b/pfxtree.pc index e609fe3..ef0ffed 100644 --- a/pfxtree.pc +++ b/pfxtree.pc @@ -3,7 +3,7 @@ exec_prefix = ${prefix} libdir = ${exec_prefix}/lib includedir = ${prefix}/include -Version: 0.1.0 +Version: 0.2.0 Cflags = -I${includedir} Description: Delwink prefix tree library Name: pfxtree -- cgit v1.2.3