Open
Graph Drawing
Framework

 v.2010.10
 

memory.h

Go to the documentation of this file.
00001 /*
00002  * $Revision: 2027 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2010-09-01 11:55:17 +0200 (Wed, 01 Sep 2010) $ 
00007  ***************************************************************/
00008  
00054 #ifdef _MSC_VER
00055 #pragma once
00056 #endif
00057 
00058 #ifndef OGDF_MEMORY_H
00059 #define OGDF_MEMORY_H
00060 
00061 #if defined(OGDF_SYSTEM_WINDOWS) || defined(__CYGWIN__)
00062 #define WIN32_EXTRA_LEAN
00063 #define WIN32_LEAN_AND_MEAN
00064 #define NOMINMAX
00065 #ifndef _WIN32_WINNT
00066 #define _WIN32_WINNT 0x0500
00067 #endif
00068 #include <windows.h>
00069 #endif
00070 
00071 #include <stdlib.h>
00072 #include <new>
00073 
00074 struct MemElem { MemElem *m_next; };
00075 typedef MemElem *MemElemPtr;
00076 
00077 //---------------------------------------------------------------------
00078 // configuration of memory-manager (can also be set by compiler flag)
00079 //
00080 // the good old pool allocator (not thread-safe)
00081 //#define OGDF_MEMORY_POOL_NTS
00082 //
00083 // just using malloc/free (thread-safe)
00084 //#define OGDF_MEMORY_MALLOC_TS
00085 //
00086 // new buffered-pool allocator per thread pool (thread-safe)
00087 //#define OGDF_MEMORY_POOL_TS
00088 //
00089 // default (nothing defined): depending on system / compiler
00090 //---------------------------------------------------------------------
00091 
00092 // By default, we use the non-thread safe variant on cygwin and g++ 3.x
00093 // since thread-local storage is not working there, and a thread-safe
00094 // pool allocator otherwise.
00095 #if !defined(OGDF_MEMORY_POOL_NTS) && !defined(OGDF_MEMORY_MALLOC_TS) && !defined(OGDF_MEMORY_POOL_TS)
00096 #define OGDF_MEMORY_POOL_TS
00097 #endif
00098 
00099 #include <ogdf/internal/basic/PoolMemoryAllocator.h>
00100 #include <ogdf/internal/basic/MallocMemoryAllocator.h>
00101 
00102 
00103 namespace ogdf {
00104 
00105 #define OGDF_MM(Alloc) \
00106 public: \
00107 void *operator new(size_t nBytes) { \
00108     if(OGDF_LIKELY(Alloc::checkSize(nBytes))) \
00109         return Alloc::allocate(nBytes); \
00110     else \
00111     return MallocMemoryAllocator::allocate(nBytes); \
00112 } \
00113 void *operator new(size_t, void *p) { return p; } \
00114 void operator delete(void *p, size_t nBytes) { \
00115     if(OGDF_LIKELY(p != 0)) { \
00116         if(OGDF_LIKELY(Alloc::checkSize(nBytes))) \
00117             Alloc::deallocate(nBytes, p); \
00118         else \
00119             MallocMemoryAllocator::deallocate(nBytes, p); \
00120     } \
00121 }
00122 
00123 #define OGDF_NEW new
00124 
00125 #ifdef OGDF_MEMORY_MALLOC_TS
00126 #define OGDF_ALLOCATOR ogdf::MallocMemoryAllocator
00127 #else
00128 #define OGDF_ALLOCATOR ogdf::PoolMemoryAllocator
00129 #endif
00130 
00132 #define OGDF_NEW_DELETE OGDF_MM(OGDF_ALLOCATOR)
00133 
00135 #define OGDF_MALLOC_NEW_DELETE OGDF_MM(MallocMemoryAllocator)
00136 
00137 } // namespace ogdf
00138 
00139 
00140 #endif