Irrlicht 3D Engine
irrAllocator.h
Go to the documentation of this file.
00001 // Copyright (C) 2002-2012 Nikolaus Gebhardt
00002 // This file is part of the "Irrlicht Engine" and the "irrXML" project.
00003 // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
00004 
00005 #ifndef __IRR_ALLOCATOR_H_INCLUDED__
00006 #define __IRR_ALLOCATOR_H_INCLUDED__
00007 
00008 #include "irrTypes.h"
00009 #include <new>
00010 // necessary for older compilers
00011 #include <memory.h>
00012 
00013 namespace irr
00014 {
00015 namespace core
00016 {
00017 
00018 #ifdef DEBUG_CLIENTBLOCK
00019 #undef DEBUG_CLIENTBLOCK
00020 #define DEBUG_CLIENTBLOCK new
00021 #endif
00022 
00024 template<typename T>
00025 class irrAllocator
00026 {
00027 public:
00028 
00030     virtual ~irrAllocator() {}
00031 
00033     T* allocate(size_t cnt)
00034     {
00035         return (T*)internal_new(cnt* sizeof(T));
00036     }
00037 
00039     void deallocate(T* ptr)
00040     {
00041         internal_delete(ptr);
00042     }
00043 
00045     void construct(T* ptr, const T&e)
00046     {
00047         new ((void*)ptr) T(e);
00048     }
00049 
00051     void destruct(T* ptr)
00052     {
00053         ptr->~T();
00054     }
00055 
00056 protected:
00057 
00058     virtual void* internal_new(size_t cnt)
00059     {
00060         return operator new(cnt);
00061     }
00062 
00063     virtual void internal_delete(void* ptr)
00064     {
00065         operator delete(ptr);
00066     }
00067 
00068 };
00069 
00070 
00072 
00074 template<typename T>
00075 class irrAllocatorFast
00076 {
00077 public:
00078 
00080     T* allocate(size_t cnt)
00081     {
00082         return (T*)operator new(cnt* sizeof(T));
00083     }
00084 
00086     void deallocate(T* ptr)
00087     {
00088         operator delete(ptr);
00089     }
00090 
00092     void construct(T* ptr, const T&e)
00093     {
00094         new ((void*)ptr) T(e);
00095     }
00096 
00098     void destruct(T* ptr)
00099     {
00100         ptr->~T();
00101     }
00102 };
00103 
00104 
00105 
00106 #ifdef DEBUG_CLIENTBLOCK
00107 #undef DEBUG_CLIENTBLOCK
00108 #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
00109 #endif
00110 
00112 enum eAllocStrategy
00113 {
00114     ALLOC_STRATEGY_SAFE    = 0,
00115     ALLOC_STRATEGY_DOUBLE  = 1,
00116     ALLOC_STRATEGY_SQRT    = 2
00117 };
00118 
00119 
00120 } // end namespace core
00121 } // end namespace irr
00122 
00123 #endif
00124