154359Sroberto/*
254359Sroberto * emalloc - return new memory obtained from the system.  Belch if none.
354359Sroberto */
4280849Scy#include <config.h>
554359Sroberto#include "ntp_types.h"
654359Sroberto#include "ntp_malloc.h"
782498Sroberto#include "ntp_syslog.h"
854359Sroberto#include "ntp_stdlib.h"
954359Sroberto
1054359Sroberto
11280849Scy/*
12280849Scy * When using the debug MS CRT allocator, each allocation stores the
13280849Scy * callsite __FILE__ and __LINE__, which is then displayed at process
14280849Scy * termination, to track down leaks.  We don't want all of our
15280849Scy * allocations to show up as coming from emalloc.c, so we preserve the
16280849Scy * original callsite's source file and line using macros which pass
17280849Scy * __FILE__ and __LINE__ as parameters to these routines.
18280849Scy * Other debug malloc implementations can be used by defining
19280849Scy * EREALLOC_IMPL() as ports/winnt/include/config.h does.
20280849Scy */
2154359Sroberto
2254359Srobertovoid *
23280849Scyereallocz(
24280849Scy	void *	ptr,
25280849Scy	size_t	newsz,
26280849Scy	size_t	priorsz,
27280849Scy	int	zero_init
28280849Scy#ifdef EREALLOC_CALLSITE		/* ntp_malloc.h */
29280849Scy			 ,
30280849Scy	const char *	file,
31280849Scy	int		line
32280849Scy#endif
3354359Sroberto	)
3454359Sroberto{
35280849Scy	char *	mem;
36280849Scy	size_t	allocsz;
3754359Sroberto
38280849Scy	if (0 == newsz)
39280849Scy		allocsz = 1;
40280849Scy	else
41280849Scy		allocsz = newsz;
42280849Scy
43280849Scy	mem = EREALLOC_IMPL(ptr, allocsz, file, line);
44280849Scy	if (NULL == mem) {
45280849Scy		msyslog_term = TRUE;
46280849Scy#ifndef EREALLOC_CALLSITE
47280849Scy		msyslog(LOG_ERR, "fatal out of memory (%lu bytes)",
48280849Scy			(u_long)newsz);
49280849Scy#else
50280849Scy		msyslog(LOG_ERR,
51280849Scy			"fatal out of memory %s line %d (%lu bytes)",
52280849Scy			file, line, (u_long)newsz);
53280849Scy#endif
5454359Sroberto		exit(1);
5554359Sroberto	}
56280849Scy
57280849Scy	if (zero_init && newsz > priorsz)
58280849Scy		zero_mem(mem + priorsz, newsz - priorsz);
59280849Scy
6054359Sroberto	return mem;
6154359Sroberto}
6254359Sroberto
63285169Scy/* oreallocarray.c is licensed under the following:
64285169Scy * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
65285169Scy *
66285169Scy * Permission to use, copy, modify, and distribute this software for any
67285169Scy * purpose with or without fee is hereby granted, provided that the above
68285169Scy * copyright notice and this permission notice appear in all copies.
69285169Scy *
70285169Scy * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
71285169Scy * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
72285169Scy * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
73285169Scy * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
74285169Scy * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
75285169Scy * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
76285169Scy * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
77285169Scy */
7854359Sroberto
79285169Scy/*
80285169Scy * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
81285169Scy * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
82285169Scy */
83285169Scy#define MUL_NO_OVERFLOW	((size_t)1 << (sizeof(size_t) * 4))
84285169Scy
85285169Scyvoid *
86316068Sdelphijoreallocarrayxz(
87285169Scy	void *optr,
88285169Scy	size_t nmemb,
89316068Sdelphij	size_t size,
90316068Sdelphij	size_t extra
91285169Scy#ifdef EREALLOC_CALLSITE		/* ntp_malloc.h */
92285169Scy	,
93285169Scy	const char *	file,
94285169Scy	int		line
95285169Scy#endif
96285169Scy	)
97285169Scy{
98285169Scy	if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
99285169Scy	    nmemb > 0 && SIZE_MAX / nmemb < size) {
100285169Scy#ifndef EREALLOC_CALLSITE
101285169Scy		msyslog(LOG_ERR, "fatal allocation size overflow");
102285169Scy#else
103285169Scy		msyslog(LOG_ERR,
104285169Scy			"fatal allocation size overflow %s line %d",
105285169Scy			file, line);
106285169Scy#endif
107285169Scy		exit(1);
108285169Scy	}
109285169Scy#ifndef EREALLOC_CALLSITE
110316068Sdelphij	return ereallocz(optr, extra + (size * nmemb), 0, TRUE);
111285169Scy#else
112316068Sdelphij	return ereallocz(optr, extra + (size * nmemb), 0, TRUE, file, line);
113285169Scy#endif
114285169Scy}
115285169Scy
116280849Scychar *
117280849Scyestrdup_impl(
118280849Scy	const char *	str
119280849Scy#ifdef EREALLOC_CALLSITE
120280849Scy			   ,
121280849Scy	const char *	file,
122280849Scy	int		line
123280849Scy#endif
12454359Sroberto	)
12554359Sroberto{
126280849Scy	char *	copy;
127280849Scy	size_t	bytes;
12854359Sroberto
129280849Scy	bytes = strlen(str) + 1;
130280849Scy	copy = ereallocz(NULL, bytes, 0, FALSE
131280849Scy#ifdef EREALLOC_CALLSITE
132280849Scy			 , file, line
133280849Scy#endif
134280849Scy			 );
135280849Scy	memcpy(copy, str, bytes);
136280849Scy
137280849Scy	return copy;
13854359Sroberto}
13954359Sroberto
14054359Sroberto
141280849Scy#if 0
142280849Scy#ifndef EREALLOC_CALLSITE
143280849Scyvoid *
144280849Scyemalloc(size_t newsz)
145280849Scy{
146280849Scy	return ereallocz(NULL, newsz, 0, FALSE);
147280849Scy}
14854359Sroberto#endif
149280849Scy#endif
150280849Scy
151