Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/include/adt/list.h

    r07525cd r062d900  
    11/*
    22 * Copyright (c) 2001-2004 Jakub Jermar
    3  * Copyright (c) 2013 Jiri Svoboda
     3 * Copyright (c) 2011 Jiri Svoboda
    44 * All rights reserved.
    55 *
     
    6565
    6666#define list_get_instance(link, type, member) \
    67         ((type *) (((void *)(link)) - list_link_to_void(&(((type *) NULL)->member))))
    68 
    69 #define list_foreach(list, member, itype, iterator) \
    70         for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \
    71             for (link_t *_link = (list).head.next; \
    72             iterator = list_get_instance(_link, itype, member), \
    73             _link != &(list).head; _link = _link->next)
     67        ((type *) (((void *)(link)) - ((void *) &(((type *) NULL)->member))))
     68
     69#define list_foreach(list, iterator) \
     70        for (link_t *iterator = (list).head.next; \
     71            iterator != &(list).head; iterator = iterator->next)
    7472
    7573/** Unlike list_foreach(), allows removing items while traversing a list.
    76  *
     74 * 
    7775 * @code
    7876 * list_t mylist;
     
    240238static inline link_t *list_last(list_t *list)
    241239{
    242         return (list->head.prev == &list->head) ? NULL : list->head.prev;
    243 }
    244 
    245 /** Get next item in list.
    246  *
    247  * @param link Current item link
    248  * @param list List containing @a link
    249  *
    250  * @return Next item or NULL if @a link is the last item.
    251  *
    252  */
    253 static inline link_t *list_next(link_t *link, const list_t *list)
    254 {
    255         return (link->next == &list->head) ? NULL : link->next;
    256 }
    257 
    258 /** Get previous item in list.
    259  *
    260  * @param link Current item link
    261  * @param list List containing @a link
    262  *
    263  * @return Previous item or NULL if @a link is the first item.
    264  *
    265  */
    266 static inline link_t *list_prev(link_t *link, const list_t *list)
    267 {
    268         return (link->prev == &list->head) ? NULL : link->prev;
     240        return ((list->head.prev == &list->head) ? NULL : list->head.prev);
    269241}
    270242
     
    336308        unsigned int cnt = 0;
    337309       
    338         link_t *link = list_first(list);
    339         while (link != NULL) {
     310        list_foreach(*list, link) {
    340311                if (cnt == n)
    341312                        return link;
    342313               
    343314                cnt++;
    344                 link = list_next(link, list);
    345315        }
    346316       
    347317        return NULL;
    348 }
    349 
    350 /** Verify that argument type is a pointer to link_t (at compile time).
    351  *
    352  * This can be used to check argument type in a macro.
    353  */
    354 static inline const void *list_link_to_void(const link_t *link)
    355 {
    356         return link;
    357318}
    358319
Note: See TracChangeset for help on using the changeset viewer.