source: molecuilder/src/memoryallocator.hpp@ bc46c1

Last change on this file since bc46c1 was 2c69a9, checked in by Frederik Heber <heber@…>, 16 years ago

cstdlib header was missing, necessary for free, malloc and calloc

This was noted on laptop with gcc 4.1 (on workstation we have gcc 4.2).

Signed-off-by: Frederik Heber <heber@tabletINS.(none)>

  • Property mode set to 100755
File size: 4.1 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
[2c69a9]18#include <cstdlib>
[390a2b]19#include <iostream>
20#include <iomanip>
21#include <fstream>
22#include <sstream>
23#include <math.h>
24#include <string>
25#include <typeinfo>
26
27#include "defs.hpp"
[543ce4]28#include "log.hpp"
[14db08]29#include "memoryusageobserver.hpp"
[543ce4]30#include "verbose.hpp"
[390a2b]31
[17b3a5c]32/********************************************** declarations *******************************/
[390a2b]33
[17b3a5c]34/** Allocates a memory range using malloc().
[390a2b]35 * Prints the provided error message in case of a failure.
36 *
37 * \param number of memory slices of type X to allocate
38 * \param failure message which is printed if the allocation fails
39 * \return pointer to the allocated memory range, will be NULL if a failure occurred
40 */
41template <typename X> X* Malloc(size_t size, const char* output)
42{
43 X* buffer = NULL;
44 buffer = (X*) malloc(sizeof(X) * size);
45
[14db08]46 if (buffer != NULL) {
47 MemoryUsageObserver::getInstance()->addMemory(buffer, size);
48 } else {
[543ce4]49 Log() << Verbose(0) << "Malloc for datatype " << typeid(X).name()
[390a2b]50 << " failed - pointer is NULL: " << output << endl;
[14db08]51 }
[390a2b]52
53 return buffer;
54};
55
56/** \see helpers.cpp for Malloc<char> */
57template <> char* Malloc<char>(size_t size, const char* output);
58
[8bc524]59/* Allocates a memory range using calloc().
[390a2b]60 * Prints the provided error message in case of a failure.
61 *
62 * \param number of memory slices of type X to allocate
63 * \param failure message which is printed if the allocation fails
64 * \return pointer to the allocated memory range, will be NULL if a failure occurred
[8bc524]65*/
[390a2b]66template <typename X> X* Calloc(size_t size, const char* output)
67{
68 X* buffer = NULL;
[8bc524]69 buffer = (X*) calloc(size, sizeof(X));
[14db08]70
71 if (buffer != NULL) {
72 MemoryUsageObserver::getInstance()->addMemory(buffer, size);
73 } else {
[543ce4]74 Log() << Verbose(0) << "Calloc for datatype " << typeid(X).name()
[390a2b]75 << " failed - pointer is NULL: " << output << endl;
[14db08]76 }
[390a2b]77
78 return buffer;
79};
80
[8bc524]81
[17b3a5c]82/** Reallocates a memory range using realloc(). If the provided pointer to the old
[390a2b]83 * memory range is NULL, malloc() is called instead.
84 * Prints the provided error message in case of a failure (of either malloc() or realloc()).
85 *
86 * \param pointer to the old memory range
87 * \param number of memory slices of type X to allocate
88 * \param failure message which is printed if the allocation fails
89 * \return pointer to the reallocated memory range, will be NULL if a failure occurred
90 */
91template <typename X> X* ReAlloc(X* OldPointer, size_t size, const char* output)
92{
93 X* buffer = NULL;
[14db08]94 if (OldPointer == NULL) {
[390a2b]95 buffer = (X*) malloc(sizeof(X) * size);
[14db08]96 } else {
[390a2b]97 buffer = (X*) realloc(OldPointer, sizeof(X) * size);
[14db08]98 MemoryUsageObserver::getInstance()->removeMemory(OldPointer);
99 }
100 if (buffer != NULL) {
101 MemoryUsageObserver::getInstance()->addMemory(buffer, size);
102 } else {
[543ce4]103 Log() << Verbose(0) << "ReAlloc for datatype " << typeid(X).name()
[390a2b]104 << " failed - new is NULL: " << output << endl;
[14db08]105 }
[390a2b]106
107 return buffer;
108};
109
[069034]110/** Frees allocated memory range using free(), NULL'ing \a **buffer.
[390a2b]111 *
[069034]112 * \param **buffer to the allocated memory range to free; may be NULL, this function is a no-op then
[58808e]113 * \param *msg optional error message
[390a2b]114 */
[58808e]115template <typename X> void Free(X** buffer, const char *msg = NULL)
[390a2b]116{
117 if ((buffer == NULL) || (*buffer == NULL))
118 return;
119
[729279]120 MemoryUsageObserver::getInstance()->removeMemory(*buffer, msg);
[390a2b]121 free(*buffer);
122 *buffer = NULL;
123};
124
[069034]125/** Frees allocated memory range using free() for ... * const \a buffer types.
126 *
127 * \param *buffer to the allocated memory range to free; may be NULL, this function is a no-op then
128 * \param *msg optional error message
129 */
130template <typename X> void Free(X* const buffer, const char *msg = NULL)
131{
132 if ((buffer == NULL))
133 return;
134
135 MemoryUsageObserver::getInstance()->removeMemory(buffer, msg);
136 free(buffer);
137};
138
[390a2b]139#endif /*MEMORYALLOCATOR_HPP_*/
Note: See TracBrowser for help on using the repository browser.