]> git.lizzy.rs Git - irrlicht.git/blob - include/irrAllocator.h
Return nullptr pointer for empty core::array
[irrlicht.git] / include / irrAllocator.h
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
4 \r
5 #ifndef __IRR_ALLOCATOR_H_INCLUDED__\r
6 #define __IRR_ALLOCATOR_H_INCLUDED__\r
7 \r
8 #include "irrTypes.h"\r
9 #include <new>\r
10 // necessary for older compilers\r
11 #include <memory.h>\r
12 \r
13 namespace irr\r
14 {\r
15 namespace core\r
16 {\r
17 \r
18 //! Very simple allocator implementation, containers using it can be used across dll boundaries\r
19 template<typename T>\r
20 class irrAllocator\r
21 {\r
22 public:\r
23 \r
24         //! Destructor\r
25         virtual ~irrAllocator() {}\r
26 \r
27         //! Allocate memory for an array of objects\r
28         T* allocate(size_t cnt)\r
29         {\r
30                 return (T*)internal_new(cnt* sizeof(T));\r
31         }\r
32 \r
33         //! Deallocate memory for an array of objects\r
34         void deallocate(T* ptr)\r
35         {\r
36                 internal_delete(ptr);\r
37         }\r
38 \r
39         //! Construct an element\r
40         void construct(T* ptr, const T&e)\r
41         {\r
42                 new ((void*)ptr) T(e);\r
43         }\r
44 \r
45         //! Destruct an element\r
46         void destruct(T* ptr)\r
47         {\r
48                 ptr->~T();\r
49         }\r
50 \r
51 protected:\r
52 \r
53         virtual void* internal_new(size_t cnt)\r
54         {\r
55                 return operator new(cnt);\r
56         }\r
57 \r
58         virtual void internal_delete(void* ptr)\r
59         {\r
60                 operator delete(ptr);\r
61         }\r
62 \r
63 };\r
64 \r
65 \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
71 {\r
72 public:\r
73 \r
74         //! Allocate memory for an array of objects\r
75         T* allocate(size_t cnt)\r
76         {\r
77                 return (T*)operator new(cnt* sizeof(T));\r
78         }\r
79 \r
80         //! Deallocate memory for an array of objects\r
81         void deallocate(T* ptr)\r
82         {\r
83                 operator delete(ptr);\r
84         }\r
85 \r
86         //! Construct an element\r
87         void construct(T* ptr, const T&e)\r
88         {\r
89                 new ((void*)ptr) T(e);\r
90         }\r
91 \r
92         //! Destruct an element\r
93         void destruct(T* ptr)\r
94         {\r
95                 ptr->~T();\r
96         }\r
97 };\r
98 \r
99 \r
100 //! defines an allocation strategy (used only by irr::array so far)\r
101 enum eAllocStrategy\r
102 {\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
106 };\r
107 \r
108 \r
109 } // end namespace core\r
110 } // end namespace irr\r
111 \r
112 #endif\r
113 \r