#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ansidecl.h"
#include "libiberty.h"
#define HAVE_EFENCE 0
#define HAVE_MMALLOC 0
#undef HAVE_SBRK
#include <stdio.h>
#ifdef __STDC__
#include <stddef.h>
#else
#define size_t unsigned long
#define ptrdiff_t long
#endif
#if VMS
#include <stdlib.h>
#include <unixlib.h>
#else
PTR malloc PARAMS ((size_t));
PTR realloc PARAMS ((PTR, size_t));
PTR calloc PARAMS ((size_t, size_t));
PTR sbrk PARAMS ((ptrdiff_t));
void free (PTR ptr);
#endif
#ifdef HAVE_EFENCE
#include "efence.h"
#endif
#if HAVE_MMALLOC
#include <mmalloc.h>
#endif
#include <assert.h>
#include <limits.h>
#define MAX_SIZE ULONG_MAX
#undef malloc
#undef realloc
#undef free
#if HAVE_EFENCE
int use_efence = 0;
#endif
#if HAVE_MMALLOC
int use_mmalloc = 0;
#endif
static const char *name = "";
#ifdef HAVE_SBRK
static char *first_break = NULL;
#endif
void
xmalloc_set_program_name (s)
const char *s;
{
name = s;
#ifdef HAVE_SBRK
if (first_break == NULL)
first_break = (char *) sbrk (0);
#endif
}
void
xmalloc_failed (size)
size_t size;
{
#ifdef HAVE_SBRK
extern char **environ;
size_t allocated;
if (first_break != NULL)
allocated = (char *) sbrk (0) - first_break;
else
allocated = (char *) sbrk (0) - (char *) &environ;
fprintf (stderr,
"\n%s%sUnable to allocate %lu bytes (%lu bytes already allocated)\n",
name, *name ? ": " : "",
(unsigned long) size, (unsigned long) allocated);
#else
fprintf (stderr,
"\n%s%sUnable to allocate %lu bytes\n",
name, *name ? ": " : "",
(unsigned long) size);
#endif
abort ();
}
PTR
xmmalloc (md, size)
PTR md;
size_t size;
{
PTR val;
assert (size < MAX_SIZE);
if (size == 0)
return NULL;
if (0) {
}
#if HAVE_EFENCE
else if (use_efence) {
val = efence_malloc (size);
}
#endif
#if HAVE_MMALLOC
else if (use_mmalloc) {
val = mmalloc (md, size);
}
#endif
else {
val = malloc (size);
}
if (val == NULL)
xmalloc_failed (size);
return val;
}
PTR
xmcalloc (md, nelem, elsize)
PTR md;
size_t nelem;
size_t elsize;
{
PTR val;
if (nelem == 0 || elsize == 0)
return NULL;
assert (nelem < (MAX_SIZE / elsize));
if (0) {
}
#if HAVE_EFENCE
else if (use_efence) {
val = efence_calloc (nelem, elsize);
}
#endif
#if HAVE_MMALLOC
else if (use_mmalloc) {
val = mcalloc (md, nelem, elsize);
}
#endif
else {
val = calloc (nelem, elsize);
}
if (val == NULL)
xmalloc_failed (nelem * elsize);
return val;
}
PTR
xmrealloc (md, ptr, size)
PTR md;
PTR ptr;
size_t size;
{
PTR val;
assert (size < MAX_SIZE);
if (0) {
}
#if HAVE_EFENCE
else if (use_efence) {
if (ptr != NULL)
val = efence_realloc (ptr, size);
else
val = efence_malloc (size);
}
#endif
#if HAVE_MMALLOC
else if (use_mmalloc) {
if (ptr != NULL)
val = mrealloc (md, ptr, size);
else
val = mmalloc (md, size);
}
#endif
else {
if (ptr != NULL)
val = realloc (ptr, size);
else
val = malloc (size);
}
if (val == NULL)
xmalloc_failed (size);
return val;
}
void
xmfree (md, ptr)
PTR md;
PTR ptr;
{
if (ptr == NULL)
return;
if (0) {
}
#if HAVE_EFENCE
else if (use_efence) {
efence_free (ptr);
}
#endif
#if HAVE_MMALLOC
else if (use_mmalloc) {
mfree (md, ptr);
}
#endif
else {
free (ptr);
}
}
PTR
xmalloc (size)
size_t size;
{
assert (size < MAX_SIZE);
return (xmmalloc ((PTR) NULL, size));
}
PTR
xcalloc (nelem, elsize)
size_t nelem;
size_t elsize;
{
assert ((nelem * elsize) < MAX_SIZE);
return (xmcalloc ((PTR) NULL, nelem, elsize));
}
PTR
xrealloc (ptr, size)
PTR ptr;
size_t size;
{
assert (size < MAX_SIZE);
return (xmrealloc ((PTR) NULL, ptr, size));
}
void
xfree (ptr)
PTR ptr;
{
return xmfree ((PTR) NULL, ptr);
}