Wm4Memory.h

Go to the documentation of this file.
00001 // Wild Magic Source Code
00002 // David Eberly
00003 // http://www.geometrictools.com
00004 // Copyright (c) 1998-2008
00005 //
00006 // This library is free software; you can redistribute it and/or modify it
00007 // under the terms of the GNU Lesser General Public License as published by
00008 // the Free Software Foundation; either version 2.1 of the License, or (at
00009 // your option) any later version.  The license is available for reading at
00010 // either of the locations:
00011 //     http://www.gnu.org/copyleft/lgpl.html
00012 //     http://www.geometrictools.com/License/WildMagicLicense.pdf
00013 //
00014 // Version: 4.0.0 (2006/06/28)
00015 
00016 #ifndef WM4MEMORY_H
00017 #define WM4MEMORY_H
00018 
00019 #ifndef WM4_MEMORY_MANAGER
00020 
00021 // Use the default memory manager.
00022 #define WM4_NEW new
00023 #define WM4_DELETE delete
00024 
00025 #else
00026 
00027 // Overrides of the global new and delete operators.  These enhance the
00028 // default memory manager by keeping track of information about allocations
00029 // and deallocations.
00030 
00031 #include "Wm4FoundationLIB.h"
00032 #include "Wm4Platforms.h"
00033 
00034 namespace Wm4
00035 {
00036 
00037 class WM4_FOUNDATION_ITEM Memory
00038 {
00039 public:
00040     // The memory chunks have information prepended of the following data
00041     // type.  The blocks are inserted and removed from a doubly linked list.
00042     struct Block
00043     {
00044         size_t Size;
00045         const char* File;
00046         unsigned int Line;
00047         bool IsArray;
00048         Block* Prev;
00049         Block* Next;
00050     };
00051 
00052     // read-write members
00053     static size_t& MaxAllowedBytes ();
00054     static bool& TrackSizes ();
00055 
00056     // read-only members
00057     static size_t GetNumNewCalls ();
00058     static size_t GetNumDeleteCalls ();
00059     static size_t GetNumBlocks ();
00060     static size_t GetNumBytes ();
00061     static size_t GetMaxAllocatedBytes ();
00062     static size_t GetMaxBlockSize ();
00063     static size_t GetHistogram (int i);
00064 
00065     // For iteration over the current list of memory blocks.
00066     static const Block* GetHead ();
00067     static const Block* GetTail ();
00068 
00069     // Generate a report about the current list memory blocks.
00070     static void GenerateReport (const char* acFilename);
00071 
00072 private:
00073     // Count the number of times the memory allocation/deallocation system
00074     // has been entered.
00075     static size_t ms_uiNumNewCalls;
00076     static size_t ms_uiNumDeleteCalls;
00077 
00078     // Set this value in your application if you want to know when NumBytes
00079     // exceeds a maximum allowed number of bytes.  An 'assert' will be
00080     // triggered in Allocate when this happens.  The default value is 0, in
00081     // which case no comparison is made between NumBytes and MaxAllowedBytes.
00082     static size_t ms_uiMaxAllowedBytes;
00083 
00084     // The current number of allocated memory blocks.
00085     static size_t ms_uiNumBlocks;
00086 
00087     // The current number of allocated bytes.
00088     static size_t ms_uiNumBytes;
00089 
00090     // Doubly linked list of headers for the memory blocks.
00091     static Block* ms_pkHead;
00092     static Block* ms_pkTail;
00093 
00094     // Set this variable to 'true' if you want the ms_uiMaxBlockSize and
00095     // ms_auiHistogram[] elements to be computed.  The default is 'false'.
00096     static bool ms_bTrackSizes;
00097 
00098     // The maximum number of bytes allocated by the application.
00099     static size_t ms_uiMaxAllocatedBytes;
00100 
00101     // The size of the largest memory block allocated by the application.
00102     static size_t ms_uiMaxBlockSize;
00103 
00104     // Keep track of the number of allocated blocks of various sizes.  The
00105     // element Histogram[0] stores the number of allocated blocks of size 1.
00106     // The element Histogram[31] stores the number of allocated blocks of
00107     // size larger than pow(2,30).  For 1 <= i <= 30, the element Histogram[i]
00108     // stores the number of allocated blocks of size N, where
00109     // pow(2,i-1) < N <= pow(2,i).
00110     static size_t ms_auiHistogram[32];
00111 
00112 // internal use
00113 public:
00114     static void* Allocate (size_t uiSize, char* acFile, unsigned int uiLine,
00115         bool bIsArray);
00116     static void Deallocate (char* pcAddr, bool bIsArray);
00117     static void InsertBlock (Block* pkBlock);
00118     static void RemoveBlock (Block* pkBlock);
00119 };
00120 
00121 #include "Wm4Memory.inl"
00122 
00123 }
00124 
00125 #define WM4_NEW new(__FILE__,__LINE__)
00126 #define WM4_DELETE delete
00127 
00128 void* operator new (size_t uiSize);
00129 void* operator new[](size_t uiSize);
00130 void* operator new (size_t uiSize, char* acFile, unsigned int uiLine);
00131 void* operator new[] (size_t uiSize, char* acFile, unsigned int uiLine);
00132 void operator delete (void* pvAddr);
00133 void operator delete[] (void* pvAddr);
00134 void operator delete (void* pvAddr, char* acFile, unsigned int uiLine);
00135 void operator delete[] (void* pvAddr, char* acFile, unsigned int uiLine);
00136 
00137 #endif
00138 #endif

Generated on Fri Feb 13 13:58:10 2009 for meshmorph by  doxygen 1.5.1