Changeset 1775d3 for molecuilder
- Timestamp:
- Apr 30, 2010, 8:01:18 AM (16 years ago)
- Children:
- 8c01ce
- Parents:
- 070651 (diff), c53e0b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - Location:
- molecuilder/src
- Files:
-
- 7 edited
-
Helpers/MemDebug.cpp (modified) (10 diffs)
-
Helpers/MemDebug.hpp (modified) (2 diffs)
-
Plane.cpp (modified) (1 diff)
-
Plane.hpp (modified) (2 diffs)
-
unittests/vectorunittest.cpp (modified) (2 diffs)
-
unittests/vectorunittest.hpp (modified) (3 diffs)
-
vector.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
molecuilder/src/Helpers/MemDebug.cpp
r070651 r1775d3 8 8 #include <iostream> 9 9 #include <cstdlib> 10 #include <cstring> 10 11 11 12 using namespace std; 12 13 13 14 namespace 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 14 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 15 28 struct info_t { 16 char file[256]; 29 enum {length = 64}; 30 char file[length+1]; 17 31 int line; 18 32 size_t nbytes; … … 26 40 }; 27 41 42 // start and end of the doubly-linked list 28 43 entry_t *begin=0; 29 44 entry_t *end=0; 30 45 46 // current amount of allocated memory 31 47 size_t state = 0; 48 // maximum amount of allocated memory 32 49 size_t max = 0; 33 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 34 57 const int alignment = 8; 35 58 59 // calculates a simple checksum for the info block 60 // the checksum is used to find memory corruptions 36 61 inline char calcChecksum(entry_t::info_t *info){ 37 62 char *buffer = (char*)info; … … 43 68 } 44 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 45 73 inline size_t doAlign(size_t nbytes){ 46 74 int nonaligned = nbytes % alignment; … … 53 81 } 54 82 83 // Output some state information 55 84 void getState(){ 56 85 cout << "Maximum allocated Memory: " << max << " bytes" << endl; 57 86 cout << "Currently allocated Memory: " << state <<" bytes" << endl; 58 87 cout << allocs << " allocated chunks total" << endl; 88 89 // simple traversal of the chunk list 59 90 for(entry_t *pos=begin;pos;pos=pos->next){ 60 91 cout << "\nChunk of " << pos->info.nbytes << " bytes" << " still available" << endl; … … 63 94 } 64 95 96 // Deletes an entry from the linked list 65 97 void deleteEntry(entry_t *entry){ 66 98 if(entry->isIgnored) 67 99 return; 100 68 101 if(entry->prev){ 69 102 entry->prev->next = entry->next; 70 103 } 71 104 else{ 105 // this node was the beginning of the list 72 106 begin = entry->next; 73 107 } … … 77 111 } 78 112 else{ 113 // this node was the end of the list 79 114 end = entry->prev; 80 115 } … … 84 119 85 120 void _ignore(void *ptr){ 121 // just deletes the node from the list, but leaves the info intact 86 122 static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t)); 87 123 entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace); … … 92 128 void *operator new(size_t nbytes,const char* file, int line) throw(std::bad_alloc) { 93 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 94 133 if(!nbytes) { 95 134 nbytes = 1; 96 135 } 97 136 137 // get the size of the entry, including alignment 98 138 static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t)); 99 139 100 140 void *res; 101 141 if(!(res=malloc(entrySpace + nbytes))){ 142 // new must throw, when space is low 102 143 throw std::bad_alloc(); 103 144 } 104 145 146 // we got the space, so update the global info 105 147 Memory::state += nbytes; 106 148 if(Memory::state>Memory::max){ 107 149 Memory::max = Memory::state; 108 150 } 109 151 Memory::allocs++; 152 153 // build the entry in front of the space 110 154 Memory::entry_t *entry = (Memory::entry_t*) res; 111 155 entry->info.nbytes = nbytes; 112 156 entry->info.isUsed = true; 113 strncpy(entry->info.file,file, 256);114 entry->info.file[ 255] = '\0';157 strncpy(entry->info.file,file,Memory::entry_t::info_t::length); 158 entry->info.file[Memory::entry_t::info_t::length] = '\0'; 115 159 entry->info.line=line; 160 // the space starts behind the info 116 161 entry->info.location = (char*)res + entrySpace; 117 162 118 entry->next=0; 119 entry->prev=Memory::end; 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 120 166 if(!Memory::begin){ 167 // the list was empty... start a new one 121 168 Memory::begin=entry; 122 169 } 123 170 else { 171 // other blocks present... we can add to the last one 124 172 Memory::end->next=entry; 125 173 } 126 174 Memory::end=entry; 127 175 176 // get the checksum... 128 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 129 180 entry->isIgnored = false; 130 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 131 185 return entry->info.location; 132 186 } 133 187 134 188 void *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 135 191 return operator new(nbytes,"Unknown",0); 136 192 } 137 193 138 194 void *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. 139 196 return operator new(nbytes,file,line); 140 197 } 141 198 142 199 void *operator new[] (size_t nbytes) throw(std::bad_alloc) { 200 // Forward again 143 201 return operator new[] (nbytes,"Unknown",0); 144 202 } 145 203 146 204 void operator delete(void *ptr) throw() { 205 // get the size for the entry, including alignment 147 206 static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t)); 148 207 208 // get the position for the entry from the pointer the user gave us 149 209 Memory::entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace); 150 210 211 // let's see if the checksum is still matching 151 212 if(Memory::calcChecksum(&entry->info)!=entry->checksum){ 152 213 cout << "Possible memory corruption detected!" << endl; … … 156 217 } 157 218 219 // this will destroy the checksum, so double deletes are caught 158 220 entry->info.isUsed = false; 159 221 Memory::deleteEntry(entry); 160 222 223 // delete the space reserved by malloc 161 224 free((char*)ptr-entrySpace); 162 225 } 163 226 227 // operator that is called when the constructor throws 228 // do not call manually 164 229 void operator delete(void *ptr,const char*, int) throw() { 165 230 operator delete(ptr); … … 167 232 168 233 void operator delete[](void *ptr){ 234 // again difference between delete and delete[] is just in compiler bookkeeping 169 235 operator delete(ptr); 170 236 } 171 237 238 // and another operator that can be called when a constructor throws 172 239 void operator delete[](void *ptr,const char*, int) throw(){ 173 240 operator delete(ptr); -
molecuilder/src/Helpers/MemDebug.hpp
r070651 r1775d3 9 9 #define MEMDEBUG_HPP_ 10 10 11 /** 12 * @file 13 * This module allows easy automatic memory tracking. Link this module to replace the 14 * operators new, new[], delete and delete[] with custom versions that add tracking 15 * information to allocated blocks. 16 * 17 * All tracking is done in O(1) for new and delete operators. Summaries for the 18 * used memory take O(N), where N is the number of currently allocated memory chunks. 19 * 20 * To use full tracking including file name and line number include this file in 21 * your sourcefiles. 22 */ 23 11 24 namespace Memory { 25 26 /** 27 * Displays a short summary of the memory state. 28 */ 12 29 void getState(); 13 30 14 31 void _ignore(void*); 15 32 33 /** 34 * Use this to disable memory for a certain pointer on the heap. 35 * This is useful for pointers which should live over the run of the 36 * program, and which are deleted automatically at the end. Usually these 37 * pointers should be wrapped inside some kind of smart pointer to 38 * ensure destruction at the end. 39 */ 16 40 template <typename T> 17 41 T *ignore(T* ptr){ … … 26 50 void operator delete[] (void *ptr,const char*, int) throw(); 27 51 28 52 /** 53 * This macro replaces all occurences of the keyword new with a replaced 54 * version that allows tracking. 55 */ 29 56 #define new new(__FILE__,__LINE__) 30 57 -
molecuilder/src/Plane.cpp
r070651 r1775d3 98 98 } 99 99 100 vector<Vector> Plane::getPointsOnPlane(){ 101 std::vector<Vector> res; 102 // first point on the plane 103 res[0] = getOffsetVector(); 104 // first is orthogonal to the plane... 105 // an orthogonal vector to this one lies on the plane 106 Vector direction; 107 direction.GetOneNormalVector(res[0]); 108 res[1] = res[0]+direction; 109 // get an orthogonal vector to direction and offset (lies on the plane) 110 direction.VectorProduct(res[0]); 111 direction.Normalize(); 112 res[2] = res[0] +direction; 113 return res; 114 } 115 116 100 117 /** Calculates the intersection point between a line defined by \a *LineVector and \a *LineVector2 and a plane defined by \a *Normal and \a *PlaneOffset. 101 118 * According to [Bronstein] the vectorial plane equation is: -
molecuilder/src/Plane.hpp
r070651 r1775d3 10 10 11 11 #include <memory> 12 #include <vector> 12 13 13 14 class Vector; … … 38 39 Vector getOffsetVector(); 39 40 41 /** 42 * returns three seperate points on this plane 43 */ 44 std::vector<Vector> getPointsOnPlane(); 45 40 46 // some calculations 41 47 Vector GetIntersection(const Vector &Origin, const Vector &LineVector); -
molecuilder/src/unittests/vectorunittest.cpp
r070651 r1775d3 43 43 notunit = Vector(0.,1.,1.); 44 44 two = Vector(2.,1.,0.); 45 three = Vector(1,2,3); 45 46 }; 46 47 … … 185 186 CPPUNIT_ASSERT_EQUAL( Vector(0.,1.,0.), two.Projection(otherunit) ); 186 187 }; 188 189 /** 190 * Unittest for operation with normals 191 */ 192 void VectorTest::NormalsTest(){ 193 Vector testVector; 194 // the zero Vector should produce an error 195 CPPUNIT_ASSERT(!testVector.GetOneNormalVector(zero)); 196 197 // first one-component system 198 CPPUNIT_ASSERT(testVector.GetOneNormalVector(unit)); 199 CPPUNIT_ASSERT(testVector.ScalarProduct(unit) < MYEPSILON); 200 201 // second one-component system 202 CPPUNIT_ASSERT(testVector.GetOneNormalVector(otherunit)); 203 CPPUNIT_ASSERT(testVector.ScalarProduct(otherunit) < MYEPSILON); 204 205 // first two-component system 206 CPPUNIT_ASSERT(testVector.GetOneNormalVector(notunit)); 207 CPPUNIT_ASSERT(testVector.ScalarProduct(notunit) < MYEPSILON); 208 209 // second two-component system 210 CPPUNIT_ASSERT(testVector.GetOneNormalVector(two)); 211 CPPUNIT_ASSERT(testVector.ScalarProduct(two) < MYEPSILON); 212 213 // three component system 214 CPPUNIT_ASSERT(testVector.GetOneNormalVector(three)); 215 CPPUNIT_ASSERT(testVector.ScalarProduct(three) < MYEPSILON); 216 } 187 217 188 218 /** UnitTest for line intersections. -
molecuilder/src/unittests/vectorunittest.hpp
r070651 r1775d3 26 26 CPPUNIT_TEST ( EuclidianAnglesTest ); 27 27 CPPUNIT_TEST ( ProjectionTest ); 28 CPPUNIT_TEST ( NormalsTest ); 28 29 CPPUNIT_TEST ( LineIntersectionTest ); 29 30 CPPUNIT_TEST ( VectorRotationTest ); … … 43 44 void EuclidianAnglesTest(); 44 45 void ProjectionTest(); 46 void NormalsTest(); 45 47 void LineIntersectionTest(); 46 48 void VectorRotationTest(); … … 56 58 Vector notunit; 57 59 Vector two; 60 Vector three; 58 61 }; 59 62 -
molecuilder/src/vector.cpp
r070651 r1775d3 647 647 for (j=NDIM;j--;) 648 648 Components[j] = -1; 649 650 // in two component-systems we need to find the one position that is zero 651 int zeroPos = -1; 649 652 // find two components != 0 650 for (j=0;j<NDIM;j++) 653 for (j=0;j<NDIM;j++){ 651 654 if (fabs(GivenVector[j]) > MYEPSILON) 652 655 Components[Last++] = j; 656 else 657 // this our zero Position 658 zeroPos = j; 659 } 653 660 654 661 switch(Last) { 655 662 case 3: // threecomponent system 663 // the position of the zero is arbitrary in three component systems 664 zeroPos = Components[2]; 656 665 case 2: // two component system 657 666 norm = sqrt(1./(GivenVector[Components[1]]*GivenVector[Components[1]]) + 1./(GivenVector[Components[0]]*GivenVector[Components[0]])); 658 x[Components[2]]= 0.;667 at(zeroPos) = 0.; 659 668 // in skp both remaining parts shall become zero but with opposite sign and third is zero 660 x[Components[1]]= -1./GivenVector[Components[1]] / norm;661 x[Components[0]]= 1./GivenVector[Components[0]] / norm;669 at(Components[1]) = -1./GivenVector[Components[1]] / norm; 670 at(Components[0]) = 1./GivenVector[Components[0]] / norm; 662 671 return true; 663 672 break; 664 673 case 1: // one component system 665 674 // set sole non-zero component to 0, and one of the other zero component pendants to 1 666 x[(Components[0]+2)%NDIM]= 0.;667 x[(Components[0]+1)%NDIM]= 1.;668 x[Components[0]]= 0.;675 at((Components[0]+2)%NDIM) = 0.; 676 at((Components[0]+1)%NDIM) = 1.; 677 at(Components[0]) = 0.; 669 678 return true; 670 679 break;
Note:
See TracChangeset
for help on using the changeset viewer.
