X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Flist.c;h=e341d45bd432e5cfe1178260eba1c276629f9b1f;hb=003ce655b7116d18c86a74c50391e54990346931;hp=4ffa8837756c36264094bf4729eea26faedc0eaf;hpb=b3907fbc6c74ddad7507d0f7abb1f5a2528cd2be;p=sliver-openvswitch.git diff --git a/lib/list.c b/lib/list.c index 4ffa88377..e341d45bd 100644 --- a/lib/list.c +++ b/lib/list.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, 2010 Nicira Networks. + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,6 @@ */ #include #include "list.h" -#include /* Initializes 'list' as an empty list. */ void @@ -36,10 +35,10 @@ list_poison(struct list *list) void list_insert(struct list *before, struct list *elem) { - elem->prev = before->prev; - elem->next = before; - before->prev->next = elem; - before->prev = elem; + elem->prev = before->prev; + elem->next = before; + before->prev->next = elem; + before->prev = elem; } /* Removes elements 'first' though 'last' (exclusive) from their current list, @@ -47,19 +46,20 @@ list_insert(struct list *before, struct list *elem) void list_splice(struct list *before, struct list *first, struct list *last) { - if (first == last) - return; - last = last->prev; - - /* Cleanly remove 'first'...'last' from its current list. */ - first->prev->next = last->next; - last->next->prev = first->prev; - - /* Splice 'first'...'last' into new list. */ - first->prev = before->prev; - last->next = before; - before->prev->next = first; - before->prev = last; + if (first == last) { + return; + } + last = last->prev; + + /* Cleanly remove 'first'...'last' from its current list. */ + first->prev->next = last->next; + last->next->prev = first->prev; + + /* Splice 'first'...'last' into new list. */ + first->prev = before->prev; + last->next = before; + before->prev->next = first; + before->prev = last; } /* Inserts 'elem' at the beginning of 'list', so that it becomes the front in @@ -67,7 +67,7 @@ list_splice(struct list *before, struct list *first, struct list *last) void list_push_front(struct list *list, struct list *elem) { - list_insert(list->next, elem); + list_insert(list->next, elem); } /* Inserts 'elem' at the end of 'list', so that it becomes the back in @@ -75,7 +75,7 @@ list_push_front(struct list *list, struct list *elem) void list_push_back(struct list *list, struct list *elem) { - list_insert(list, elem); + list_insert(list, elem); } /* Puts 'elem' in the position currently occupied by 'position'. @@ -90,21 +90,39 @@ 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) { list->prev->next = list->next->prev = list; } +/* Initializes 'dst' with the contents of 'src', compensating for moving it + * around in memory. The effect is that, if 'src' was the head of a list, now + * 'dst' is the head of a list containing the same elements. */ +void +list_move(struct list *dst, struct list *src) +{ + if (!list_is_empty(src)) { + *dst = *src; + list_moved(dst); + } else { + list_init(dst); + } +} + /* Removes 'elem' from its list and returns the element that followed it. Undefined behavior if 'elem' is not in a list. */ struct list * list_remove(struct list *elem) { - elem->prev->next = elem->next; - elem->next->prev = elem->prev; - return elem->next; + elem->prev->next = elem->next; + elem->next->prev = elem->prev; + return elem->next; } /* Removes the front element from 'list' and returns it. Undefined behavior if @@ -112,9 +130,9 @@ list_remove(struct list *elem) struct list * list_pop_front(struct list *list) { - struct list *front = list->next; - list_remove(front); - return front; + struct list *front = list->next; + list_remove(front); + return front; } /* Removes the back element from 'list' and returns it. @@ -122,27 +140,31 @@ list_pop_front(struct list *list) struct list * list_pop_back(struct list *list) { - struct list *back = list->prev; - list_remove(back); - return back; + struct list *back = list->prev; + list_remove(back); + 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_) { - assert(!list_is_empty(list)); - return list->next; + struct list *list = CONST_CAST(struct list *, list_); + + ovs_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_) { - assert(!list_is_empty(list)); - return list->prev; + struct list *list = CONST_CAST(struct list *, list_); + + ovs_assert(!list_is_empty(list)); + return list->prev; } /* Returns the number of elements in 'list'. @@ -150,17 +172,32 @@ list_back(struct list *list) size_t list_size(const struct list *list) { - const struct list *e; - size_t cnt = 0; + const struct list *e; + size_t cnt = 0; - for (e = list->next; e != list; e = e->next) - cnt++; - return cnt; + for (e = list->next; e != list; e = e->next) { + cnt++; + } + return cnt; } /* Returns true if 'list' is empty, false otherwise. */ bool list_is_empty(const struct list *list) { - return list->next == 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; }