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 //! Very simple allocator implementation, containers using it can be used across dll boundaries
\r
19 template<typename T>
\r
25 virtual ~irrAllocator() {}
\r
27 //! Allocate memory for an array of objects
\r
28 T* allocate(size_t cnt)
\r
30 return (T*)internal_new(cnt* sizeof(T));
\r
33 //! Deallocate memory for an array of objects
\r
34 void deallocate(T* ptr)
\r
36 internal_delete(ptr);
\r
39 //! Construct an element
\r
40 void construct(T* ptr, const T&e)
\r
42 new ((void*)ptr) T(e);
\r
45 //! Destruct an element
\r
46 void destruct(T* ptr)
\r
53 virtual void* internal_new(size_t cnt)
\r
55 return operator new(cnt);
\r
58 virtual void internal_delete(void* ptr)
\r
60 operator delete(ptr);
\r
66 //! Fast allocator, only to be used in containers inside the same memory heap.
\r
67 /** Containers using it are NOT able to be used it across dll boundaries. Use this
\r
68 when using in an internal class or function or when compiled into a static lib */
\r
69 template<typename T>
\r
70 class irrAllocatorFast
\r
74 //! Allocate memory for an array of objects
\r
75 T* allocate(size_t cnt)
\r
77 return (T*)operator new(cnt* sizeof(T));
\r
80 //! Deallocate memory for an array of objects
\r
81 void deallocate(T* ptr)
\r
83 operator delete(ptr);
\r
86 //! Construct an element
\r
87 void construct(T* ptr, const T&e)
\r
89 new ((void*)ptr) T(e);
\r
92 //! Destruct an element
\r
93 void destruct(T* ptr)
\r
100 //! defines an allocation strategy (used only by irr::array so far)
\r
101 enum eAllocStrategy
\r
103 ALLOC_STRATEGY_SAFE = 0, // increase size by 1
\r
104 ALLOC_STRATEGY_DOUBLE = 1, // double size when under 500 elements, beyond that increase by 1/4th size. Plus a small constant.
\r
105 ALLOC_STRATEGY_SQRT = 2 // not implemented
\r
109 } // end namespace core
\r
110 } // end namespace irr
\r