]> git.lizzy.rs Git - irrlicht.git/blob - include/irrAllocator.h
Avoid some warnings from static code analysis.
[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 #ifdef DEBUG_CLIENTBLOCK\r
19 #undef DEBUG_CLIENTBLOCK\r
20 #define DEBUG_CLIENTBLOCK new\r
21 #endif\r
22 \r
23 //! Very simple allocator implementation, containers using it can be used across dll boundaries\r
24 template<typename T>\r
25 class irrAllocator\r
26 {\r
27 public:\r
28 \r
29         //! Destructor\r
30         virtual ~irrAllocator() {}\r
31 \r
32         //! Allocate memory for an array of objects\r
33         T* allocate(size_t cnt)\r
34         {\r
35                 return (T*)internal_new(cnt* sizeof(T));\r
36         }\r
37 \r
38         //! Deallocate memory for an array of objects\r
39         void deallocate(T* ptr)\r
40         {\r
41                 internal_delete(ptr);\r
42         }\r
43 \r
44         //! Construct an element\r
45         void construct(T* ptr, const T&e)\r
46         {\r
47                 new ((void*)ptr) T(e);\r
48         }\r
49 \r
50         //! Destruct an element\r
51         void destruct(T* ptr)\r
52         {\r
53                 ptr->~T();\r
54         }\r
55 \r
56 protected:\r
57 \r
58         virtual void* internal_new(size_t cnt)\r
59         {\r
60                 return operator new(cnt);\r
61         }\r
62 \r
63         virtual void internal_delete(void* ptr)\r
64         {\r
65                 operator delete(ptr);\r
66         }\r
67 \r
68 };\r
69 \r
70 \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
76 {\r
77 public:\r
78 \r
79         //! Allocate memory for an array of objects\r
80         T* allocate(size_t cnt)\r
81         {\r
82                 return (T*)operator new(cnt* sizeof(T));\r
83         }\r
84 \r
85         //! Deallocate memory for an array of objects\r
86         void deallocate(T* ptr)\r
87         {\r
88                 operator delete(ptr);\r
89         }\r
90 \r
91         //! Construct an element\r
92         void construct(T* ptr, const T&e)\r
93         {\r
94                 new ((void*)ptr) T(e);\r
95         }\r
96 \r
97         //! Destruct an element\r
98         void destruct(T* ptr)\r
99         {\r
100                 ptr->~T();\r
101         }\r
102 };\r
103 \r
104 \r
105 \r
106 #ifdef DEBUG_CLIENTBLOCK\r
107 #undef DEBUG_CLIENTBLOCK\r
108 #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)\r
109 #endif\r
110 \r
111 //! defines an allocation strategy (used only by irr::array so far)\r
112 enum eAllocStrategy\r
113 {\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
117 };\r
118 \r
119 \r
120 } // end namespace core\r
121 } // end namespace irr\r
122 \r
123 #endif\r
124 \r