LIST_REMOVE problem

Sergey Matveychuk sem at FreeBSD.org
Fri Nov 7 11:27:49 PST 2008


Hi!

I wonder how this example from queue(3) works:

      while (!LIST_EMPTY(&head)) {            /* List Deletion. */
              n1 = LIST_FIRST(&head);
              LIST_REMOVE(n1, entries);
              free(n1);
      }

If we'll take a look at code, LIST_REMOVE don't change a head pointer if 
you remove the first element:

#define LIST_FIRST(head)        ((head)->lh_first)
...
#define LIST_REMOVE(elm, field) do {                        \
         if (LIST_NEXT((elm), field) != NULL)                \
                 LIST_NEXT((elm), field)->field.le_prev =    \
                     (elm)->field.le_prev;                   \
         *(elm)->field.le_prev = LIST_NEXT((elm), field);    \
} while (0)

So n1 in the example above will point to removed (and freed) element.

-- 
Dixi.
Sem.


More information about the freebsd-hackers mailing list