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