ofp-util: Work on decoding OF1.1 flow_mods.
[openvswitch] / lib / list.c
index 6d5f6fe069fb9acec22f5c657777ed718ba40b8f..71790cc373e78a393e362f257ea10c540102556d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010, 2011 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -90,7 +90,11 @@ list_replace(struct list *element, const struct list *position)
 }
 
 /* Adjusts pointers around 'list' to compensate for 'list' having been moved
- * around in memory (e.g. as a consequence of realloc()). */
+ * around in memory (e.g. as a consequence of realloc()).
+ *
+ * This always works if 'list' is a member of a list, or if 'list' is the head
+ * of a non-empty list.  It fails badly, however, if 'list' is the head of an
+ * empty list; just use list_init() in that case. */
 void
 list_moved(struct list *list)
 {
@@ -127,20 +131,24 @@ list_pop_back(struct list *list)
     return back;
 }
 
-/* Returns the front element in 'list'.
-   Undefined behavior if 'list' is empty. */
+/* Returns the front element in 'list_'.
+   Undefined behavior if 'list_' is empty. */
 struct list *
-list_front(struct list *list)
+list_front(const struct list *list_)
 {
+    struct list *list = (struct list *) list_;
+
     assert(!list_is_empty(list));
     return list->next;
 }
 
-/* Returns the back element in 'list'.
-   Undefined behavior if 'list' is empty. */
+/* Returns the back element in 'list_'.
+   Undefined behavior if 'list_' is empty. */
 struct list *
-list_back(struct list *list)
+list_back(const struct list *list_)
 {
+    struct list *list = (struct list *) list_;
+
     assert(!list_is_empty(list));
     return list->prev;
 }
@@ -164,3 +172,17 @@ list_is_empty(const struct list *list)
 {
     return list->next == list;
 }
+
+/* Returns true if 'list' has exactly 1 element, false otherwise. */
+bool
+list_is_singleton(const struct list *list)
+{
+    return list_is_short(list) && !list_is_empty(list);
+}
+
+/* Returns true if 'list' has 0 or 1 elements, false otherwise. */
+bool
+list_is_short(const struct list *list)
+{
+    return list->next == list->prev;
+}