1 /* $NetBSD: emalloc.c,v 1.7 2016/01/08 21:35:38 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 oreallocarray( 89 void *optr, 90 size_t nmemb, 91 size_t size 92 #ifdef EREALLOC_CALLSITE /* ntp_malloc.h */ 93 , 94 const char * file, 95 int line 96 #endif 97 ) 98 { 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"); 103 #else 104 msyslog(LOG_ERR, 105 "fatal allocation size overflow %s line %d", 106 file, line); 107 #endif 108 exit(1); 109 } 110 #ifndef EREALLOC_CALLSITE 111 return ereallocz(optr, (size * nmemb), 0, FALSE); 112 #else 113 return ereallocz(optr, (size * nmemb), 0, FALSE, file, line); 114 #endif 115 } 116 117 char * 118 estrdup_impl( 119 const char * str 120 #ifdef EREALLOC_CALLSITE 121 , 122 const char * file, 123 int line 124 #endif 125 ) 126 { 127 char * copy; 128 size_t bytes; 129 130 bytes = strlen(str) + 1; 131 copy = ereallocz(NULL, bytes, 0, FALSE 132 #ifdef EREALLOC_CALLSITE 133 , file, line 134 #endif 135 ); 136 memcpy(copy, str, bytes); 137 138 return copy; 139 } 140 141 142 #if 0 143 #ifndef EREALLOC_CALLSITE 144 void * 145 emalloc(size_t newsz) 146 { 147 return ereallocz(NULL, newsz, 0, FALSE); 148 } 149 #endif 150 #endif 151 152