freebsd-skq/contrib/ntp/include/safecast.h
delphij 31ece5769d MFV r293415:
ntp 4.2.8p5

Reviewed by:	cy, roberto
Relnotes:	yes
Differential Revision:	https://reviews.freebsd.org/D4828
2016-01-08 15:53:48 +00:00

35 lines
1.0 KiB
C

#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