arsa  2.7
irrAllocator.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine" and the "irrXML" project.
3 // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
4 
5 #ifndef __IRR_ALLOCATOR_H_INCLUDED__
6 #define __IRR_ALLOCATOR_H_INCLUDED__
7 
8 #include "irrTypes.h"
9 #include <new>
10 // necessary for older compilers
11 #include <memory.h>
12 
13 namespace irr
14 {
15 namespace core
16 {
17 
18 #ifdef DEBUG_CLIENTBLOCK
19 #undef DEBUG_CLIENTBLOCK
20 #define DEBUG_CLIENTBLOCK new
21 #endif
22 
24 template<typename T>
26 {
27 public:
28 
30  virtual ~irrAllocator() {}
31 
33  T* allocate(size_t cnt)
34  {
35  return (T*)internal_new(cnt* sizeof(T));
36  }
37 
39  void deallocate(T* ptr)
40  {
41  internal_delete(ptr);
42  }
43 
45  void construct(T* ptr, const T&e)
46  {
47  new ((void*)ptr) T(e);
48  }
49 
51  void destruct(T* ptr)
52  {
53  ptr->~T();
54  }
55 
56 protected:
57 
58  virtual void* internal_new(size_t cnt)
59  {
60  return operator new(cnt);
61  }
62 
63  virtual void internal_delete(void* ptr)
64  {
65  operator delete(ptr);
66  }
67 
68 };
69 
70 
72 
74 template<typename T>
76 {
77 public:
78 
80  T* allocate(size_t cnt)
81  {
82  return (T*)operator new(cnt* sizeof(T));
83  }
84 
86  void deallocate(T* ptr)
87  {
88  operator delete(ptr);
89  }
90 
92  void construct(T* ptr, const T&e)
93  {
94  new ((void*)ptr) T(e);
95  }
96 
98  void destruct(T* ptr)
99  {
100  ptr->~T();
101  }
102 };
103 
104 
105 
106 #ifdef DEBUG_CLIENTBLOCK
107 #undef DEBUG_CLIENTBLOCK
108 #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
109 #endif
110 
113 {
114  ALLOC_STRATEGY_SAFE = 0, // increase size by 1
115  ALLOC_STRATEGY_DOUBLE = 1, // double size when under 500 elements, beyond that increase by 1/4th size. Plus a small constant.
116  ALLOC_STRATEGY_SQRT = 2 // not implemented
117 };
118 
119 
120 } // end namespace core
121 } // end namespace irr
122 
123 #endif
124 
virtual ~irrAllocator()
Destructor.
Definition: irrAllocator.h:30
void construct(T *ptr, const T &e)
Construct an element.
Definition: irrAllocator.h:92
void deallocate(T *ptr)
Deallocate memory for an array of objects.
Definition: irrAllocator.h:86
void destruct(T *ptr)
Destruct an element.
Definition: irrAllocator.h:51
Everything in the Irrlicht Engine can be found in this namespace.
Definition: CARSADPad.h:6
T * allocate(size_t cnt)
Allocate memory for an array of objects.
Definition: irrAllocator.h:80
eAllocStrategy
defines an allocation strategy (used only by irr::array so far)
Definition: irrAllocator.h:112
Fast allocator, only to be used in containers inside the same memory heap.
Definition: irrAllocator.h:75
void destruct(T *ptr)
Destruct an element.
Definition: irrAllocator.h:98
T * allocate(size_t cnt)
Allocate memory for an array of objects.
Definition: irrAllocator.h:33
virtual void internal_delete(void *ptr)
Definition: irrAllocator.h:63
void construct(T *ptr, const T &e)
Construct an element.
Definition: irrAllocator.h:45
virtual void * internal_new(size_t cnt)
Definition: irrAllocator.h:58
Very simple allocator implementation, containers using it can be used across dll boundaries.
Definition: irrAllocator.h:25
void deallocate(T *ptr)
Deallocate memory for an array of objects.
Definition: irrAllocator.h:39