bit::memory
bump_down_lifo_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains the definition of a bump-down allocator that
4  * allows for stack-based LIFO deallocations alongside truncated
5  * deallocations
6  *****************************************************************************/
7 
8 /*
9  The MIT License (MIT)
10 
11  Copyright (c) 2018 Matthew Rodusek
12 
13  Permission is hereby granted, free of charge, to any person obtaining a copy
14  of this software and associated documentation files (the "Software"), to deal
15  in the Software without restriction, including without limitation the rights
16  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17  copies of the Software, and to permit persons to whom the Software is
18  furnished to do so, subject to the following conditions:
19 
20  The above copyright notice and this permission notice shall be included in
21  all copies or substantial portions of the Software.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29  SOFTWARE.
30 */
31 #ifndef BIT_MEMORY_ALLOCATORS_BUMP_DOWN_LIFO_ALLOCATOR_HPP
32 #define BIT_MEMORY_ALLOCATORS_BUMP_DOWN_LIFO_ALLOCATOR_HPP
33 
34 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
35 # pragma once
36 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
37 
38 #include "detail/named_allocator.hpp" // detail::named_allocator
39 
40 #include "../utilities/macros.hpp" // BIT_MEMORY_UNLIKELY
41 #include "../utilities/memory_block.hpp" // memory_block
42 #include "../utilities/owner.hpp" // owner
43 #include "../utilities/pointer_utilities.hpp" // offset_align_backward
44 
45 #include <cassert> // assert
46 #include <cstddef> // std::size_t, std::ptrdiff_t
47 #include <type_traits> // std:integral_constant, std::true_type, etc
48 
49 namespace bit {
50  namespace memory {
51 
74  {
75  //-----------------------------------------------------------------------
76  // Public Member Types
77  //-----------------------------------------------------------------------
78  public:
79 
80  // limited to 256 byte alignment due to requiring an adjustment byte
81  using max_alignment = std::integral_constant<std::size_t,256>;
82 
83  //-----------------------------------------------------------------------
84  // Constructors
85  //-----------------------------------------------------------------------
86  public:
87 
89  explicit bump_down_lifo_allocator( memory_block block ) noexcept;
90 
94  bump_down_lifo_allocator( bump_down_lifo_allocator&& other ) noexcept = default;
95 
96  // Deleted copy constructor
97  bump_down_lifo_allocator( const bump_down_lifo_allocator& other ) = delete;
98 
99  // Deleted nullblock constructor
100  bump_down_lifo_allocator( nullblock_t ) = delete;
101 
102  //----------------------------------------------------------------------
103 
104  // Deleted copy assignment
105  bump_down_lifo_allocator& operator=( const bump_down_lifo_allocator& ) = delete;
106 
107  // Deleted move assignment
108  bump_down_lifo_allocator& operator=( bump_down_lifo_allocator&& other ) = delete;
109 
110  //-----------------------------------------------------------------------
111  // Allocations / Deallocations
112  //-----------------------------------------------------------------------
113  public:
114 
122  owner<void*> try_allocate( std::size_t size,
123  std::size_t align,
124  std::size_t offset = 0 ) noexcept;
125 
130  void deallocate( owner<void*> p, std::size_t size );
131 
133  void deallocate_all() noexcept;
134 
135  //-----------------------------------------------------------------------
136  // Observers
137  //-----------------------------------------------------------------------
138  public:
139 
144  bool owns( const void* p ) const noexcept;
145 
152  allocator_info info() const noexcept;
153 
154  //-----------------------------------------------------------------------
155  // Private Members
156  //-----------------------------------------------------------------------
157  private:
158 
159  memory_block m_block;
160  void* m_current;
161  };
162 
163  //-------------------------------------------------------------------------
164  // Utilities
165  //-------------------------------------------------------------------------
166 
167  using named_bump_down_lifo_allocator = detail::named_allocator<bump_down_lifo_allocator>;
168 
169  } // namespace memory
170 } // namespace bit
171 
172 #include "detail/bump_down_lifo_allocator.inl"
173 
174 #endif /* BIT_MEMORY_ALLOCATORS_BUMP_DOWN_LIFO_ALLOCATOR_HPP */
allocator_info info() const noexcept
Gets the info about this allocator.
bump_down_lifo_allocator(memory_block block) noexcept
Constructs a linear_allocator.
bool owns(const void *p) const noexcept
Checks whether linear_allocator contains the pointer p.
void deallocate(owner< void *> p, std::size_t size)
Does nothing for linear_allocator. Use deallocate_all.
An allocator that allocates memory in contiguous memory by &#39;bumping&#39; the head pointer to lower memory...
owner< 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.
void deallocate_all() noexcept
Deallocates everything from this allocator.
Wrapper around a block of memory, containing both the size and the address of the memory block...
This type is used to hold the generic information for a given allocator.