source: src/Helpers/MemDebug.cpp@ 632bc3

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 632bc3 was 632bc3, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added a small memory tracker to the programm.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 * MemDebug.cpp
3 *
4 * Created on: Apr 28, 2010
5 * Author: crueger
6 */
7
8#include <iostream>
9#include <cstdlib>
10
11using namespace std;
12
13namespace Memory {
14 struct entry_t {
15 struct info_t {
16 char file[256];
17 int line;
18 size_t nbytes;
19 bool isUsed;
20 void *location;
21 } info;
22 bool isIgnored;
23 char checksum;
24 entry_t *prev;
25 entry_t *next;
26 };
27
28 entry_t *begin=0;
29 entry_t *end=0;
30
31 size_t state = 0;
32 size_t max = 0;
33
34 const int alignment = 8;
35
36 inline char calcChecksum(entry_t::info_t *info){
37 char *buffer = (char*)info;
38 char checksum =0;
39 for(size_t i=0;i<sizeof(entry_t::info_t);i++){
40 checksum+=buffer[i];
41 }
42 return checksum;
43 }
44
45 inline size_t doAlign(size_t nbytes){
46 int nonaligned = nbytes % alignment;
47 if(nonaligned) {
48 return(nbytes - nonaligned + alignment);
49 }
50 else{
51 return nbytes;
52 }
53 }
54
55 void getState(){
56 cout << "Maximum allocated Memory: " << max << " bytes" << endl;
57 cout << "Currently allocated Memory: " << state <<" bytes" << endl;
58
59 for(entry_t *pos=begin;pos;pos=pos->next){
60 cout << "\nChunk of " << pos->info.nbytes << " bytes" << " still available" << endl;
61 cout << "Chunk reserved at: " << pos->info.file << ":" << pos->info.line << endl;
62 }
63 }
64
65 void deleteEntry(entry_t *entry){
66 if(entry->isIgnored)
67 return;
68 if(entry->prev){
69 entry->prev->next = entry->next;
70 }
71 else{
72 begin = entry->next;
73 }
74
75 if(entry->next){
76 entry->next->prev = entry->prev;
77 }
78 else{
79 end = entry->prev;
80 }
81 entry->isIgnored = true;
82 Memory::state -= entry->info.nbytes;
83 }
84
85 void _ignore(void *ptr){
86 static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
87 entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
88 deleteEntry(entry);
89 }
90}
91
92void *operator new(size_t nbytes,const char* file, int line) throw(std::bad_alloc) {
93
94 if(!nbytes) {
95 nbytes = 1;
96 }
97
98 static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
99
100 void *res;
101 if(!(res=malloc(entrySpace + nbytes))){
102 throw std::bad_alloc();
103 }
104
105 Memory::state += nbytes;
106 if(Memory::state>Memory::max){
107 Memory::max = Memory::state;
108 }
109
110 Memory::entry_t *entry = (Memory::entry_t*) res;
111 entry->info.nbytes = nbytes;
112 entry->info.isUsed = true;
113 strncpy(entry->info.file,file,256);
114 entry->info.file[255] = '\0';
115 entry->info.line=line;
116 entry->info.location = (char*)res + entrySpace;
117
118 entry->next=0;
119 entry->prev=Memory::end;
120 if(!Memory::begin){
121 Memory::begin=entry;
122 }
123 else {
124 Memory::end->next=entry;
125 }
126 Memory::end=entry;
127
128 entry->checksum = Memory::calcChecksum(&entry->info);
129 entry->isIgnored = false;
130
131 return entry->info.location;
132}
133
134void *operator new(size_t nbytes) throw(std::bad_alloc) {
135 return operator new(nbytes,"Unknown",0);
136}
137
138void *operator new[] (size_t nbytes,const char* file, int line) throw(std::bad_alloc) {
139 return operator new(nbytes,file,line);
140}
141
142void *operator new[] (size_t nbytes) throw(std::bad_alloc) {
143 return operator new[] (nbytes,"Unknown",0);
144}
145
146void operator delete(void *ptr) throw() {
147 static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
148
149 Memory::entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
150
151 if(Memory::calcChecksum(&entry->info)!=entry->checksum){
152 cout << "Possible memory corruption detected!" << endl;
153 cout << "Trying to recover allocation information..." << endl;
154 cout << "Memory was allocated at " << entry->info.file << ":" << entry->info.line << endl;
155 terminate();
156 }
157
158 entry->info.isUsed = false;
159 Memory::deleteEntry(entry);
160
161 free((char*)ptr-entrySpace);
162}
163
164void operator delete(void *ptr,const char*, int) throw() {
165 operator delete(ptr);
166}
167
168void operator delete[](void *ptr){
169 operator delete(ptr);
170}
171
172void operator delete[](void *ptr,const char*, int) throw(){
173 operator delete(ptr);
174}
Note: See TracBrowser for help on using the repository browser.