source: src/memoryallocator.hpp@ fc1b24

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since fc1b24 was 3e50ff, checked in by Frederik Heber <heber@…>, 15 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
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/*********************************************** includes ***********************************/
12
13// include config.h
14#ifdef HAVE_CONFIG_H
15#include <config.h>
16#endif
17
18#include <cstdlib>
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"
28#include "log.hpp"
29#include "memoryusageobserver.hpp"
30#include "verbose.hpp"
31
32/********************************************** declarations *******************************/
33
34/** Allocates a memory range using malloc().
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
46 if (buffer != NULL) {
47 MemoryUsageObserver::getInstance()->addMemory(buffer, size);
48 } else {
49 Log() << Verbose(0) << "Malloc for datatype " << typeid(X).name()
50 << " failed - pointer is NULL: " << output << endl;
51 }
52
53 return buffer;
54};
55
56/** \see helpers.cpp for Malloc<char> */
57template <> char* Malloc<char>(size_t size, const char* output);
58
59/* Allocates a memory range using calloc().
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
65*/
66template <typename X> X* Calloc(size_t size, const char* output)
67{
68 X* buffer = NULL;
69 buffer = (X*) calloc(size, sizeof(X));
70
71 if (buffer != NULL) {
72 MemoryUsageObserver::getInstance()->addMemory(buffer, size);
73 } else {
74 Log() << Verbose(0) << "Calloc for datatype " << typeid(X).name()
75 << " failed - pointer is NULL: " << output << endl;
76 }
77
78 return buffer;
79};
80
81
82/** Reallocates a memory range using realloc(). If the provided pointer to the old
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;
94 if (OldPointer == NULL) {
95 buffer = (X*) malloc(sizeof(X) * size);
96 } else {
97 buffer = (X*) realloc(OldPointer, sizeof(X) * size);
98 MemoryUsageObserver::getInstance()->removeMemory(OldPointer);
99 }
100 if (buffer != NULL) {
101 MemoryUsageObserver::getInstance()->addMemory(buffer, size);
102 } else {
103 Log() << Verbose(0) << "ReAlloc for datatype " << typeid(X).name()
104 << " failed - new is NULL: " << output << endl;
105 }
106
107 return buffer;
108};
109
110/** Frees allocated memory range using free(), NULL'ing \a **buffer.
111 *
112 * \param **buffer to the allocated memory range to free; may be NULL, this function is a no-op then
113 * \param *msg optional error message
114 */
115template <typename X> void Free(X** buffer, const char *msg = NULL)
116{
117 if ((buffer == NULL) || (*buffer == NULL))
118 return;
119
120 MemoryUsageObserver::getInstance()->removeMemory(*buffer, msg);
121 free(*buffer);
122 *buffer = NULL;
123};
124
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
139#endif /*MEMORYALLOCATOR_HPP_*/
Note: See TracBrowser for help on using the repository browser.