Gizmo3D

gzMemory.h

Go to the documentation of this file.
00001 //******************************************************************************
00002 // File         : gzMemory.h
00003 // Module       : gzBase
00004 // Description  : Class definition of memory allocation utilities
00005 // Author       : Anders Modén      
00006 // Product      : GizmoBase 2.1.1
00007 //      
00008 // Copyright © 2003- Saab Training Systems AB, Sweden
00009 //          
00010 // NOTE:    GizmoBase is a platform abstraction utility layer for C++. It contains 
00011 //          design patterns and C++ solutions for the advanced programmer.
00012 //
00013 //
00014 // Revision History...                          
00015 //                                  
00016 // Who  Date    Description                     
00017 //                                  
00018 // AMO  980915  Created file    
00019 //
00020 //******************************************************************************
00021 
00022 #ifndef __GZ_MEMORY_H__
00023 #define __GZ_MEMORY_H__
00024 
00029 #include "gzBasicTypes.h"
00030 #include "gzArgument.h"
00031 
00032 //-------------------------- turn of mem debug for this header -------------------
00033 
00034 #ifdef GZ_MEM_DEBUG
00035 #undef new
00036 #undef gz_malloc
00037 #endif
00038 
00039 //******************************************************************************
00040 // Class    : gzMemoryManagerInterface
00041 //                                  
00042 // Purpose  : Memory manager interface
00043 //                                  
00044 // Notes    : - 
00045 //                                  
00046 // Revision History...                          
00047 //                                  
00048 // Who  Date    Description                     
00049 //                                  
00050 // AMO  980819  Created 
00051 //                                  
00052 //******************************************************************************
00054 class gzMemoryManagerInterface
00055 {
00056 public:
00057 
00058     GZ_BASE_EXPORT virtual gzVoid *malloc(size_t size)=0;
00059     GZ_BASE_EXPORT virtual gzVoid *calloc(size_t num,size_t size)=0;
00060     GZ_BASE_EXPORT virtual gzVoid free(gzVoid *address)=0;
00061     GZ_BASE_EXPORT virtual gzVoid *realloc(gzVoid *address,size_t size)=0;
00062 };
00063 
00064 //******************************************************************************
00065 // Struct   : gzMemInfoItem
00066 //                                  
00067 // Purpose  : Hold memory allocation info
00068 //                                  
00069 // Notes    : - 
00070 //                                  
00071 // Revision History...                          
00072 //                                  
00073 // Who  Date    Description                     
00074 //                                  
00075 // AMO  031120  Created 
00076 //                                  
00077 //******************************************************************************
00078 struct gzMemInfoItem
00079 {
00080     gzMemPointer    address;
00081     gzMemSize       size;
00082     char            *info;
00083     gzULong         pid;
00084     gzULong         counter;
00085     gzULong         state;
00086 };
00087 
00088 
00090 const gzULong GZ_MEM_DEFAULT_STATE = 1;
00091 
00092 //******************************************************************************
00093 // Class    : gzSharedMemory
00094 //                                  
00095 // Purpose  : Memory allocator from one shared pool
00096 //                                  
00097 // Notes    : - 
00098 //                                  
00099 // Revision History...                          
00100 //                                  
00101 // Who  Date    Description                     
00102 //                                  
00103 // AMO  980819  Created 
00104 //                                  
00105 //******************************************************************************
00107 
00132 class GZ_BASE_EXPORT gzSharedMemory 
00133 {
00134 public:
00135 
00136 #ifndef GZ_DISABLE_MEMORY_NEW_HANDLER
00137 
00138     void * operator new(size_t size) GZ_THROW_BAD_ALLOC;
00139     void * operator new(size_t size, void* placement) { return placement; }
00140 
00141     void operator delete(void *address);
00142     void operator delete(void *address, void* placement) {}
00143 
00144 #ifndef GZ_NO_DEBUG_MEM
00145 
00146     void * operator new(size_t size,const char *info) GZ_THROW_BAD_ALLOC;
00147 
00148     void operator delete(void *address,const char *info);
00149 
00150 #endif // GZ_NO_DEBUG_MEM
00151 
00152 #ifdef GZ_ADVANCED_MEM  // Newer compilers
00153 
00154     void * operator new[](size_t size) GZ_THROW_BAD_ALLOC;
00155 
00156     void operator delete[](void *address);
00157 
00158 #ifndef GZ_NO_DEBUG_MEM
00159 
00160     void * operator new[](size_t size,const char *info) GZ_THROW_BAD_ALLOC;
00161 
00162     void operator delete[](void *address,const char *info);
00163 
00164 #endif // GZ_NO_DEBUG_MEM
00165 
00166 #endif // GZ_ADVANCED_MEM
00167 
00168 #endif // GZ_DISABLE_MEMORY_NEW_HANDLER
00169 
00170 };
00171 
00172 #define GIZMO_INTERNAL_MEM_STATE    (99999)
00173 
00174 class GZ_BASE_EXPORT gzMemoryControl
00175 {
00176 public:
00177 
00179 
00180     static gzVoid   traceAlloc(gzBool on);
00181 
00183     static gzVoid   debugMem(gzBool on);
00184 
00186 
00187     static gzVoid   dumpAllocMem(gzULong state=0,gzULong pid=0,gzBool dumpInternalGizmoMem=FALSE);
00188 
00190 
00193     static gzULong  updateState(gzULong state=0,gzULong pid=0);
00194 
00196     static gzULong  getState(gzULong pid=0);
00197 
00199     static gzULongLong  getAllocMem(gzULong state=0,gzULong pid=0);
00200 
00202     static gzVoid   cleanAllocMem();
00203 
00205     static gzVoid   useFormatOutput(gzBool on);
00206 
00208     static gzVoid   memprintf(const char *fmt , ARG_DECL_LIST );
00209 
00210     static gzVoid   setMemoryManager(gzMemoryManagerInterface *manager);
00211 
00212     static inline gzMemoryManagerInterface *getMemoryManagerInterface() { return s_currentMemoryManager; }
00213 
00214     static gzULong getMemInfo(gzMemInfoItem *buffer,gzULong maxBufferItems);
00215 
00216 private:
00217 
00218     static gzMemoryManagerInterface *s_currentMemoryManager;
00219 };
00220 
00221 #define PUSH_INTERNAL_MEM_STATE(x)  gzULong state_##x=gzMemoryControl::updateState(GIZMO_INTERNAL_MEM_STATE)
00222 #define POP_INTERNAL_MEM_STATE(x)   gzMemoryControl::updateState(state_##x)
00223 
00224 //************** Memory  Allocation utilities **********************************
00225 
00226 extern "C"  // C functions
00227 {
00228 GZ_BASE_EXPORT gzVoid *gz_malloc(size_t size);
00229 GZ_BASE_EXPORT gzVoid *gz_calloc(size_t size);
00230 GZ_BASE_EXPORT gzVoid gz_free(gzVoid *address);
00231 }
00232 
00233 GZ_BASE_EXPORT gzVoid *gz_malloc(size_t size,const char *info);
00234 GZ_BASE_EXPORT gzVoid *gz_calloc(size_t size,const char *info);
00235 
00236 //******************************************************************************
00237 // Class    : gzMemoryCheck
00238 //                                  
00239 // Purpose  : Checks to see if memeory is valid (allocated)
00240 //                                  
00241 // Notes    : - 
00242 //                                  
00243 // Revision History...                          
00244 //                                  
00245 // Who  Date    Description                     
00246 //                                  
00247 // AMO  010720  Created 
00248 //                                  
00249 //******************************************************************************
00250 class GZ_BASE_EXPORT gzMemoryCheck 
00251 {
00252 public:
00253 
00254     gzMemoryCheck();
00255 
00256     virtual ~gzMemoryCheck();
00257 
00258     gzBool isValidMemory();
00259 
00260 private:
00261 
00262     gzULong m_isValid;
00263 };
00264 
00265 
00266 class gzMutex;  // Forward decl
00267 class gzString; // The same
00268 
00269 //******************************************************************************
00270 // Function : gzMemOptimizer
00271 //                                  
00272 // Purpose  : Generic accelleration of memory allocations
00273 //                                  
00274 // Revision History...                          
00275 //                                  
00276 // Who  Date    Description                     
00277 //                                  
00278 // AMO  011204  Created release 
00279 //                                  
00280 //******************************************************************************
00281 class GZ_BASE_EXPORT gzMemOptimizer
00282 {
00283 public:
00284     gzMemOptimizer(gzULong slots , gzULong byteSize,const char *name=NULL,gzBool enable=FALSE);
00285     
00286     virtual ~gzMemOptimizer();
00287 
00288     gzBool tryResize(gzULong slots);
00289     
00290     gzVoid *malloc(size_t size);
00291     
00292     gzVoid free(gzVoid *adress);
00293     
00294     gzBool isManaged(gzVoid *adress);
00295     
00296     gzVoid disable(gzBool force=FALSE);
00297     
00298     gzVoid enable();
00299 
00300     gzVoid setReportPercentage(gzULong perc=10);
00301     
00302 private:
00303 
00304     gzVoid enable_unsafe();
00305 
00306     gzUByte         *m_pMemOptimArray;
00307 
00308     gzMemPointer    *m_pMemOptimFreeStackIndex;
00309 
00310     gzULong         m_isInitialized;
00311 
00312     gzULong         m_stackPos;
00313 
00314     gzMutex         *m_locker;
00315 
00316     gzULong         m_byteSize;
00317 
00318     gzULong         m_slots;
00319 
00320     gzMemPointer    m_pOptimMemEnd;
00321 
00322     gzString        *m_name;
00323 
00324     gzULong         m_allocCount;
00325 
00326     gzULong         m_lastPerc;
00327 
00328     gzULong         m_repPerc;
00329 };
00330 
00331 
00332 //****************** MemBase custom allocations *********************************
00333 
00334 #ifndef GZ_MEM_DEBUG    // In mem debug we don't want allocations to be private
00335 
00336 #define GZ_MEMBASE(className) gzMemBaseAlloc_##className
00337 
00338 #define GZ_MEMBASE_IMP(className)   void * operator new(size_t size) GZ_THROW_BAD_ALLOC \
00339 {                                                                                       \
00340     return s_allocator_##className.malloc(size);                                        \
00341 }                                                                                       \
00342                                                                                         \
00343 void operator delete(void *address)                                                     \
00344 {                                                                                       \
00345     s_allocator_##className.free(address);                                              \
00346 }
00347 
00348 
00349 #define GZ_DECLARE_MEMBASE(className) class GZ_MEMBASE(className)           \
00350 {                                                                           \
00351 public:                                                                     \
00352     GZ_MEMBASE_IMP(className)                                               \
00353     static gzMemOptimizer   s_allocator_##className;                        \
00354 };
00355 
00356 #define GZ_DECLARE_MEMBASE_EXPORT(className,export) class GZ_MEMBASE(className)     \
00357 {                                                                                   \
00358 public:                                                                             \
00359     GZ_MEMBASE_IMP(className)                                                       \
00360     export static gzMemOptimizer    s_allocator_##className;                        \
00361 };
00362 
00363 #define GZ_DECLARE_MEMBASE_IMP(className,size) gzMemOptimizer GZ_MEMBASE(className)::s_allocator_##className(size,sizeof(className),#className,TRUE)
00364 
00365 #else // GZ_MEM_DEBUG - no active allocators
00366 
00367 class gzDummyMem {}; // Empty class as base to MEMBASE
00368 
00369 #define GZ_MEMBASE(className) gzDummyMem
00370 #define GZ_MEMBASE_IMP(className) 
00371 #define GZ_DECLARE_MEMBASE(className)
00372 #define GZ_DECLARE_MEMBASE_EXPORT(className,export)
00373 #define GZ_DECLARE_MEMBASE_IMP(className,size) 
00374 
00375 #endif // GZ_MEM_DEBUG
00376 
00377 //--------------------- redefine memory debug info -----------------
00378 
00379 #ifdef GZ_MEM_DEBUG
00380 #ifndef GZ_NO_DEBUG_MEM
00381 #define new new (GZ_DEBUG_INFO(GZ_VERSION_STR))
00382 #define gz_malloc(arg) gz_malloc(arg,GZ_DEBUG_INFO(GZ_VERSION_STR))
00383 #endif
00384 #endif
00385 
00386 #endif
00387 

Documentation for Gizmo3D generated at Wed Feb 20 11:54:06 2008 by   Saab Training Systems AB, ¸ (c) 2003-and beyond