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 "log.hpp"
|
---|
28 | #include "memoryusageobserver.hpp"
|
---|
29 | #include "verbose.hpp"
|
---|
30 |
|
---|
31 | /********************************************** declarations *******************************/
|
---|
32 |
|
---|
33 | /** Allocates a memory range using malloc().
|
---|
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 |
|
---|
45 | if (buffer != NULL) {
|
---|
46 | MemoryUsageObserver::getInstance()->addMemory(buffer, size);
|
---|
47 | } else {
|
---|
48 | Log() << Verbose(0) << "Malloc for datatype " << typeid(X).name()
|
---|
49 | << " failed - pointer is NULL: " << output << endl;
|
---|
50 | }
|
---|
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 |
|
---|
58 | /* Allocates a memory range using calloc().
|
---|
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
|
---|
64 | */
|
---|
65 | template <typename X> X* Calloc(size_t size, const char* output)
|
---|
66 | {
|
---|
67 | X* buffer = NULL;
|
---|
68 | buffer = (X*) calloc(size, sizeof(X));
|
---|
69 |
|
---|
70 | if (buffer != NULL) {
|
---|
71 | MemoryUsageObserver::getInstance()->addMemory(buffer, size);
|
---|
72 | } else {
|
---|
73 | Log() << Verbose(0) << "Calloc for datatype " << typeid(X).name()
|
---|
74 | << " failed - pointer is NULL: " << output << endl;
|
---|
75 | }
|
---|
76 |
|
---|
77 | return buffer;
|
---|
78 | };
|
---|
79 |
|
---|
80 |
|
---|
81 | /** Reallocates a memory range using realloc(). If the provided pointer to the old
|
---|
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;
|
---|
93 | if (OldPointer == NULL) {
|
---|
94 | buffer = (X*) malloc(sizeof(X) * size);
|
---|
95 | } else {
|
---|
96 | buffer = (X*) realloc(OldPointer, sizeof(X) * size);
|
---|
97 | MemoryUsageObserver::getInstance()->removeMemory(OldPointer);
|
---|
98 | }
|
---|
99 | if (buffer != NULL) {
|
---|
100 | MemoryUsageObserver::getInstance()->addMemory(buffer, size);
|
---|
101 | } else {
|
---|
102 | Log() << Verbose(0) << "ReAlloc for datatype " << typeid(X).name()
|
---|
103 | << " failed - new is NULL: " << output << endl;
|
---|
104 | }
|
---|
105 |
|
---|
106 | return buffer;
|
---|
107 | };
|
---|
108 |
|
---|
109 | /** Frees allocated memory range using free(), NULL'ing \a **buffer.
|
---|
110 | *
|
---|
111 | * \param **buffer to the allocated memory range to free; may be NULL, this function is a no-op then
|
---|
112 | * \param *msg optional error message
|
---|
113 | */
|
---|
114 | template <typename X> void Free(X** buffer, const char *msg = NULL)
|
---|
115 | {
|
---|
116 | if ((buffer == NULL) || (*buffer == NULL))
|
---|
117 | return;
|
---|
118 |
|
---|
119 | MemoryUsageObserver::getInstance()->removeMemory(*buffer, msg);
|
---|
120 | free(*buffer);
|
---|
121 | *buffer = NULL;
|
---|
122 | };
|
---|
123 |
|
---|
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 |
|
---|
138 | #endif /*MEMORYALLOCATOR_HPP_*/
|
---|