Changeset 36795edf in mainline for uspace/lib/c


Ignore:
Timestamp:
2021-03-12T19:16:51Z (4 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.

Location:
uspace/lib/c
Files:
1 added
5 edited

Legend:

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

    r17fac946 r36795edf  
    5252#include <adt/list.h>
    5353#include <assert.h>
     54#include <member.h>
    5455#include <stdlib.h>
    5556#include <str.h>
  • uspace/lib/c/generic/adt/list.c

    r17fac946 r36795edf  
    4141#include <adt/list.h>
    4242#include <stdbool.h>
     43#include <stdint.h>
    4344
    4445/** Check for membership
     
    100101 * @return              Number of items in the list.
    101102 */
    102 unsigned long list_count(const list_t *list)
     103size_t list_count(const list_t *list)
    103104{
    104         unsigned long count = 0;
     105        size_t count = 0;
    105106
    106107        link_t *link = list_first(list);
  • uspace/lib/c/include/adt/hash_table.h

    r17fac946 r36795edf  
    4141#include <stdbool.h>
    4242#include <macros.h>
     43#include <member.h>
    4344
    4445/** Opaque hash table link type. */
  • 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);
  • uspace/lib/c/include/macros.h

    r17fac946 r36795edf  
    5555            | ((((uint64_t) (up)) & 0xffffffff) << 32))
    5656
    57 #define member_to_inst(ptr_member, type, member_identif) \
    58         ((type *) (((void *) (ptr_member)) - \
    59             ((void *) &(((type *) 0)->member_identif))))
    60 
    6157#define _paddname(line)     PADD_ ## line ## __
    6258#define _padd(width, line, n)  uint ## width ## _t _paddname(line) [n]
Note: See TracChangeset for help on using the changeset viewer.