AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Allocator.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_ALLOCATOR_H
18 #define AGX_ALLOCATOR_H
19 
21 #include <stdlib.h>
22 #include <agx/agxCore_export.h>
23 #include <agx/Math.h>
24 #include <agx/Integer.h>
25 #include <agx/debug.h>
26 
27 
28 #if defined(AGX_DEBUG)
29 //#define AGX_TRACK_ALLOCATIONS
30 #endif
31 
35 #if defined(AGX_TRACK_ALLOCATIONS)
36 #ifdef _MSC_VER
37 //Visual Studio does not implement declarations using exception specification. Turn off the warning
38 # pragma warning(push)
39 # pragma warning( disable : 4290 )
40 #endif
41 void *operator new(size_t size) throw(std::bad_alloc);
42 void *operator new(size_t size, void *ptr) throw();
43 void operator delete(void *ptr) throw();
44 #endif
45 
46 // #define agxNew(type) new (Thread::getCurrentThread()->allocate<type>())
47 
48 namespace agx
49 {
50 
55  {
56  public:
57  ByteAllocator(const char *name = "ByteAllocator");
58 
59  ByteAllocator(const ByteAllocator& other);
60  ByteAllocator& operator=(const ByteAllocator& other);
61 
63  const char *getName() const;
64 
66  void *allocateBytes(size_t numBytes, size_t alignment = 16);
67 
69  void deallocateBytes(void *buffer, size_t numBytes);
70  void deallocateBytes(void *buffer);
72 
73 
74  // Allocation and deallocation that isn't tracked.
75  static void* standaloneAllocateBytes(size_t numBytes, size_t alignment = 16);
76  static void standaloneDeallocateTypes(void* buffer);
77 
78  static bool initMemoryFlag; // Set true if allocated memory should be initialized
79  static agx::UInt32 allocatedMemoryPattern; // The byte pattern which the initialized memory is given
80  static agx::UInt32 deallocatedMemoryPattern; // The byte pattern which the initialized memory is given
81  static void initMemory(void *data, size_t numBytes, agx::UInt32 pattern);
82 
83 
84  #ifdef AGX_TRACK_ALLOCATIONS
85  size_t getNumActiveAllocations() const;
86  size_t getTotalActiveAllocationSize() const;
87  #endif
88 
89  private:
90  #if AGX_DEBUG_INTRUSIVE()
91  const char *m_name;
92  #endif
93 
94 
95  #ifdef AGX_TRACK_ALLOCATIONS
96  size_t m_numActiveAllocations;
97  size_t m_totalActiveAllocationSize;
98  #endif
99  };
100 
101 
105  template <typename T>
106  class Allocator : public ByteAllocator
107  {
108  public:
109  Allocator(const char *name = "Allocator");
110 
112  T *allocate(size_t numElements);
113  void deallocate(T *buffer);
114 
115  /* Allocate/deallocate and run constructors/destructors */
116  // T *create(size_t numElements);
117  // void destroy(T *buffer);
118  };
119 
120 
121 
122 
123 
124 
128  template <typename T>
129  class AlignedAllocator : public Allocator<T>
130  {
131  public:
132  AlignedAllocator(const char *name = "AlignedAllocator", agx::UInt32 aligment = 128);
133 
134  /* Allocate/deallocate raw memory */
135  T *allocate(size_t numElements);
136  void deallocate(T *buffer);
137 
138  /* Allocate/deallocate and run constructors/destructors */
139  //T *create(size_t numElements);
140  //void destroy(T *buffer);
141 
142 
143  private:
144  agx::UInt32 m_alignment;
145  };
146 
147 
148 
149 
150  /* Implementation */
151  #if AGX_DEBUG_INTRUSIVE()
152  inline ByteAllocator::ByteAllocator(const char *name)
153  : m_name(name)
154  #else
155  inline ByteAllocator::ByteAllocator(const char * /*name*/)
156  #endif
157  {
158  #ifdef AGX_TRACK_ALLOCATIONS
159  m_numActiveAllocations = 0;
160  m_totalActiveAllocationSize = 0;
161  #endif
162  }
163 
165  {
166  #if AGX_DEBUG_INTRUSIVE()
167  return m_name;
168  #else
169  return "AllocatorNameOnlyAvaiableInDebugBuild";
170  #endif
171  }
172 
173  AGX_FORCE_INLINE void ByteAllocator::deallocateBytes(void *buffer) { this->deallocateBytes(buffer, 0); }
174 
175 
176  //-----------------------------------------------------------------------------------------------------
177 
178  template <typename T>
179  inline Allocator<T>::Allocator(const char *name) : ByteAllocator(name)
180  {
181  }
182 
183 
184  template <typename T>
186  {
187  return reinterpret_cast<T*>(ByteAllocator::allocateBytes(numElements * sizeof(T)));
188  }
189 
190  template <typename T>
192  {
193  ByteAllocator::deallocateBytes((void *)buffer);
194  }
195 
196 
197  //-----------------------------------------------------------------------------------------------------
198 
199  template <typename T>
200  inline AlignedAllocator<T>::AlignedAllocator(const char *name, agx::UInt32 aligment) : Allocator<T>(name), m_alignment(aligment)
201  {
202  }
203 
204 
205  template <typename T>
207  {
208  return reinterpret_cast<T*>(ByteAllocator::allocateBytes(numElements * sizeof(T), m_alignment));
209  }
210 
211  template <typename T>
213  {
214  ByteAllocator::deallocateBytes((void *)buffer);
215  }
216 
217 }
218 
219 #if defined(AGX_TRACK_ALLOCATIONS)
220 # ifdef _MSC_VER
221 # pragma warning(pop)
222 # endif
223 #endif
224 
225 #endif /* _AGX_ALLOCATOR_H_ */
Aligned allocator.
Definition: Allocator.h:129
The Container is the base class for several of the container classes proided by AGX, such as agx::Vector and agx::HashSet.
Definition: Container.h:34
void deallocate(T *buffer)
Definition: Allocator.h:191
Templated allocator.
Definition: Allocator.h:106
const char * getName() const
Definition: Allocator.h:164
T * allocate(size_t numElements)
Definition: Allocator.h:206
AlignedAllocator(const char *name="AlignedAllocator", agx::UInt32 aligment=128)
Definition: Allocator.h:200
static agx::UInt32 allocatedMemoryPattern
Definition: Allocator.h:79
#define AGX_FORCE_INLINE
Definition: Object.h:62
Allocator(const char *name="Allocator")
Definition: Allocator.h:179
void deallocateBytes(void *buffer, size_t numBytes)
Deallocate a section previously allocated using allocateBytes method.
static bool initMemoryFlag
Definition: Allocator.h:78
void * allocateBytes(size_t numBytes, size_t alignment=16)
Allocate a memory section of specified alignment.
Byte allocator.
Definition: Allocator.h:54
static agx::UInt32 deallocatedMemoryPattern
Definition: Allocator.h:80
void setContainer(class Container *)
Definition: Allocator.h:71
T * allocate(size_t numElements)
Allocate/deallocate raw memory.
Definition: Allocator.h:185
void deallocate(T *buffer)
Definition: Allocator.h:212
ByteAllocator(const char *name="ByteAllocator")
Definition: Allocator.h:155
uint32_t UInt32
Definition: Integer.h:40
#define AGXCORE_EXPORT