Added an incomplete implementation of a memory iterator
This commit is contained in:
parent
e8d1edf453
commit
f56cbf7cc5
71
src/pool.hpp
71
src/pool.hpp
@ -15,6 +15,66 @@ namespace CosmoTool
|
||||
PoolNode<T> *next;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class MemoryIterator
|
||||
{
|
||||
private:
|
||||
friend class MemoryPool<T>;
|
||||
|
||||
PoolNode<T> *cur, *previous;
|
||||
uint32_t in_node;
|
||||
|
||||
MemoryIterator(PoolNode<T> *h)
|
||||
{
|
||||
cur = h;
|
||||
previous = h;
|
||||
in_node = 0;
|
||||
}
|
||||
public:
|
||||
MemoryIterator() { cur = 0; }
|
||||
~MemoryIterator() {}
|
||||
|
||||
const MemoryIterator& operator=(const MemoryIterator& i)
|
||||
{
|
||||
cur = i.cur;
|
||||
previous = i.previous;
|
||||
in_node = i.in_node;
|
||||
}
|
||||
|
||||
bool MemoryIterator::operator==(const MemoryIterator& i) const
|
||||
{
|
||||
return (cur == i.cur) && (in_node == i.in_node);
|
||||
}
|
||||
|
||||
MemoryIterator& operator++()
|
||||
{
|
||||
if (cur == 0)
|
||||
return *this;
|
||||
|
||||
in_node++;
|
||||
if (in_node == cur->size)
|
||||
{
|
||||
in_node = 0;
|
||||
previous = cur;
|
||||
cur = cur->next;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
T& operator*()
|
||||
{
|
||||
return cur->data[in_node];
|
||||
}
|
||||
|
||||
T& operator->()
|
||||
{
|
||||
return cur->data[in_node];
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
// This is bare simple memory pools
|
||||
template<typename T>
|
||||
class MemoryPool
|
||||
@ -22,6 +82,7 @@ namespace CosmoTool
|
||||
private:
|
||||
uint32_t m_allocSize;
|
||||
PoolNode<T> *head, *current;
|
||||
typedef typename MemoryIterator<T> iterator;
|
||||
public:
|
||||
MemoryPool(uint32_t allocSize)
|
||||
: m_allocSize(allocSize), head(0), current(0) {}
|
||||
@ -52,6 +113,16 @@ namespace CosmoTool
|
||||
return (ret == 0) ? alloc_new_in_node() : ret;
|
||||
}
|
||||
|
||||
iterator begin()
|
||||
{
|
||||
return iterator(head);
|
||||
}
|
||||
|
||||
iterator end()
|
||||
{
|
||||
return iterator(0);
|
||||
}
|
||||
|
||||
protected:
|
||||
T *alloc_in_node()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user