// **************************************************************************
// **************************************************************************
// **************************************************************************
// **                                                                      **
// ** MEMORY.H                                                      MODULE **
// **                                                                      **
// ** Replacements for the standard 'C' memory management functions.       **
// **                                                                      **
// ** Last modified : 13 Jan 1998 by John Brandwood                        **
// **                                                                      **
// ** Release 2.0 (19Nov96) - Another round of modifications to make the   **
// **                         code more platform-independant.              **
// **         2.1 (27Mar97) - Allocate a specific region of memory.        **
// **         2.2 (24Sep97) - Realloc() in place and allocate memory from  **
// **                         from btm, both to reduce fragmentation.      **
// **         3.0 (15Jan98) - Switch to doubly-linked-list of free blocks. **
// **                         Always allocate from start of memory to make **
// **                         system more deterministic. Add flag MEM_TOP  **
// **                         to allocate from the top of memory so that   **
// **                         temporary memory can be allocated seperately **
// **                         from permanent memory to help fragmentation. **
// **                                                                      **
// ** These functions have the optional advantage of providing debugging   **
// ** features for detecting errant pointers, memory overruns and memory   **
// ** underruns.                                                           **
// **                                                                      **
// ** To take advantage of these routines' ability to store the file and   **
// ** line of each memory allocation, you will need to either change all   **
// ** your memory allocations to use HeapMallocfl() and HeapCallocfl()     **
// ** or use #define directives like these ...                             **
// **                                                                      **
// **   #define HeapMalloc(xx)     HeapMallocfl(xx,__FILE__,__LINE__)      **
// **   #define HeapCalloc(xx,yy)  HeapCallocfl(xx,yy,__FILE__,__LINE__)   **
// **   #define HeapRealloc(xx,yy) HeapReallocfl(xx,yy,__FILE__,__LINE__)  **
// **                                                                      **
// **************************************************************************
// **************************************************************************
// **************************************************************************

#ifndef __MEMORY_h
#define __MEMORY_h

#ifndef __LFPTYPES_h
 #include "LFPTypes.h"
#endif

//
// GLOBAL DATA STRUCTURES AND DEFINITIONS
//

// Control mem package functions ...

#define	INCL_MEM_CLRFREE    0

// Define generic memory types.

#define MEM_TMP             0x01
#define MEM_HI              0x02
#define MEM_LO              0x04
#define MEM_VRAM            0x08
#define MEM_CLEAR           0x16
#define MEM_ANY             0x00

// Structures

typedef struct MEMHEAD_S
	{
	struct MEMHEAD_S *  pcl__mhNextBlock;		// Next block in list.
	struct MEMHEAD_S *  pcl__mhPrevBlock;		// Previous block in list.
	UL                  ul___mhBlockSize;		// Size allocated (incl header).
	UL                  ul___mhMagic;			// Magic number.
	} MEMHEAD_T;

typedef struct MEMLIST_S
	{
	struct MEMHEAD_S *  pcl__mlNextBlock;		// Next block (always NULL).
	struct MEMHEAD_S *  pcl__mlPrevBlock;		// Previous block.
	UL                  ul___mlBlockSize;		// Size allocated (always zero).
	UI                  ui___mlMagic;			// Magic number.
	char *              pcz__mlFile;			// File where allocated.
	UI                  ui___mlLine;			// Line where allocated.
	struct MEMLIST_S *  pcl__mlNextList;		// Link to next list header.
	struct MEMLIST_S *  pcl__mlPrevList;		// Link to previous list header.
	} MEMLIST_T;

//
// GLOBAL VARIABLES
//

extern	SL                  sl___CurMemUsed;
extern	SL                  sl___MaxMemUsed;

extern	long                sl___gCurMemFree;
extern	long                sl___gCurMemUsed;
extern	long                sl___gMaxMemUsed;

//
// GLOBAL FUNCTION PROTOTYPES
//

extern	void                MemMarkClr          (void);
extern	void                MemMarkSet          (void);

extern	int                 MemInit             (unsigned long *, unsigned long, unsigned long *, unsigned long);
extern	void *              MemMallocfl         (size_t, unsigned int, char *, unsigned long);
extern	void *              MemCallocfl         (size_t, unsigned int, char *, unsigned long);
extern	void *              MemReallocfl        (void *, size_t, unsigned int, char *, unsigned long);
extern	void *              MemReservefl        (void *, size_t, char *, unsigned long);
extern	void                MemFree             (void *);
extern	int                 MemTest             (void);
extern	void                MemDump             (void);
extern	void                MemFrag             (void);

extern	size_t              MemBlockSize        (void *);

#if 1
 #define MemMalloc(xx,yy)     MemMallocfl(xx,yy,__FILE__,__LINE__)
 #define MemCalloc(xx,yy)     MemCallocfl(xx,yy,__FILE__,__LINE__)
 #define MemRealloc(xx,yy,zz) MemReallocfl(xx,yy,zz,__FILE__,__LINE__)
 #define MemReserve(xx,yy)    MemReservefl(xx,yy,__FILE__,__LINE__)
#else
 extern  void *               MemMalloc           (size_t, unsigned int);
 extern  void *               MemCalloc           (size_t, unsigned int);
 extern  void *               MemRealloc          (void *, size_t, unsigned int);
 extern  void *               MemReserve          (void *, size_t);
#endif

/*
extern	SI                  MemInitPackage      (void);
extern	SI                  MemTermPackage      (void);
extern	MEMLIST_T *         MemCreateList_F     (char *, UI);
extern	SI                  MemKillList_F       (MEMLIST_T *, char *, UI);
extern	SI                  MemFreeList_F       (MEMLIST_T *, char *, UI);
extern	void *              MemTestList         (MEMLIST_T *);
*/

//
// End of __MEMORY_h
//

#endif



// **************************************************************************
// **************************************************************************
// **************************************************************************
//	END OF MEMORY.H
// **************************************************************************
// **************************************************************************
// **************************************************************************

