1 /* $NetBSD: emalloc.c,v 1.8 2017/04/13 20:17:42 christos Exp $ */ 2 3 /* 4 * emalloc - return new memory obtained from the system. Belch if none. 5 */ 6 #include <config.h> 7 #include "ntp_types.h" 8 #include "ntp_malloc.h" 9 #include "ntp_syslog.h" 10 #include "ntp_stdlib.h" 11 12 13 /* 14 * When using the debug MS CRT allocator, each allocation stores the 15 * callsite __FILE__ and __LINE__, which is then displayed at process 16 * termination, to track down leaks. We don't want all of our 17 * allocations to show up as coming from emalloc.c, so we preserve the 18 * original callsite's source file and line using macros which pass 19 * __FILE__ and __LINE__ as parameters to these routines. 20 * Other debug malloc implementations can be used by defining 21 * EREALLOC_IMPL() as ports/winnt/include/config.h does. 22 */ 23 24 void * 25 ereallocz( 26 void * ptr, 27 size_t newsz, 28 size_t priorsz, 29 int zero_init 30 #ifdef EREALLOC_CALLSITE /* ntp_malloc.h */ 31 , 32 const char * file, 33 int line 34 #endif 35 ) 36 { 37 char * mem; 38 size_t allocsz; 39 40 if (0 == newsz) 41 allocsz = 1; 42 else 43 allocsz = newsz; 44 45 mem = EREALLOC_IMPL(ptr, allocsz, file, line); 46 if (NULL == mem) { 47 msyslog_term = TRUE; 48 #ifndef EREALLOC_CALLSITE 49 msyslog(LOG_ERR, "fatal out of memory (%lu bytes)", 50 (u_long)newsz); 51 #else 52 msyslog(LOG_ERR, 53 "fatal out of memory %s line %d (%lu bytes)", 54 file, line, (u_long)newsz); 55 #endif 56 exit(1); 57 } 58 59 if (zero_init && newsz > priorsz) 60 zero_mem(mem + priorsz, newsz - priorsz); 61 62 return mem; 63 } 64 65 /* oreallocarray.c is licensed under the following: 66 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> 67 * 68 * Permission to use, copy, modify, and distribute this software for any 69 * purpose with or without fee is hereby granted, provided that the above 70 * copyright notice and this permission notice appear in all copies. 71 * 72 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 73 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 74 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 75 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 76 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 77 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 78 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 79 */ 80 81 /* 82 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX 83 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW 84 */ 85 #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) 86 87 void * 88 oreallocarrayxz( 89 void *optr, 90 size_t nmemb, 91 size_t size, 92 size_t extra 93 #ifdef EREALLOC_CALLSITE /* ntp_malloc.h */ 94 , 95 const char * file, 96 int line 97 #endif 98 ) 99 { 100 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && 101 nmemb > 0 && SIZE_MAX / nmemb < size) { 102 #ifndef EREALLOC_CALLSITE 103 msyslog(LOG_ERR, "fatal allocation size overflow"); 104 #else 105 msyslog(LOG_ERR, 106 "fatal allocation size overflow %s line %d", 107 file, line); 108 #endif 109 exit(1); 110 } 111 #ifndef EREALLOC_CALLSITE 112 return ereallocz(optr, extra + (size * nmemb), 0, TRUE); 113 #else 114 return ereallocz(optr, extra + (size * nmemb), 0, TRUE, file, line); 115 #endif 116 } 117 118 char * 119 estrdup_impl( 120 const char * str 121 #ifdef EREALLOC_CALLSITE 122 , 123 const char * file, 124 int line 125 #endif 126 ) 127 { 128 char * copy; 129 size_t bytes; 130 131 bytes = strlen(str) + 1; 132 copy = ereallocz(NULL, bytes, 0, FALSE 133 #ifdef EREALLOC_CALLSITE 134 , file, line 135 #endif 136 ); 137 memcpy(copy, str, bytes); 138 139 return copy; 140 } 141 142 143 #if 0 144 #ifndef EREALLOC_CALLSITE 145 void * 146 emalloc(size_t newsz) 147 { 148 return ereallocz(NULL, newsz, 0, FALSE); 149 } 150 #endif 151 #endif 152 153