Skip to content

Commit 16d55c5

Browse files
committed
Static / Dynamic allocator implemented
- Allocator identified at startup - Completely untested (doesn't compile yet) - Still need to handle WM_COMMAND, and pass them through to MenuManager - Some class names are wrong / non-ideal :)
1 parent 09840f0 commit 16d55c5

12 files changed

Lines changed: 340 additions & 17 deletions

PythonScript/project/PythonScript2010.vcxproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,13 @@
238238
<ClCompile Include="..\src\AboutDialog2.cpp" />
239239
<ClCompile Include="..\src\ConfigFile.cpp" />
240240
<ClCompile Include="..\src\ConsoleDialog.cpp" />
241+
<ClCompile Include="..\src\DynamicIDManager.cpp" />
241242
<ClCompile Include="..\src\EnumsWrapper.cpp" />
242243
<ClCompile Include="..\src\HelpController.cpp" />
243244
<ClCompile Include="..\src\MenuManager.cpp" />
244245
<ClCompile Include="..\src\NotepadPlusWrapper.cpp" />
245246
<ClCompile Include="..\src\NotepadPython.cpp" />
247+
<ClCompile Include="..\src\NppAllocator.cpp" />
246248
<ClCompile Include="..\src\ProcessExecute.cpp" />
247249
<ClCompile Include="..\src\PromptDialog.cpp" />
248250
<ClCompile Include="..\src\PyProducerConsumer.cpp" />
@@ -254,6 +256,7 @@
254256
<ClCompile Include="..\src\ScintillaWrapper.cpp" />
255257
<ClCompile Include="..\src\ScintillaWrapperGenerated.cpp" />
256258
<ClCompile Include="..\src\ShortcutDlg.cpp" />
259+
<ClCompile Include="..\src\StaticIDAllocator.cpp" />
257260
<ClCompile Include="..\src\stdafx.cpp">
258261
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
259262
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='DebugStartup|Win32'">Create</PrecompiledHeader>
@@ -267,12 +270,15 @@
267270
<ClInclude Include="..\src\ConfigFile.h" />
268271
<ClInclude Include="..\src\ConsoleDialog.h" />
269272
<ClInclude Include="..\src\ConsoleInterface.h" />
273+
<ClInclude Include="..\src\DynamicIDManager.h" />
270274
<ClInclude Include="..\src\Enums.h" />
271275
<ClInclude Include="..\src\HelpController.h" />
276+
<ClInclude Include="..\src\IDAllocator.h" />
272277
<ClInclude Include="..\src\MenuManager.h" />
273278
<ClInclude Include="..\src\NotepadPlusBuffer.h" />
274279
<ClInclude Include="..\src\NotepadPlusWrapper.h" />
275280
<ClInclude Include="..\src\NotepadPython.h" />
281+
<ClInclude Include="..\src\NppAllocator.h" />
276282
<ClInclude Include="..\src\ProcessExecute.h" />
277283
<ClInclude Include="..\src\PromptDialog.h" />
278284
<ClInclude Include="..\src\PyProducerConsumer.h" />
@@ -286,6 +292,7 @@
286292
<ClInclude Include="..\src\ScintillaPython.h" />
287293
<ClInclude Include="..\src\ScintillaWrapper.h" />
288294
<ClInclude Include="..\src\ShortcutDlg.h" />
295+
<ClInclude Include="..\src\StaticIDAllocator.h" />
289296
<ClInclude Include="..\src\stdafx.h" />
290297
<ClInclude Include="..\src\WcharMbcsConverter.h" />
291298
<ClInclude Include="..\src\resource1.h" />

PythonScript/project/PythonScript2010.vcxproj.filters

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@
135135
<ClCompile Include="..\src\HelpController.cpp">
136136
<Filter>Source Files</Filter>
137137
</ClCompile>
138+
<ClCompile Include="..\src\DynamicIDManager.cpp">
139+
<Filter>Source Files</Filter>
140+
</ClCompile>
141+
<ClCompile Include="..\src\StaticIDAllocator.cpp">
142+
<Filter>Source Files</Filter>
143+
</ClCompile>
144+
<ClCompile Include="..\src\NppAllocator.cpp">
145+
<Filter>Source Files</Filter>
146+
</ClCompile>
138147
</ItemGroup>
139148
<ItemGroup>
140149
<ClInclude Include="..\src\AboutDialog.h">
@@ -218,6 +227,18 @@
218227
<ClInclude Include="..\src\HelpController.h">
219228
<Filter>Header Files</Filter>
220229
</ClInclude>
230+
<ClInclude Include="..\src\DynamicIDManager.h">
231+
<Filter>Header Files</Filter>
232+
</ClInclude>
233+
<ClInclude Include="..\src\IDAllocator.h">
234+
<Filter>Header Files</Filter>
235+
</ClInclude>
236+
<ClInclude Include="..\src\StaticIDAllocator.h">
237+
<Filter>Header Files</Filter>
238+
</ClInclude>
239+
<ClInclude Include="..\src\NppAllocator.h">
240+
<Filter>Header Files</Filter>
241+
</ClInclude>
221242
</ItemGroup>
222243
<ItemGroup>
223244
<ResourceCompile Include="..\src\PythonScript.rc">
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
#include "stdafx.h"
3+
4+
#include "DynamicIDManager.h"
5+
6+
using namespace std;
7+
8+
9+
void DynamicIDManager::reserve(int quantity)
10+
{
11+
if (quantity < m_capacity)
12+
{
13+
reserveAdditional(quantity - m_capacity);
14+
}
15+
}
16+
17+
void DynamicIDManager::reserveAdditional(int quantity)
18+
{
19+
20+
int start;
21+
if (allocateIDs(quantity, &start))
22+
{
23+
t_idList::reverse_iterator iter = m_idList.rbegin();
24+
25+
// If this is a continuation of the last block,
26+
// just increase the quantity of the last block
27+
28+
if (iter != m_idList.rend()
29+
&& start == (iter->first + iter->second))
30+
{
31+
iter->second += quantity;
32+
}
33+
34+
else // Otherwise just add a new block
35+
{
36+
m_idList.push_back(pair<int, int>(start, quantity));
37+
}
38+
39+
m_capacity += quantity;
40+
}
41+
}
42+
43+
44+
int DynamicIDManager::begin()
45+
{
46+
m_current = m_idList.begin();
47+
if (m_current == m_idList.end())
48+
{
49+
reserveAdditional(10);
50+
m_current = m_idList.begin();
51+
m_nextID = m_current->first;
52+
}
53+
54+
return m_nextID++;
55+
}
56+
57+
int DynamicIDManager::currentID()
58+
{
59+
return m_nextID;
60+
}
61+
62+
DynamicIDManager& DynamicIDManager::operator++(int)
63+
{
64+
// If nothing has ever been allocated
65+
if (m_current == m_idList.end())
66+
{
67+
reserveAdditional(10);
68+
m_current = m_idList.begin();
69+
m_nextID = m_current->first;
70+
}
71+
else
72+
{
73+
++m_nextID;
74+
if (m_nextID >= (m_current->first + m_current->second))
75+
{
76+
++m_current;
77+
if (m_current == m_idList.end())
78+
{
79+
reserveAdditional(10);
80+
m_current = m_idList.end();
81+
--m_current;
82+
if (m_nextID >= (m_current->first + m_current->second))
83+
{
84+
throw exception("Out of IDs");
85+
}
86+
}
87+
}
88+
}
89+
90+
return *this;
91+
}
92+
93+
94+
bool DynamicIDManager::allocateIDs(int quantity, int *start)
95+
{
96+
return m_allocator->allocate(quantity, start);
97+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ifndef _DYNAMICIDMANAGER_H
2+
#define _DYNAMICIDMANAGER_H
3+
4+
#include "IDAllocator.h"
5+
6+
class DynamicIDManager
7+
{
8+
public:
9+
10+
DynamicIDManager(IDAllocator *allocator)
11+
: m_allocator (allocator)
12+
{
13+
m_current = m_idList.begin();
14+
};
15+
16+
DynamicIDManager(IDAllocator *allocator, int initialStart, int quantity)
17+
: m_allocator (allocator)
18+
{
19+
m_idList.push_back(std::pair<int, int>(initialStart, quantity));
20+
m_current = m_idList.begin();
21+
m_capacity = quantity;
22+
m_nextID = initialStart + quantity;
23+
24+
};
25+
26+
27+
void reserve(int quantity);
28+
29+
void reserveAdditional(int quantity);
30+
31+
int begin();
32+
33+
int currentID();
34+
35+
// Post-increment operator
36+
DynamicIDManager& operator++(int);
37+
38+
39+
int capacity() { return m_capacity; };
40+
41+
private:
42+
// Methods
43+
bool allocateIDs(int quantity, int *start);
44+
45+
46+
IDAllocator* m_allocator;
47+
48+
// Data
49+
typedef std::list< std::pair<int, int> > t_idList;
50+
t_idList m_idList;
51+
t_idList::iterator m_current;
52+
53+
int m_nextID;
54+
55+
int m_capacity;
56+
57+
HWND m_nppHandle;
58+
};
59+
60+
61+
#endif

PythonScript/src/IDAllocator.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
#ifndef _IDALLOCATOR_H
3+
#define _IDALLOCATOR_H
4+
5+
class IDAllocator
6+
{
7+
public:
8+
virtual bool allocate(int quantity, int *start) = 0;
9+
};
10+
11+
#endif
12+

0 commit comments

Comments
 (0)