Ignore:
File:
1 edited

Legend:

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

    r062d900 r07525cd  
    11/*
    22 * Copyright (c) 2001-2004 Jakub Jermar
    3  * Copyright (c) 2011 Jiri Svoboda
     3 * Copyright (c) 2013 Jiri Svoboda
    44 * All rights reserved.
    55 *
     
    6565
    6666#define list_get_instance(link, type, member) \
    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)
     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)
    7274
    7375/** Unlike list_foreach(), allows removing items while traversing a list.
    74  * 
     76 *
    7577 * @code
    7678 * list_t mylist;
     
    238240static inline link_t *list_last(list_t *list)
    239241{
    240         return ((list->head.prev == &list->head) ? NULL : list->head.prev);
     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 */
     253static 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 */
     266static inline link_t *list_prev(link_t *link, const list_t *list)
     267{
     268        return (link->prev == &list->head) ? NULL : link->prev;
    241269}
    242270
     
    308336        unsigned int cnt = 0;
    309337       
    310         list_foreach(*list, link) {
     338        link_t *link = list_first(list);
     339        while (link != NULL) {
    311340                if (cnt == n)
    312341                        return link;
    313342               
    314343                cnt++;
     344                link = list_next(link, list);
    315345        }
    316346       
    317347        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 */
     354static inline const void *list_link_to_void(const link_t *link)
     355{
     356        return link;
    318357}
    319358
Note: See TracChangeset for help on using the changeset viewer.