Ignore:
File:
1 edited

Legend:

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

    r0a02653 r7a0359b  
    4141/** Doubly linked list head and link type. */
    4242typedef struct link {
    43         struct link *prev;  /**< Pointer to the previous item in the list. */
    44         struct link *next;  /**< Pointer to the next item in the list. */
     43        struct link *prev;      /**< Pointer to the previous item in the list. */
     44        struct link *next;      /**< Pointer to the next item in the list. */
    4545} link_t;
    4646
     
    4848 *
    4949 * @param name Name of the new statically allocated list.
    50  *
    5150 */
    5251#define LIST_INITIALIZE(name) \
    53         link_t name = { \
    54                 .prev = &name, \
    55                 .next = &name \
    56         }
    57 
    58 #define list_get_instance(link, type, member) \
    59         ((type *) (((void *)(link)) - ((void *) &(((type *) NULL)->member))))
    60 
    61 #define list_foreach(list, iterator) \
    62         for (link_t *iterator = (list).next; \
    63             iterator != &(list); iterator = iterator->next)
     52        link_t name = { .prev = &name, .next = &name }
    6453
    6554/** Initialize doubly-linked circular list link
     
    6857 *
    6958 * @param link Pointer to link_t structure to be initialized.
    70  *
    7159 */
    7260NO_TRACE static inline void link_initialize(link_t *link)
     
    8068 * Initialize doubly-linked circular list.
    8169 *
    82  * @param list Pointer to link_t structure representing the list.
    83  *
     70 * @param head Pointer to link_t structure representing head of the list.
    8471 */
    85 NO_TRACE static inline void list_initialize(link_t *list)
     72NO_TRACE static inline void list_initialize(link_t *head)
    8673{
    87         list->prev = list;
    88         list->next = list;
     74        head->prev = head;
     75        head->next = head;
    8976}
    9077
     
    9481 *
    9582 * @param link Pointer to link_t structure to be added.
    96  * @param list Pointer to link_t structure representing the list.
    97  *
     83 * @param head Pointer to link_t structure representing head of the list.
    9884 */
    99 NO_TRACE static inline void list_prepend(link_t *link, link_t *list)
     85NO_TRACE static inline void list_prepend(link_t *link, link_t *head)
    10086{
    101         link->next = list->next;
    102         link->prev = list;
    103         list->next->prev = link;
    104         list->next = link;
     87        link->next = head->next;
     88        link->prev = head;
     89        head->next->prev = link;
     90        head->next = link;
    10591}
    10692
     
    11096 *
    11197 * @param link Pointer to link_t structure to be added.
    112  * @param list Pointer to link_t structure representing the list.
    113  *
     98 * @param head Pointer to link_t structure representing head of the list.
    11499 */
    115 NO_TRACE static inline void list_append(link_t *link, link_t *list)
     100NO_TRACE static inline void list_append(link_t *link, link_t *head)
    116101{
    117         link->prev = list->prev;
    118         link->next = list;
    119         list->prev->next = link;
    120         list->prev = link;
    121 }
    122 
    123 /** Insert item before another item in doubly-linked circular list.
    124  *
    125  */
    126 static inline void list_insert_before(link_t *link, link_t *list)
    127 {
    128         list_append(link, list);
    129 }
    130 
    131 /** Insert item after another item in doubly-linked circular list.
    132  *
    133  */
    134 static inline void list_insert_after(link_t *link, link_t *list)
    135 {
    136         list_prepend(list, link);
     102        link->prev = head->prev;
     103        link->next = head;
     104        head->prev->next = link;
     105        head->prev = link;
    137106}
    138107
     
    141110 * Remove item from doubly-linked circular list.
    142111 *
    143  * @param link Pointer to link_t structure to be removed from the list
    144  *             it is contained in.
    145  *
     112 * @param link  Pointer to link_t structure to be removed from the list it is
     113 *              contained in.
    146114 */
    147115NO_TRACE static inline void list_remove(link_t *link)
     
    156124 * Query emptiness of doubly-linked circular list.
    157125 *
    158  * @param list Pointer to link_t structure representing the list.
    159  *
     126 * @param head Pointer to link_t structure representing head of the list.
    160127 */
    161 NO_TRACE static inline int list_empty(link_t *list)
     128NO_TRACE static inline bool list_empty(link_t *head)
    162129{
    163         return (list->next == list);
     130        return head->next == head ? true : false;
    164131}
    165132
    166 /** Get head item of a list.
    167  *
    168  * @param list Pointer to link_t structure representing the list.
    169  *
    170  * @return Head item of the list.
    171  * @return NULL if the list is empty.
    172  *
    173  */
    174 static inline link_t *list_head(link_t *list)
    175 {
    176         return ((list->next == list) ? NULL : list->next);
    177 }
    178133
    179134/** Split or concatenate headless doubly-linked circular list
     
    184139 * concatenates splitted lists and splits concatenated lists.
    185140 *
    186  * @param part1 Pointer to link_t structure leading the first
    187  *              (half of the headless) list.
    188  * @param part2 Pointer to link_t structure leading the second
    189  *              (half of the headless) list.
    190  *
     141 * @param part1 Pointer to link_t structure leading the first (half of the
     142 *              headless) list.
     143 * @param part2 Pointer to link_t structure leading the second (half of the
     144 *              headless) list.
    191145 */
    192146NO_TRACE static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
    193147{
     148        link_t *hlp;
     149
    194150        part1->prev->next = part2;
    195         part2->prev->next = part1;
    196        
    197         link_t *hlp = part1->prev;
    198        
     151        part2->prev->next = part1;     
     152        hlp = part1->prev;
    199153        part1->prev = part2->prev;
    200154        part2->prev = hlp;
    201155}
     156
    202157
    203158/** Split headless doubly-linked circular list
     
    205160 * Split headless doubly-linked circular list.
    206161 *
    207  * @param part1 Pointer to link_t structure leading
    208  *              the first half of the headless list.
    209  * @param part2 Pointer to link_t structure leading
    210  *              the second half of the headless list.
    211  *
     162 * @param part1 Pointer to link_t structure leading the first half of the
     163 *              headless list.
     164 * @param part2 Pointer to link_t structure leading the second half of the
     165 *              headless list.
    212166 */
    213167NO_TRACE static inline void headless_list_split(link_t *part1, link_t *part2)
     
    220174 * Concatenate two headless doubly-linked circular lists.
    221175 *
    222  * @param part1 Pointer to link_t structure leading
    223  *              the first headless list.
    224  * @param part2 Pointer to link_t structure leading
    225  *              the second headless list.
    226  *
     176 * @param part1 Pointer to link_t structure leading the first headless list.
     177 * @param part2 Pointer to link_t structure leading the second headless list.
    227178 */
    228179NO_TRACE static inline void headless_list_concat(link_t *part1, link_t *part2)
     
    231182}
    232183
    233 /** Get n-th item of a list.
    234  *
    235  * @param list Pointer to link_t structure representing the list.
    236  * @param n    Item number (indexed from zero).
    237  *
    238  * @return n-th item of the list.
    239  * @return NULL if no n-th item found.
    240  *
    241  */
    242 static inline link_t *list_nth(link_t *list, unsigned int n)
    243 {
    244         unsigned int cnt = 0;
    245        
    246         list_foreach(*list, link) {
    247                 if (cnt == n)
    248                         return link;
    249                
    250                 cnt++;
    251         }
    252        
    253         return NULL;
    254 }
     184#define list_get_instance(link, type, member) \
     185        ((type *)(((uint8_t *)(link)) - ((uint8_t *)&(((type *)NULL)->member))))
    255186
    256 extern int list_member(const link_t *, const link_t *);
    257 extern void list_concat(link_t *, link_t *);
    258 extern unsigned int list_count(const link_t *);
     187extern bool list_member(const link_t *link, const link_t *head);
     188extern void list_concat(link_t *head1, link_t *head2);
    259189
    260190#endif
Note: See TracChangeset for help on using the changeset viewer.