Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Removed awful bugs noticed by Henri. And this code is 5 years old... can you
[simgrid.git] / src / xbt / fifo.c
index 3ea147b..252d2d1 100644 (file)
@@ -119,6 +119,7 @@ void *xbt_fifo_shift(xbt_fifo_t l)
  */
 void xbt_fifo_push_item(xbt_fifo_t l, xbt_fifo_item_t new)
 {
+  xbt_assert0((new->next == NULL)&&(new->prev == NULL),"Invalid item!");
   (l->count)++;
   if (l->head == NULL) {
     l->head = new;
@@ -144,7 +145,7 @@ xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
     return NULL;
 
   item = l->tail;
-
+  
   l->tail = item->prev;
   if (l->tail == NULL)
     l->head = NULL;
@@ -152,6 +153,9 @@ xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
     l->tail->next = NULL;
 
   (l->count)--;
+
+  item->prev = NULL;
+
   return item;
 }
 
@@ -163,6 +167,7 @@ xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
  */
 void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
 {
+  xbt_assert0((new->next == NULL)&&(new->prev == NULL),"Invalid item!");
   (l->count)++;
   if (l->head == NULL) {
     l->head = new;
@@ -197,6 +202,9 @@ xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
     l->head->prev = NULL;
 
   (l->count)--;
+
+  item->next = NULL;
+
   return item;
 }
 
@@ -229,28 +237,32 @@ void xbt_fifo_remove(xbt_fifo_t l, void *t)
  * \param l a list
  * \param current a bucket
  *
- * removes a the bucket \a current from the list \a l
+ * removes a bucket \a current from the list \a l. This function implicitely 
+ * assumes (and doesn't check!) that this item belongs to this list... 
  */
 void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current)
 {
   if (l->head == l->tail) {    /* special case */
-      l->head = NULL;
-      l->tail = NULL;
-      (l->count)--;
-      return;
-    }
-
-    if (current == l->head) {  /* It's the head */
-      l->head = current->next;
-      l->head->prev = NULL;
-    } else if (current == l->tail) {   /* It's the tail */
-      l->tail = current->prev;
-      l->tail->next = NULL;
-    } else {                   /* It's in the middle */
-      current->prev->next = current->next;
-      current->next->prev = current->prev;
-    }
+    xbt_assert0((current==l->head),"This item is not in the list!");
+    l->head = NULL;
+    l->tail = NULL;
     (l->count)--;
+    current->prev = current->next = NULL;
+    return;
+  }
+
+  if (current == l->head) {    /* It's the head */
+    l->head = current->next;
+    l->head->prev = NULL;
+  } else if (current == l->tail) {     /* It's the tail */
+    l->tail = current->prev;
+    l->tail->next = NULL;
+  } else {                     /* It's in the middle */
+    current->prev->next = current->next;
+    current->next->prev = current->prev;
+  }
+  (l->count)--;
+  current->prev = current->next = NULL;
 }
 
 /**