source: src/memoryallocator.hpp@ 29812d

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 Candidate_v1.7.0 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 29812d was 29812d, 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.