2011-07-01 13:46:39 +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
|
2011-07-01 13:46:39 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-01 13:46:39 +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.
|
2011-07-01 13:46:39 +10:00
|
|
|
*
|
2012-08-22 16:12:26 +10:00
|
|
|
* Copyright 2011-2012 Danny Robson <danny@nerdcruft.net>
|
2011-07-01 13:46:39 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_POOL_HPP
|
|
|
|
#define __UTIL_POOL_HPP
|
|
|
|
|
|
|
|
#include "nocopy.hpp"
|
|
|
|
|
2014-12-31 19:07:25 +11:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2011-10-07 21:58:16 +11:00
|
|
|
namespace util {
|
2012-08-22 16:12:26 +10:00
|
|
|
template <typename T>
|
2011-10-07 21:58:16 +11:00
|
|
|
class pool : public nocopy {
|
|
|
|
protected:
|
|
|
|
union node {
|
|
|
|
char _data[sizeof (T)];
|
|
|
|
node *_chain;
|
|
|
|
};
|
|
|
|
|
2014-12-31 19:07:25 +11:00
|
|
|
node *m_head;
|
|
|
|
node *m_next;
|
|
|
|
size_t m_capacity;
|
2011-10-07 21:58:16 +11:00
|
|
|
|
|
|
|
public:
|
2012-08-22 16:12:26 +10:00
|
|
|
pool (unsigned int _capacity);
|
|
|
|
~pool ();
|
2011-07-01 13:46:39 +10:00
|
|
|
|
2014-12-31 19:07:25 +11:00
|
|
|
// Data management
|
2011-10-07 21:58:16 +11:00
|
|
|
template <typename ...Args>
|
2012-08-22 16:12:26 +10:00
|
|
|
T* acquire (Args&... args);
|
2011-07-01 13:46:39 +10:00
|
|
|
|
2012-08-22 16:12:26 +10:00
|
|
|
void release (T *data);
|
2014-12-31 19:07:25 +11:00
|
|
|
|
|
|
|
size_t capacity (void) const;
|
|
|
|
|
|
|
|
// Indexing
|
|
|
|
size_t index (const T*) const;
|
|
|
|
|
|
|
|
T& operator[] (size_t idx);
|
|
|
|
const T& operator[] (size_t idx) const;
|
2011-10-07 21:58:16 +11:00
|
|
|
};
|
|
|
|
}
|
2011-07-01 13:46:39 +10:00
|
|
|
|
2012-08-22 16:12:26 +10:00
|
|
|
#include "pool.ipp"
|
|
|
|
|
2011-07-01 13:46:39 +10:00
|
|
|
#endif // __UTIL_POOL_HPP
|