00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef CU_UTIL_H
00019 #define CU_UTIL_H
00020
00021 #include <cu/fwd.h>
00022
00023 CU_BEGIN_DECLARATIONS
00024
00025
00026
00027 #define CU_SWAP(value_t, x, y) \
00028 do { \
00029 value_t cuL_tmp = (x); \
00030 (x) = (y); \
00031 (y) = cuL_tmp; \
00032 } while (0)
00033
00034 #define CU_RETURN_UNLESS_FIRST_CALL \
00035 do { \
00036 static int cuL_done = 0; \
00037 if (cuL_done) \
00038 return; \
00039 cuL_done = 1; \
00040 } while (0)
00041
00042
00043
00044 #define cu_ptr_context(type, field, ptr) \
00045 ((type *)((char *)(ptr) - offsetof(type, field)))
00046
00047
00048
00049
00050
00051 union cuP_alignment_u {
00052 double cuP_dummy_0;
00053 intmax_t cuP_dummy_1;
00054 void *cuP_dummy_2;
00055 void (*cuP_dummy_3)();
00056 };
00057 struct cuP_alignment_test_s {
00058 char ch;
00059 union cuP_alignment_u aligner;
00060 };
00061 #define CU_FULL_ALIGNMENT \
00062 (sizeof(struct cuP_alignment_test_s) - sizeof(union cuP_alignment_u))
00063
00064 #define cu_aligned_floor(size) \
00065 ((size)/CU_FULL_ALIGNMENT*CU_FULL_ALIGNMENT)
00066 #define cu_aligned_ceil(size) \
00067 (((size) + CU_FULL_ALIGNMENT - 1)/CU_FULL_ALIGNMENT*CU_FULL_ALIGNMENT)
00068 #define CU_ALIGNED_SIZEOF(type) cu_aligned_ceil(sizeof(type))
00069 #define CU_ALIGNED_PTR_END(ptr) \
00070 ((void *)((char*)(ptr) + cu_aligned_ceil(sizeof(*ptr))))
00071 #define CU_ALIGNED_PTR_FROM_END(ptr_t, ptr) \
00072 ((ptr_t)((char*)(ptr) - cu_aligned_ceil(sizeof(*(ptr_t)NULL))))
00073 #define CU_ALIGNED_MARG_END(ptr_t, ptr) \
00074 CU_ALIGNED_PTR_END(CU_MARG(ptr_t, ptr))
00075
00076
00077 CU_END_DECLARATIONS
00078
00079 #endif