libcruft-util/pool.ipp

133 lines
3.7 KiB
Plaintext
Raw Normal View History

2012-08-22 16:12:26 +10:00
/*
2015-04-13 18:05:28 +10:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2012-08-22 16:12:26 +10:00
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
2012-08-22 16:12:26 +10:00
*
2015-04-13 18:05:28 +10:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2012-08-22 16:12:26 +10:00
*
2016-01-19 18:29:13 +11:00
* Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
2012-08-22 16:12:26 +10:00
*/
#ifdef __UTIL_POOL_IPP
2016-01-19 18:29:13 +11:00
#error
2012-08-22 16:12:26 +10:00
#endif
#define __UTIL_POOL_IPP
namespace util {
2016-01-19 18:29:13 +11:00
//-------------------------------------------------------------------------
2012-08-22 16:12:26 +10:00
template <typename T>
pool<T>::pool (unsigned int _capacity):
2016-01-19 18:29:13 +11:00
m_capacity (_capacity),
m_size (0u)
2012-08-22 16:12:26 +10:00
{
static_assert (sizeof (T) >= sizeof (uintptr_t),
"pool<T>'s chained block system requires that T be at least pointer sized");
2016-01-19 18:29:13 +11:00
// allocate the memory and note the base address for deletion in destructor
m_next = m_head = new node[m_capacity]; // static_cast<node *> (operator new (sizeof (T) * m_capacity));
2012-08-22 16:12:26 +10:00
2016-01-19 18:29:13 +11:00
// initialise the linked list of nodes
2012-08-22 16:12:26 +10:00
for (unsigned int i = 0; i < m_capacity - 1; ++i)
2016-01-19 18:29:13 +11:00
m_next[i]._node = m_next + i + 1;
m_next[m_capacity - 1]._node = nullptr;
2012-08-22 16:12:26 +10:00
}
2016-01-19 18:29:13 +11:00
//-------------------------------------------------------------------------
2012-08-22 16:12:26 +10:00
template <typename T>
2016-01-19 18:29:13 +11:00
pool<T>::~pool ()
{
// don't check if everything's been returned as pools are often used
// for PODs which don't need to be destructed via calling release.
delete [] m_head;
}
2012-08-22 16:12:26 +10:00
2016-01-19 18:29:13 +11:00
//-------------------------------------------------------------------------
template <typename T>
size_t
pool<T>::capacity (void) const
{
return m_capacity;
2012-08-22 16:12:26 +10:00
}
2014-05-20 13:44:27 +10:00
2012-08-22 16:12:26 +10:00
2016-01-19 18:29:13 +11:00
//-------------------------------------------------------------------------
2012-08-22 16:12:26 +10:00
template <typename T>
2014-12-31 19:07:25 +11:00
size_t
pool<T>::size (void) const
2016-01-19 18:29:13 +11:00
{
return m_size;
2016-01-19 18:29:13 +11:00
}
2012-08-22 16:12:26 +10:00
2016-01-19 18:29:13 +11:00
//-------------------------------------------------------------------------
template <typename T>
bool
pool<T>::empty (void) const
{
return m_size == m_capacity;
2016-01-19 18:29:13 +11:00
}
//-------------------------------------------------------------------------
2012-08-22 16:12:26 +10:00
template <typename T>
template <typename ...Args>
T*
2016-01-19 18:29:13 +11:00
pool<T>::acquire (Args&... args)
{
// double check we have enough capacity left
2012-08-22 16:12:26 +10:00
if (!m_next)
throw std::bad_alloc ();
CHECK_LT (m_size, m_capacity);
2012-08-22 16:12:26 +10:00
2016-01-19 18:29:13 +11:00
// save what will become the next node shortly. it could be overwritten
// in the constructor we're about to call.
node *newnext = m_next->_node;
T *data = reinterpret_cast<T*> (m_next->_data);
2014-05-20 13:44:27 +10:00
2016-01-19 18:29:13 +11:00
// try to construct the returnable object.
2012-08-22 16:12:26 +10:00
try {
new (data) T (args...);
} catch (...) {
2016-01-19 18:29:13 +11:00
// the constructor may have overwritten the node linkages before
// throwing. fix this up before forwarding the exception.
m_next->_node = newnext;
2012-08-22 16:12:26 +10:00
throw;
}
2016-01-19 18:29:13 +11:00
// the object is valid. save the new linked list head and bump the
// stats for availability.
2012-08-22 16:12:26 +10:00
m_next = newnext;
m_size++;
2016-01-19 18:29:13 +11:00
2012-08-22 16:12:26 +10:00
return data;
}
2016-01-19 18:29:13 +11:00
//-------------------------------------------------------------------------
2012-08-22 16:12:26 +10:00
template <typename T>
void
2016-01-19 18:29:13 +11:00
pool<T>::release (T *data)
{
CHECK_NEZ (m_size);
2016-01-19 18:29:13 +11:00
2012-08-22 16:12:26 +10:00
data->~T();
2014-05-20 13:44:49 +10:00
node *newnode = reinterpret_cast<node *> (data);
2012-08-22 16:12:26 +10:00
2016-01-19 18:29:13 +11:00
newnode->_node = m_next;
2012-08-22 16:12:26 +10:00
m_next = newnode;
m_size--;
2012-08-22 16:12:26 +10:00
}
}