Skip to content

Commit e0b1a67

Browse files
committed
To avoid memory corruption with debug runtime. And code cleanup.
1 parent c914177 commit e0b1a67

File tree

1 file changed

+15
-35
lines changed

1 file changed

+15
-35
lines changed

src/xrCore/FixedMap.h

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class FixedMAP
1616
K key;
1717
T val;
1818
TNode *left, *right;
19+
20+
static void *operator new (size_t size) { return allocator::alloc(size); }
21+
static void *operator new[](size_t size) { return allocator::alloc(size); }
22+
static void operator delete (void *block) { allocator::dealloc(block); }
23+
static void operator delete[](void *block) { allocator::dealloc(block); }
1924
};
2025
typedef void __fastcall callback(TNode*);
2126
typedef bool __fastcall callback_cmp(TNode& N1, TNode& N2);
@@ -30,12 +35,11 @@ class FixedMAP
3035
{
3136
u32 newLimit = limit + SG_REALLOC_ADVANCE;
3237
VERIFY(newLimit % SG_REALLOC_ADVANCE == 0);
33-
TNode* newNodes = (TNode*)allocator::alloc(sizeof(TNode) * newLimit);
38+
TNode* newNodes = new TNode[newLimit];
3439
VERIFY(newNodes);
3540

36-
ZeroMemory(newNodes, Size(newLimit));
3741
if (limit)
38-
CopyMemory(newNodes, nodes, Size(limit));
42+
std::copy(nodes, nodes + limit, newNodes);
3943

4044
for (u32 I = 0; I < pool; I++)
4145
{
@@ -55,7 +59,7 @@ class FixedMAP
5559
}
5660
}
5761
if (nodes)
58-
allocator::dealloc(nodes);
62+
delete[] nodes;
5963

6064
nodes = newNodes;
6165
limit = newLimit;
@@ -139,11 +143,10 @@ class FixedMAP
139143
void destroy()
140144
{
141145
if (nodes)
142-
{
143-
for (TNode* cur = begin(); cur != last(); cur++)
144-
cur->~TNode();
145-
allocator::dealloc(nodes);
146-
}
146+
delete[] nodes;
147+
nodes = 0;
148+
pool = 0;
149+
limit = 0;
147150
}
148151
IC TNode* insert(const K& k)
149152
{
@@ -241,14 +244,6 @@ class FixedMAP
241244
N->val = v;
242245
return N;
243246
}
244-
IC void discard()
245-
{
246-
if (nodes)
247-
allocator::dealloc(nodes);
248-
nodes = 0;
249-
pool = 0;
250-
limit = 0;
251-
}
252247
IC u32 allocated() { return this->limit; }
253248
IC void clear() { pool = 0; }
254249
IC TNode* begin() { return nodes; }
@@ -293,27 +288,12 @@ class FixedMAP
293288
if (pool)
294289
getRL_P(nodes, D);
295290
}
296-
IC void getANY(xr_vector<T, typename allocator::template helper<T>::result>& D)
297-
{
298-
TNode* _end = end();
299-
for (TNode* cur = begin(); cur != _end; cur++)
300-
D.push_back(cur->val);
301-
}
302291
IC void getANY_P(xr_vector<TNode*, typename allocator::template helper<TNode*>::result>& D)
303292
{
304-
D.resize(size());
305-
TNode** _it = &*D.begin();
293+
D.reserve(size());
306294
TNode* _end = end();
307-
for (TNode *cur = begin(); cur != _end; cur++, _it++)
308-
*_it = cur;
309-
}
310-
IC void getANY_P(xr_vector<void*, typename allocator::template helper<void*>::result>& D)
311-
{
312-
D.resize(size());
313-
void** _it = &*D.begin();
314-
TNode* _end = end();
315-
for (TNode *cur = begin(); cur != _end; cur++, _it++)
316-
*_it = cur;
295+
for (TNode* cur = begin(); cur != _end; cur++)
296+
D.push_back(cur);
317297
}
318298
IC void setup(callback CB)
319299
{

0 commit comments

Comments
 (0)