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