Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Friday smell chase
[simgrid.git] / src / xbt / fifo.c
index df2439f..55a6ab0 100644 (file)
@@ -23,13 +23,11 @@ static xbt_mallocator_t item_mallocator = NULL;
  */
 xbt_fifo_t xbt_fifo_new(void)
 {
-  xbt_fifo_t fifo;
-  fifo = xbt_new0(struct xbt_fifo, 1);
+  xbt_fifo_t fifo = xbt_new0(struct xbt_fifo, 1);
 
   return fifo;
 }
 
-
 /** Destructor
  * \param l poor victim
  *
@@ -49,11 +47,15 @@ void xbt_fifo_free(xbt_fifo_t l)
  */
 void xbt_fifo_reset(xbt_fifo_t l)
 {
-  xbt_fifo_item_t b, tmp;
+  xbt_fifo_item_t b = xbt_fifo_get_first_item(l);
 
-  for (b = xbt_fifo_get_first_item(l); b;
-       tmp = b, b = b->next, xbt_fifo_free_item(tmp));
-  l->head = l->tail = NULL;
+  while (b) {
+    xbt_fifo_item_t tmp = b;
+    b = b->next;
+    xbt_fifo_free_item(tmp);
+  }
+  l->head = NULL;
+  l->tail = NULL;
 }
 
 /** Push
@@ -65,9 +67,7 @@ void xbt_fifo_reset(xbt_fifo_t l)
  */
 xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t)
 {
-  xbt_fifo_item_t new;
-
-  new = xbt_fifo_new_item();
+  xbt_fifo_item_t new = xbt_fifo_new_item();
   new->content = t;
 
   xbt_fifo_push_item(l, new);
@@ -83,15 +83,13 @@ xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t)
  */
 void *xbt_fifo_pop(xbt_fifo_t l)
 {
-  xbt_fifo_item_t item;
-  void *content;
-
   if (l == NULL)
     return NULL;
-  if (!(item = xbt_fifo_pop_item(l)))
+  xbt_fifo_item_t item = xbt_fifo_pop_item(l);
+  if (!item)
     return NULL;
 
-  content = item->content;
+  void *content = item->content;
   xbt_fifo_free_item(item);
   return content;
 }
@@ -105,9 +103,7 @@ void *xbt_fifo_pop(xbt_fifo_t l)
  */
 xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t)
 {
-  xbt_fifo_item_t new;
-
-  new = xbt_fifo_new_item();
+  xbt_fifo_item_t new = xbt_fifo_new_item();
   new->content = t;
   xbt_fifo_unshift_item(l, new);
   return new;
@@ -122,15 +118,13 @@ xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t)
  */
 void *xbt_fifo_shift(xbt_fifo_t l)
 {
-  xbt_fifo_item_t item;
-  void *content;
-
   if (l == NULL)
     return NULL;
-  if (!(item = xbt_fifo_shift_item(l)))
+  xbt_fifo_item_t item = xbt_fifo_shift_item(l);
+  if (!item)
     return NULL;
 
-  content = item->content;
+  void *content = item->content;
   xbt_fifo_free_item(item);
   return content;
 }
@@ -163,12 +157,10 @@ void xbt_fifo_push_item(xbt_fifo_t l, xbt_fifo_item_t new)
  */
 xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
 {
-  xbt_fifo_item_t item;
-
   if (l->tail == NULL)
     return NULL;
 
-  item = l->tail;
+  xbt_fifo_item_t item = l->tail;
 
   l->tail = item->prev;
   if (l->tail == NULL)
@@ -201,7 +193,6 @@ void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
   new->next = l->head;
   new->next->prev = new;
   l->head = new;
-  return;
 }
 
 /** Shift bucket
@@ -212,12 +203,10 @@ void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
  */
 xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
 {
-  xbt_fifo_item_t item;
-
   if (l->head == NULL)
     return NULL;
 
-  item = l->head;
+  xbt_fifo_item_t item = l->head;
 
   l->head = item->next;
   if (l->head == NULL)
@@ -236,49 +225,49 @@ xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
  * \param l
  * \param t an objet
  *
- * removes the first occurence of \a t from \a l.
+ * removes the first occurrence of \a t from \a l.
  * \warning it will not remove duplicates
  * \return 1 if an item was removed and 0 otherwise.
  */
 int xbt_fifo_remove(xbt_fifo_t l, void *t)
 {
-  xbt_fifo_item_t current, current_next;
-
+  xbt_fifo_item_t current;
+  xbt_fifo_item_t current_next;
 
   for (current = l->head; current; current = current_next) {
     current_next = current->next;
-    if (current->content != t)
-      continue;
-    /* remove the item */
-    xbt_fifo_remove_item(l, current);
-    xbt_fifo_free_item(current);
-    /* WILL NOT REMOVE DUPLICATES */
-    return 1;
+    if (current->content == t) {
+      /* remove the item */
+      xbt_fifo_remove_item(l, current);
+      xbt_fifo_free_item(current);
+      /* WILL NOT REMOVE DUPLICATES */
+      return 1;
+    }
   }
   return 0;
 }
 
-
 /**
  * \param l
  * \param t an objet
  *
- * removes all occurences of \a t from \a l.
+ * removes all occurrences of \a t from \a l.
  * \return 1 if an item was removed and 0 otherwise.
  */
 int xbt_fifo_remove_all(xbt_fifo_t l, void *t)
 {
-  xbt_fifo_item_t current, current_next;
+  xbt_fifo_item_t current;
+  xbt_fifo_item_t current_next;
   int res = 0;
 
   for (current = l->head; current; current = current_next) {
     current_next = current->next;
-    if (current->content != t)
-      continue;
-    /* remove the item */
-    xbt_fifo_remove_item(l, current);
-    xbt_fifo_free_item(current);
-    res = 1;
+    if (current->content == t){
+      /* remove the item */
+      xbt_fifo_remove_item(l, current);
+      xbt_fifo_free_item(current);
+      res = 1;
+    }
   }
   return res;
 }
@@ -287,7 +276,7 @@ int xbt_fifo_remove_all(xbt_fifo_t l, void *t)
  * \param l a list
  * \param current a bucket
  *
- * removes a bucket \a current from the list \a l. This function implicitely
+ * removes a bucket \a current from the list \a l. This function implicitly
  * 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)
