Changeset feeac0d in mainline for uspace/lib/c


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
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/adt/hash_table.c

    re8d6ce2 rfeeac0d  
    227227       
    228228        /* Check for duplicates. */
    229         list_foreach(h->bucket[idx], cur) {
     229        list_foreach(h->bucket[idx], link, ht_link_t, cur_link) {
    230230                /*
    231231                 * We could filter out items using their hashes first, but
    232232                 * calling equal() might very well be just as fast.
    233233                 */
    234                 ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
    235234                if (h->op->equal(cur_link, item))
    236235                        return false;
     
    258257        size_t idx = h->op->key_hash(key) % h->bucket_cnt;
    259258
    260         list_foreach(h->bucket[idx], cur) {
    261                 ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
     259        list_foreach(h->bucket[idx], link, ht_link_t, cur_link) {
    262260                /*
    263261                 * Is this is the item we are looking for? We could have first
  • uspace/lib/c/generic/adt/list.c

    re8d6ce2 rfeeac0d  
    102102        unsigned int count = 0;
    103103       
    104         list_foreach(*list, link) {
     104        link_t *link = list_first(list);
     105        while (link != NULL) {
    105106                count++;
     107                link = list_next(link, list);
    106108        }
    107109       
  • uspace/lib/c/generic/cfg.c

    re8d6ce2 rfeeac0d  
    8383                return true;
    8484       
    85         list_foreach(data->sections, link) {
    86                 const cfg_section_t *section = cfg_section_instance(link);
    87                
     85        cfg_file_foreach(data, section) {
    8886                if (!list_empty(&section->entries))
    8987                        return false;
     
    413411const cfg_section_t *cfg_find_section(const cfg_file_t *data, const char *title)
    414412{
    415         list_foreach(data->sections, link) {
    416                 const cfg_section_t *section = cfg_section_instance(link);
    417                
     413        cfg_file_foreach(data, section) {
    418414                if (str_cmp(section->title, title) == 0)
    419415                        return section;
     
    434430const char *cfg_find_value(const cfg_section_t *section, const char *key)
    435431{
    436         list_foreach(section->entries, link) {
    437                 const cfg_entry_t *entry = cfg_entry_instance(link);
    438                
     432        cfg_section_foreach(section, entry) {
    439433                if (str_cmp(entry->key, key) == 0)
    440434                        return entry->value;
  • uspace/lib/c/generic/devman.c

    re8d6ce2 rfeeac0d  
    230230        }
    231231       
    232         match_id_t *match_id = NULL;
    233        
    234         list_foreach(match_ids->ids, link) {
    235                 match_id = list_get_instance(link, match_id_t, link);
    236                
     232        list_foreach(match_ids->ids, link, match_id_t, match_id) {
    237233                ipc_call_t answer2;
    238234                aid_t req2 = async_send_1(exch, DEVMAN_ADD_MATCH_ID,
  • uspace/lib/c/generic/pio_trace.c

    re8d6ce2 rfeeac0d  
    9595        pio_regions_t *regions = get_regions();
    9696        fibril_rwlock_read_lock(&regions->guard);
    97         list_foreach(regions->list, it) {
    98                 assert(it);
    99                 region_t *reg = region_instance(it);
    100                 assert(reg);
     97        list_foreach(regions->list, link, region_t, reg) {
    10198                if ((r >= reg->base) && (r < reg->base + reg->size)) {
    10299                        if (reg->log)
     
    131128        fibril_rwlock_write_lock(&regions->guard);
    132129        list_foreach_safe(regions->list, it, next) {
    133                 assert(it);
    134130                region_t *reg = region_instance(it);
    135                 assert(reg);
    136131                if (r >= reg->base && (r < reg->base + reg->size)) {
    137                                 list_remove(&reg->link);
    138                                 region_destroy(reg);
     132                        list_remove(&reg->link);
     133                        region_destroy(reg);
    139134                }
    140135        }
  • 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.