src/rrdd.c et al.: Use the tree implementation from GLib 2.0.
[rrdd.git] / src / rrdd.c
index 6b382df..e82f0d9 100644 (file)
@@ -22,6 +22,8 @@
 #define RRDD_DEBUG 1
 
 #include "rrdd.h"
+#include <glib-2.0/glib.h>
+#include <rrd.h>
 
 #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);
   }