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