2016-10-10 18:18:29 +11: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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2017-08-29 16:53:11 +10:00
|
|
|
#ifndef CRUFT_UTIL_ALLOC_RAW_ALIGNED_HPP
|
|
|
|
#define CRUFT_UTIL_ALLOC_RAW_ALIGNED_HPP
|
2016-10-10 18:18:29 +11:00
|
|
|
|
2017-09-21 15:06:35 +10:00
|
|
|
#include <cstddef>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "../../debug.hpp"
|
|
|
|
|
2017-08-29 16:53:11 +10:00
|
|
|
namespace util::alloc::raw {
|
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>
|
2016-10-10 18:18:29 +11:00
|
|
|
class aligned {
|
|
|
|
public:
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename ...Args>
|
2017-09-21 15:06:35 +10:00
|
|
|
aligned (std::size_t _alignment, Args &&...args):
|
2016-10-10 18:18:29 +11:00
|
|
|
m_successor (std::forward<Args> (args)...),
|
|
|
|
m_alignment (_alignment)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
auto
|
2017-09-21 15:06:35 +10:00
|
|
|
allocate (std::size_t bytes)
|
2016-10-10 18:18:29 +11:00
|
|
|
{
|
|
|
|
return m_successor.allocate (bytes, m_alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2016-10-10 20:59:26 +11:00
|
|
|
auto
|
2017-09-21 15:06:35 +10:00
|
|
|
allocate (std::size_t bytes, std::size_t alignment)
|
2016-10-10 20:59:26 +11:00
|
|
|
{
|
2016-10-11 21:43:07 +11:00
|
|
|
(void)alignment;
|
2016-10-10 20:59:26 +11:00
|
|
|
CHECK_EQ (alignment, m_alignment);
|
|
|
|
return m_successor.allocate (bytes, m_alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
auto
|
2017-09-21 15:06:35 +10:00
|
|
|
deallocate (void *ptr, std::size_t bytes)
|
2016-10-10 18:18:29 +11:00
|
|
|
{
|
|
|
|
return m_successor.deallocate (ptr, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-10 20:59:26 +11:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
auto
|
2017-09-21 15:06:35 +10:00
|
|
|
deallocate (void *ptr, std::size_t bytes, std::size_t alignment)
|
2016-10-10 18:18:29 +11:00
|
|
|
{
|
2016-10-11 21:43:07 +11:00
|
|
|
(void)alignment;
|
2016-10-10 20:59:26 +11:00
|
|
|
CHECK_EQ (alignment, m_alignment);
|
|
|
|
return m_successor.deallocate (ptr, bytes, m_alignment);
|
2016-10-10 18:18:29 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-10 20:59:26 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
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
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|