xref: /freebsd/contrib/ntp/include/ntp_malloc.h (revision 416ba5c74546f32a993436a99516d35008e9f384)
1c0b746e5SOllivier Robert /*
2c0b746e5SOllivier Robert  * Define malloc and friends.
3c0b746e5SOllivier Robert  */
4*2b15cb3dSCy Schubert #ifndef NTP_MALLOC_H
5*2b15cb3dSCy Schubert #define NTP_MALLOC_H
6c0b746e5SOllivier Robert 
7c0b746e5SOllivier Robert #ifdef HAVE_STDLIB_H
8c0b746e5SOllivier Robert # include <stdlib.h>
9*2b15cb3dSCy Schubert #else
10c0b746e5SOllivier Robert # ifdef HAVE_MALLOC_H
11c0b746e5SOllivier Robert #  include <malloc.h>
12c0b746e5SOllivier Robert # endif
13*2b15cb3dSCy Schubert #endif
14c0b746e5SOllivier Robert 
15*2b15cb3dSCy Schubert /*
16*2b15cb3dSCy Schubert  * Deal with platform differences declaring alloca()
17*2b15cb3dSCy Schubert  * This comes nearly verbatim from:
18*2b15cb3dSCy Schubert  *
19*2b15cb3dSCy Schubert  * http://www.gnu.org/software/autoconf/manual/autoconf.html#Particular-Functions
20*2b15cb3dSCy Schubert  *
21*2b15cb3dSCy Schubert  * The only modifications were to remove C++ support and guard against
22*2b15cb3dSCy Schubert  * redefining alloca.
23*2b15cb3dSCy Schubert  */
24*2b15cb3dSCy Schubert #ifdef HAVE_ALLOCA_H
25*2b15cb3dSCy Schubert # include <alloca.h>
26*2b15cb3dSCy Schubert #elif defined __GNUC__
27*2b15cb3dSCy Schubert # ifndef alloca
28*2b15cb3dSCy Schubert #  define alloca __builtin_alloca
29*2b15cb3dSCy Schubert # endif
30*2b15cb3dSCy Schubert #elif defined _AIX
31*2b15cb3dSCy Schubert # ifndef alloca
32*2b15cb3dSCy Schubert #  define alloca __alloca
33*2b15cb3dSCy Schubert # endif
34*2b15cb3dSCy Schubert #elif defined _MSC_VER
35*2b15cb3dSCy Schubert # include <malloc.h>
36*2b15cb3dSCy Schubert # ifndef alloca
37*2b15cb3dSCy Schubert #  define alloca _alloca
38*2b15cb3dSCy Schubert # endif
39*2b15cb3dSCy Schubert #else
40*2b15cb3dSCy Schubert # include <stddef.h>
41*2b15cb3dSCy Schubert void * alloca(size_t);
42*2b15cb3dSCy Schubert #endif
43*2b15cb3dSCy Schubert 
44*2b15cb3dSCy Schubert #ifdef EREALLOC_IMPL
45*2b15cb3dSCy Schubert # define EREALLOC_CALLSITE	/* preserve __FILE__ and __LINE__ */
46*2b15cb3dSCy Schubert #else
47*2b15cb3dSCy Schubert # define EREALLOC_IMPL(ptr, newsz, filenm, loc) \
48*2b15cb3dSCy Schubert 	 realloc(ptr, (newsz))
49*2b15cb3dSCy Schubert #endif
50*2b15cb3dSCy Schubert 
51*2b15cb3dSCy Schubert #ifdef HAVE_STRINGS_H
52*2b15cb3dSCy Schubert # include <strings.h>
53*2b15cb3dSCy Schubert # define zero_mem(p, s)		bzero(p, s)
54*2b15cb3dSCy Schubert #endif
55*2b15cb3dSCy Schubert 
56*2b15cb3dSCy Schubert #ifndef zero_mem
57*2b15cb3dSCy Schubert # define zero_mem(p, s)		memset(p, 0, s)
58*2b15cb3dSCy Schubert #endif
59*2b15cb3dSCy Schubert #define ZERO(var)		zero_mem(&(var), sizeof(var))
60*2b15cb3dSCy Schubert 
61*2b15cb3dSCy Schubert #endif	/* NTP_MALLOC_H */
62