diff options
Diffstat (limited to 'contrib/ntp/include/safecast.h')
-rw-r--r-- | contrib/ntp/include/safecast.h | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/contrib/ntp/include/safecast.h b/contrib/ntp/include/safecast.h new file mode 100644 index 0000000..9300463 --- /dev/null +++ b/contrib/ntp/include/safecast.h @@ -0,0 +1,34 @@ +#ifndef SAFECAST_H +#define SAFECAST_H + +#include <limits.h> +static inline int size2int_chk(size_t v) +{ + if (v > INT_MAX) + abort(); + return (int)(v); +} + +static inline int size2int_sat(size_t v) +{ + return (v > INT_MAX) ? INT_MAX : (int)v; +} + +/* Compilers can emit warning about increased alignment requirements + * when casting pointers. The impact is tricky: on machines where + * alignment is just a performance issue (x86,x64,...) this might just + * cause a performance penalty. On others, an address error can occur + * and the process dies... + * + * Still, there are many cases where the pointer arithmetic and the + * buffer alignment make sure this does not happen. OTOH, the compiler + * doesn't know this and still emits warnings. + * + * The following cast macros are going through void pointers to tell + * the compiler that there is no alignment requirement to watch. + */ +#define UA_PTR(ptype,pval) ((ptype *)(void*)(pval)) +#define UAC_PTR(ptype,pval) ((const ptype *)(const void*)(pval)) +#define UAV_PTR(ptype,pval) ((volatile ptype *)(volatile void*)(pval)) + +#endif |