source: molecuilder/src/memoryallocator.hpp@ c1b4a4

Last change on this file since c1b4a4 was 8bc524, checked in by Frederik Heber <heber@…>, 16 years ago

Several memory bugfixes (thx valgrind).

Fixed Calloc:

Signed-off-by: Frederik Heber <heber@…>

  • Property mode set to 100755
File size: 4.0 KB
RevLine 
[390a2b]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
9using namespace std;
10
[17b3a5c]11/*********************************************** includes ***********************************/
12
[390a2b]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"
[14db08]28#include "memoryusageobserver.hpp"
[390a2b]29
[17b3a5c]30/********************************************** declarations *******************************/
[390a2b]31
[17b3a5c]32/** Allocates a memory range using malloc().
[390a2b]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 */
39template <typename X> X* Malloc(size_t size, const char* output)
40{
41 X* buffer = NULL;
42 buffer = (X*) malloc(sizeof(X) * size);
43
[14db08]44 if (buffer != NULL) {
45 MemoryUsageObserver::getInstance()->addMemory(buffer, size);
46 } else {
[390a2b]47 cout << Verbose(0) << "Malloc for datatype " << typeid(X).name()
48 << " failed - pointer is NULL: " << output << endl;
[14db08]49 }
[390a2b]50
51 return buffer;
52};
53
54/** \see helpers.cpp for Malloc<char> */
55template <> char* Malloc<char>(size_t size, const char* output);
56
[8bc524]57/* Allocates a memory range using calloc().
[390a2b]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
[8bc524]63*/
[390a2b]64template <typename X> X* Calloc(size_t size, const char* output)
65{
66 X* buffer = NULL;
[8bc524]67 buffer = (X*) calloc(size, sizeof(X));
[14db08]68
69 if (buffer != NULL) {
70 MemoryUsageObserver::getInstance()->addMemory(buffer, size);
71 } else {
[390a2b]72 cout << Verbose(0) << "Calloc for datatype " << typeid(X).name()
73 << " failed - pointer is NULL: " << output << endl;
[14db08]74 }
[390a2b]75
76 return buffer;
77};
78
[8bc524]79
[17b3a5c]80/** Reallocates a memory range using realloc(). If the provided pointer to the old
[390a2b]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 */
89template <typename X> X* ReAlloc(X* OldPointer, size_t size, const char* output)
90{
91 X* buffer = NULL;
[14db08]92 if (OldPointer == NULL) {
[390a2b]93 buffer = (X*) malloc(sizeof(X) * size);
[14db08]94 } else {
[390a2b]95 buffer = (X*) realloc(OldPointer, sizeof(X) * size);
[14db08]96 MemoryUsageObserver::getInstance()->removeMemory(OldPointer);
97 }
98 if (buffer != NULL) {
99 MemoryUsageObserver::getInstance()->addMemory(buffer, size);
100 } else {
[390a2b]101 cout << Verbose(0) << "ReAlloc for datatype " << typeid(X).name()
102 << " failed - new is NULL: " << output << endl;
[14db08]103 }
[390a2b]104
105 return buffer;
106};
107
[069034]108/** Frees allocated memory range using free(), NULL'ing \a **buffer.
[390a2b]109 *
[069034]110 * \param **buffer to the allocated memory range to free; may be NULL, this function is a no-op then
[58808e]111 * \param *msg optional error message
[390a2b]112 */
[58808e]113template <typename X> void Free(X** buffer, const char *msg = NULL)
[390a2b]114{
115 if ((buffer == NULL) || (*buffer == NULL))
116 return;
117
[729279]118 MemoryUsageObserver::getInstance()->removeMemory(*buffer, msg);
[390a2b]119 free(*buffer);
120 *buffer = NULL;
121};
122
[069034]123/** Frees allocated memory range using free() for ... * const \a buffer types.
124 *
125 * \param *buffer to the allocated memory range to free; may be NULL, this function is a no-op then
126 * \param *msg optional error message
127 */
128template <typename X> void Free(X* const buffer, const char *msg = NULL)
129{
130 if ((buffer == NULL))
131 return;
132
133 MemoryUsageObserver::getInstance()->removeMemory(buffer, msg);
134 free(buffer);
135};
136
[390a2b]137#endif /*MEMORYALLOCATOR_HPP_*/
Note: See TracBrowser for help on using the repository browser.