libcruft-util/alloc/raw/aligned/direct.hpp
Danny Robson c2265b9ed2 alloc: add aligned::foreign allocator
sometimes we need to ensure memory allocation has a particular alignment
in an _offset_ buffer (which we have no control over, eg renderdoc's
OpenGL buffers).

this applies an offset to various operations that make the
aligned::direct allocator correctly align allocations for buffers that
aren't themselves aligned.
2018-03-02 12:21:38 +11:00

92 lines
3.0 KiB
C++

/*
* 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_DIRECT_HPP
#define CRUFT_UTIL_ALLOC_RAW_ALIGNED_DIRECT_HPP
#include "../../../debug.hpp"
#include <cstddef>
#include <utility>
namespace util::alloc::raw::aligned {
/// wraps a child allocator and enforces a fixed alignment
template <typename ChildT>
class direct {
public:
///////////////////////////////////////////////////////////////////////
template <typename ...Args>
direct (util::view<std::byte*> _data, std::size_t _alignment, Args &&...args):
m_successor (_data, std::forward<Args> (args)...),
m_alignment (_alignment)
{ ; }
///////////////////////////////////////////////////////////////////////
auto
allocate (std::size_t bytes)
{
return m_successor.allocate (bytes, m_alignment);
}
//---------------------------------------------------------------------
auto
deallocate (void *ptr, std::size_t bytes)
{
return m_successor.deallocate (ptr, bytes);
}
///////////////////////////////////////////////////////////////////////
constexpr auto alignment (void) const noexcept { return m_alignment; }
///////////////////////////////////////////////////////////////////////
auto data (void) { return m_successor.data (); }
//---------------------------------------------------------------------
auto begin (void) { return m_successor.begin (); }
auto begin (void) const { return m_successor.begin (); }
auto end (void) { return m_successor.end (); }
auto end (void) const { return m_successor.end (); }
//---------------------------------------------------------------------
auto
offset (const void *ptr) const
{
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:
ChildT m_successor;
std::size_t m_alignment;
};
}
#endif