source: src/memoryallocator.hpp@ e138de

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