Move pool implementation to ipp file
This commit is contained in:
parent
0921037c89
commit
b8ef0afc1d
@ -90,6 +90,7 @@ UTIL_FILES = \
|
|||||||
point.hpp \
|
point.hpp \
|
||||||
pool.cpp \
|
pool.cpp \
|
||||||
pool.hpp \
|
pool.hpp \
|
||||||
|
pool.ipp \
|
||||||
preprocessor.hpp \
|
preprocessor.hpp \
|
||||||
quaternion.cpp \
|
quaternion.cpp \
|
||||||
quaternion.hpp \
|
quaternion.hpp \
|
||||||
|
65
pool.hpp
65
pool.hpp
@ -14,7 +14,7 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
* Copyright 2011-2012 Danny Robson <danny@nerdcruft.net>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __UTIL_POOL_HPP
|
#ifndef __UTIL_POOL_HPP
|
||||||
@ -23,7 +23,7 @@
|
|||||||
#include "nocopy.hpp"
|
#include "nocopy.hpp"
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class pool : public nocopy {
|
class pool : public nocopy {
|
||||||
protected:
|
protected:
|
||||||
union node {
|
union node {
|
||||||
@ -36,65 +36,18 @@ template <typename T>
|
|||||||
unsigned int m_capacity;
|
unsigned int m_capacity;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
pool (unsigned int _capacity):
|
pool (unsigned int _capacity);
|
||||||
m_capacity (_capacity)
|
~pool ();
|
||||||
{
|
|
||||||
static_assert (sizeof (T) >= sizeof (uintptr_t),
|
|
||||||
"pool<T>'s chained block system requires that T be at least pointer sized");
|
|
||||||
|
|
||||||
m_head = (node *)operator new (sizeof (T) * m_capacity);
|
|
||||||
m_next = m_head;
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < m_capacity - 1; ++i)
|
|
||||||
m_next[i]._chain = &m_next[i + 1];
|
|
||||||
m_next[m_capacity - 1]._chain = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
~pool () {
|
|
||||||
CHECK (m_next != NULL);
|
|
||||||
|
|
||||||
unsigned int doomed_count = 0;
|
|
||||||
for (node *cursor = m_next; cursor != NULL; cursor = cursor->_chain)
|
|
||||||
++doomed_count;
|
|
||||||
|
|
||||||
CHECK_EQ (doomed_count, m_capacity);
|
|
||||||
operator delete (m_head);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
unsigned int capacity (void) const
|
|
||||||
{ return m_capacity; }
|
|
||||||
|
|
||||||
|
unsigned int capacity (void) const;
|
||||||
|
|
||||||
template <typename ...Args>
|
template <typename ...Args>
|
||||||
T* acquire (Args&... args) {
|
T* acquire (Args&... args);
|
||||||
if (!m_next)
|
|
||||||
throw std::bad_alloc ();
|
|
||||||
|
|
||||||
node *newnext = m_next->_chain;
|
void release (T *data);
|
||||||
T *data = (T*)&m_next->_data;
|
|
||||||
|
|
||||||
try {
|
|
||||||
new (data) T (args...);
|
|
||||||
} catch (...) {
|
|
||||||
m_next->_chain = newnext;
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_next = newnext;
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void release (T *data) {
|
|
||||||
data->~T();
|
|
||||||
node *newnode = (node *)data;
|
|
||||||
|
|
||||||
newnode->_chain = m_next;
|
|
||||||
m_next = newnode;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "pool.ipp"
|
||||||
|
|
||||||
#endif // __UTIL_POOL_HPP
|
#endif // __UTIL_POOL_HPP
|
||||||
|
96
pool.ipp
Normal file
96
pool.ipp
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of libgim.
|
||||||
|
*
|
||||||
|
* libgim is free software: you can redistribute it and/or modify it under the
|
||||||
|
* terms of the GNU General Public License as published by the Free Software
|
||||||
|
* Foundation, either version 3 of the License, or (at your option) any later
|
||||||
|
* version.
|
||||||
|
*
|
||||||
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
* details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Copyright 2011-2012 Danny Robson <danny@nerdcruft.net>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef __UTIL_POOL_IPP
|
||||||
|
#error Double inclusion of pool.ipp
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define __UTIL_POOL_IPP
|
||||||
|
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
template <typename T>
|
||||||
|
pool<T>::pool (unsigned int _capacity):
|
||||||
|
m_capacity (_capacity)
|
||||||
|
{
|
||||||
|
static_assert (sizeof (T) >= sizeof (uintptr_t),
|
||||||
|
"pool<T>'s chained block system requires that T be at least pointer sized");
|
||||||
|
|
||||||
|
m_head = (node *)operator new (sizeof (T) * m_capacity);
|
||||||
|
m_next = m_head;
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < m_capacity - 1; ++i)
|
||||||
|
m_next[i]._chain = &m_next[i + 1];
|
||||||
|
m_next[m_capacity - 1]._chain = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
pool<T>::~pool () {
|
||||||
|
CHECK (m_next != NULL);
|
||||||
|
|
||||||
|
unsigned int doomed_count = 0;
|
||||||
|
for (node *cursor = m_next; cursor != NULL; cursor = cursor->_chain)
|
||||||
|
++doomed_count;
|
||||||
|
|
||||||
|
CHECK_EQ (doomed_count, m_capacity);
|
||||||
|
operator delete (m_head);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
unsigned int
|
||||||
|
pool<T>::capacity (void) const
|
||||||
|
{ return m_capacity; }
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
template <typename ...Args>
|
||||||
|
T*
|
||||||
|
pool<T>::acquire (Args&... args) {
|
||||||
|
if (!m_next)
|
||||||
|
throw std::bad_alloc ();
|
||||||
|
|
||||||
|
node *newnext = m_next->_chain;
|
||||||
|
T *data = (T*)&m_next->_data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
new (data) T (args...);
|
||||||
|
} catch (...) {
|
||||||
|
m_next->_chain = newnext;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_next = newnext;
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void
|
||||||
|
pool<T>::release (T *data) {
|
||||||
|
data->~T();
|
||||||
|
node *newnode = (node *)data;
|
||||||
|
|
||||||
|
newnode->_chain = m_next;
|
||||||
|
m_next = newnode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user