source: src/memoryallocator.hpp@ 0e01b4

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