//---------------------------------------------------------------------------
//
// Heap.h -- This file contains the definition for the Base HEAP
//		 			Manager Class.  The Base HEAP manager creates,
//					manages and destroys block of memory using Win32
//					virtual memory calls.
//
//      Honor Bound -- FASA Interactive Technologies
//
//      Copyright (c) 1995 FASA Interactive Technologies
//
//---------------------------------------------------------------------------

#if !defined(HEAP_HPP)
#	define HEAP_HPP

	#if !defined(STYLE_HPP)
		#include <style.hpp>
	#endif

#	if !defined(MEMREG_HPP)
#		include <memreg.hpp>
#	endif

#if defined(MW3)
	extern fstream debugfile;
#endif

	struct HeapBlock;
	struct HeapFreeBlock;

	class UserHeap SIGNATURED
	{
	protected:
		char
			*bigHeap;
		const char
			*heapName;
		HeapBlock
			*heapStart,
			*firstNearBlock;
		size_t
			heapSize,
			lowestTotalHeapLeft;
		size_t
			allocateCount,
			releaseCount;

	protected:
		void
			Relink(HeapBlock *newBlock);
		void
			Unlink(HeapBlock *oldBlock);
		void
			Sort(HeapBlock *sortBlock);
		void
			MergeWithLower(HeapBlock *block);

	public:
		UserHeap(
			const char* heap_name,
			size_t mem_size
		);
		~UserHeap();

		size_t
			GetTotalHeapLeft();
		size_t
			GetBiggestHeapLeft();
		size_t
			GetLowestTotalHeapLeft()
				{return lowestTotalHeapLeft;}
		size_t
			GetHeapUsed()
				{return heapSize - GetTotalHeapLeft();}
		size_t
			GetMostHeapUsed()
				{return heapSize - lowestTotalHeapLeft;}

		Logical
			TestHeap(
				Logical print_it = False,
				Logical skip_allocated = False,
				Logical skip_free = False
			);

		void*
			Allocate(size_t mem_size);
		void
			Release(void *where);
		void
			ReleaseAll();

		Logical
			TestInstance() const;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Global memory allocation
	//
	protected:
		static LWord
			HeapMark;
		Logical
			mainHeap;

	public:
		static UserHeap
			MainStorage;

		static size_t
			GetMainHeapInitialSize();

		friend void*
			operator new(size_t mem_size);
		friend void*
			operator new(size_t mem_size, void* where);
		friend void
			operator delete(void *mem_block);
	};

	void*
		Get_Caller(int levels);

#endif

