Open
Graph Drawing
Framework

 v.2012.05
 

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