source: molecuilder/src/memoryallocator.hpp@ 390a2b

Last change on this file since 390a2b was 390a2b, checked in by Saskia Metzler <metzler@…>, 16 years ago

Ticket 11: use templates and/or traits to fix Malloc/ReAlloc-Free warnings in a clean manner

  • Property mode set to 100755
File size: 3.0 KB
Line 
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
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include <iostream>
17#include <iomanip>
18#include <fstream>
19#include <sstream>
20#include <math.h>
21#include <string>
22#include <typeinfo>
23
24#include "defs.hpp"
25#include "verbose.hpp"
26
27/******************* wrappers for memory allocation functions ***********************/
28
29/**
30 * Allocates a memory range using malloc().
31 * Prints the provided error message in case of a failure.
32 *
33 * \param number of memory slices of type X to allocate
34 * \param failure message which is printed if the allocation fails
35 * \return pointer to the allocated memory range, will be NULL if a failure occurred
36 */
37template <typename X> X* Malloc(size_t size, const char* output)
38{
39 X* buffer = NULL;
40 buffer = (X*) malloc(sizeof(X) * size);
41
42 if (buffer == NULL)
43 cout << Verbose(0) << "Malloc for datatype " << typeid(X).name()
44 << " failed - pointer is NULL: " << output << endl;
45
46 return buffer;
47};
48
49/** \see helpers.cpp for Malloc<char> */
50template <> char* Malloc<char>(size_t size, const char* output);
51
52/**
53 * Allocates a memory range using calloc().
54 * Prints the provided error message in case of a failure.
55 *
56 * \param number of memory slices of type X to allocate
57 * \param failure message which is printed if the allocation fails
58 * \return pointer to the allocated memory range, will be NULL if a failure occurred
59 */
60template <typename X> X* Calloc(size_t size, const char* output)
61{
62 X* buffer = NULL;
63 buffer = (X*) calloc(sizeof(X) * size, (size_t) 0);
64 if (buffer == NULL)
65 cout << Verbose(0) << "Calloc for datatype " << typeid(X).name()
66 << " failed - pointer is NULL: " << output << endl;
67
68 return buffer;
69};
70
71/**
72 * Reallocates a memory range using realloc(). If the provided pointer to the old
73 * memory range is NULL, malloc() is called instead.
74 * Prints the provided error message in case of a failure (of either malloc() or realloc()).
75 *
76 * \param pointer to the old memory range
77 * \param number of memory slices of type X to allocate
78 * \param failure message which is printed if the allocation fails
79 * \return pointer to the reallocated memory range, will be NULL if a failure occurred
80 */
81template <typename X> X* ReAlloc(X* OldPointer, size_t size, const char* output)
82{
83 X* buffer = NULL;
84 if (OldPointer == NULL)
85 buffer = (X*) malloc(sizeof(X) * size);
86 else
87 buffer = (X*) realloc(OldPointer, sizeof(X) * size);
88 if (buffer == NULL)
89 cout << Verbose(0) << "ReAlloc for datatype " << typeid(X).name()
90 << " failed - new is NULL: " << output << endl;
91
92 return buffer;
93};
94
95/**
96 * Frees allocated memory range using free().
97 *
98 * \param pointer to the allocated memory range to free; may be NULL, this function is a no-op then
99 */
100template <typename X> void Free(X** buffer)
101{
102 if ((buffer == NULL) || (*buffer == NULL))
103 return;
104
105 free(*buffer);
106 *buffer = NULL;
107};
108
109#endif /*MEMORYALLOCATOR_HPP_*/
Note: See TracBrowser for help on using the repository browser.