#include "ntp_types.h"
#include "ntp_malloc.h"
#include "ntp_syslog.h"
#include "ntp_stdlib.h"
extern char *progname;
#if !defined(_MSC_VER) || !defined(_DEBUG)
void *
erealloc(
void * prev,
size_t size
)
{
void * mem;
mem = realloc(prev, size ? size : 1);
if (NULL == mem) {
msyslog(LOG_ERR,
"fatal out of memory (%u bytes)", (u_int)size);
fprintf(stderr,
"%s: fatal out of memory (%u bytes)", progname,
(u_int)size);
exit(1);
}
return mem;
}
void *
emalloc(
size_t size
)
{
return erealloc(NULL, size);
}
char *
estrdup(
const char * str
)
{
char * copy;
copy = strdup(str);
if (NULL == copy) {
msyslog(LOG_ERR,
"fatal out of memory duplicating %u bytes",
(u_int)strlen(str) + 1);
fprintf(stderr,
"%s: fatal out of memory duplicating %u bytes",
progname, (u_int)strlen(str) + 1);
exit(1);
}
return copy;
}
#else
void *
debug_erealloc(
void * prev,
size_t size,
const char * file,
int line
)
{
void * mem;
mem = _realloc_dbg(prev, size ? size : 1,
_NORMAL_BLOCK, file, line);
if (NULL == mem) {
msyslog(LOG_ERR,
"fatal: out of memory in %s line %d size %u",
file, line, (u_int)size);
fprintf(stderr,
"%s: fatal: out of memory in %s line %d size %u",
progname, file, line, (u_int)size);
exit(1);
}
return mem;
}
char *
debug_estrdup(
const char * str,
const char * file,
int line
)
{
char * copy;
size_t bytes;
bytes = strlen(str) + 1;
copy = debug_erealloc(NULL, bytes, file, line);
memcpy(copy, str, bytes);
return copy;
}
#endif