2 * emalloc - return new memory obtained from the system. Belch if none.
6 #include "ntp_malloc.h"
7 #include "ntp_syslog.h"
8 #include "ntp_stdlib.h"
12 * When using the debug MS CRT allocator, each allocation stores the
13 * callsite __FILE__ and __LINE__, which is then displayed at process
14 * termination, to track down leaks. We don't want all of our
15 * allocations to show up as coming from emalloc.c, so we preserve the
16 * original callsite's source file and line using macros which pass
17 * __FILE__ and __LINE__ as parameters to these routines.
18 * Other debug malloc implementations can be used by defining
19 * EREALLOC_IMPL() as ports/winnt/include/config.h does.
28 #ifdef EREALLOC_CALLSITE /* ntp_malloc.h */
43 mem = EREALLOC_IMPL(ptr, allocsz, file, line);
46 #ifndef EREALLOC_CALLSITE
47 msyslog(LOG_ERR, "fatal out of memory (%lu bytes)",
51 "fatal out of memory %s line %d (%lu bytes)",
52 file, line, (u_long)newsz);
57 if (zero_init && newsz > priorsz)
58 zero_mem(mem + priorsz, newsz - priorsz);
63 /* oreallocarray.c is licensed under the following:
64 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
66 * Permission to use, copy, modify, and distribute this software for any
67 * purpose with or without fee is hereby granted, provided that the above
68 * copyright notice and this permission notice appear in all copies.
70 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
71 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
72 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
73 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
74 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
75 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
76 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
82 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
83 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
85 #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
92 #ifdef EREALLOC_CALLSITE /* ntp_malloc.h */
99 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
100 nmemb > 0 && SIZE_MAX / nmemb < size) {
101 #ifndef EREALLOC_CALLSITE
102 msyslog(LOG_ERR, "fatal allocation size overflow");
105 "fatal allocation size overflow %s line %d",
110 #ifndef EREALLOC_CALLSITE
111 return ereallocz(optr, (size * nmemb), 0, FALSE);
113 return ereallocz(optr, (size * nmemb), 0, FALSE, file, line);
120 #ifdef EREALLOC_CALLSITE
130 bytes = strlen(str) + 1;
131 copy = ereallocz(NULL, bytes, 0, FALSE
132 #ifdef EREALLOC_CALLSITE
136 memcpy(copy, str, bytes);
143 #ifndef EREALLOC_CALLSITE
145 emalloc(size_t newsz)
147 return ereallocz(NULL, newsz, 0, FALSE);