From 7e808d96454d9490014c90a8093d5f2a67ac3e3e Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 22 Jun 2008 14:50:46 +0200 Subject: [PATCH] src/rrdd.c et al.: Use the tree implementation from GLib 2.0. --- configure.ac | 30 +++++++++++++++++++++++++++--- src/Makefile.am | 3 ++- src/rrdd.c | 32 +++++++++----------------------- src/rrdd.h | 3 --- 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/configure.ac b/configure.ac index 80dcb94..d94114b 100644 --- a/configure.ac +++ b/configure.ac @@ -6,10 +6,11 @@ AC_LANG(C) AC_PREFIX_DEFAULT("/opt/rrdd") -AC_PROG_CC AC_PROG_CPP +AC_PROG_CC +AM_PROG_CC_C_O AC_PROG_INSTALL - +PKG_PROG_PKG_CONFIG AC_LIBLTDL_CONVENIENCE AC_SUBST(LTDLINCL) @@ -19,6 +20,29 @@ AC_PROG_LIBTOOL AC_CONFIG_SUBDIRS(libltdl) AC_HEADER_STDC -AC_CHECK_HEADERS(avl.h) + +$PKG_CONFIG --exists glib-2.0 2>/dev/null +if test "$?" != "0" +then + AC_MSG_ERROR("Cannot find the glib-2.0 library.") +fi +GLIB_CPPFLAGS=`$PKG_CONFIG --cflags glib-2.0` +GLIB_LDADD=`$PKG_CONFIG --libs glib-2.0` +AC_SUBST(GLIB_CPPFLAGS) +AC_SUBST(GLIB_LDADD) + +SAVE_CPPFLAGS="$CPPFLAGS" +SAVE_LDFLAGS="$LDFLAGS" +CPPFLAGS="$CPPFLAGS $GLIB_CPPFLAGS" +LDFLAGS="$LDFLAGS $GLIB_LDADD" + +AC_CHECK_HEADERS(glib-2.0/glib.h) +AC_CHECK_LIB(glib-2.0, g_tree_new, [have_libglib_2_0="yes"], [have_libglib_2_0="no"]) + +CPPFLAGS="$SAVE_CPPFLAGS" +LDFLAGS="$SAVE_LDFLAGS" + +AC_CHECK_HEADERS(rrd.h) +AC_CHECK_LIB(rrd_th, rrd_update_r, [have_librrd_th="yes"], [have_librrd_th="no"]) AC_OUTPUT(Makefile src/Makefile) diff --git a/src/Makefile.am b/src/Makefile.am index 2fce8ad..b97c84d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,8 @@ rrdc_SOURCES = rrdc.c rrdd.h rrdc.h rrdc_LDADD = librrdc.la rrdd_SOURCES = rrdd.c rrdd.h -rrdd_LDADD = -lavl -lrrd_th +rrdd_CPPFLAGS = $(GLIB_CPPFLAGS) +rrdd_LDADD = $(GLIB_LDADD) -lrrd_th all-local: rrdd.1 diff --git a/src/rrdd.c b/src/rrdd.c index 6b382df..e82f0d9 100644 --- a/src/rrdd.c +++ b/src/rrdd.c @@ -22,6 +22,8 @@ #define RRDD_DEBUG 1 #include "rrdd.h" +#include +#include #if RRDD_DEBUG # define RRDD_LOG(severity, ...) do { fprintf (stderr, __VA_ARGS__); fprintf (stderr, "\n"); } while (0) @@ -69,7 +71,7 @@ static pthread_mutex_t connetion_threads_lock = PTHREAD_MUTEX_INITIALIZER; static int connetion_threads_num = 0; /* Cache stuff */ -static avl_tree_t *cache_tree = NULL; +static GTree *cache_tree = NULL; static cache_item_t *cache_queue_head = NULL; static cache_item_t *cache_queue_tail = NULL; static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER; @@ -198,10 +200,7 @@ static int handle_request_update (int fd, /* {{{ */ time_t now; - avl_node_t *node; - cache_item_t ci_temp; cache_item_t *ci; - char answer[4096]; now = time (NULL); @@ -214,12 +213,10 @@ static int handle_request_update (int fd, /* {{{ */ file = buffer_ptr; buffer_ptr += strlen (file) + 1; - ci_temp.file = file; - pthread_mutex_lock (&cache_lock); - node = avl_search (cache_tree, (void *) &ci_temp); - if (node == NULL) + ci = g_tree_lookup (cache_tree, file); + if (ci == NULL) { ci = (cache_item_t *) malloc (sizeof (cache_item_t)); if (ci == NULL) @@ -244,22 +241,11 @@ static int handle_request_update (int fd, /* {{{ */ ci->last_flush_time = now; ci->flags = CI_FLAGS_IN_TREE; - if (avl_insert (cache_tree, (void *) ci) == NULL) - { - pthread_mutex_unlock (&cache_lock); - RRDD_LOG (LOG_ERR, "handle_request_update: avl_insert failed."); - free (ci->file); - free (ci); - return (-1); - } + g_tree_insert (cache_tree, (void *) ci->file, (void *) ci); - RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new AVL node %s.", + RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new tree node %s.", ci->file); } - else /* if (ci != NULL) */ - { - ci = (cache_item_t *) node->item; - } assert (ci != NULL); while (*buffer_ptr != 0) @@ -787,10 +773,10 @@ static int daemonize (void) /* {{{ */ openlog ("rrdd", LOG_PID, LOG_DAEMON); - cache_tree = avl_alloc_tree (cache_tree_compare, cache_tree_free); + cache_tree = g_tree_new ((GCompareFunc) strcmp); if (cache_tree == NULL) { - RRDD_LOG (LOG_ERR, "daemonize: avl_alloc_tree failed."); + RRDD_LOG (LOG_ERR, "daemonize: g_tree_new failed."); return (-1); } diff --git a/src/rrdd.h b/src/rrdd.h index 52d5066..21801b3 100644 --- a/src/rrdd.h +++ b/src/rrdd.h @@ -69,9 +69,6 @@ #include #include -#include -#include - #include "config.h" #define RRDD_SOCK_PATH "unix:/tmp/rrdd.sock" -- 2.11.0