Skip to content

Commit 40b941f

Browse files
committed
Review updates.
1 parent c7f6f0f commit 40b941f

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

source/ScmFunction.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@
66
namespace CLEO
77
{
88
ScmFunction* ScmFunction::store[Store_Size] = {0};
9-
WORD ScmFunction::lastAllocIdx = 0;
9+
WORD ScmFunction::lastAllocIdx = Id_None + 1;
1010

1111
ScmFunction* ScmFunction::Get(WORD id)
1212
{
1313
if (id == Id_None) return nullptr;
1414

15-
auto idx = id - 1; // skip Id_None
16-
17-
if (idx >= Store_Size || store[idx] == nullptr)
15+
if (id >= Store_Size || store[id] == nullptr)
1816
{
1917
SHOW_ERROR("CLEO function with id %d not found in the storage!", id);
2018
return nullptr;
2119
}
2220

23-
return store[idx];
21+
return store[id];
2422
}
2523

2624
void ScmFunction::Clear()
@@ -38,7 +36,7 @@ namespace CLEO
3836
while (store[lastAllocIdx] != nullptr) // find first unused position in store
3937
{
4038
lastAllocIdx++;
41-
if (lastAllocIdx >= Store_Size) lastAllocIdx = 0; // warp around
39+
if (lastAllocIdx >= Store_Size) lastAllocIdx = Id_None + 1; // warp around
4240

4341
if (lastAllocIdx == searchStart)
4442
{
@@ -54,16 +52,23 @@ namespace CLEO
5452

5553
void ScmFunction::operator delete(void* mem)
5654
{
57-
auto idx = reinterpret_cast<ScmFunction*>(mem)->thisScmFunctionId - 1;
58-
store[idx] = nullptr;
55+
auto id = reinterpret_cast<ScmFunction*>(mem)->thisScmFunctionId;
56+
57+
if (store[id] != mem)
58+
{
59+
SHOW_ERROR("Failed to delete corrupted CLEO function from storage!");
60+
return;
61+
}
62+
63+
store[id] = nullptr;
5964
::operator delete(mem);
6065
}
6166

6267
ScmFunction::ScmFunction(CLEO::CRunningScript* thread, BYTE* callIP)
6368
{
6469
auto cs = reinterpret_cast<CCustomScript*>(thread);
6570

66-
thisScmFunctionId = ScmFunction::lastAllocIdx + 1; // skip Id_None
71+
thisScmFunctionId = ScmFunction::lastAllocIdx;
6772
prevScmFunctionId = cs->GetScmFunction();
6873
this->callIP = callIP;
6974

@@ -137,7 +142,7 @@ namespace CLEO
137142

138143
size_t ScmFunction::GetCallStackSize() const
139144
{
140-
if (prevScmFunctionId == Id_None) return 0;
141-
return 1 + Get(prevScmFunctionId)->GetCallStackSize();
145+
auto parent = Get(prevScmFunctionId);
146+
return parent ? 1 + parent->GetCallStackSize() : 0;
142147
}
143148
}; // namespace CLEO

0 commit comments

Comments
 (0)