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

Generated on Wed Nov 23 19:01:06 2011 for FreeCAD by  doxygen 1.6.1