the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 164 lines 3.6 kB view raw
1#include "stdafx.h" 2#include "IntCache.h" 3 4DWORD IntCache::tlsIdx = TlsAlloc(); 5 6void IntCache::CreateNewThreadStorage() 7{ 8 ThreadStorage *tls = new ThreadStorage(); 9 TlsSetValue(tlsIdx, (void *)tls); 10 tls->maxSize = TINY_CUTOFF; 11} 12 13IntCache::ThreadStorage::~ThreadStorage() 14{ 15 for(unsigned int i = 0; i < tcache.size(); i++ ) 16 { 17 delete [] tcache[i].data; 18 } 19 for(unsigned int i = 0; i < tallocated.size(); i++ ) 20 { 21 delete [] tallocated[i].data; 22 } 23 for(unsigned int i = 0; i < cache.size(); i++ ) 24 { 25 delete [] cache[i].data; 26 } 27 for(unsigned int i = 0; i < allocated.size(); i++ ) 28 { 29 delete [] allocated[i].data; 30 } 31 for( int i = 0; i < toosmall.size(); i++ ) 32 { 33 delete [] toosmall[i].data; 34 } 35} 36 37void IntCache::ReleaseThreadStorage() 38{ 39 ThreadStorage *tls = (ThreadStorage *)TlsGetValue(tlsIdx); 40 41 42 delete tls; 43} 44 45intArray IntCache::allocate(int size) 46{ 47 ThreadStorage *tls = (ThreadStorage *)TlsGetValue(tlsIdx); 48 49 if (size <= TINY_CUTOFF) 50 { 51 if (tls->tcache.empty()) 52 { 53 intArray result = intArray(TINY_CUTOFF, true); 54 tls->tallocated.push_back(result); 55 return result; 56 } 57 else 58 { 59 intArray result = tls->tcache.back(); 60 tls->tcache.pop_back(); 61 tls->tallocated.push_back(result); 62 return result; 63 } 64 } 65 66 if (size > tls->maxSize) 67 { 68// app.DebugPrintf("IntCache: New max size: %d\n" , size); 69 tls->maxSize = size; 70 71 // 4J - added - all the vectors in cache & allocated are smaller than maxSize so should be discarded. However, we 72 // can't delete them until the next releaseAll so copy into another vector until then 73 tls->toosmall.insert(tls->toosmall.end(),tls->cache.begin(),tls->cache.end()); 74 tls->toosmall.insert(tls->toosmall.end(),tls->allocated.begin(),tls->allocated.end()); 75 76 tls->cache.clear(); 77 tls->allocated.clear(); 78 79 intArray result = intArray(tls->maxSize, true); 80 tls->allocated.push_back(result); 81 return result; 82 } 83 else 84 { 85 if (tls->cache.empty()) 86 { 87 intArray result = intArray(tls->maxSize, true); 88 tls->allocated.push_back(result); 89 return result; 90 } 91 else 92 { 93 intArray result = tls->cache.back(); 94 tls->cache.pop_back(); 95 tls->allocated.push_back(result); 96 return result; 97 } 98 } 99} 100 101void IntCache::releaseAll() 102{ 103 ThreadStorage *tls = (ThreadStorage *)TlsGetValue(tlsIdx); 104 105 // 4J - added - we can now remove the vectors that were deemed as too small (see comment in IntCache::allocate) 106 for( int i = 0; i < tls->toosmall.size(); i++ ) 107 { 108 delete [] tls->toosmall[i].data; 109 } 110 tls->toosmall.clear(); 111 112 if (!tls->cache.empty()) 113 { 114 delete [] tls->cache.back().data; 115 tls->cache.pop_back(); 116 } 117 if (!tls->tcache.empty()) 118 { 119 delete [] tls->tcache.back().data; 120 tls->tcache.pop_back(); 121 } 122 123 tls->cache.insert(tls->cache.end(),tls->allocated.begin(),tls->allocated.end()); 124 tls->tcache.insert(tls->tcache.end(),tls->tallocated.begin(),tls->tallocated.end()); 125 126 tls->allocated.clear(); 127 tls->tallocated.clear(); 128} 129 130// 4J added so that we can fully reset between levels 131void IntCache::Reset() 132{ 133 ThreadStorage *tls = (ThreadStorage *)TlsGetValue(tlsIdx); 134 tls->maxSize = TINY_CUTOFF; 135 for( int i = 0; i < tls->allocated.size(); i++ ) 136 { 137 delete [] tls->allocated[i].data; 138 } 139 tls->allocated.clear(); 140 141 for( int i = 0; i < tls->cache.size(); i++ ) 142 { 143 delete [] tls->cache[i].data; 144 } 145 tls->cache.clear(); 146 147 for( int i = 0; i < tls->tallocated.size(); i++ ) 148 { 149 delete [] tls->tallocated[i].data; 150 } 151 tls->tallocated.clear(); 152 153 for( int i = 0; i < tls->tcache.size(); i++ ) 154 { 155 delete [] tls->tcache[i].data; 156 } 157 tls->tcache.clear(); 158 159 for( int i = 0; i < tls->toosmall.size(); i++ ) 160 { 161 delete [] tls->toosmall[i].data; 162 } 163 tls->toosmall.clear(); 164}