1*cdfa2a7eSchristos /* $NetBSD: emalloc.c,v 1.9 2020/05/25 20:47:24 christos Exp $ */
2abb0f93cSkardel
3abb0f93cSkardel /*
4abb0f93cSkardel * emalloc - return new memory obtained from the system. Belch if none.
5abb0f93cSkardel */
6f003fb54Skardel #include <config.h>
7abb0f93cSkardel #include "ntp_types.h"
8abb0f93cSkardel #include "ntp_malloc.h"
9abb0f93cSkardel #include "ntp_syslog.h"
10abb0f93cSkardel #include "ntp_stdlib.h"
11abb0f93cSkardel
12abb0f93cSkardel
13abb0f93cSkardel /*
14abb0f93cSkardel * When using the debug MS CRT allocator, each allocation stores the
15abb0f93cSkardel * callsite __FILE__ and __LINE__, which is then displayed at process
16abb0f93cSkardel * termination, to track down leaks. We don't want all of our
17abb0f93cSkardel * allocations to show up as coming from emalloc.c, so we preserve the
18abb0f93cSkardel * original callsite's source file and line using macros which pass
19abb0f93cSkardel * __FILE__ and __LINE__ as parameters to these routines.
20f003fb54Skardel * Other debug malloc implementations can be used by defining
21f003fb54Skardel * EREALLOC_IMPL() as ports/winnt/include/config.h does.
22abb0f93cSkardel */
23abb0f93cSkardel
24abb0f93cSkardel void *
ereallocz(void * ptr,size_t newsz,size_t priorsz,int zero_init,const char * file,int line)25f003fb54Skardel ereallocz(
26f003fb54Skardel void * ptr,
27f003fb54Skardel size_t newsz,
28f003fb54Skardel size_t priorsz,
29f003fb54Skardel int zero_init
30f003fb54Skardel #ifdef EREALLOC_CALLSITE /* ntp_malloc.h */
31f003fb54Skardel ,
32f003fb54Skardel const char * file,
33f003fb54Skardel int line
34f003fb54Skardel #endif
35abb0f93cSkardel )
36abb0f93cSkardel {
37f003fb54Skardel char * mem;
38f003fb54Skardel size_t allocsz;
39abb0f93cSkardel
40f003fb54Skardel if (0 == newsz)
41f003fb54Skardel allocsz = 1;
42f003fb54Skardel else
43f003fb54Skardel allocsz = newsz;
44abb0f93cSkardel
45f003fb54Skardel mem = EREALLOC_IMPL(ptr, allocsz, file, line);
46abb0f93cSkardel if (NULL == mem) {
47f003fb54Skardel msyslog_term = TRUE;
48f003fb54Skardel #ifndef EREALLOC_CALLSITE
49f003fb54Skardel msyslog(LOG_ERR, "fatal out of memory (%lu bytes)",
50f003fb54Skardel (u_long)newsz);
51f003fb54Skardel #else
52abb0f93cSkardel msyslog(LOG_ERR,
53f003fb54Skardel "fatal out of memory %s line %d (%lu bytes)",
54f003fb54Skardel file, line, (u_long)newsz);
55f003fb54Skardel #endif
56abb0f93cSkardel exit(1);
57abb0f93cSkardel }
58abb0f93cSkardel
59f003fb54Skardel if (zero_init && newsz > priorsz)
60f003fb54Skardel zero_mem(mem + priorsz, newsz - priorsz);
61f003fb54Skardel
62abb0f93cSkardel return mem;
63abb0f93cSkardel }
64abb0f93cSkardel
655d681e99Schristos /* oreallocarray.c is licensed under the following:
665d681e99Schristos * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
675d681e99Schristos *
685d681e99Schristos * Permission to use, copy, modify, and distribute this software for any
695d681e99Schristos * purpose with or without fee is hereby granted, provided that the above
705d681e99Schristos * copyright notice and this permission notice appear in all copies.
715d681e99Schristos *
725d681e99Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
735d681e99Schristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
745d681e99Schristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
755d681e99Schristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
765d681e99Schristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
775d681e99Schristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
785d681e99Schristos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
795d681e99Schristos */
805d681e99Schristos
815d681e99Schristos /*
825d681e99Schristos * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
835d681e99Schristos * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
845d681e99Schristos */
855d681e99Schristos #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
865d681e99Schristos
875d681e99Schristos void *
oreallocarrayxz(void * optr,size_t nmemb,size_t size,size_t extra,const char * file,int line)88ccc794f0Schristos oreallocarrayxz(
895d681e99Schristos void *optr,
905d681e99Schristos size_t nmemb,
91ccc794f0Schristos size_t size,
92ccc794f0Schristos size_t extra
935d681e99Schristos #ifdef EREALLOC_CALLSITE /* ntp_malloc.h */
945d681e99Schristos ,
955d681e99Schristos const char * file,
965d681e99Schristos int line
975d681e99Schristos #endif
985d681e99Schristos )
995d681e99Schristos {
1005d681e99Schristos if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1015d681e99Schristos nmemb > 0 && SIZE_MAX / nmemb < size) {
1025d681e99Schristos #ifndef EREALLOC_CALLSITE
1035d681e99Schristos msyslog(LOG_ERR, "fatal allocation size overflow");
1045d681e99Schristos #else
1055d681e99Schristos msyslog(LOG_ERR,
1065d681e99Schristos "fatal allocation size overflow %s line %d",
1075d681e99Schristos file, line);
1085d681e99Schristos #endif
1095d681e99Schristos exit(1);
1105d681e99Schristos }
1115d681e99Schristos #ifndef EREALLOC_CALLSITE
112ccc794f0Schristos return ereallocz(optr, extra + (size * nmemb), 0, TRUE);
1135d681e99Schristos #else
114ccc794f0Schristos return ereallocz(optr, extra + (size * nmemb), 0, TRUE, file, line);
1155d681e99Schristos #endif
1165d681e99Schristos }
117f003fb54Skardel
118abb0f93cSkardel char *
estrdup_impl(const char * str,const char * file,int line)119f003fb54Skardel estrdup_impl(
120f003fb54Skardel const char * str
121f003fb54Skardel #ifdef EREALLOC_CALLSITE
122f003fb54Skardel ,
123f003fb54Skardel const char * file,
124f003fb54Skardel int line
125f003fb54Skardel #endif
126abb0f93cSkardel )
127abb0f93cSkardel {
128abb0f93cSkardel char * copy;
129abb0f93cSkardel size_t bytes;
130abb0f93cSkardel
131abb0f93cSkardel bytes = strlen(str) + 1;
132f003fb54Skardel copy = ereallocz(NULL, bytes, 0, FALSE
133f003fb54Skardel #ifdef EREALLOC_CALLSITE
134f003fb54Skardel , file, line
135f003fb54Skardel #endif
136f003fb54Skardel );
137abb0f93cSkardel memcpy(copy, str, bytes);
138abb0f93cSkardel
139abb0f93cSkardel return copy;
140abb0f93cSkardel }
141abb0f93cSkardel
142f003fb54Skardel
143b8867d0eSchristos #if 0
144f003fb54Skardel #ifndef EREALLOC_CALLSITE
145f003fb54Skardel void *
146f003fb54Skardel emalloc(size_t newsz)
147f003fb54Skardel {
148f003fb54Skardel return ereallocz(NULL, newsz, 0, FALSE);
149f003fb54Skardel }
150f003fb54Skardel #endif
151b8867d0eSchristos #endif
152f003fb54Skardel
153