Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/adt/list.h

    r9d58539 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#define assert_link_not_used(link) \
     
    204206}
    205207
     208/** Get next item in list.
     209 *
     210 * @param link Current item link
     211 * @param list List containing @a link
     212 *
     213 * @return Next item or NULL if @a link is the last item.
     214 */
     215static inline link_t *list_next(link_t *link, const list_t *list)
     216{
     217        return (link->next == &list->head) ? NULL : link->next;
     218}
     219
     220/** Get previous item in list.
     221 *
     222 * @param link Current item link
     223 * @param list List containing @a link
     224 *
     225 * @return Previous item or NULL if @a link is the first item.
     226 */
     227static inline link_t *list_prev(link_t *link, const list_t *list)
     228{
     229        return (link->prev == &list->head) ? NULL : link->prev;
     230}
     231
    206232/** Split or concatenate headless doubly-linked circular list
    207233 *
     
    270296{
    271297        unsigned int cnt = 0;
    272        
    273         list_foreach(*list, link) {
     298        link_t *link;
     299       
     300        link = list_first(list);
     301        while (link != NULL) {
    274302                if (cnt == n)
    275303                        return link;
    276304               
    277305                cnt++;
     306                link = list_next(link, list);
    278307        }
    279308       
    280309        return NULL;
     310}
     311
     312/** Verify that argument type is a pointer to link_t (at compile time).
     313 *
     314 * This can be used to check argument type in a macro.
     315 */
     316static inline const void *list_link_to_void(const link_t *link)
     317{
     318        return link;
    281319}
    282320
Note: See TracChangeset for help on using the changeset viewer.