2018-03-02 12:18:20 +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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CRUFT_UTIL_ALLOC_RAW_ALIGNED_OFFSET_HPP
|
|
|
|
#define CRUFT_UTIL_ALLOC_RAW_ALIGNED_OFFSET_HPP
|
|
|
|
|
|
|
|
#include "direct.hpp"
|
|
|
|
|
|
|
|
#include "../../../pointer.hpp"
|
|
|
|
#include "../../../debug.hpp"
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace util::alloc::raw::aligned {
|
|
|
|
/// wraps a child allocator and enforces a fixed alignment that is
|
|
|
|
/// independant of the alignment of the provided source buffer.
|
|
|
|
///
|
|
|
|
/// we use the approach of:
|
|
|
|
/// * shifting the source buffer's alignment to satisfy the requested
|
|
|
|
/// alignment
|
|
|
|
/// * using a direct alignment child to service the requests
|
|
|
|
/// * then applying the reverse offset to values as we return the values
|
2018-03-02 12:45:52 +11:00
|
|
|
///
|
|
|
|
/// This approach will explode if a child allocator wants to write to the
|
|
|
|
/// range, so the user is mostly restricted to very simple allocators
|
2018-03-05 15:57:32 +11:00
|
|
|
/// (like 'linear').
|
|
|
|
///
|
|
|
|
/// We supply a proxy view at the (probably) invalid address:
|
|
|
|
/// (void*)alignment
|
|
|
|
/// This removes any potential bias in the successor allocators. We very
|
|
|
|
/// specifically cannot use nullptr because it breaks under optimisations
|
|
|
|
/// due to undefined behaviour of null pointers.
|
|
|
|
///
|
|
|
|
/// The proxy view will _probably_ result in a segfault if the successor
|
|
|
|
/// allocator attempts to read/write directly to it given typical
|
|
|
|
/// alignments will give addresses far below typical mappings.
|
2018-03-02 12:18:20 +11:00
|
|
|
template <typename ChildT>
|
|
|
|
class foreign {
|
|
|
|
public:
|
|
|
|
template <typename ...Args>
|
|
|
|
foreign (util::view<std::byte*> _data, std::size_t _alignment, Args &&...args):
|
|
|
|
m_successor (
|
2018-03-05 15:57:32 +11:00
|
|
|
view<std::byte*> {
|
|
|
|
reinterpret_cast<std::byte*> (_alignment),
|
|
|
|
reinterpret_cast<std::byte*> (_alignment + _data.size ()),
|
|
|
|
},
|
2018-03-02 12:18:20 +11:00
|
|
|
_alignment,
|
|
|
|
std::forward<Args> (args)...
|
|
|
|
),
|
2018-03-05 15:57:32 +11:00
|
|
|
m_offset (_data.data () - m_successor.data ()),
|
2018-03-02 12:18:20 +11:00
|
|
|
m_alignment (_alignment)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
void*
|
|
|
|
allocate (std::size_t size)
|
|
|
|
{
|
|
|
|
auto ptr= reinterpret_cast<std::byte*> (
|
2018-05-10 12:50:37 +10:00
|
|
|
m_successor.allocate (size).data ()
|
2018-03-02 12:18:20 +11:00
|
|
|
);
|
2018-03-05 15:57:32 +11:00
|
|
|
return ptr + m_offset;
|
2018-03-02 12:18:20 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
auto
|
|
|
|
deallocate (void *ptr, std::size_t size)
|
|
|
|
{
|
|
|
|
return m_successor.deallocate (
|
2018-03-05 15:57:32 +11:00
|
|
|
reinterpret_cast<std::byte*> (ptr) - m_offset, size
|
2018-03-02 12:18:20 +11:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto alignment (void) const noexcept { return m_alignment; }
|
|
|
|
|
|
|
|
auto offset (const void *ptr) const
|
|
|
|
{
|
2018-03-02 12:45:27 +11:00
|
|
|
return m_successor.offset (
|
2018-03-05 15:57:32 +11:00
|
|
|
reinterpret_cast<const std::byte*> (ptr) - m_offset
|
2018-03-02 12:45:27 +11:00
|
|
|
);
|
2018-03-02 12:18:20 +11:00
|
|
|
}
|
|
|
|
|
2018-03-05 15:57:32 +11:00
|
|
|
auto data (void) { return m_successor.data () + m_offset; }
|
|
|
|
auto data (void) const { return m_successor.data () + m_offset; }
|
2018-03-02 12:18:20 +11:00
|
|
|
|
2018-03-05 15:57:32 +11:00
|
|
|
auto begin (void) { return m_successor.begin () + m_offset; }
|
|
|
|
auto begin (void) const { return m_successor.begin () + m_offset; }
|
2018-03-02 12:18:20 +11:00
|
|
|
|
2018-03-05 15:57:32 +11:00
|
|
|
auto end (void) { return m_successor.end () + m_offset; }
|
|
|
|
auto end (void) const { return m_successor.end () + m_offset; }
|
2018-03-02 12:18:20 +11:00
|
|
|
|
|
|
|
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:
|
|
|
|
direct<ChildT> m_successor;
|
|
|
|
std::ptrdiff_t m_offset;
|
|
|
|
std::size_t m_alignment;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|