Changeset feeac0d in mainline for uspace/lib/c/include


Ignore:
Timestamp:
2013-09-10T16:32:35Z (12 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4982d87
Parents:
e8d6ce2
Message:

Simplify use of list_foreach.

Location:
uspace/lib/c/include
Files:
2 edited

Legend:

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

    re8d6ce2 rfeeac0d  
    6767        ((type *) (((void *)(link)) - list_link_to_void(&(((type *) NULL)->member))))
    6868
    69 #define list_foreach(list, iterator) \
    70         for (link_t *iterator = (list).head.next; \
    71             iterator != &(list).head; iterator = iterator->next)
     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.
     
    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 */
     252static inline link_t *list_next(link_t *link, const list_t *list)
     253{
     254        return (link->next == &list->head) ? NULL : link->next;
     255}
     256
     257/** Get previous item in list.
     258 *
     259 * @param link Current item link
     260 * @param list List containing @a link
     261 *
     262 * @return Previous item or NULL if @a link is the first item.
     263 */
     264static inline link_t *list_prev(link_t *link, const list_t *list)
     265{
     266        return (link->prev == &list->head) ? NULL : link->prev;
    241267}
    242268
     
    308334        unsigned int cnt = 0;
    309335       
    310         list_foreach(*list, link) {
     336        link_t *link = list_first(list);
     337        while (link != NULL) {
    311338                if (cnt == n)
    312339                        return link;
    313340               
    314341                cnt++;
     342                link = list_next(link, list);
    315343        }
    316344       
  • uspace/lib/c/include/cfg.h

    re8d6ce2 rfeeac0d  
    8080
    8181#define cfg_file_foreach(file, cur) \
    82         list_foreach((file)->sections, (cur))
     82        list_foreach((file)->sections, link, const cfg_section_t, (cur))
    8383
    8484#define cfg_section_instance(cur) \
     
    8686
    8787#define cfg_section_foreach(section, cur) \
    88         list_foreach((section)->entries, (cur))
     88        list_foreach((section)->entries, link, const cfg_entry_t, (cur))
    8989
    9090#define cfg_entry_instance(cur) \
Note: See TracChangeset for help on using the changeset viewer.