X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flib%2Fkernel%2Flist.c;h=316d9ef64cd166e6422117f9c914efde5c5fdfde;hb=f017336df083c40df490f2a88e196b3df03ecbd1;hp=1a1e24d42107390b0e4f70499792c814894eb50f;hpb=a5a2217b84404ff5b92d70464989f0c7cf7643c8;p=pintos-anon diff --git a/src/lib/kernel/list.c b/src/lib/kernel/list.c index 1a1e24d..316d9ef 100644 --- a/src/lib/kernel/list.c +++ b/src/lib/kernel/list.c @@ -31,6 +31,9 @@ elements allows us to do a little bit of checking on some operations, which can be valuable.) */ +static bool is_sorted (struct list_elem *a, struct list_elem *b, + list_less_func *less, void *aux) UNUSED; + /* Returns true if ELEM is a head, false otherwise. */ static inline bool is_head (struct list_elem *elem) @@ -217,7 +220,31 @@ list_push_back (struct list *list, struct list_elem *elem) } /* Removes ELEM from its list and returns the element that - followed it. Undefined behavior if ELEM is not in a list. */ + followed it. Undefined behavior if ELEM is not in a list. + + A list element must be treated very carefully after removing + it from its list. Calling list_next() or list_prev() on ELEM + will return the item that was previously before or after ELEM, + but, e.g., list_prev(list_next(ELEM)) is no longer ELEM! + + The list_remove() return value provides a convenient way to + iterate and remove elements from a list: + + for (e = list_begin (&list); e != list_end (&list); e = list_remove (e)) + { + ...do something with e... + } + + If you need to free() elements of the list then you need to be + more conservative. Here's an alternate strategy that works + even in that case: + + while (!list_empty (&list)) + { + struct list_elem *e = list_pop_front (&list); + ...do something with e... + } +*/ struct list_elem * list_remove (struct list_elem *elem) {