| [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" | 
|---|
|  | 26 |  | 
|---|
|  | 27 | /******************* wrappers for memory allocation functions ***********************/ | 
|---|
|  | 28 |  | 
|---|
|  | 29 | /** | 
|---|
|  | 30 | * Allocates a memory range using malloc(). | 
|---|
|  | 31 | * Prints the provided error message in case of a failure. | 
|---|
|  | 32 | * | 
|---|
|  | 33 | * \param number of memory slices of type X to allocate | 
|---|
|  | 34 | * \param failure message which is printed if the allocation fails | 
|---|
|  | 35 | * \return pointer to the allocated memory range, will be NULL if a failure occurred | 
|---|
|  | 36 | */ | 
|---|
|  | 37 | template <typename X> X* Malloc(size_t size, const char* output) | 
|---|
|  | 38 | { | 
|---|
|  | 39 | X* buffer = NULL; | 
|---|
|  | 40 | buffer = (X*) malloc(sizeof(X) * size); | 
|---|
|  | 41 |  | 
|---|
|  | 42 | if (buffer == NULL) | 
|---|
|  | 43 | cout << Verbose(0) << "Malloc for datatype " << typeid(X).name() | 
|---|
|  | 44 | << " failed - pointer is NULL: " << output << endl; | 
|---|
|  | 45 |  | 
|---|
|  | 46 | return buffer; | 
|---|
|  | 47 | }; | 
|---|
|  | 48 |  | 
|---|
|  | 49 | /** \see helpers.cpp for Malloc<char> */ | 
|---|
|  | 50 | template <> char* Malloc<char>(size_t size, const char* output); | 
|---|
|  | 51 |  | 
|---|
|  | 52 | /** | 
|---|
|  | 53 | * Allocates a memory range using calloc(). | 
|---|
|  | 54 | * Prints the provided error message in case of a failure. | 
|---|
|  | 55 | * | 
|---|
|  | 56 | * \param number of memory slices of type X to allocate | 
|---|
|  | 57 | * \param failure message which is printed if the allocation fails | 
|---|
|  | 58 | * \return pointer to the allocated memory range, will be NULL if a failure occurred | 
|---|
|  | 59 | */ | 
|---|
|  | 60 | template <typename X> X* Calloc(size_t size, const char* output) | 
|---|
|  | 61 | { | 
|---|
|  | 62 | X* buffer = NULL; | 
|---|
|  | 63 | buffer = (X*) calloc(sizeof(X) * size, (size_t) 0); | 
|---|
|  | 64 | if (buffer == NULL) | 
|---|
|  | 65 | cout << Verbose(0) << "Calloc for datatype " << typeid(X).name() | 
|---|
|  | 66 | << " failed - pointer is NULL: " << output << endl; | 
|---|
|  | 67 |  | 
|---|
|  | 68 | return buffer; | 
|---|
|  | 69 | }; | 
|---|
|  | 70 |  | 
|---|
|  | 71 | /** | 
|---|
|  | 72 | * Reallocates a memory range using realloc(). If the provided pointer to the old | 
|---|
|  | 73 | * memory range is NULL, malloc() is called instead. | 
|---|
|  | 74 | * Prints the provided error message in case of a failure (of either malloc() or realloc()). | 
|---|
|  | 75 | * | 
|---|
|  | 76 | * \param pointer to the old memory range | 
|---|
|  | 77 | * \param number of memory slices of type X to allocate | 
|---|
|  | 78 | * \param failure message which is printed if the allocation fails | 
|---|
|  | 79 | * \return pointer to the reallocated memory range, will be NULL if a failure occurred | 
|---|
|  | 80 | */ | 
|---|
|  | 81 | template <typename X> X* ReAlloc(X* OldPointer, size_t size, const char* output) | 
|---|
|  | 82 | { | 
|---|
|  | 83 | X* buffer = NULL; | 
|---|
|  | 84 | if (OldPointer == NULL) | 
|---|
|  | 85 | buffer = (X*) malloc(sizeof(X) * size); | 
|---|
|  | 86 | else | 
|---|
|  | 87 | buffer = (X*) realloc(OldPointer, sizeof(X) * size); | 
|---|
|  | 88 | if (buffer == NULL) | 
|---|
|  | 89 | cout << Verbose(0) << "ReAlloc for datatype " << typeid(X).name() | 
|---|
|  | 90 | << " failed - new is NULL: " << output << endl; | 
|---|
|  | 91 |  | 
|---|
|  | 92 | return buffer; | 
|---|
|  | 93 | }; | 
|---|
|  | 94 |  | 
|---|
|  | 95 | /** | 
|---|
|  | 96 | * Frees allocated memory range using free(). | 
|---|
|  | 97 | * | 
|---|
|  | 98 | * \param pointer to the allocated memory range to free; may be NULL, this function is a no-op then | 
|---|
|  | 99 | */ | 
|---|
|  | 100 | template <typename X> void Free(X** buffer) | 
|---|
|  | 101 | { | 
|---|
|  | 102 | if ((buffer == NULL) || (*buffer == NULL)) | 
|---|
|  | 103 | return; | 
|---|
|  | 104 |  | 
|---|
|  | 105 | free(*buffer); | 
|---|
|  | 106 | *buffer = NULL; | 
|---|
|  | 107 | }; | 
|---|
|  | 108 |  | 
|---|
|  | 109 | #endif /*MEMORYALLOCATOR_HPP_*/ | 
|---|