2016-10-10 18:18:29 +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/.
|
2016-10-10 18:18:29 +11:00
|
|
|
*
|
2018-12-19 17:16:57 +11:00
|
|
|
* Copyright 2016-2018 Danny Robson <danny@nerdcruft.net>
|
2016-10-10 18:18:29 +11:00
|
|
|
*/
|
|
|
|
|
2018-12-19 17:16:57 +11:00
|
|
|
#pragma once
|
2018-03-02 12:18:20 +11:00
|
|
|
|
2018-12-19 17:55:24 +11:00
|
|
|
#include "../../std.hpp"
|
|
|
|
#include "../../view.hpp"
|
2016-10-10 18:18:29 +11:00
|
|
|
|
2017-09-21 15:06:35 +10:00
|
|
|
#include <utility>
|
|
|
|
|
2018-12-19 17:16:57 +11:00
|
|
|
|
2018-12-19 17:55:24 +11:00
|
|
|
namespace cruft::alloc::aligned {
|
2016-10-10 18:18:29 +11:00
|
|
|
/// wraps a child allocator and enforces a fixed alignment
|
2016-10-10 19:26:40 +11:00
|
|
|
template <typename ChildT>
|
2018-03-02 12:18:20 +11:00
|
|
|
class direct {
|
2016-10-10 18:18:29 +11:00
|
|
|
public:
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename ...Args>
|
2018-12-19 17:16:57 +11:00
|
|
|
direct (
|
|
|
|
cruft::view<u08*> _data,
|
|
|
|
std::size_t _alignment,
|
|
|
|
Args &&...args
|
|
|
|
)
|
|
|
|
: m_successor (_data, std::forward<Args> (args)...)
|
|
|
|
, m_alignment (_alignment)
|
2016-10-10 18:18:29 +11:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2018-12-19 17:16:57 +11:00
|
|
|
auto allocate (std::size_t bytes)
|
2016-10-10 18:18:29 +11:00
|
|
|
{
|
2018-12-19 17:16:57 +11:00
|
|
|
return m_successor.allocate (bytes, m_alignment);
|
2016-10-10 18:18:29 +11:00
|
|
|
}
|
|
|
|
|
2018-12-19 17:16:57 +11:00
|
|
|
|
2016-10-10 20:59:26 +11:00
|
|
|
//---------------------------------------------------------------------
|
2018-12-19 17:16:57 +11:00
|
|
|
auto allocate (std::size_t bytes, std::size_t alignment)
|
2016-10-10 18:18:29 +11:00
|
|
|
{
|
2018-12-19 17:16:57 +11:00
|
|
|
(void)alignment;
|
|
|
|
return m_successor.allocate (bytes, m_alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
auto deallocate (u08 *ptr, std::size_t bytes, std::size_t alignment)
|
|
|
|
{
|
|
|
|
(void)alignment;
|
|
|
|
return m_successor.deallocate (ptr, bytes, m_alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
auto deallocate (u08 *ptr, std::size_t bytes)
|
|
|
|
{
|
|
|
|
return m_successor.deallocate (ptr, bytes, m_alignment);
|
2016-10-10 18:18:29 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-01 14:42:32 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
constexpr auto alignment (void) const noexcept { return m_alignment; }
|
|
|
|
|
|
|
|
|
2016-10-10 20:59:26 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
2018-02-28 16:19:27 +11:00
|
|
|
auto data (void) { return m_successor.data (); }
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2017-08-31 13:48:33 +10:00
|
|
|
auto begin (void) { return m_successor.begin (); }
|
|
|
|
auto begin (void) const { return m_successor.begin (); }
|
2016-10-10 20:59:26 +11:00
|
|
|
|
2018-02-28 17:55:56 +11:00
|
|
|
auto end (void) { return m_successor.end (); }
|
|
|
|
auto end (void) const { return m_successor.end (); }
|
2016-10-10 20:59:26 +11:00
|
|
|
|
2016-10-10 18:18:29 +11:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
auto
|
2016-10-10 20:59:26 +11:00
|
|
|
offset (const void *ptr) const
|
2016-10-10 18:18:29 +11:00
|
|
|
{
|
|
|
|
return m_successor.offset (ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
auto reset (void) { return m_successor.reset (); }
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
auto capacity (void) const { return m_successor.capacity (); }
|
|
|
|
auto used (void) const { return m_successor.used (); }
|
|
|
|
auto remain (void) const { return m_successor.remain (); }
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2016-10-10 19:26:40 +11:00
|
|
|
ChildT m_successor;
|
2017-09-21 15:06:35 +10:00
|
|
|
std::size_t m_alignment;
|
2016-10-10 18:18:29 +11:00
|
|
|
};
|
|
|
|
}
|