00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef CU_PTR_H
00019 #define CU_PTR_H
00020
00021 #include <cu/fwd.h>
00022 #include <stddef.h>
00023 #include <stdint.h>
00024
00025 CU_BEGIN_DECLARATIONS
00026
00027
00028
00029
00030 CU_SINLINE void *cu_ptr_add(void *ptr, ptrdiff_t diff)
00031 { return (char *)ptr + diff; }
00032
00033
00034 CU_SINLINE void *cu_ptr_sub(void *ptr, ptrdiff_t diff)
00035 { return (char *)ptr - diff; }
00036
00037
00038
00039 CU_SINLINE ptrdiff_t cu_ptr_diff(void const *ptr_lhs, void const *ptr_rhs)
00040 { return (char const *)ptr_lhs - (char const *)ptr_rhs; }
00041
00042
00043 CU_SINLINE int cu_ptr_cmp(void const *ptr0, void const *ptr1)
00044 {
00045 return (char const *)ptr0 < (char const *)ptr1 ? -1
00046 : (char const *)ptr0 > (char const *)ptr1 ? 1 : 0;
00047 }
00048
00049
00050 CU_SINLINE void *cu_ptr_mulfloor(void *ptr, cu_offset_t m)
00051 { return (void *)((uintptr_t)ptr/m*m); }
00052
00053
00054 CU_SINLINE void *cu_ptr_mulceil(void *ptr, cu_offset_t m)
00055 { return (void *)(((uintptr_t)ptr + m - 1)/m*m); }
00056
00057
00058 CU_SINLINE void *cu_ptr_scal2floor(void *ptr, unsigned int b)
00059 { return (void *)((uintptr_t)ptr & ~(((uintptr_t)1 << b) - (uintptr_t)1)); }
00060
00061
00062 CU_SINLINE void *cu_ptr_scal2ceil(void *ptr, unsigned int b)
00063 {
00064 uintptr_t m = ((uintptr_t)1 << b) - (uintptr_t)1;
00065 return (void *)(((uintptr_t)ptr + m) & ~m);
00066 }
00067
00068
00069
00070 CU_SINLINE void *cu_ptr_alignfloor(void *ptr)
00071 { return cu_ptr_mulfloor(ptr, CUCONF_MAXALIGN); }
00072
00073
00074
00075 CU_SINLINE void *cu_ptr_alignceil(void *ptr)
00076 { return cu_ptr_mulceil(ptr, CUCONF_MAXALIGN); }
00077
00078
00079 CU_END_DECLARATIONS
00080
00081 #endif