#include "platform.h"
#include "falloc.h"
#include <stdlib.h>
#define FALLOC_WATCH 0
#if FALLOC_WATCH
#include <stdio.h>
void *mallocWatchAddrs;
void *freeWatchAddrs;
#endif
static mallocExternFcn *mallocExt = NULL;
static freeExternFcn *freeExt = NULL;
static reallocExternFcn *reallocExt = NULL;
void fallocRegister(mallocExternFcn *mallocExtern,
freeExternFcn *freeExtern,
reallocExternFcn *reallocExtern)
{
mallocExt = mallocExtern;
freeExt = freeExtern;
reallocExt = reallocExtern;
}
void *fmalloc(unsigned size)
{
void *rtn;
if(mallocExt != NULL) {
rtn = (mallocExt)(size);
}
else {
rtn = malloc(size);
}
#if FALLOC_WATCH
if(rtn == mallocWatchAddrs) {
printf("====fmalloc watchpoint (0x%x) hit\n",
(unsigned)mallocWatchAddrs);
}
#endif
return rtn;
}
void *fmallocWithData(const void *origData,
unsigned origDataLen)
{
void *rtn = fmalloc(origDataLen);
bcopy(origData, rtn, origDataLen);
return rtn;
}
void ffree(void *data)
{
#if FALLOC_WATCH
if(data == freeWatchAddrs) {
printf("====ffree watchpoint (0x%x) hit\n",
(unsigned)freeWatchAddrs);
}
#endif
if(freeExt != NULL) {
(freeExt)(data);
}
else {
free(data);
}
}
void *frealloc(void *oldPtr, unsigned newSize)
{
#if FALLOC_WATCH
if(oldPtr == freeWatchAddrs) {
printf("====frealloc watchpoint (0x%x) hit\n",
(unsigned)freeWatchAddrs);
}
#endif
if(reallocExt != NULL) {
return (reallocExt)(oldPtr, newSize);
}
else {
return realloc(oldPtr, newSize);
}
}