Gainput  v1.0.0
GainputAllocator.h
1 
2 #ifndef GAINPUTALLOCATOR_H_
3 #define GAINPUTALLOCATOR_H_
4 
5 namespace gainput
6 {
7 
9 
16 class GAINPUT_LIBEXPORT Allocator
17 {
18 public:
19  enum { DefaultAlign = 0 };
20 
22 
26  virtual void* Allocate(size_t size, size_t align = DefaultAlign) = 0;
28 
31  virtual void Deallocate(void* ptr) = 0;
32 
34 
37  template <class T>
38  T* New()
39  {
40  return new (Allocate(sizeof(T))) T();
41  }
42 
44 
47  template <class T, class P0>
48  T* New(P0& p0)
49  {
50  return new (Allocate(sizeof(T))) T(p0);
51  }
52 
54 
57  template <class T, class P0>
58  T* New(const P0& p0)
59  {
60  return new (Allocate(sizeof(T))) T(p0);
61  }
62 
64 
67  template <class T, class P0, class P1>
68  T* New(P0& p0, P1& p1)
69  {
70  return new (Allocate(sizeof(T))) T(p0, p1);
71  }
72 
74 
77  template <class T, class P0, class P1>
78  T* New(const P0& p0, P1& p1)
79  {
80  return new (Allocate(sizeof(T))) T(p0, p1);
81  }
82 
84 
87  template <class T, class P0, class P1>
88  T* New(P0& p0, const P1& p1)
89  {
90  return new (Allocate(sizeof(T))) T(p0, p1);
91  }
92 
94 
97  template <class T, class P0, class P1, class P2>
98  T* New(P0& p0, const P1& p1, const P2& p2)
99  {
100  return new (Allocate(sizeof(T))) T(p0, p1, p2);
101  }
102 
104 
107  template <class T, class P0, class P1, class P2>
108  T* New(P0& p0, const P1& p1, P2& p2)
109  {
110  return new (Allocate(sizeof(T))) T(p0, p1, p2);
111  }
112 
114 
117  template <class T, class P0, class P1, class P2, class P3>
118  T* New(P0& p0, P1& p1, P2& p2, P3& p3)
119  {
120  return new (Allocate(sizeof(T))) T(p0, p1, p2, p3);
121  }
122 
124 
127  template <class T, class P0, class P1, class P2, class P3>
128  T* New(P0& p0, const P1& p1, P2& p2, P3& p3)
129  {
130  return new (Allocate(sizeof(T))) T(p0, p1, p2, p3);
131  }
132 
134 
137  template <class T, class P0, class P1, class P2, class P3, class P4>
138  T* New(P0& p0, P1& p1, P2& p2, P3& p3, P4& p4)
139  {
140  return new (Allocate(sizeof(T))) T(p0, p1, p2, p3, p4);
141  }
142 
144 
147  template <class T, class P0, class P1, class P2, class P3, class P4>
148  T* New(P0& p0, const P1& p1, P2& p2, P3& p3, P4& p4)
149  {
150  return new (Allocate(sizeof(T))) T(p0, p1, p2, p3, p4);
151  }
152 
154 
157  template <class T>
158  void Delete(T* ptr)
159  {
160  if (ptr)
161  {
162  ptr->~T();
163  Deallocate(ptr);
164  }
165  }
166 };
167 
169 
173 class GAINPUT_LIBEXPORT DefaultAllocator : public Allocator
174 {
175 public:
176  void* Allocate(size_t size, size_t /*align*/)
177  {
178  return malloc(size);
179  }
180 
181  void Deallocate(void* ptr)
182  {
183  free(ptr);
184  }
185 };
186 
188 
191 GAINPUT_LIBEXPORT DefaultAllocator& GetDefaultAllocator();
192 
193 template<class K, class V>
194 class GAINPUT_LIBEXPORT HashMap;
195 
197 
201 class GAINPUT_LIBEXPORT TrackingAllocator : public Allocator
202 {
203 public:
204  TrackingAllocator(Allocator& backingAllocator, Allocator& internalAllocator = GetDefaultAllocator());
206 
207  void* Allocate(size_t size, size_t align = DefaultAlign);
208  void Deallocate(void* ptr);
209 
210  size_t GetAllocateCount() const { return allocateCount_; }
211  size_t GetDeallocateCount() const { return deallocateCount_; }
212  size_t GetAllocatedMemory() const { return allocatedMemory_; }
213 
214 private:
215  Allocator& backingAllocator_;
216  Allocator& internalAllocator_;
217  HashMap<void*, size_t>* allocations_;
218  size_t allocateCount_;
219  size_t deallocateCount_;
220  size_t allocatedMemory_;
221 };
222 
223 
224 
225 }
226 
227 #endif
228 
T * New(P0 &p0, const P1 &p1)
An operator new-like function that allocates memory and calls T&#39;s constructor with two parameters...
Definition: GainputAllocator.h:88
T * New(P0 &p0, const P1 &p1, P2 &p2, P3 &p3)
An operator new-like function that allocates memory and calls T&#39;s constructor with the given paramete...
Definition: GainputAllocator.h:128
T * New(P0 &p0, const P1 &p1, const P2 &p2)
An operator new-like function that allocates memory and calls T&#39;s constructor with the given paramete...
Definition: GainputAllocator.h:98
GAINPUT_LIBEXPORT DefaultAllocator & GetDefaultAllocator()
Returns the default instance of the default allocator.
void Delete(T *ptr)
An operator delete-like function that calls ptr&#39;s constructor and deallocates the memory...
Definition: GainputAllocator.h:158
An allocator that tracks an allocations that were done.
Definition: GainputAllocator.h:201
T * New(P0 &p0)
An operator new-like function that allocates memory and calls T&#39;s constructor with one parameter...
Definition: GainputAllocator.h:48
The default allocator used by the library.
Definition: GainputAllocator.h:173
T * New(const P0 &p0)
An operator new-like function that allocates memory and calls T&#39;s constructor with one parameter...
Definition: GainputAllocator.h:58
T * New(P0 &p0, P1 &p1)
An operator new-like function that allocates memory and calls T&#39;s constructor with two parameters...
Definition: GainputAllocator.h:68
void Deallocate(void *ptr)
Deallocates the given memory.
Definition: GainputAllocator.h:181
void * Allocate(size_t size, size_t)
Allocates a number of bytes and returns a pointer to the allocated memory.
Definition: GainputAllocator.h:176
T * New(const P0 &p0, P1 &p1)
An operator new-like function that allocates memory and calls T&#39;s constructor with two parameters...
Definition: GainputAllocator.h:78
T * New(P0 &p0, const P1 &p1, P2 &p2, P3 &p3, P4 &p4)
An operator new-like function that allocates memory and calls T&#39;s constructor with the given paramete...
Definition: GainputAllocator.h:148
T * New(P0 &p0, P1 &p1, P2 &p2, P3 &p3)
An operator new-like function that allocates memory and calls T&#39;s constructor with the given paramete...
Definition: GainputAllocator.h:118
T * New(P0 &p0, P1 &p1, P2 &p2, P3 &p3, P4 &p4)
An operator new-like function that allocates memory and calls T&#39;s constructor with the given paramete...
Definition: GainputAllocator.h:138
T * New()
An operator new-like function that allocates memory and calls T&#39;s constructor.
Definition: GainputAllocator.h:38
T * New(P0 &p0, const P1 &p1, P2 &p2)
An operator new-like function that allocates memory and calls T&#39;s constructor with the given paramete...
Definition: GainputAllocator.h:108
Interface used to pass custom allocators to the library.
Definition: GainputAllocator.h:16
Contains all Gainput related classes, types, and functions.
Definition: gainput.h:103