Changeset 36795edf in mainline for uspace/lib/c/include/adt/list.h


Ignore:
Timestamp:
2021-03-12T19:16:51Z (3 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a85d5c6
Parents:
17fac946
Message:

Improve lists and other data structures

Provide more standard-compliant member_to_inst implementation that uses
offsetof. Avoid potential undefined behavior in list_foreach and
list_foreach_rev by avoiding assinging an unaligned pointer value. Use
size_t instead of unsigned long for list length.

File:
1 edited

Legend:

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

    r17fac946 r36795edf  
    3838
    3939#include <assert.h>
     40#include <member.h>
    4041#include <stdbool.h>
    4142#include <stddef.h>
     
    8586
    8687#define list_get_instance(link, type, member) \
    87         ((type *) (((void *)(link)) - list_link_to_void(&(((type *) NULL)->member))))
     88        member_to_inst(link, type, member)
    8889
    8990#define list_foreach(list, member, itype, iterator) \
    90         for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \
     91        for (itype *iterator = NULL; iterator == NULL; iterator = &((itype *) NULL)[1]) \
    9192                for (link_t *_link = (list).head.next; \
    9293                    iterator = list_get_instance(_link, itype, member), \
     
    9495
    9596#define list_foreach_rev(list, member, itype, iterator) \
    96         for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \
     97        for (itype *iterator = NULL; iterator == NULL; iterator = &((itype *) NULL)[1]) \
    9798                for (link_t *_link = (list).head.prev; \
    9899                    iterator = list_get_instance(_link, itype, member), \
     
    154155extern bool list_member(const link_t *, const list_t *);
    155156extern void list_splice(list_t *, link_t *);
    156 extern unsigned long list_count(const list_t *);
     157extern size_t list_count(const list_t *);
    157158
    158159/** Returns true if the link is definitely part of a list. False if not sure. */
     
    394395 *
    395396 */
    396 static inline link_t *list_nth(const list_t *list, unsigned long n)
    397 {
    398         unsigned long cnt = 0;
     397static inline link_t *list_nth(const list_t *list, size_t n)
     398{
     399        size_t cnt = 0;
    399400
    400401        link_t *link = list_first(list);
Note: See TracChangeset for help on using the changeset viewer.