source: src/memoryallocator.hpp@ ccd9f5

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 ccd9f5 was c111db, checked in by Frederik Heber <heber@…>, 15 years ago

Merge branch 'new-delete-conversion' into CodeRefactoring

Conflicts:

molecuilder/src/Makefile.am
molecuilder/src/helpers.cpp
molecuilder/src/helpers.hpp
molecuilder/src/memoryusageobserver.cpp

  • FIX: performCriticalExit() was declared static but not defined a such.
  • Merge was basically only due to libmolecuilder which was not used in CodeRefactoring branch before.
  • added ActOnAll Unit test to new unittests sub folder and to Makefile.am
  • Property mode set to 100755
File size: 3.6 KB
RevLine 
[29812d]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
[f66195]11/*********************************************** includes ***********************************/
12
[29812d]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 "verbose.hpp"
[c30180]28#include "memoryusageobserver.hpp"
[29812d]29
[f66195]30/********************************************** declarations *******************************/
[29812d]31
[f66195]32/** Allocates a memory range using malloc().
[29812d]33 * Prints the provided error message in case of a failure.
34 *
35 * \param number of memory slices of type X to allocate
36 * \param failure message which is printed if the allocation fails
37 * \return pointer to the allocated memory range, will be NULL if a failure occurred
38 */
39template <typename X> X* Malloc(size_t size, const char* output)
40{
41 X* buffer = NULL;
42 buffer = (X*) malloc(sizeof(X) * size);
43
[c30180]44 if (buffer != NULL) {
45 MemoryUsageObserver::getInstance()->addMemory(buffer, size);
46 } else {
[29812d]47 cout << Verbose(0) << "Malloc for datatype " << typeid(X).name()
48 << " failed - pointer is NULL: " << output << endl;
[c30180]49 }
[29812d]50
51 return buffer;
52};
53
54/** \see helpers.cpp for Malloc<char> */
55template <> char* Malloc<char>(size_t size, const char* output);
56
[f66195]57/** Allocates a memory range using calloc().
[29812d]58 * Prints the provided error message in case of a failure.
59 *
60 * \param number of memory slices of type X to allocate
61 * \param failure message which is printed if the allocation fails
62 * \return pointer to the allocated memory range, will be NULL if a failure occurred
63 */
64template <typename X> X* Calloc(size_t size, const char* output)
65{
66 X* buffer = NULL;
67 buffer = (X*) calloc(sizeof(X) * size, (size_t) 0);
[c30180]68
69 if (buffer != NULL) {
70 MemoryUsageObserver::getInstance()->addMemory(buffer, size);
71 } else {
[29812d]72 cout << Verbose(0) << "Calloc for datatype " << typeid(X).name()
73 << " failed - pointer is NULL: " << output << endl;
[c30180]74 }
[29812d]75
76 return buffer;
77};
78
[f66195]79/** Reallocates a memory range using realloc(). If the provided pointer to the old
[29812d]80 * memory range is NULL, malloc() is called instead.
81 * Prints the provided error message in case of a failure (of either malloc() or realloc()).
82 *
83 * \param pointer to the old memory range
84 * \param number of memory slices of type X to allocate
85 * \param failure message which is printed if the allocation fails
86 * \return pointer to the reallocated memory range, will be NULL if a failure occurred
87 */
88template <typename X> X* ReAlloc(X* OldPointer, size_t size, const char* output)
89{
90 X* buffer = NULL;
[c30180]91 if (OldPointer == NULL) {
[29812d]92 buffer = (X*) malloc(sizeof(X) * size);
[c30180]93 } else {
[29812d]94 buffer = (X*) realloc(OldPointer, sizeof(X) * size);
[c30180]95 MemoryUsageObserver::getInstance()->removeMemory(OldPointer);
96 }
97 if (buffer != NULL) {
98 MemoryUsageObserver::getInstance()->addMemory(buffer, size);
99 } else {
[29812d]100 cout << Verbose(0) << "ReAlloc for datatype " << typeid(X).name()
101 << " failed - new is NULL: " << output << endl;
[c30180]102 }
[29812d]103
104 return buffer;
105};
106
[f66195]107/** Frees allocated memory range using free().
[29812d]108 *
109 * \param pointer to the allocated memory range to free; may be NULL, this function is a no-op then
[c26f44]110 * \param *msg optional error message
[29812d]111 */
[c26f44]112template <typename X> void Free(X** buffer, const char *msg = NULL)
[29812d]113{
114 if ((buffer == NULL) || (*buffer == NULL))
115 return;
116
[21b9c3]117 MemoryUsageObserver::getInstance()->removeMemory(*buffer, msg);
[29812d]118 free(*buffer);
119 *buffer = NULL;
120};
121
122#endif /*MEMORYALLOCATOR_HPP_*/
Note: See TracBrowser for help on using the repository browser.