[29812d] | 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 |
|
---|
[f66195] | 11 | /*********************************************** includes ***********************************/
|
---|
| 12 |
|
---|
[29812d] | 13 | // include config.h
|
---|
| 14 | #ifdef HAVE_CONFIG_H
|
---|
| 15 | #include <config.h>
|
---|
| 16 | #endif
|
---|
| 17 |
|
---|
[3e50ff] | 18 | #include <cstdlib>
|
---|
[29812d] | 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"
|
---|
[e138de] | 28 | #include "log.hpp"
|
---|
[c30180] | 29 | #include "memoryusageobserver.hpp"
|
---|
[e138de] | 30 | #include "verbose.hpp"
|
---|
[29812d] | 31 |
|
---|
[f66195] | 32 | /********************************************** declarations *******************************/
|
---|
[29812d] | 33 |
|
---|
[f66195] | 34 | /** Allocates a memory range using malloc().
|
---|
[29812d] | 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 |
|
---|
[c30180] | 46 | if (buffer != NULL) {
|
---|
| 47 | MemoryUsageObserver::getInstance()->addMemory(buffer, size);
|
---|
| 48 | } else {
|
---|
[e138de] | 49 | Log() << Verbose(0) << "Malloc for datatype " << typeid(X).name()
|
---|
[29812d] | 50 | << " failed - pointer is NULL: " << output << endl;
|
---|
[c30180] | 51 | }
|
---|
[29812d] | 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 |
|
---|
[7218f8] | 59 | /* Allocates a memory range using calloc().
|
---|
[29812d] | 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
|
---|
[7218f8] | 65 | */
|
---|
[29812d] | 66 | template <typename X> X* Calloc(size_t size, const char* output)
|
---|
| 67 | {
|
---|
| 68 | X* buffer = NULL;
|
---|
[7218f8] | 69 | buffer = (X*) calloc(size, sizeof(X));
|
---|
[c30180] | 70 |
|
---|
| 71 | if (buffer != NULL) {
|
---|
| 72 | MemoryUsageObserver::getInstance()->addMemory(buffer, size);
|
---|
| 73 | } else {
|
---|
[e138de] | 74 | Log() << Verbose(0) << "Calloc for datatype " << typeid(X).name()
|
---|
[29812d] | 75 | << " failed - pointer is NULL: " << output << endl;
|
---|
[c30180] | 76 | }
|
---|
[29812d] | 77 |
|
---|
| 78 | return buffer;
|
---|
| 79 | };
|
---|
| 80 |
|
---|
[7218f8] | 81 |
|
---|
[f66195] | 82 | /** Reallocates a memory range using realloc(). If the provided pointer to the old
|
---|
[29812d] | 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;
|
---|
[c30180] | 94 | if (OldPointer == NULL) {
|
---|
[29812d] | 95 | buffer = (X*) malloc(sizeof(X) * size);
|
---|
[c30180] | 96 | } else {
|
---|
[29812d] | 97 | buffer = (X*) realloc(OldPointer, sizeof(X) * size);
|
---|
[c30180] | 98 | MemoryUsageObserver::getInstance()->removeMemory(OldPointer);
|
---|
| 99 | }
|
---|
| 100 | if (buffer != NULL) {
|
---|
| 101 | MemoryUsageObserver::getInstance()->addMemory(buffer, size);
|
---|
| 102 | } else {
|
---|
[e138de] | 103 | Log() << Verbose(0) << "ReAlloc for datatype " << typeid(X).name()
|
---|
[29812d] | 104 | << " failed - new is NULL: " << output << endl;
|
---|
[c30180] | 105 | }
|
---|
[29812d] | 106 |
|
---|
| 107 | return buffer;
|
---|
| 108 | };
|
---|
| 109 |
|
---|
[fb73b8] | 110 | /** Frees allocated memory range using free(), NULL'ing \a **buffer.
|
---|
[29812d] | 111 | *
|
---|
[fb73b8] | 112 | * \param **buffer to the allocated memory range to free; may be NULL, this function is a no-op then
|
---|
[c26f44] | 113 | * \param *msg optional error message
|
---|
[29812d] | 114 | */
|
---|
[c26f44] | 115 | template <typename X> void Free(X** buffer, const char *msg = NULL)
|
---|
[29812d] | 116 | {
|
---|
| 117 | if ((buffer == NULL) || (*buffer == NULL))
|
---|
| 118 | return;
|
---|
| 119 |
|
---|
[21b9c3] | 120 | MemoryUsageObserver::getInstance()->removeMemory(*buffer, msg);
|
---|
[29812d] | 121 | free(*buffer);
|
---|
| 122 | *buffer = NULL;
|
---|
| 123 | };
|
---|
| 124 |
|
---|
[fb73b8] | 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 |
|
---|
[29812d] | 139 | #endif /*MEMORYALLOCATOR_HPP_*/
|
---|