57 lines
1.4 KiB
C
57 lines
1.4 KiB
C
#ifndef UCORE_UTILS_H_
|
|
#define UCORE_UTILS_H_
|
|
#include <stddef.h>
|
|
|
|
//Gnerate a compiler error if the compile time
|
|
//constant expression fails
|
|
#define STATIC_ASSERT(expr) \
|
|
do { \
|
|
enum { assert_static__ = 1/(expr) }; \
|
|
} while (0)
|
|
|
|
#ifdef DEBUG
|
|
#define TRACEF(fmt, args...)
|
|
do { \
|
|
fprintf(stdout, "%s:%d (%s)\t" fmt, __FILE__, __LINE__, __FUNCTION__, ##args);\
|
|
fflush(stdout);\
|
|
} while(0)
|
|
#else
|
|
#define TRACEF(fmt, ...)
|
|
#endif
|
|
|
|
//MAX of a and b
|
|
#define UC_MAX(a,b) \
|
|
({ __typeof__ (a) _a = (a); \
|
|
__typeof__ (b) _b = (b); \
|
|
_a > _b ? _a : _b; })
|
|
|
|
//min of a and b
|
|
#define UC_MIN(a,b) \
|
|
({ __typeof__ (a) _a = (a); \
|
|
__typeof__ (b) _b = (b); \
|
|
_a < _b ? _a : _b; })
|
|
|
|
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a))
|
|
|
|
//given 'ptr' as a pointer to a struct 'member',
|
|
//find the struct that ptr is the member of, where
|
|
//'type' is the type of the containing struct
|
|
//e.g.
|
|
//struct foo {
|
|
// int i;
|
|
// struct bar zap;
|
|
//};
|
|
// ...
|
|
//struct bar *p = ..; //the local p is a pointer to
|
|
//a 'zap' member inside a struct foo.
|
|
//give us the struct foo*:
|
|
//struct foo *f = CONTAINER_OF(p, struct foo, zap);
|
|
#define CONTAINER_OF(ptr, type, member) ({ \
|
|
typeof( ((type *)0)->member ) *__mptr = (ptr); \
|
|
(type *)( (void *)__mptr - offsetof(type, member) ); })
|
|
|
|
#define UC_ALIGN(val,align) (((val)+(align)-1UL)&~((align)-1UL))
|
|
|
|
#endif
|
|
|