Merge branch 'master' of /home/guilhem/Dropbox/gitRoot/CosmoToolbox

This commit is contained in:
Guilhem Lavaux 2012-05-20 09:22:54 -04:00
commit d4a14d7d85

View File

@ -15,6 +15,66 @@ namespace CosmoTool
PoolNode<T> *next; 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 // This is bare simple memory pools
template<typename T> template<typename T>
class MemoryPool class MemoryPool
@ -22,6 +82,7 @@ namespace CosmoTool
private: private:
uint32_t m_allocSize; uint32_t m_allocSize;
PoolNode<T> *head, *current; PoolNode<T> *head, *current;
typedef typename MemoryIterator<T> iterator;
public: public:
MemoryPool(uint32_t allocSize) MemoryPool(uint32_t allocSize)
: m_allocSize(allocSize), head(0), current(0) {} : m_allocSize(allocSize), head(0), current(0) {}
@ -52,6 +113,16 @@ namespace CosmoTool
return (ret == 0) ? alloc_new_in_node() : ret; return (ret == 0) ? alloc_new_in_node() : ret;
} }
iterator begin()
{
return iterator(head);
}
iterator end()
{
return iterator(0);
}
protected: protected:
T *alloc_in_node() T *alloc_in_node()
{ {