xref: /freebsd/contrib/ntp/libntp/emalloc.c (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1c0b746e5SOllivier Robert /*
2c0b746e5SOllivier Robert  * emalloc - return new memory obtained from the system.  Belch if none.
3c0b746e5SOllivier Robert  */
4*2b15cb3dSCy Schubert #include <config.h>
5c0b746e5SOllivier Robert #include "ntp_types.h"
6c0b746e5SOllivier Robert #include "ntp_malloc.h"
7c0b746e5SOllivier Robert #include "ntp_syslog.h"
8224ba2bdSOllivier Robert #include "ntp_stdlib.h"
9c0b746e5SOllivier Robert 
10c0b746e5SOllivier Robert 
11*2b15cb3dSCy Schubert /*
12*2b15cb3dSCy Schubert  * When using the debug MS CRT allocator, each allocation stores the
13*2b15cb3dSCy Schubert  * callsite __FILE__ and __LINE__, which is then displayed at process
14*2b15cb3dSCy Schubert  * termination, to track down leaks.  We don't want all of our
15*2b15cb3dSCy Schubert  * allocations to show up as coming from emalloc.c, so we preserve the
16*2b15cb3dSCy Schubert  * original callsite's source file and line using macros which pass
17*2b15cb3dSCy Schubert  * __FILE__ and __LINE__ as parameters to these routines.
18*2b15cb3dSCy Schubert  * Other debug malloc implementations can be used by defining
19*2b15cb3dSCy Schubert  * EREALLOC_IMPL() as ports/winnt/include/config.h does.
20*2b15cb3dSCy Schubert  */
21c0b746e5SOllivier Robert 
22c0b746e5SOllivier Robert void *
23*2b15cb3dSCy Schubert ereallocz(
24*2b15cb3dSCy Schubert 	void *	ptr,
25*2b15cb3dSCy Schubert 	size_t	newsz,
26*2b15cb3dSCy Schubert 	size_t	priorsz,
27*2b15cb3dSCy Schubert 	int	zero_init
28*2b15cb3dSCy Schubert #ifdef EREALLOC_CALLSITE		/* ntp_malloc.h */
29*2b15cb3dSCy Schubert 			 ,
30*2b15cb3dSCy Schubert 	const char *	file,
31c0b746e5SOllivier Robert 	int		line
32c0b746e5SOllivier Robert #endif
33*2b15cb3dSCy Schubert 	)
34*2b15cb3dSCy Schubert {
35*2b15cb3dSCy Schubert 	char *	mem;
36*2b15cb3dSCy Schubert 	size_t	allocsz;
37*2b15cb3dSCy Schubert 
38*2b15cb3dSCy Schubert 	if (0 == newsz)
39*2b15cb3dSCy Schubert 		allocsz = 1;
40*2b15cb3dSCy Schubert 	else
41*2b15cb3dSCy Schubert 		allocsz = newsz;
42*2b15cb3dSCy Schubert 
43*2b15cb3dSCy Schubert 	mem = EREALLOC_IMPL(ptr, allocsz, file, line);
44*2b15cb3dSCy Schubert 	if (NULL == mem) {
45*2b15cb3dSCy Schubert 		msyslog_term = TRUE;
46*2b15cb3dSCy Schubert #ifndef EREALLOC_CALLSITE
47*2b15cb3dSCy Schubert 		msyslog(LOG_ERR, "fatal out of memory (%lu bytes)",
48*2b15cb3dSCy Schubert 			(u_long)newsz);
49*2b15cb3dSCy Schubert #else
50*2b15cb3dSCy Schubert 		msyslog(LOG_ERR,
51*2b15cb3dSCy Schubert 			"fatal out of memory %s line %d (%lu bytes)",
52*2b15cb3dSCy Schubert 			file, line, (u_long)newsz);
53*2b15cb3dSCy Schubert #endif
54*2b15cb3dSCy Schubert 		exit(1);
55*2b15cb3dSCy Schubert 	}
56*2b15cb3dSCy Schubert 
57*2b15cb3dSCy Schubert 	if (zero_init && newsz > priorsz)
58*2b15cb3dSCy Schubert 		zero_mem(mem + priorsz, newsz - priorsz);
59*2b15cb3dSCy Schubert 
60*2b15cb3dSCy Schubert 	return mem;
61*2b15cb3dSCy Schubert }
62*2b15cb3dSCy Schubert 
63*2b15cb3dSCy Schubert 
64*2b15cb3dSCy Schubert char *
65*2b15cb3dSCy Schubert estrdup_impl(
66*2b15cb3dSCy Schubert 	const char *	str
67*2b15cb3dSCy Schubert #ifdef EREALLOC_CALLSITE
68*2b15cb3dSCy Schubert 			   ,
69*2b15cb3dSCy Schubert 	const char *	file,
70*2b15cb3dSCy Schubert 	int		line
71*2b15cb3dSCy Schubert #endif
72*2b15cb3dSCy Schubert 	)
73*2b15cb3dSCy Schubert {
74*2b15cb3dSCy Schubert 	char *	copy;
75*2b15cb3dSCy Schubert 	size_t	bytes;
76*2b15cb3dSCy Schubert 
77*2b15cb3dSCy Schubert 	bytes = strlen(str) + 1;
78*2b15cb3dSCy Schubert 	copy = ereallocz(NULL, bytes, 0, FALSE
79*2b15cb3dSCy Schubert #ifdef EREALLOC_CALLSITE
80*2b15cb3dSCy Schubert 			 , file, line
81*2b15cb3dSCy Schubert #endif
82*2b15cb3dSCy Schubert 			 );
83*2b15cb3dSCy Schubert 	memcpy(copy, str, bytes);
84*2b15cb3dSCy Schubert 
85*2b15cb3dSCy Schubert 	return copy;
86*2b15cb3dSCy Schubert }
87*2b15cb3dSCy Schubert 
88*2b15cb3dSCy Schubert 
89*2b15cb3dSCy Schubert #if 0
90*2b15cb3dSCy Schubert #ifndef EREALLOC_CALLSITE
91*2b15cb3dSCy Schubert void *
92*2b15cb3dSCy Schubert emalloc(size_t newsz)
93*2b15cb3dSCy Schubert {
94*2b15cb3dSCy Schubert 	return ereallocz(NULL, newsz, 0, FALSE);
95*2b15cb3dSCy Schubert }
96*2b15cb3dSCy Schubert #endif
97*2b15cb3dSCy Schubert #endif
98*2b15cb3dSCy Schubert 
99