Skip to content

Commit

Permalink
Static / Dynamic allocator implemented
Browse files Browse the repository at this point in the history
 - 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 :)
  • Loading branch information
bruderstein committed Sep 18, 2010
1 parent 09840f0 commit 16d55c5
Show file tree
Hide file tree
Showing 12 changed files with 340 additions and 17 deletions.
7 changes: 7 additions & 0 deletions PythonScript/project/PythonScript2010.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,13 @@
<ClCompile Include="..\src\AboutDialog2.cpp" />
<ClCompile Include="..\src\ConfigFile.cpp" />
<ClCompile Include="..\src\ConsoleDialog.cpp" />
<ClCompile Include="..\src\DynamicIDManager.cpp" />
<ClCompile Include="..\src\EnumsWrapper.cpp" />
<ClCompile Include="..\src\HelpController.cpp" />
<ClCompile Include="..\src\MenuManager.cpp" />
<ClCompile Include="..\src\NotepadPlusWrapper.cpp" />
<ClCompile Include="..\src\NotepadPython.cpp" />
<ClCompile Include="..\src\NppAllocator.cpp" />
<ClCompile Include="..\src\ProcessExecute.cpp" />
<ClCompile Include="..\src\PromptDialog.cpp" />
<ClCompile Include="..\src\PyProducerConsumer.cpp" />
Expand All @@ -254,6 +256,7 @@
<ClCompile Include="..\src\ScintillaWrapper.cpp" />
<ClCompile Include="..\src\ScintillaWrapperGenerated.cpp" />
<ClCompile Include="..\src\ShortcutDlg.cpp" />
<ClCompile Include="..\src\StaticIDAllocator.cpp" />
<ClCompile Include="..\src\stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='DebugStartup|Win32'">Create</PrecompiledHeader>
Expand All @@ -267,12 +270,15 @@
<ClInclude Include="..\src\ConfigFile.h" />
<ClInclude Include="..\src\ConsoleDialog.h" />
<ClInclude Include="..\src\ConsoleInterface.h" />
<ClInclude Include="..\src\DynamicIDManager.h" />
<ClInclude Include="..\src\Enums.h" />
<ClInclude Include="..\src\HelpController.h" />
<ClInclude Include="..\src\IDAllocator.h" />
<ClInclude Include="..\src\MenuManager.h" />
<ClInclude Include="..\src\NotepadPlusBuffer.h" />
<ClInclude Include="..\src\NotepadPlusWrapper.h" />
<ClInclude Include="..\src\NotepadPython.h" />
<ClInclude Include="..\src\NppAllocator.h" />
<ClInclude Include="..\src\ProcessExecute.h" />
<ClInclude Include="..\src\PromptDialog.h" />
<ClInclude Include="..\src\PyProducerConsumer.h" />
Expand All @@ -286,6 +292,7 @@
<ClInclude Include="..\src\ScintillaPython.h" />
<ClInclude Include="..\src\ScintillaWrapper.h" />
<ClInclude Include="..\src\ShortcutDlg.h" />
<ClInclude Include="..\src\StaticIDAllocator.h" />
<ClInclude Include="..\src\stdafx.h" />
<ClInclude Include="..\src\WcharMbcsConverter.h" />
<ClInclude Include="..\src\resource1.h" />
Expand Down
21 changes: 21 additions & 0 deletions PythonScript/project/PythonScript2010.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@
<ClCompile Include="..\src\HelpController.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\DynamicIDManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\StaticIDAllocator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\NppAllocator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\AboutDialog.h">
Expand Down Expand Up @@ -218,6 +227,18 @@
<ClInclude Include="..\src\HelpController.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\DynamicIDManager.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\IDAllocator.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\StaticIDAllocator.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\NppAllocator.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\src\PythonScript.rc">
Expand Down
97 changes: 97 additions & 0 deletions PythonScript/src/DynamicIDManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

#include "stdafx.h"

#include "DynamicIDManager.h"

using namespace std;


void DynamicIDManager::reserve(int quantity)
{
if (quantity < m_capacity)
{
reserveAdditional(quantity - m_capacity);
}
}

void DynamicIDManager::reserveAdditional(int quantity)
{

int start;
if (allocateIDs(quantity, &start))
{
t_idList::reverse_iterator iter = m_idList.rbegin();

// If this is a continuation of the last block,
// just increase the quantity of the last block

if (iter != m_idList.rend()
&& start == (iter->first + iter->second))
{
iter->second += quantity;
}

else // Otherwise just add a new block
{
m_idList.push_back(pair<int, int>(start, quantity));
}

m_capacity += quantity;
}
}


int DynamicIDManager::begin()
{
m_current = m_idList.begin();
if (m_current == m_idList.end())
{
reserveAdditional(10);
m_current = m_idList.begin();
m_nextID = m_current->first;
}

return m_nextID++;
}

int DynamicIDManager::currentID()
{
return m_nextID;
}

DynamicIDManager& DynamicIDManager::operator++(int)
{
// If nothing has ever been allocated
if (m_current == m_idList.end())
{
reserveAdditional(10);
m_current = m_idList.begin();
m_nextID = m_current->first;
}
else
{
++m_nextID;
if (m_nextID >= (m_current->first + m_current->second))
{
++m_current;
if (m_current == m_idList.end())
{
reserveAdditional(10);
m_current = m_idList.end();
--m_current;
if (m_nextID >= (m_current->first + m_current->second))
{
throw exception("Out of IDs");
}
}
}
}

return *this;
}


bool DynamicIDManager::allocateIDs(int quantity, int *start)
{
return m_allocator->allocate(quantity, start);
}
61 changes: 61 additions & 0 deletions PythonScript/src/DynamicIDManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef _DYNAMICIDMANAGER_H
#define _DYNAMICIDMANAGER_H

#include "IDAllocator.h"

class DynamicIDManager
{
public:

DynamicIDManager(IDAllocator *allocator)
: m_allocator (allocator)
{
m_current = m_idList.begin();
};

DynamicIDManager(IDAllocator *allocator, int initialStart, int quantity)
: m_allocator (allocator)
{
m_idList.push_back(std::pair<int, int>(initialStart, quantity));
m_current = m_idList.begin();
m_capacity = quantity;
m_nextID = initialStart + quantity;

};


void reserve(int quantity);

void reserveAdditional(int quantity);

int begin();

int currentID();

// Post-increment operator
DynamicIDManager& operator++(int);


int capacity() { return m_capacity; };

private:
// Methods
bool allocateIDs(int quantity, int *start);


IDAllocator* m_allocator;

// Data
typedef std::list< std::pair<int, int> > t_idList;
t_idList m_idList;
t_idList::iterator m_current;

int m_nextID;

int m_capacity;

HWND m_nppHandle;
};


#endif
12 changes: 12 additions & 0 deletions PythonScript/src/IDAllocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

#ifndef _IDALLOCATOR_H
#define _IDALLOCATOR_H

class IDAllocator
{
public:
virtual bool allocate(int quantity, int *start) = 0;
};

#endif

Loading

0 comments on commit 16d55c5

Please sign in to comment.