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