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