2015-11-13 17:17:37 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2015-11-13 17:17:37 +11:00
|
|
|
*
|
2018-05-10 12:44:03 +10:00
|
|
|
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
|
2015-11-13 17:17:37 +11:00
|
|
|
*/
|
|
|
|
|
2018-05-10 12:44:03 +10:00
|
|
|
#ifndef CRUFT_UTIL_ALLOC_ARENA_HPP
|
|
|
|
#define CRUFT_UTIL_ALLOC_ARENA_HPP
|
2015-11-13 17:17:37 +11:00
|
|
|
|
2015-11-24 16:52:14 +11:00
|
|
|
#include "../memory/deleter.hpp"
|
2018-05-10 12:44:03 +10:00
|
|
|
#include "../cast.hpp"
|
2018-05-10 13:53:06 +10:00
|
|
|
#include "../view.hpp"
|
2015-11-24 16:52:14 +11:00
|
|
|
|
2016-02-25 13:17:14 +11:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
2015-11-13 17:17:37 +11:00
|
|
|
|
2016-10-10 17:58:59 +11:00
|
|
|
namespace util::alloc {
|
2017-08-29 12:28:59 +10:00
|
|
|
/// wraps a block allocator with an interface suitable for allocating
|
|
|
|
/// individual objects.
|
2015-11-13 17:17:37 +11:00
|
|
|
template <class T>
|
|
|
|
class arena {
|
|
|
|
public:
|
2018-02-28 11:49:13 +11:00
|
|
|
explicit arena (T &store):
|
|
|
|
m_store (store)
|
|
|
|
{ ; }
|
2015-11-13 17:17:37 +11:00
|
|
|
|
2015-11-24 16:52:14 +11:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
template <typename U, typename ...Args>
|
|
|
|
U*
|
2018-02-28 11:49:13 +11:00
|
|
|
acquire (Args&&... args)
|
|
|
|
{
|
2018-05-10 13:53:06 +10:00
|
|
|
U *data = m_store.template allocate<U> (1).data ();
|
2018-02-28 11:49:13 +11:00
|
|
|
|
|
|
|
try {
|
|
|
|
new (data) U (std::forward<Args> (args)...);
|
|
|
|
} catch (...) {
|
2018-05-10 13:53:06 +10:00
|
|
|
m_store.template deallocate<U> ({data,1});
|
2018-02-28 11:49:13 +11:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2015-11-24 16:52:14 +11:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2015-11-13 17:17:37 +11:00
|
|
|
template <typename U>
|
2015-11-24 16:52:14 +11:00
|
|
|
void
|
2018-02-28 11:49:13 +11:00
|
|
|
release (U *u)
|
|
|
|
{
|
|
|
|
u->~U ();
|
2018-05-10 13:53:06 +10:00
|
|
|
m_store.template deallocate<U> (util::view {u,1u});
|
2018-02-28 11:49:13 +11:00
|
|
|
}
|
|
|
|
|
2015-11-13 17:17:37 +11:00
|
|
|
|
2015-11-24 16:52:14 +11:00
|
|
|
//---------------------------------------------------------------------
|
2015-11-13 17:17:37 +11:00
|
|
|
template <typename U>
|
2015-11-24 16:52:14 +11:00
|
|
|
using deleter_t = util::memory::owner_deleter<
|
|
|
|
U,arena<T>,&arena::release
|
|
|
|
>;
|
2015-11-13 17:17:37 +11:00
|
|
|
|
|
|
|
template <typename U>
|
2015-11-24 16:52:14 +11:00
|
|
|
using unique_t = std::unique_ptr<U,deleter_t<U>>;
|
|
|
|
|
|
|
|
// the return type must be auto and the implementation must be inline
|
|
|
|
// otherwise we trigger an internal compiler error in gcc-5.2.0
|
|
|
|
// "sorry, unimplemented: mangling offset_ref"
|
|
|
|
template <typename U, typename ...Args>
|
|
|
|
auto
|
|
|
|
unique (Args&& ...args)
|
|
|
|
{
|
|
|
|
return unique_t<U> {
|
|
|
|
acquire<U> (std::forward<Args> (args)...),
|
|
|
|
deleter_t<U> (*this)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-11-13 17:17:37 +11:00
|
|
|
|
|
|
|
private:
|
|
|
|
T &m_store;
|
|
|
|
};
|
2016-10-10 17:58:59 +11:00
|
|
|
}
|
2015-11-13 17:17:37 +11:00
|
|
|
|
|
|
|
#endif
|