Debian Codegraph @

include/jwt/impl/stack_alloc.ipp

raw
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_IPP
26 #define STACK_ALLOC_IPP
27
28 namespace jwt {
29
30 template <size_t N, size_t alignment>
31 template <size_t reqested_alignment>
32 char* Arena<N, alignment>::allocate(size_t n) noexcept
33 {
34 static_assert (reqested_alignment <= alignment,
35 "Requested alignment is too small for this arena");
36
37 assert (pointer_in_storage(ptr_) &&
38 "No more space in the arena or it has outgrown its capacity");
39
40 n = align_up(n);
41
42 if ((ptr_ + n) <= (buf_ + N)) {
43 char* ret = ptr_;
44 ptr_ += n;
45
46 return ret;
47 }
48
49 assert (0 && "Code should not reach here");
50
51 return nullptr;
52 }
53
54 template <size_t N, size_t alignment>
55 void Arena<N, alignment>::deallocate(char* p, size_t n) noexcept
56 {
57 assert (pointer_in_storage(p) &&
58 "The address to de deleted does not lie inside the storage");
59
60 n = align_up(n);
61
62 if ((p + n) == ptr_) {
63 ptr_ = p;
64 }
65
66 return;
67 }
68
69 template <typename T, size_t N, size_t alignment>
70 T* stack_alloc<T, N, alignment>::allocate(size_t n) noexcept
71 {
72 return reinterpret_cast<T*>(
73 arena_.template allocate<alignof(T)>(n * sizeof(T))
74 );
75 }
76
77 template <typename T, size_t N, size_t alignment>
78 void stack_alloc<T, N, alignment>::deallocate(T* p, size_t n) noexcept
79 {
80 arena_.deallocate(reinterpret_cast<char*>(p), n);
81 return;
82 }
83
84 }
85
86
87 #endif