-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 :)
- Loading branch information
1 parent
09840f0
commit 16d55c5
Showing
12 changed files
with
340 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Oops, something went wrong.