bit::memory
stack_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains the definition of a stack allocator for
4  * temporary, automatic reclamation
5  *****************************************************************************/
6 
7 /*
8  The MIT License (MIT)
9 
10  Copyright (c) 2018 Matthew Rodusek
11 
12  Permission is hereby granted, free of charge, to any person obtaining a copy
13  of this software and associated documentation files (the "Software"), to deal
14  in the Software without restriction, including without limitation the rights
15  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the Software is
17  furnished to do so, subject to the following conditions:
18 
19  The above copyright notice and this permission notice shall be included in
20  all copies or substantial portions of the Software.
21 
22  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  SOFTWARE.
29 */
30 #ifndef BIT_MEMORY_ALLOCATORS_STACK_ALLOCATOR_HPP
31 #define BIT_MEMORY_ALLOCATORS_STACK_ALLOCATOR_HPP
32 
33 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
34 # pragma once
35 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
36 
37 #include "detail/named_allocator.hpp" // detail::named_allocator
38 
39 #include "../utilities/macros.hpp" // BIT_MEMORY_UNLIKELY
40 #include "../utilities/pointer_utilities.hpp" // offset_align_forward
41 
42 #include <cstddef> // std::size_t, std::max_align_t
43 #include <type_traits> // std::true_type, std::integral_constant
44 #include <cassert> // assert
45 
46 namespace bit {
47  namespace memory {
48 
65  template<std::size_t Size, std::size_t Align = alignof(std::max_align_t)>
67  {
68  //-----------------------------------------------------------------------
69  // Public Member Types
70  //-----------------------------------------------------------------------
71  public:
72 
73  using max_alignment = std::integral_constant<std::size_t,Align>;
74 
75  //-----------------------------------------------------------------------
76  // Constructors
77  //-----------------------------------------------------------------------
78  public:
79 
81  stack_allocator() noexcept;
82 
83  // Deleted move constructor.
84  stack_allocator( stack_allocator&& other ) = delete;
85 
86  // Deleted copy constructor
87  stack_allocator( const stack_allocator& other ) = delete;
88 
89  //----------------------------------------------------------------------
90 
93 
94  //----------------------------------------------------------------------
95 
96  // Deleted copy assignment
97  stack_allocator& operator=( const stack_allocator& ) = delete;
98 
99  // Deleted move assignment
100  stack_allocator& operator=( stack_allocator&& other ) = delete;
101 
102  //-----------------------------------------------------------------------
103  // Allocations / Deallocations
104  //-----------------------------------------------------------------------
105  public:
106 
114  void* try_allocate( std::size_t size,
115  std::size_t align,
116  std::size_t offset = 0 ) noexcept;
117 
122  void deallocate( void* p, std::size_t size );
123 
125  void deallocate_all();
126 
127  //-----------------------------------------------------------------------
128  // Observers
129  //-----------------------------------------------------------------------
130  public:
131 
136  bool owns( const void* p ) const noexcept;
137 
144  allocator_info info() const noexcept;
145 
146  //-----------------------------------------------------------------------
147  // Private Members
148  //-----------------------------------------------------------------------
149  private:
150 
151  alignas(Align) char m_storage[Size];
152  void* m_current;
153  };
154 
155  //-------------------------------------------------------------------------
156  // Utilities
157  //-------------------------------------------------------------------------
158 
159  template<std::size_t Size, std::size_t Align = alignof(std::max_align_t)>
160  using named_stack_allocator = detail::named_allocator<stack_allocator<Size,Align>>;
161 
162  } // namespace memory
163 } // namespace bit
164 
165 #include "detail/stack_allocator.inl"
166 
167 #endif /* BIT_MEMORY_ALLOCATORS_STACK_ALLOCATOR_HPP */
bool owns(const void *p) const noexcept
Checks whether linear_allocator contains the pointer p.
void * try_allocate(std::size_t size, std::size_t align, std::size_t offset=0) noexcept
Tries to allocate memory of size size, aligned to the boundary align, offset by offset.
stack_allocator() noexcept
Default-constructs a stack_allocator.
void deallocate_all()
Deallocates all memory in this allocator.
allocator_info info() const noexcept
Gets the info about this allocator.
~stack_allocator()
Deallocates all memory from the stack.
A stack-allocation strategy that allocates off of stack-memory.
void deallocate(void *p, std::size_t size)
Does nothing for linear_allocator. Use deallocate_all.
This type is used to hold the generic information for a given allocator.