toys

toys.git
git clone git://git.lenczewski.org/toys.git
Log | Files | Refs | README | LICENSE

utils.h (991B)


      1 #ifndef UTILS_H
      2 #define UTILS_H
      3 
      4 #include <stdalign.h>
      5 #include <stddef.h>
      6 #include <stdint.h>
      7 
      8 #define KiB(v) (1024ull * (v))
      9 #define MiB(v) (1024 * KiB(v))
     10 #define GiB(v) (1024 * MiB(v))
     11 #define TiB(v) (1024 * GiB(v))
     12 
     13 #define ARRLEN(arr) (sizeof (arr) / sizeof (arr)[0])
     14 
     15 /* we define an "intrusive" list, meaning that out data struct contains a
     16  * list_node member. this allows a type-generic list implementation, but
     17  * necessitates need a way to get from a pointer to this list_node, to a
     18  * pointer to the parent struct
     19  */
     20 #define TO_PARENT(ptr, T, member) \
     21 	((T *) ((uintptr_t) (ptr) - offsetof(T, member)))
     22 
     23 /* helper macros to allow us to use bitwise tricks to quickly and efficiently
     24  * calculate aligned addresses and sizes.
     25  */
     26 #define IS_POW2(v) (((v) & ((v) - 1)) == 0)
     27 #define IS_ALIGNED(v, align) (((v) & ((align) - 1)) == 0)
     28 #define ALIGN_PREV(v, align) ((v) & ~((align) - 1))
     29 #define ALIGN_NEXT(v, align) ALIGN_PREV(((v) + ((align) - 1)), (align))
     30 
     31 #endif /* UTILS_H */