Ignore:
File:
1 edited

Legend:

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

    r7856d09 r9d58539  
    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 *
     
    3737#define KERN_LIST_H_
    3838
    39 #include <debug.h>
    4039#include <typedefs.h>
    4140#include <trace.h>
     
    6665
    6766#define list_get_instance(link, type, member) \
    68         ((type *) (((void *)(link)) - list_link_to_void(&(((type *) NULL)->member))))
    69 
    70 #define list_foreach(list, member, itype, iterator) \
    71         for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \
    72             for (link_t *_link = (list).head.next; \
    73             iterator = list_get_instance(_link, itype, member), \
    74             _link != &(list).head; _link = _link->next)
    75 
    76 #define list_foreach_rev(list, member, itype, iterator) \
    77         for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \
    78             for (link_t *_link = (list).head.prev; \
    79             iterator = list_get_instance(_link, itype, member), \
    80             _link != &(list).head; _link = _link->prev)
     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)
    8172
    8273#define assert_link_not_used(link) \
    83         ASSERT(!link_used(link))
     74        ASSERT(((link)->prev == NULL) && ((link)->next == NULL))
    8475
    8576/** Initialize doubly-linked circular list link
     
    213204}
    214205
    215 /** Get next item in list.
    216  *
    217  * @param link Current item link
    218  * @param list List containing @a link
    219  *
    220  * @return Next item or NULL if @a link is the last item.
    221  */
    222 static inline link_t *list_next(link_t *link, const list_t *list)
    223 {
    224         return (link->next == &list->head) ? NULL : link->next;
    225 }
    226 
    227 /** Get previous item in list.
    228  *
    229  * @param link Current item link
    230  * @param list List containing @a link
    231  *
    232  * @return Previous item or NULL if @a link is the first item.
    233  */
    234 static inline link_t *list_prev(link_t *link, const list_t *list)
    235 {
    236         return (link->prev == &list->head) ? NULL : link->prev;
    237 }
    238 
    239206/** Split or concatenate headless doubly-linked circular list
    240207 *
     
    303270{
    304271        unsigned int cnt = 0;
    305         link_t *link;
    306        
    307         link = list_first(list);
    308         while (link != NULL) {
     272       
     273        list_foreach(*list, link) {
    309274                if (cnt == n)
    310275                        return link;
    311276               
    312277                cnt++;
    313                 link = list_next(link, list);
    314278        }
    315279       
    316280        return NULL;
    317 }
    318 
    319 /** Verify that argument type is a pointer to link_t (at compile time).
    320  *
    321  * This can be used to check argument type in a macro.
    322  */
    323 static inline const void *list_link_to_void(const link_t *link)
    324 {
    325         return link;
    326 }
    327 
    328 /** Determine if link is used.
    329  *
    330  * @param link Link
    331  * @return @c true if link is used, @c false if not.
    332  */
    333 static inline bool link_used(link_t *link)
    334 {
    335         if (link->prev == NULL && link->next == NULL)
    336                 return false;
    337 
    338         ASSERT(link->prev != NULL && link->next != NULL);
    339         return true;
    340281}
    341282
Note: See TracChangeset for help on using the changeset viewer.