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