Changeset 36795edf in mainline for kernel/generic/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
  • kernel/generic/include/adt/list.h

    r17fac946 r36795edf  
    3838
    3939#include <assert.h>
     40#include <macros.h>
     41#include <member.h>
    4042#include <stdbool.h>
    4143#include <stddef.h>
     44#include <stdint.h>
    4245#include <trace.h>
    4346
     
    8992
    9093#define list_get_instance(link, type, member) \
    91         ((type *) (((void *)(link)) - list_link_to_void(&(((type *) NULL)->member))))
     94        member_to_inst(link, type, member)
    9295
    9396#define list_foreach(list, member, itype, iterator) \
    94         for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \
     97        for (itype *iterator = NULL; iterator == NULL; iterator = &((itype *) NULL)[1]) \
    9598                for (link_t *_link = (list).head.next; \
    9699                    iterator = list_get_instance(_link, itype, member), \
     
    98101
    99102#define list_foreach_rev(list, member, itype, iterator) \
    100         for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \
     103        for (itype *iterator = NULL; iterator == NULL; iterator = &((itype *) NULL)[1]) \
    101104                for (link_t *_link = (list).head.prev; \
    102105                    iterator = list_get_instance(_link, itype, member), \
     
    375378 *
    376379 */
    377 static inline link_t *list_nth(const list_t *list, unsigned long n)
    378 {
    379         unsigned long cnt = 0;
     380static inline link_t *list_nth(const list_t *list, size_t n)
     381{
     382        size_t cnt = 0;
    380383
    381384        link_t *link = list_first(list);
     
    391394}
    392395
    393 /** Verify that argument type is a pointer to link_t (at compile time).
    394  *
    395  * This can be used to check argument type in a macro.
    396  */
    397 static inline const void *list_link_to_void(const link_t *link)
    398 {
    399         return link;
    400 }
    401 
    402396/** Determine if link is used.
    403397 *
Note: See TracChangeset for help on using the changeset viewer.