source: src/Helpers/MemDebug.cpp@ 28c351

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

FIX: strncpy was lacking cstring include.

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