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