source: src/Helpers/MemDebug.cpp@ 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 cdf8fb, checked in by Frederik Heber <heber@…>, 15 years ago

Merge branch 'FixMemDebug_strncpy' into StructureRefactoring

  • Property mode set to 100644
File size: 7.0 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
16 // This struct is added before each memory chunk
17 // and contains tracking information. Anything used
18 // to track memory cannot use any dynamic memory, so
19 // we have to resort to classic C-idioms here.
20 // This struct also contains pointers to the next
21 // an previous chunks to allow fast traversion of
22 // all allocated memory blocks
23 struct entry_t {
24 // we seperate the tracking info from the rest
25 // A checksum will be calculated for this part of
26 // the struct, so the information in here should
27 // not change during the lifetime of the memory
28 struct info_t {
29 enum {length = 64};
30 char file[length+1];
31 int line;
32 size_t nbytes;
33 bool isUsed;
34 void *location;
35 } info;
36 bool isIgnored;
37 char checksum;
38 entry_t *prev;
39 entry_t *next;
40 };
41
42 // start and end of the doubly-linked list
43 entry_t *begin=0;
44 entry_t *end=0;
45
46 // current amount of allocated memory
47 size_t state = 0;
48 // maximum amount of allocated memory
49 size_t max = 0;
50 // number of allocations that have been done so far
51 unsigned int allocs = 0;
52
53
54 // this sets the alignment of the returned memory block
55 // malloc guarantees an alignment at the 8 byte border,
56 // so we just do the same
57 const int alignment = 8;
58
59 // calculates a simple checksum for the info block
60 // the checksum is used to find memory corruptions
61 inline char calcChecksum(entry_t::info_t *info){
62 char *buffer = (char*)info;
63 char checksum =0;
64 for(size_t i=0;i<sizeof(entry_t::info_t);i++){
65 checksum+=buffer[i];
66 }
67 return checksum;
68 }
69
70 // gets the next alignet point which is greater than nbytes
71 // this function is only called a fixed number of times, so
72 // there is no need to optimize
73 inline size_t doAlign(size_t nbytes){
74 int nonaligned = nbytes % alignment;
75 if(nonaligned) {
76 return(nbytes - nonaligned + alignment);
77 }
78 else{
79 return nbytes;
80 }
81 }
82
83 // Output some state information
84 void getState(){
85 cout << "Maximum allocated Memory: " << max << " bytes" << endl;
86 cout << "Currently allocated Memory: " << state <<" bytes" << endl;
87 cout << allocs << " allocated chunks total" << endl;
88
89 // simple traversal of the chunk list
90 for(entry_t *pos=begin;pos;pos=pos->next){
91 cout << "\nChunk of " << pos->info.nbytes << " bytes" << " still available" << endl;
92 cout << "Chunk reserved at: " << pos->info.file << ":" << pos->info.line << endl;
93 }
94 }
95
96 // Deletes an entry from the linked list
97 void deleteEntry(entry_t *entry){
98 if(entry->isIgnored)
99 return;
100
101 if(entry->prev){
102 entry->prev->next = entry->next;
103 }
104 else{
105 // this node was the beginning of the list
106 begin = entry->next;
107 }
108
109 if(entry->next){
110 entry->next->prev = entry->prev;
111 }
112 else{
113 // this node was the end of the list
114 end = entry->prev;
115 }
116 entry->isIgnored = true;
117 Memory::state -= entry->info.nbytes;
118 }
119
120 void _ignore(void *ptr){
121 // just deletes the node from the list, but leaves the info intact
122 static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
123 entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
124 deleteEntry(entry);
125 }
126}
127
128void *operator new(size_t nbytes,const char* file, int line) throw(std::bad_alloc) {
129
130 // to avoid allocations of 0 bytes if someone screws up
131 // allocation with 0 byte size are undefined behavior, so we are
132 // free to handle it this way
133 if(!nbytes) {
134 nbytes = 1;
135 }
136
137 // get the size of the entry, including alignment
138 static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
139
140 void *res;
141 if(!(res=malloc(entrySpace + nbytes))){
142 // new must throw, when space is low
143 throw std::bad_alloc();
144 }
145
146 // we got the space, so update the global info
147 Memory::state += nbytes;
148 if(Memory::state>Memory::max){
149 Memory::max = Memory::state;
150 }
151 Memory::allocs++;
152
153 // build the entry in front of the space
154 Memory::entry_t *entry = (Memory::entry_t*) res;
155 entry->info.nbytes = nbytes;
156 entry->info.isUsed = true;
157 strncpy(entry->info.file,file,Memory::entry_t::info_t::length);
158 entry->info.file[Memory::entry_t::info_t::length] = '\0';
159 entry->info.line=line;
160 // the space starts behind the info
161 entry->info.location = (char*)res + entrySpace;
162
163 // add the entry at the end of the list
164 entry->next=0; // the created block is last in the list
165 entry->prev=Memory::end; // the created block is last in the list
166 if(!Memory::begin){
167 // the list was empty... start a new one
168 Memory::begin=entry;
169 }
170 else {
171 // other blocks present... we can add to the last one
172 Memory::end->next=entry;
173 }
174 Memory::end=entry;
175
176 // get the checksum...
177 entry->checksum = Memory::calcChecksum(&entry->info);
178 // this will be set to true, when the block is removed from
179 // the list for any reason
180 entry->isIgnored = false;
181
182 // ok, space is prepared... the user can have it.
183 // the rest (constructor, deleting when something is thrown etc)
184 // is handled automatically
185 return entry->info.location;
186}
187
188void *operator new(size_t nbytes) throw(std::bad_alloc) {
189 // Just forward to the other operator, when we do not know from
190 // where the allocation came
191 return operator new(nbytes,"Unknown",0);
192}
193
194void *operator new[] (size_t nbytes,const char* file, int line) throw(std::bad_alloc) {
195 // The difference between new and new[] is just for compiler bookkeeping.
196 return operator new(nbytes,file,line);
197}
198
199void *operator new[] (size_t nbytes) throw(std::bad_alloc) {
200 // Forward again
201 return operator new[] (nbytes,"Unknown",0);
202}
203
204void operator delete(void *ptr) throw() {
205 // get the size for the entry, including alignment
206 static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
207
208 // get the position for the entry from the pointer the user gave us
209 Memory::entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
210
211 // let's see if the checksum is still matching
212 if(Memory::calcChecksum(&entry->info)!=entry->checksum){
213 cout << "Possible memory corruption detected!" << endl;
214 cout << "Trying to recover allocation information..." << endl;
215 cout << "Memory was allocated at " << entry->info.file << ":" << entry->info.line << endl;
216 terminate();
217 }
218
219 // this will destroy the checksum, so double deletes are caught
220 entry->info.isUsed = false;
221 Memory::deleteEntry(entry);
222
223 // delete the space reserved by malloc
224 free((char*)ptr-entrySpace);
225}
226
227// operator that is called when the constructor throws
228// do not call manually
229void operator delete(void *ptr,const char*, int) throw() {
230 operator delete(ptr);
231}
232
233void operator delete[](void *ptr){
234 // again difference between delete and delete[] is just in compiler bookkeeping
235 operator delete(ptr);
236}
237
238// and another operator that can be called when a constructor throws
239void operator delete[](void *ptr,const char*, int) throw(){
240 operator delete(ptr);
241}
Note: See TracBrowser for help on using the repository browser.