29#ifdef MAIA_EXTRA_DEBUG
32 MString allocatedObjectName = (objectName == elementsName) ? objectName : objectName + elementsName;
33 typedef std::vector<GenericObject*>::iterator GenericObjIt;
35 if((*i)->getName() == allocatedObjectName) {
36 std::cerr <<
"Warning: object " << objectName <<
" has already been allocated!" << std::endl;
37 m_log <<
"Warning: object " << objectName <<
" has already been allocated!" << std::endl;
44 m_log <<
"Creating collector '" << objectName <<
"' with " << maxSize <<
" elements as requested by " << function
50 m_log <<
"Creating list '" << objectName <<
"' with " << maxSize <<
" elements as requested by " << function <<
"."
55inline void debug(
const MString&) {}
71 if(
static_cast<T*
>((*object).getObjectPointer()) ==
a) {
74 if((*object).objectIsArray) {
76 while((j !=
g_allocatedObjects.end()) && ((*j)->getObjectId() == (*i)->getObjectId())) {
91 std::vector<GenericObject*>::iterator hit;
97#ifdef MAIA_DEBUG_ALLOC
101 std::cerr <<
"Warning in mAlloc: pointer for object '" << objectName
102 <<
"' already in list of allocated objects, call mDeallocate before reallocating! " << function
104 }
else if(
a !=
nullptr) {
105 std::cerr <<
"Warning in mAlloc: passed pointer != nullptr for object '" << objectName <<
"'! " << function
117 const MLong size = (T::staticElementSize() * (maxSize + 2) *
sizeof(
char)) + ((maxSize + 1) *
sizeof(T));
129 return new Collector<T>(size, dummy1, dummy2, dummy3, dummy4);
133 return new Collector<T>(size, dimension, dummy, maxNoSets);
139 return new Collector<T>(size, dimension, dummy, dummy1);
143 const MInt distributions1,
const MInt maxNoSurfaces,
const MInt dummy1) {
144 return new Collector<T>(size, dimension, distributions, distributions1, maxNoSurfaces, dummy1);
150 return new Collector<T>(size, dimension, distributions);
178#ifdef MAIA_ASSERT_ALLOC
179 ASSERT(N > 0,
"Error in mAlloc: size must be >0, is " + std::to_string(N) +
" for " + objectName +
" allocated in "
184#ifdef MAIA_DEBUG_ALLOC
185 std::cerr <<
"Warning in mAlloc: size should be >0, is " << std::to_string(N) <<
" for " << objectName
186 <<
" allocated in " << function << std::endl;
192 g_allocatedObjects.push_back(
new GenericPointer<T>(
a, objectName, N *
sizeof(T), function,
true));
202 mAlloc(
a, N, objectName, function);
203#ifndef SKIP_INITIAL_VALUE_ALLOC
204 for(
MInt i = 0; i < N; i++)
220#ifdef MAIA_ASSERT_ALLOC
221 ASSERT(N > 0 && M > 0,
"Error in mAlloc: sizes must be >0, but they are " + std::to_string(N) +
" and "
222 + std::to_string(M) +
" for " + objectName +
" allocated in " + function);
225 if(N <= 0 || M <= 0) {
226#ifdef MAIA_DEBUG_ALLOC
227 std::cerr <<
"Error in mAlloc: sizes should be >0, but they are " << std::to_string(N) <<
" and "
228 << std::to_string(M) <<
" for " << objectName <<
" allocated in " << function << std::endl;
234 g_allocatedObjects.push_back(
new GenericPointer<T*>(
a, objectName, N *
sizeof(T*), function,
true));
236 const MString elementsName =
"_elements";
239 T* dummy =
new T[N * M];
240 g_allocatedObjects.push_back(
241 new GenericPointer<T>(dummy, objectName + elementsName, N * M *
sizeof(T), function,
true,
false));
243 for(
MInt i = 0; i < N; i++) {
244 a[i] = (T*)(&(dummy[M * i]));
247#ifndef SKIP_INITIAL_VALUE_ALLOC
248 for(
MInt i = 0; i < N * M; i++)
249 dummy[i] = initialValue;
265#ifdef MAIA_ASSERT_ALLOC
266 ASSERT(N > 0 && M > 0,
"Error in mAlloc: sizes must be >0, but they are " + std::to_string(N) +
" and "
267 + std::to_string(M) +
" for " + objectName +
" allocated in " + function);
270 if(N <= 0 || M <= 0) {
271#ifdef MAIA_DEBUG_ALLOC
272 std::cerr <<
"Error in mAlloc: sizes should be >0, but they are " << std::to_string(N) <<
" and "
273 << std::to_string(M) <<
" for " << objectName <<
" allocated in " << function << std::endl;
280 g_allocatedObjects.push_back(
new GenericPointer<T*>(
a, objectName, N *
sizeof(T*), function,
true));
282 const MString elementsName =
"[0]";
286 g_allocatedObjects.push_back(
287 new GenericPointer<T>(
a[0], objectName + elementsName, N * M *
sizeof(T), function,
true,
false));
289 for(
MInt i = 1; i < N; i++) {
290 a[i] = (T*)(&(
a[0][M * i]));
307 for(
MInt k = 0; k < N; k++) {
308 size += arraySizes[k];
311#ifdef MAIA_ASSERT_ALLOC
312 ASSERT(N > 0 && size > 0,
"Error in mAlloc: sizes must be >0, but they are " + std::to_string(N) +
" and "
313 + std::to_string(size) +
" for " + objectName +
" allocated in " + function);
316 if(N <= 0 || size <= 0) {
317#ifdef MAIA_DEBUG_ALLOC
318 std::cerr <<
"Error in mAlloc: sizes should be >0, but they are " << std::to_string(N) <<
" and "
319 << std::to_string(size) <<
" for " << objectName <<
" allocated in " << function << std::endl;
325 g_allocatedObjects.push_back(
new GenericPointer<T*>(
a, objectName, N *
sizeof(T*), function,
true));
327 const MString elementsName =
"_elements";
330 T* dummy =
new T[size];
331 g_allocatedObjects.push_back(
332 new GenericPointer<T>(dummy, objectName + elementsName, size *
sizeof(T), function,
true,
false));
335 for(
MInt i = 0; i < N; i++) {
336 a[i] = (T*)(&(dummy[cnt]));
337 cnt += arraySizes[i];
356 for(
MInt k = 0; k < N; k++) {
357 size += factor * arraySizes[k];
360#ifdef MAIA_ASSERT_ALLOC
361 ASSERT(N > 0 && size > 0,
"Error in mAlloc: sizes must be >0, but they are " + std::to_string(N) +
" and "
362 + std::to_string(size) +
" for " + objectName +
" allocated in " + function);
365 if(N <= 0 || size <= 0) {
366#ifdef MAIA_DEBUG_ALLOC
367 std::cerr <<
"Error in mAlloc: sizes should be >0, but they are " << std::to_string(N) <<
" and "
368 << std::to_string(size) <<
" for " << objectName <<
" allocated in " << function << std::endl;
374 g_allocatedObjects.push_back(
new GenericPointer<T*>(
a, objectName, N *
sizeof(T*), function,
true));
376 const MString elementsName =
"_elements";
379 T* dummy =
new T[size];
380 g_allocatedObjects.push_back(
381 new GenericPointer<T>(dummy, objectName + elementsName, size *
sizeof(T), function,
true,
false));
384 for(
MInt i = 0; i < N; i++) {
385 a[i] = (T*)(&(dummy[cnt]));
386 cnt += factor * arraySizes[i];
402#ifdef MAIA_ASSERT_ALLOC
403 ASSERT(N > 0,
"Error in mAlloc: size must be >0, is " + std::to_string(N) +
" for " + objectName +
" allocated in "
408#ifdef MAIA_DEBUG_ALLOC
409 std::cerr <<
"Warning in mAlloc: size should be >0, is " << std::to_string(N) <<
" for " << objectName
410 <<
" allocated in " << function << std::endl;
419#ifndef SKIP_INITIAL_VALUE_ALLOC
420 for(
MInt i = 0; i < N; i++)
421 a->a[i] = initialValue;
436 a = maia::alloc::make_collector<T>(maxSize, dummy1, dummy2, dummy3, dummy4);
451 a = maia::alloc::make_collector<T>(maxSize, dimension, dummy, maxNoSets);
469 a = maia::alloc::make_collector<T>(maxSize, dimension, dummy, dummy1);
487 a = maia::alloc::make_collector<T>(maxSize, dimension, distributions, distributions1, maxNoSurfaces, dummy1);
505 a = maia::alloc::make_collector<T>(maxSize, dimension, distributions);
519 a = maia::alloc::make_collector<T>(maxSize, dimension);
533 a = maia::alloc::make_collector<T>(maxSize);
547#ifdef MAIA_EXTRA_DEBUG
548 m_log <<
"cannot deallocate zero pointer " <<
a << std::endl;
553 std::vector<GenericObject*>::iterator hit;
554 const MInt count = findAllocatedObject(
a, hit);
556#ifdef MAIA_EXTRA_DEBUG
558 m_log <<
"Deallocating " << (*hit)->getName() <<
"."
559 <<
" " << &
a <<
" " << count << std::endl;
562 std::cerr <<
"Deallocating " << (*hit)->getName() <<
"."
563 <<
" " << &
a <<
" " << count << std::endl;
568 std::cerr <<
"mDeallocate error: " << count <<
" " <<
a << std::endl;
570 std::cerr <<
"Pointer should have been reset to nullptr!" << std::endl;
578 for(
MInt k = count; k--;) {
583 g_allocatedObjects.erase(hit);
585 g_allocatedObjects.erase(hit, hit + count);
608#ifdef MAIA_EXTRA_DEBUG
609 std::cout <<
"Name " << obj->getName() <<
" Function " << obj->getCallingFunction() << std::endl;
611 std::cout <<
"Name " << obj->getName() << std::endl;
622#ifdef MAIA_EXTRA_DEBUG
625 const MString fName = obj->getCallingFunction();
626 const MBool containsFilterString = (fName.find(filterString) != std::string::npos);
627 if(containsFilterString) {
628 std::cout << filterString <<
": Memory for " << obj->getName() <<
" from function " << fName
629 <<
" should have been deallocated by now!" << std::endl;
void mAlloc(T *&a, const MLong N, const MString &objectName, MString function)
allocates memory for one-dimensional array 'a' of size N
void printAllocatedObjects()
Prints the name of all objects which are currently allocated via mAlloc.
MLong maxAllocatedBytes()
Return the maximum number of allocated bytes.
MBool mDeallocate(T *&a)
deallocates the memory previously allocated for element 'a'
void mDealloc()
Deallocates all memory allocated previously by mAlloc(...)
MLong allocatedBytes()
Return the number of allocated bytes.
class containing a generic pointer
std::basic_string< char > MString
std::vector< GenericObject * > g_allocatedObjects
MBool isAllocated(const T *const a)
Check if the given pointer belongs to an allocated object.
void debug_collector(const MString &objectName, const MInt maxSize, const MString &function)
Collector< T > * make_collector(const MLong size, const MInt dummy1, const MInt dummy2, const MInt dummy3, const MInt dummy4)
void debug_list(const MString &objectName, const MInt maxSize, const MString &function)
void store_collector(Collector< T > *a, MLong maxSize, const MString &objectName, const MString &functionName)
MInt findAllocatedObject(const T *const a, std::vector< GenericObject * >::iterator &hit)
Find the allocated object for a given pointer.
void debug(const MString &objectName, const MString &elementsName)
Debug output for mAlloc.
void checkPointer(const T *const a, const MString objectName, const MString function)
Namespace for auxiliary functions/classes.