X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=lib%2Flist.c;h=a236f8c73bfaf4232c9331dd46ab216f3f6f23ca;hb=7593daa2473c464cd179420b9c7f68989044fdee;hp=0f4f2f84b91618cf95b6525e0aef9392d7d198e5;hpb=0574c613cd00c03d287316a4c78c797ba0beea51;p=openvswitch diff --git a/lib/list.c b/lib/list.c index 0f4f2f84..a236f8c7 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 Nicira Networks. * * 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) { @@ -168,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; +}