/* * Check at compile time that something is of a particular type. * Always evaluates to 1 so you may use it easily in comparisons. */ #define typecheck(type,x) \ ({ type __dummy; \ typeof(x) __dummy2; \ (void)(&__dummy == &__dummy2); \ 1; \ })
/* * Check at compile time that 'function' is a certain type, or is a pointer * to that type (needs to use typedef for the function type.) */ #define typecheck_fn(type,function) \ ({ typeof(type) __tmp = function; \ (void)__tmp; \ })
/* * swap - swap value of @a and @b */ #define swap(a, b) \ do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
/** * container_of_const - cast a member of a structure out to the containing * structure and preserve the const-ness of the pointer * @ptr: the pointer to the member * @type: the type of the container struct this is embedded in. * @member: the name of the member within the struct. */ #define container_of_const(ptr, type, member) \ _Generic(ptr, \ const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\ default: ((type *)container_of(ptr, type, member)) \ )