Changeset 0773396 in mainline for kernel/generic/include/adt/list.h


Ignore:
Timestamp:
2013-12-25T13:05:25Z (10 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
bc54126c
Parents:
f4a47e52 (diff), 6946f23 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

merge mainline changes

File:
1 edited

Legend:

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

    rf4a47e52 r0773396  
    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 *
     
    3737#define KERN_LIST_H_
    3838
     39#include <debug.h>
    3940#include <typedefs.h>
    4041#include <trace.h>
     
    6566
    6667#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)
     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)
    7281
    7382#define assert_link_not_used(link) \
    74         ASSERT(((link)->prev == NULL) && ((link)->next == NULL))
     83        ASSERT(!link_used(link))
    7584
    7685/** Initialize doubly-linked circular list link
     
    204213}
    205214
     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 */
     222static 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 */
     234static inline link_t *list_prev(link_t *link, const list_t *list)
     235{
     236        return (link->prev == &list->head) ? NULL : link->prev;
     237}
     238
    206239/** Split or concatenate headless doubly-linked circular list
    207240 *
     
    270303{
    271304        unsigned int cnt = 0;
    272        
    273         list_foreach(*list, link) {
     305        link_t *link;
     306       
     307        link = list_first(list);
     308        while (link != NULL) {
    274309                if (cnt == n)
    275310                        return link;
    276311               
    277312                cnt++;
     313                link = list_next(link, list);
    278314        }
    279315       
    280316        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 */
     323static 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 */
     333static 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;
    281340}
    282341
Note: See TracChangeset for help on using the changeset viewer.