| 1 | /** \file memoryallocator.hpp
 | 
|---|
| 2 |  *
 | 
|---|
| 3 |  * This file provides wrappers for C++'s memory allocation functions.
 | 
|---|
| 4 |  */
 | 
|---|
| 5 | 
 | 
|---|
| 6 | #ifndef MEMORYALLOCATOR_HPP_
 | 
|---|
| 7 | #define MEMORYALLOCATOR_HPP_
 | 
|---|
| 8 | 
 | 
|---|
| 9 | using namespace std;
 | 
|---|
| 10 | 
 | 
|---|
| 11 | /*********************************************** includes ***********************************/
 | 
|---|
| 12 | 
 | 
|---|
| 13 | // include config.h
 | 
|---|
| 14 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 15 | #include <config.h>
 | 
|---|
| 16 | #endif
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include <cstdlib>
 | 
|---|
| 19 | #include <iostream>
 | 
|---|
| 20 | #include <iomanip>
 | 
|---|
| 21 | #include <fstream>
 | 
|---|
| 22 | #include <sstream>
 | 
|---|
| 23 | #include <math.h>
 | 
|---|
| 24 | #include <string>
 | 
|---|
| 25 | #include <typeinfo>
 | 
|---|
| 26 | 
 | 
|---|
| 27 | #include "defs.hpp"
 | 
|---|
| 28 | #include "log.hpp"
 | 
|---|
| 29 | #include "memoryusageobserver.hpp"
 | 
|---|
| 30 | #include "verbose.hpp"
 | 
|---|
| 31 | 
 | 
|---|
| 32 | /********************************************** declarations *******************************/
 | 
|---|
| 33 | 
 | 
|---|
| 34 | /** Allocates a memory range using malloc().
 | 
|---|
| 35 |  * Prints the provided error message in case of a failure.
 | 
|---|
| 36 |  *
 | 
|---|
| 37 |  * \param number of memory slices of type X to allocate
 | 
|---|
| 38 |  * \param failure message which is printed if the allocation fails
 | 
|---|
| 39 |  * \return pointer to the allocated memory range, will be NULL if a failure occurred
 | 
|---|
| 40 |  */
 | 
|---|
| 41 | template <typename X> X* Malloc(size_t size, const char* output)
 | 
|---|
| 42 | {
 | 
|---|
| 43 |   X* buffer = NULL;
 | 
|---|
| 44 |   buffer = (X*) malloc(sizeof(X) * size);
 | 
|---|
| 45 | 
 | 
|---|
| 46 |   if (buffer != NULL) {
 | 
|---|
| 47 |     MemoryUsageObserver::getInstance()->addMemory(buffer, size);
 | 
|---|
| 48 |   } else {
 | 
|---|
| 49 |     Log() << Verbose(0) << "Malloc for datatype " << typeid(X).name()
 | 
|---|
| 50 |       << " failed - pointer is NULL: " << output << endl;
 | 
|---|
| 51 |   }
 | 
|---|
| 52 | 
 | 
|---|
| 53 |   return buffer;
 | 
|---|
| 54 | };
 | 
|---|
| 55 | 
 | 
|---|
| 56 | /** \see helpers.cpp for Malloc<char> */
 | 
|---|
| 57 | template <> char* Malloc<char>(size_t size, const char* output);
 | 
|---|
| 58 | 
 | 
|---|
| 59 | /* Allocates a memory range using calloc().
 | 
|---|
| 60 |  * Prints the provided error message in case of a failure.
 | 
|---|
| 61 |  *
 | 
|---|
| 62 |  * \param number of memory slices of type X to allocate
 | 
|---|
| 63 |  * \param failure message which is printed if the allocation fails
 | 
|---|
| 64 |  * \return pointer to the allocated memory range, will be NULL if a failure occurred
 | 
|---|
| 65 | */
 | 
|---|
| 66 | template <typename X> X* Calloc(size_t size, const char* output)
 | 
|---|
| 67 | {
 | 
|---|
| 68 |   X* buffer = NULL;
 | 
|---|
| 69 |   buffer = (X*) calloc(size, sizeof(X));
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   if (buffer != NULL) {
 | 
|---|
| 72 |     MemoryUsageObserver::getInstance()->addMemory(buffer, size);
 | 
|---|
| 73 |   } else {
 | 
|---|
| 74 |     Log() << Verbose(0) << "Calloc for datatype " << typeid(X).name()
 | 
|---|
| 75 |       << " failed - pointer is NULL: " << output << endl;
 | 
|---|
| 76 |   }
 | 
|---|
| 77 | 
 | 
|---|
| 78 |   return buffer;
 | 
|---|
| 79 | };
 | 
