Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

RAM.H

Go to the documentation of this file.
00001 /****************************************************************************************/
00002 /*  RAM.H                                                                               */
00003 /*                                                                                      */
00004 /*  Author:                                                                             */
00005 /*  Description: Replacement for malloc, realloc and free                               */
00006 /*                                                                                      */
00007 /*  The contents of this file are subject to the Genesis3D Public License               */
00008 /*  Version 1.01 (the "License"); you may not use this file except in                   */
00009 /*  compliance with the License. You may obtain a copy of the License at                */
00010 /*  http://www.genesis3d.com                                                            */
00011 /*                                                                                      */
00012 /*  Software distributed under the License is distributed on an "AS IS"                 */
00013 /*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See                */
00014 /*  the License for the specific language governing rights and limitations              */
00015 /*  under the License.                                                                  */
00016 /*                                                                                      */
00017 /*  The Original Code is Genesis3D, released March 25, 1999.                            */
00018 /*  Genesis3D Version 1.1 released November 15, 1999                                 */
00019 /*  Copyright (C) 1999 WildTangent, Inc. All Rights Reserved           */
00020 /*                                                                                      */
00021 /****************************************************************************************/
00022 #ifndef GE_RAM_H
00023 #define GE_RAM_H
00024 
00025 #include "basetype.h"
00026 
00027 #ifdef __cplusplus
00028 extern "C" {
00029 #endif
00030 
00031 typedef int (* geRam_CriticalCallbackFunction)(void);
00032 
00033 /*
00034   Set the critical callback function.  ram_allocate will call the critical
00035   callback function if it's unable to allocate memory.
00036 */
00037 GENESISAPI geRam_CriticalCallbackFunction geRam_SetCriticalCallback
00038     (
00039       geRam_CriticalCallbackFunction callback
00040     );
00041 
00042 /*
00043   increments or decrements a counter .  if the counter is >0
00044   the critical callback function (if set) is called for a failed memory allocation.
00045   add is added to the current counter value.  the new counter value is returned.
00046 */
00047 GENESISAPI int geRam_EnableCriticalCallback(int add);
00048 
00049 
00050 /*
00051   Allocate memory of the given size.  In debug mode, the memory is filled
00052   with 0xA5, and we keep track of the amount of memory allocated.  Also, in debug
00053   mode, we track where the memory was allocated and can optionally provide a
00054   report of allocated blocks.  See geRam_ReportAllocations.
00055 */
00056 #ifndef NDEBUG
00057 
00058 #define geRam_Allocate(size) _geRam_DebugAllocate(size, __FILE__, __LINE__)
00059 
00060 // Do not call _geRam_DebugAllocate directly.
00061 GENESISAPI void* _geRam_DebugAllocate(uint32 size, const char* pFile, int line);
00062 
00063 #else
00064 
00065 GENESISAPI void *geRam_Allocate(uint32 size);
00066 
00067 #endif
00068 
00069 /*
00070   Free an allocated memory block.
00071 */
00072 GENESISAPI void geRam_Free_(void *ptr);
00073 
00074                 extern void *StupidUnusedPointer;    // never used, except to mask the
00075                 // possible warning you get if you use the geRam_Free macro below, without
00076                 // using the xxx pointer again in the same block.  This is ugly.
00077  
00078 #define geRam_Free(xxx) geRam_Free_(xxx) ,(xxx)=NULL, StupidUnusedPointer=(xxx)
00079 
00080 /*
00081   Reallocate memory.  This function supports shrinking and expanding blocks,
00082   and will also act like ram_allocate if the pointer passed to it is NULL.
00083   It won't, however, free the memory if you pass it a 0 size.
00084 */
00085 #ifndef NDEBUG
00086 
00087 #define geRam_Realloc(ptr, newsize) _geRam_DebugRealloc(ptr, newsize, __FILE__, __LINE__)
00088 
00089 // Do not call _geRam_DebugRealloc directly.
00090 GENESISAPI void* _geRam_DebugRealloc(void* ptr, uint32 size, const char* pFile, int line);
00091 
00092 #else
00093 
00094 GENESISAPI void *geRam_Realloc(void *ptr,uint32 newsize);
00095 
00096 #endif
00097 
00098 #ifndef NDEBUG
00099 
00100 GENESISAPI void geRam_ReportAllocations(void);
00101 
00102 #else
00103 
00104 #define geRam_ReportAllocations() 
00105 
00106 #endif
00107 
00108 #ifndef NDEBUG
00109     extern int32 geRam_CurrentlyUsed;
00110     extern int32 geRam_NumberOfAllocations;
00111     extern int32 geRam_MaximumUsed;
00112     extern int32 geRam_MaximumNumberOfAllocations;
00113 
00114 GENESISAPI     void geRam_AddAllocation(int n,uint32 size);
00115 #else
00116     #define geRam_AddAllocation(n,s)
00117 #endif
00118 
00119 // allocate the ram & clear it. (calloc)
00120 GENESISAPI void * geRam_AllocateClear(uint32 size);
00121 
00122 #define GE_RAM_ALLOCATE_STRUCT(type)      (type *)geRam_Allocate (sizeof (type))
00123 #define GE_RAM_ALLOCATE_ARRAY(type,count) (type *)geRam_Allocate (sizeof (type) * (count))
00124 
00125 #ifndef NDEBUG
00126 #define GE_RAM_REALLOC_ARRAY(ptr,type,count)  (type *)geRam_Realloc(  (ptr), sizeof(type) * (count) );{type *XX=(ptr);}
00127 #else
00128 #define GE_RAM_REALLOC_ARRAY(ptr,type,count)  (type *)geRam_Realloc(  (ptr), sizeof(type) * (count) )
00129 #endif
00130 
00131 #ifndef NDEBUG
00132 geBoolean geRam_IsValidPtr(void *ptr);
00133 #endif
00134 
00135 #ifdef __cplusplus
00136   }
00137 #endif
00138 
00139 #endif
00140 

Generated on Tue Sep 30 12:36:15 2003 for GTestAndEngine by doxygen 1.3.2