X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flib%2Fkernel%2Flist.c;h=e9993cbd0a5e6295d806a566b9ce4c8d76aa299c;hb=04b1db625129936325474664d7f400b9993e9611;hp=1a1e24d42107390b0e4f70499792c814894eb50f;hpb=a5a2217b84404ff5b92d70464989f0c7cf7643c8;p=pintos-anon diff --git a/src/lib/kernel/list.c b/src/lib/kernel/list.c index 1a1e24d..e9993cb 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,39 @@ 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. + + It's not safe to treat ELEM as an element in a list after + removing it. In particular, using list_next() or list_prev() + on ELEM after removal yields undefined behavior. This means + that a naive loop to remove the elements in a list will fail: + + ** DON'T DO THIS ** + for (e = list_begin (&list); e != list_end (&list); e = list_next (e)) + { + ...do something with e... + list_remove (e); + } + ** DON'T DO THIS ** + + Here is one correct 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) {