/////////////////////////////////////////////////////////////
// CINEMA 4D SDK                                           //
/////////////////////////////////////////////////////////////
// (c) 1989-2004 MAXON Computer GmbH, all rights reserved  //
/////////////////////////////////////////////////////////////

#ifndef __C4D_MEMORY_H
#define __C4D_MEMORY_H

#ifdef __MAC
	#ifdef __MWERKS__
	#include <size_t.h>
	#endif
	#if defined(__cplusplus) && defined(_MSL_USING_NAMESPACE)
		using namespace std;
	#endif
#elif !defined __PC
  #include <stddef.h>
#endif

#include "c4d_plugin.h"

void _GeFree(void **Daten);
Bool	GeGetAllocSize( void *p, VLONG *out_size );
Bool	IsAlienMem( void *p );

#ifdef _DEBUG
	#define GeAlloc(x)     (C4DOS.Ge->Alloc((x),__LINE__,__FILE__))
	#define GeAllocNC(x)   (C4DOS.Ge->AllocNC((x),__LINE__,__FILE__))
#else
	#define GeAlloc(x)     (C4DOS.Ge->Alloc((x),0,NULL))
	#define GeAllocNC(x)   (C4DOS.Ge->AllocNC((x),0,NULL))
#endif

#define GeFree(x) _GeFree((void **)(&(x)))

void ClearMem(void *d, VLONG size, LONG value=0);
void CopyMem(const void *s, void *d, VLONG size);

void* operator new(size_t s);
void* operator new(size_t s,int line,const CHAR *file);
void* operator new(size_t s,int line,const CHAR *file, Bool clear);
void* operator new[](size_t s);
void* operator new[](size_t s,int line,const CHAR *file);
void* operator new[](size_t s,int line,const CHAR *file, Bool clear);
void operator delete(void *p);
void operator delete[](void *p);
void operator delete(void *p,int line,const CHAR *file);
void operator delete(void *p,int line,const CHAR *file, Bool clear);
void operator delete[](void *p,int line,const CHAR *file);
void operator delete[](void *p,int line,const CHAR *file, Bool clear);

#ifdef _DEBUG
	#define gNew new(__LINE__,__FILE__)
	#define bNew new(__LINE__,__FILE__)
	#define gNewNC new(__LINE__,__FILE__,FALSE)
	#define bNewNC new(__LINE__,__FILE__,FALSE)
#else
	#define gNew new(0,NULL)
	#define bNew new(0,NULL)
	#define gNewNC new(0,NULL,FALSE)
	#define bNewNC new(0,NULL,FALSE)
#endif

#define gDelete(v) {if(v) delete v; v=0;}
#define bDelete(v) {if(v) delete [] v; v=0;}

#include "ge_autoptr.h"

#define MEMORYPOOL_DEFAULT_BLOCKSIZE 0x80000

class MemoryPool
{
	private:
		MemoryPool();
		~MemoryPool();
	public:
		void *MemAlloc (VLONG size, Bool clear=TRUE);
		void MemFree   (void *mem, VLONG size);
		void *MemAllocS(VLONG size, Bool clear=TRUE);
		void MemFreeS	 (void *mem);

		static MemoryPool *Alloc(void);
		static MemoryPool *Alloc(VLONG block_size);
		static void Free(MemoryPool *&pool);
};

#endif
