Commit f24e28c2 authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Include inttypes.h and stdint.h. Check USE_MALLOC_GUARDS.

(union overhead): New union, to fix alignmentproblems.
(OVERHEAD): New macro.
(isc_malloc): Use union overhead instead of unsigned long.
	Removed unneeded casts.  Handle USE_MALLOC_GUARDS.
(isc_realloc): Ditto.
(isc_free): Ditto.
parent 46821311
......@@ -44,6 +44,14 @@
# include <stdio.h>
#endif
#include <time.h>
/* The order between inttypes.h and stdint.h is mandated by autoconf-2.57. */
#if HAVE_INTTYPES_H
# include <inttypes.h>
#else
# if HAVE_STDINT_H
# include <stdint.h>
# endif
#endif
#include "isc.h"
#include "intern.h"
......@@ -51,9 +59,52 @@
/*
** Some magic numbers
*/
#ifdef USE_MALLOC_GUARDS
#define ISC_MAGIC_ALLOC 0x12F54ACEu
#define ISC_MAGIC_FREE 0xEE47A37Fu
/* A union of "all types", used to get the maximum alignment needed
for any type. */
union overhead {
/* We prefer to store stuf in a size_t, since we are storing a
size. Use an unsigned int if size_t isn't available. */
# ifdef HAVE_SIZE_T
size_t val;
unsigned int a;
# else
unsigned int val;
# endif
void *b;
long c;
long *d;
long long e;
long long *f;
float g;
double h;
void (*i)(void);
long double j;
# ifdef HAVE_INTPTR_T
intptr_t k;
# endif
# ifdef HAVE_INTMAX_T
intmax_t l;
# endif
};
# define OVERHEAD (sizeof(union overhead))
#else
union overhead;
# define OVERHEAD (0)
#endif
/*
** Pointers to functions to handle storage allocation
......@@ -81,50 +132,60 @@ isc_setallocfn(void * (*mallocfn)(size_t size),
void *
isc_malloc(size_t size)
{
unsigned long * buf;
union overhead *buf;
if (isc_mallocfn)
buf = (unsigned long *) (*isc_mallocfn)(size + sizeof(unsigned long));
buf = (*isc_mallocfn)(size + OVERHEAD);
else
{
buf = (unsigned long *) malloc(size + sizeof(unsigned long));
buf = malloc(size + OVERHEAD);
if (!buf)
isc_abort("isc_malloc");
}
*buf++ = ISC_MAGIC_ALLOC;
#ifdef USE_MALLOC_GUARDS
buf->val = ISC_MAGIC_ALLOC;
buf++;
#endif
return (void *) buf;
return buf;
}
void *
isc_realloc(void *oldbuf, size_t size)
{
unsigned long *buf;
union overhead *buf;
if (!oldbuf)
return isc_malloc(size);
buf = (unsigned long *) oldbuf;
if (*--buf != ISC_MAGIC_ALLOC)
buf = oldbuf;
#ifdef USE_MALLOC_GUARDS
buf--;
if (buf->val != ISC_MAGIC_ALLOC)
isc_abort("isc_realloc");
*buf = ISC_MAGIC_FREE;
buf->val = ISC_MAGIC_FREE;
#endif
if (isc_reallocfn)
buf = (unsigned long *) (*isc_reallocfn)((void *) buf,
size + sizeof(unsigned long));
buf = (*isc_reallocfn)(buf, size + OVERHEAD);
else
{
buf = (void *) realloc((void *) buf, size + sizeof(unsigned long));
buf = realloc(buf, size + OVERHEAD);
if (!buf)
isc_abort("isc_realloc");
}
*buf++ = ISC_MAGIC_ALLOC;
#ifdef USE_MALLOC_GUARDS
buf->val = ISC_MAGIC_ALLOC;
buf++;
#endif
return (void *) buf;
}
......@@ -133,25 +194,29 @@ isc_realloc(void *oldbuf, size_t size)
void
isc_free(void *buf)
{
unsigned long *ibuf;
union overhead *ibuf;
if (!buf)
isc_abort("isc_free");
ibuf = (unsigned long *) buf;
if (*--ibuf != ISC_MAGIC_ALLOC)
ibuf = buf;
#ifdef USE_MALLOC_GUARDS
ibuf--;
if (ibuf->val != ISC_MAGIC_ALLOC)
isc_abort("isc_free");
*ibuf = ISC_MAGIC_FREE;
ibuf->val = ISC_MAGIC_FREE;
#endif
if (isc_freefn)
{
(*isc_freefn)((void *) ibuf);
(*isc_freefn)(ibuf);
return;
}
else
free((void *) ibuf);
free(ibuf);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment