#ifndef RTMALLOC_H
#define RTMALLOC_H

/******************************************************************************/
/******************************************************************************/
/*****************     Copyright 2004, 2005 Chronolytics Inc.   ***************/
/*****************     All Right Reserved                       ***************/
/******************************************************************************/
/******************************************************************************/

#include <RTMagic.h>
#include <RTLog.h>
#include <RTRefCnt_Object.h>

static volatile const char __attribute__ ((unused)) rcsid_RTMALLOC_H[] = "$Id: RTMalloc.h,v 1.4 2005/07/19 00:32:09 dave Exp $";

//#define RTMALLOC_DEBUG

#ifndef FAILS
#define FAILS (-1)
#endif

extern void *RTMalloc(const int n);
extern void free();

static inline void RTFree(void *x) 
   {
   char magicstring[12];
   extern char *RTCommon_Stringize_Magic();
   // extern int RTRefCnt_Validate();  /* RTRefCnt_Object is mutually dependent: can't include here */

   if (!x) return;  /* std c */

   if (RTRefCnt_Validate(&x) != FAILS)  {
      unsigned long magic = (unsigned long) x;
      RTLog_Synchronous(LOGCOMMON|LOGDEBUG, "RTFree: attempt to free RTRefCnt_Object: 0x%x magic %s.\n", x, RTCommon_Stringize_Magic(magic, magicstring));
      return;
   }
#ifdef RTMALLOC_DEBUG

   if (IS_MAGIC(x))  {
      unsigned long magic = (unsigned long) x;
      RTLog_Synchronous(LOGMALLOC|LOGDEBUG, "RTFree: 0x%x magic %s.\n", x, RTCommon_Stringize_Magic(magic, magicstring));
   } 

#endif /* RTMALLOC_DEBUG */
   *((unsigned long *) x) = MAGIC("Free");  /* wipe out any magic lurking */
   free(x);
}

#endif /* RTMALLOC_H */

