00001
00002
00003
00004
00005 #ifndef __IRR_ALLOCATOR_H_INCLUDED__
00006 #define __IRR_ALLOCATOR_H_INCLUDED__
00007
00008 #include "irrTypes.h"
00009 #include <new>
00010 #include <memory.h>
00011
00012 namespace irr
00013 {
00014 namespace core
00015 {
00016
00017 #ifdef DEBUG_CLIENTBLOCK
00018 #undef DEBUG_CLIENTBLOCK
00019 #define DEBUG_CLIENTBLOCK new
00020 #endif
00021
00023 template<typename T>
00024 class irrAllocator
00025 {
00026 public:
00027
00029 virtual ~irrAllocator() {}
00030
00032 T* allocate(size_t cnt)
00033 {
00034 return (T*)internal_new(cnt* sizeof(T));
00035 }
00036
00038 void deallocate(T* ptr)
00039 {
00040 internal_delete(ptr);
00041 }
00042
00044 void construct(T* ptr, const T&e)
00045 {
00046 new ((void*)ptr) T(e);
00047 }
00048
00050 void destruct(T* ptr)
00051 {
00052 ptr->~T();
00053 }
00054
00055 protected:
00056
00057 virtual void* internal_new(size_t cnt)
00058 {
00059 return operator new(cnt);
00060 }
00061
00062 virtual void internal_delete(void* ptr)
00063 {
00064 operator delete(ptr);
00065 }
00066
00067 };
00068
00069
00071
00073 template<typename T>
00074 class irrAllocatorFast
00075 {
00076 public:
00077
00079 T* allocate(size_t cnt)
00080 {
00081 return (T*)operator new(cnt* sizeof(T));
00082 }
00083
00085 void deallocate(T* ptr)
00086 {
00087 operator delete(ptr);
00088 }
00089
00091 void construct(T* ptr, const T&e)
00092 {
00093 new ((void*)ptr) T(e);
00094 }
00095
00097 void destruct(T* ptr)
00098 {
00099 ptr->~T();
00100 }
00101 };
00102
00103
00104
00105 #ifdef DEBUG_CLIENTBLOCK
00106 #undef DEBUG_CLIENTBLOCK
00107 #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
00108 #endif
00109
00110
00111 }
00112 }
00113
00114 #endif
00115