1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
/*
// The MIT License (MIT)
//
// Copyright (c) 2015 Howard Hinnant
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
*/
#ifndef STACK_ALLOC_HPP
#define STACK_ALLOC_HPP
/*
* Based on Howard Hinnants awesome allocator boilerplate code
* https://howardhinnant.github.io/short_alloc.h
*/
#include <cstddef>
#include <cassert>
namespace jwt {
/*
*/
template <
/// Size of the stack allocated byte buffer.
size_t N,
/// The alignment required for the buffer.
size_t alignment = alignof(std::max_align_t)
>
class Arena
{
public: // 'tors
Arena() noexcept
: ptr_(buf_)
{
static_assert (alignment <= alignof(std::max_align_t),
"Alignment chosen is more than the maximum supported alignment");
}
/// Non copyable and assignable
Arena(const Arena&) = delete;
Arena& operator=(const Arena&) = delete;
~Arena()
{
ptr_ = nullptr;
}
public: // Public APIs
/*
* Reserves space within the buffer of size atleast 'n'
* bytes.
* More bytes maybe reserved based on the alignment requirements.
*
* Returns:
* 1. The pointer within the storage buffer where the object can be constructed.
* 2. nullptr if space cannot be reserved for requested number of bytes
* (+ alignment padding if applicable)
*/
template <
/// The requested alignment for this allocation.
/// Must be less than or equal to the 'alignment'.
size_t requested_alignment
>
char* allocate(size_t n) noexcept;
/*
* Free back the space pointed by p within the storage buffer.
*/
void deallocate(char* p, size_t n) noexcept;
/*
* The size of the internal storage buffer.
*/
constexpr static size_t size() noexcept
{
return N;
}
/*
* Returns number of remaining bytes within the storage buffer
* that can be used for further allocation requests.
*/
size_t used() const noexcept
{
return static_cast<size_t>(ptr_ - buf_);
}
private: // Private member functions
/*
* A check to determine if the pointer 'p'
* points to a region within storage.
*/
bool pointer_in_storage(char* p) const noexcept
{
return (buf_ <= p) && (p <= (buf_ + N));
}
/*
* Rounds up the number to the next closest number
* as per the alignment.
*/
constexpr static size_t align_up(size_t n) noexcept
{
return (n + (alignment - 1)) & ~(alignment - 1);
}
private: // data members
/// Storage
alignas(alignment) char buf_[N];
/// Current allocation pointer within storage
char* ptr_ = nullptr;
};
/*
*/
template <
/// The allocator for type T
typename T,
/// Number of bytes for the arena
size_t N,
/// Alignment of the arena
size_t align = alignof(std::max_align_t)
>
class stack_alloc
{
public: // typedefs
using value_type = T;
using arena_type = Arena<N, align>;
static auto constexpr alignment = align;
static auto constexpr size = N;
public: // 'tors
stack_alloc(arena_type& a)
: arena_(a)
{
}
stack_alloc(const stack_alloc&) = default;
stack_alloc& operator=(const stack_alloc&) = delete;
template <typename U>
stack_alloc(const stack_alloc<U, N, alignment>& other)
: arena_(other.arena_)
{
}
template <typename U>
struct rebind {
using other = stack_alloc<U, N, alignment>;
};
public: // Exposed APIs
/*
* Allocate memory of 'n' bytes for object
* of type 'T'
*/
T* allocate(size_t n) noexcept;
/*
* Deallocate the storage reserved for the object
* of type T pointed by pointer 'p'
*/
void deallocate(T* p, size_t n) noexcept;
private: // Private APIs
private: // Private data members
/// The arena
arena_type& arena_;
};
} // END namespace jwt
#include "jwt/impl/stack_alloc.ipp"
#endif
|