@@ -297,7 +286,8 @@ void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current)
     l->head = NULL;
     l->tail = NULL;
     (l->count)--;
-    current->prev = current->next = NULL;
+    current->prev = NULL;
+    current->next = NULL;
     return;
   }
 
@@ -312,7 +302,8 @@ void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current)
     current->next->prev = current->prev;
   }
   (l->count)--;
-  current->prev = current->next = NULL;
+  current->prev = NULL;
+  current->next = NULL;
 }
 
 /**
@@ -337,18 +328,16 @@ int xbt_fifo_is_in(xbt_fifo_t f, void *content)
  * This function allows to search an item with a user provided function instead
  * of the pointer comparison used elsewhere in this module. Assume for example that you have a fifo of
  * strings. You cannot use xbt_fifo_remove() to remove, say, "TOTO" from it because internally, xbt_fifo_remove()
- * will do something like "if (item->content == "toto"), then remove it". And the pointer to the item content
- * and the pointer to "toto" will never match. As a solution, the current function provides a way to search elements
- * that are semanticaly equivalent instead of only syntaxically. So, removing "Toto" from a fifo can be
- * achieved this way:
+ * will do something like "if (item->content == "toto"), then remove it". And the pointer to the item content and the
+ * pointer to "toto" will never match. As a solution, the current function provides a way to search elements that are
+ * semantically equivalent instead of only syntactically. So, removing "Toto" from a fifo can be achieved this way:
  *
  *  @verbatim
 int my_comparison_function(void *searched, void *seen) {
   return !strcmp(searched, seen);
 }
 
-  xbt_fifo_remove_item(fifo,
-                       xbt_fifo_search_item(fifo, my_comparison_function, "Toto"));
+  xbt_fifo_remove_item(fifo, xbt_fifo_search_item(fifo, my_comparison_function, "Toto"));
 @endverbatim
  *
  * \param f a fifo list
@@ -364,7 +353,6 @@ xbt_fifo_item_t xbt_fifo_search_item(xbt_fifo_t f, int_f_pvoid_pvoid_t cmp_fun,
     item = item->next;
   }
   return NULL;
-
 }
 
 /**
@@ -394,11 +382,9 @@ void **xbt_fifo_to_array(xbt_fifo_t f)
  */
 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
 {
-  xbt_fifo_t copy = NULL;
+  xbt_fifo_t copy = xbt_fifo_new();
   xbt_fifo_item_t b;
 
-  copy = xbt_fifo_new();
-
   for (b = xbt_fifo_get_first_item(f); b; b = b->next) {
     xbt_fifo_push(copy, b->content);
   }
@@ -461,7 +447,6 @@ inline void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
 inline void xbt_fifo_free_item(xbt_fifo_item_t b)
 {
   xbt_mallocator_release(item_mallocator, b);
-  return;
 }
 
 /** Destructor
@@ -471,7 +456,6 @@ inline void xbt_fifo_freeitem(xbt_fifo_item_t b)
 {
   XBT_CWARN(xbt_fifo, "This function is deprecated. Use xbt_fifo_free_item.");
   xbt_fifo_free_item(b);
-  return;
 }
 
 /**
@@ -559,20 +543,17 @@ xbt_fifo_item_t xbt_fifo_getPrevItem(xbt_fifo_item_t i)
  * These are internal XBT functions called by xbt_preinit/postexit().
  * It can be used several times to recreate the mallocator, for example when you switch to MC mode
  */
-void xbt_fifo_preinit(void)
+void xbt_fifo_preinit()
 {
-  item_mallocator = xbt_mallocator_new(65536,
-                                       fifo_item_mallocator_new_f,
-                                       fifo_item_mallocator_free_f,
-                                       fifo_item_mallocator_reset_f);
+  item_mallocator = xbt_mallocator_new(65536, fifo_item_mallocator_new_f,
+                                       fifo_item_mallocator_free_f, fifo_item_mallocator_reset_f);
 }
 
-void xbt_fifo_postexit(void)
+void xbt_fifo_postexit()
 {
   if (item_mallocator != NULL) {
     xbt_mallocator_free(item_mallocator);
     item_mallocator = NULL;
   }
 }
-
 /* @} */