- add user feedback
[oweals/gnunet.git] / src / util / container_heap.c
index cd4d7909aac6ef1f1c32e8b3c1da6e33ce59e520..54da89f7a28dbf008290475df7918abd9bd95dcd 100644 (file)
@@ -4,7 +4,7 @@
 
   GNUnet is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
 
   GNUnet is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
-  by the Free Software Foundation; either version 2, or (at your
+  by the Free Software Foundation; either version 3, or (at your
   option) any later version.
 
   GNUnet is distributed in the hope that it will be useful, but
   option) any later version.
 
   GNUnet is distributed in the hope that it will be useful, but
@@ -28,8 +28,9 @@
 #include "platform.h"
 #include "gnunet_util_lib.h"
 
 #include "platform.h"
 #include "gnunet_util_lib.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
 
 
-#define DEBUG 0
+#define EXTRA_CHECKS 0
 
 /**
  * Node in the heap.
 
 /**
  * Node in the heap.
@@ -103,7 +104,7 @@ struct GNUNET_CONTAINER_Heap
 };
 
 
 };
 
 
-#if DEBUG
+#if EXTRA_CHECKS
 /**
  * Check if internal invariants hold for the given node.
  *
 /**
  * Check if internal invariants hold for the given node.
  *
@@ -141,7 +142,7 @@ GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder order)
 {
   struct GNUNET_CONTAINER_Heap *heap;
 
 {
   struct GNUNET_CONTAINER_Heap *heap;
 
-  heap = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_Heap));
+  heap = GNUNET_new (struct GNUNET_CONTAINER_Heap);
   heap->order = order;
   return heap;
 }
   heap->order = order;
   return heap;
 }
@@ -350,7 +351,7 @@ GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, void *element,
 {
   struct GNUNET_CONTAINER_HeapNode *node;
 
 {
   struct GNUNET_CONTAINER_HeapNode *node;
 
-  node = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_HeapNode));
+  node = GNUNET_new (struct GNUNET_CONTAINER_HeapNode);
   node->heap = heap;
   node->element = element;
   node->cost = cost;
   node->heap = heap;
   node->element = element;
   node->cost = cost;
@@ -399,8 +400,10 @@ GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap)
     heap->root = root->left_child;
     insert_node (heap, heap->root, root->right_child);
   }
     heap->root = root->left_child;
     insert_node (heap, heap->root, root->right_child);
   }
+  if (heap->walk_pos == root)
+    heap->walk_pos = heap->root;
   GNUNET_free (root);
   GNUNET_free (root);
-#if DEBUG
+#if EXTRA_CHECKS
   GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 1));
   CHECK (heap->root);
   GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 1));
   CHECK (heap->root);
@@ -501,7 +504,7 @@ GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node)
   if (heap->walk_pos == node)
     heap->walk_pos = NULL;
   GNUNET_free (node);
   if (heap->walk_pos == node)
     heap->walk_pos = NULL;
   GNUNET_free (node);
-#if DEBUG
+#if EXTRA_CHECKS
   CHECK (heap->root);
   GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 1));
   CHECK (heap->root);
   GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 1));
@@ -522,13 +525,13 @@ GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
                                    struct GNUNET_CONTAINER_HeapNode *node,
                                    GNUNET_CONTAINER_HeapCostType new_cost)
 {
                                    struct GNUNET_CONTAINER_HeapNode *node,
                                    GNUNET_CONTAINER_HeapCostType new_cost)
 {
-#if DEBUG
+#if EXTRA_CHECKS
   GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 1));
   CHECK (heap->root);
 #endif
   remove_node (node);
   GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 1));
   CHECK (heap->root);
 #endif
   remove_node (node);
-#if DEBUG
+#if EXTRA_CHECKS
   CHECK (heap->root);
   GNUNET_assert (((heap->size == 1) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 2));
   CHECK (heap->root);
   GNUNET_assert (((heap->size == 1) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 2));
@@ -538,7 +541,7 @@ GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
     heap->root = node;
   else
     insert_node (heap, heap->root, node);
     heap->root = node;
   else
     insert_node (heap, heap->root, node);
-#if DEBUG
+#if EXTRA_CHECKS
   CHECK (heap->root);
   GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 1));
   CHECK (heap->root);
   GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
                  (heap->size == heap->root->tree_size + 1));