1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine" and the "irrXML" project.
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
\r
5 #ifndef __IRR_ALLOCATOR_H_INCLUDED__
\r
6 #define __IRR_ALLOCATOR_H_INCLUDED__
\r
8 #include "irrTypes.h"
\r
10 // necessary for older compilers
\r
18 #ifdef DEBUG_CLIENTBLOCK
\r
19 #undef DEBUG_CLIENTBLOCK
\r
20 #define DEBUG_CLIENTBLOCK new
\r
23 //! Very simple allocator implementation, containers using it can be used across dll boundaries
\r
24 template<typename T>
\r
30 virtual ~irrAllocator() {}
\r
32 //! Allocate memory for an array of objects
\r
33 T* allocate(size_t cnt)
\r
35 return (T*)internal_new(cnt* sizeof(T));
\r
38 //! Deallocate memory for an array of objects
\r
39 void deallocate(T* ptr)
\r
41 internal_delete(ptr);
\r
44 //! Construct an element
\r
45 void construct(T* ptr, const T&e)
\r
47 new ((void*)ptr) T(e);
\r
50 //! Destruct an element
\r
51 void destruct(T* ptr)
\r
58 virtual void* internal_new(size_t cnt)
\r
60 return operator new(cnt);
\r
63 virtual void internal_delete(void* ptr)
\r
65 operator delete(ptr);
\r
71 //! Fast allocator, only to be used in containers inside the same memory heap.
\r
72 /** Containers using it are NOT able to be used it across dll boundaries. Use this
\r
73 when using in an internal class or function or when compiled into a static lib */
\r
74 template<typename T>
\r
75 class irrAllocatorFast
\r
79 //! Allocate memory for an array of objects
\r
80 T* allocate(size_t cnt)
\r
82 return (T*)operator new(cnt* sizeof(T));
\r
85 //! Deallocate memory for an array of objects
\r
86 void deallocate(T* ptr)
\r
88 operator delete(ptr);
\r
91 //! Construct an element
\r
92 void construct(T* ptr, const T&e)
\r
94 new ((void*)ptr) T(e);
\r
97 //! Destruct an element
\r
98 void destruct(T* ptr)
\r
106 #ifdef DEBUG_CLIENTBLOCK
\r
107 #undef DEBUG_CLIENTBLOCK
\r
108 #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
\r
111 //! defines an allocation strategy (used only by irr::array so far)
\r
112 enum eAllocStrategy
\r
114 ALLOC_STRATEGY_SAFE = 0, // increase size by 1
\r
115 ALLOC_STRATEGY_DOUBLE = 1, // double size when under 500 elements, beyond that increase by 1/4th size. Plus a small constant.
\r
116 ALLOC_STRATEGY_SQRT = 2 // not implemented
\r
120 } // end namespace core
\r
121 } // end namespace irr
\r