|---|
| 80 | 
 | 
|---|
| 81 | 
 | 
|---|
| 82 | /** Reallocates a memory range using realloc(). If the provided pointer to the old
 | 
|---|
| 83 |  * memory range is NULL, malloc() is called instead.
 | 
|---|
| 84 |  * Prints the provided error message in case of a failure (of either malloc() or realloc()).
 | 
|---|
| 85 |  *
 | 
|---|
| 86 |  * \param pointer to the old memory range
 | 
|---|
| 87 |  * \param number of memory slices of type X to allocate
 | 
|---|
| 88 |  * \param failure message which is printed if the allocation fails
 | 
|---|
| 89 |  * \return pointer to the reallocated memory range, will be NULL if a failure occurred
 | 
|---|
| 90 |  */
 | 
|---|
| 91 | template <typename X> X* ReAlloc(X* OldPointer, size_t size, const char* output)
 | 
|---|
| 92 | {
 | 
|---|
| 93 |   X* buffer = NULL;
 | 
|---|
| 94 |   if (OldPointer == NULL) {
 | 
|---|
| 95 |     buffer = (X*) malloc(sizeof(X) * size);
 | 
|---|
| 96 |   } else {
 | 
|---|
| 97 |     buffer = (X*) realloc(OldPointer, sizeof(X) * size);
 | 
|---|
| 98 |     MemoryUsageObserver::getInstance()->removeMemory(OldPointer);
 | 
|---|
| 99 |   }
 | 
|---|
| 100 |   if (buffer != NULL) {
 | 
|---|
| 101 |     MemoryUsageObserver::getInstance()->addMemory(buffer, size);
 | 
|---|
| 102 |   } else {
 | 
|---|
| 103 |     Log() << Verbose(0) << "ReAlloc for datatype " << typeid(X).name()
 | 
|---|
| 104 |       << " failed - new is NULL: " << output << endl;
 | 
|---|
| 105 |   }
 | 
|---|
| 106 | 
 | 
|---|
| 107 |   return buffer;
 | 
|---|
| 108 | };
 | 
|---|
| 109 | 
 | 
|---|
| 110 | /** Frees allocated memory range using free(), NULL'ing \a **buffer.
 | 
|---|
| 111 |  *
 | 
|---|
| 112 |  * \param **buffer to the allocated memory range to free; may be NULL, this function is a no-op then
 | 
|---|
| 113 |  * \param *msg optional error message
 | 
|---|
| 114 |  */
 | 
|---|
| 115 | template <typename X> void Free(X** buffer, const char *msg = NULL)
 | 
|---|
| 116 | {
 | 
|---|
| 117 |   if ((buffer == NULL) || (*buffer == NULL))
 | 
|---|
| 118 |     return;
 | 
|---|
| 119 | 
 | 
|---|
| 120 |   MemoryUsageObserver::getInstance()->removeMemory(*buffer, msg);
 | 
|---|
| 121 |   free(*buffer);
 | 
|---|
| 122 |   *buffer = NULL;
 | 
|---|
| 123 | };
 | 
|---|
| 124 | 
 | 
|---|
| 125 | /** Frees allocated memory range using free() for ... * const \a buffer types.
 | 
|---|
| 126 |  *
 | 
|---|
| 127 |  * \param *buffer to the allocated memory range to free; may be NULL, this function is a no-op then
 | 
|---|
| 128 |  * \param *msg optional error message
 | 
|---|
| 129 |  */
 | 
|---|
| 130 | template <typename X> void Free(X* const buffer, const char *msg = NULL)
 | 
|---|
| 131 | {
 | 
|---|
| 132 |   if ((buffer == NULL))
 | 
|---|
| 133 |     return;
 | 
|---|
| 134 | 
 | 
|---|
| 135 |   MemoryUsageObserver::getInstance()->removeMemory(buffer, msg);
 | 
|---|
| 136 |   free(buffer);
 | 
|---|
| 137 | };
 | 
|---|
| 138 | 
 | 
|---|
| 139 | #endif /*MEMORYALLOCATOR_HPP_*/
 | 
|---|