source: molecuilder/src/memoryallocator.hpp@ 543ce4

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

Huge change from ofstream * (const) out --> Log().

  • first shift was done via regular expressions
  • then via error messages from the code
  • note that class atom, class element and class molecule kept in parts their output stream, was they print to file.
  • make check runs fine
  • MISSING: Verbosity is not fixed for everything (i.e. if no endl; is present and next has Verbose(0) ...)

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