bit::memory
bump_down_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains the definition of the ExtendedAllocator class,
4  * linear_allocator.
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_BUMP_DOWN_ALLOCATOR_HPP
31 #define BIT_MEMORY_ALLOCATORS_BUMP_DOWN_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/memory_block.hpp" // memory_block
41 #include "../utilities/owner.hpp" // owner
42 #include "../utilities/pointer_utilities.hpp" // offset_align_backward
43 
44 #include <cassert> // assert
45 #include <cstddef> // std::size_t, std::ptrdiff_t
46 #include <type_traits> // std:integral_constant, std::true_type, etc
47 
48 namespace bit {
49  namespace memory {
50 
71  {
72  //-----------------------------------------------------------------------
73  // Public Member Types
74  //-----------------------------------------------------------------------
75  public:
76 
77  using max_alignment = std::integral_constant<std::size_t,(1 << (sizeof(std::size_t)-1))>;
78 
79  //-----------------------------------------------------------------------
80  // Constructors
81  //-----------------------------------------------------------------------
82  public:
83 
85  explicit bump_down_allocator( memory_block block ) noexcept;
86 
90  bump_down_allocator( bump_down_allocator&& other ) noexcept = default;
91 
92  // Deleted copy constructor
93  bump_down_allocator( const bump_down_allocator& other ) = delete;
94 
95  // Deleted nullblock constructor
96  bump_down_allocator( nullblock_t ) = delete;
97 
98  //----------------------------------------------------------------------
99 
100  // Deleted copy assignment
101  bump_down_allocator& operator=( const bump_down_allocator& ) = delete;
102 
103  // Deleted move assignment
104  bump_down_allocator& operator=( bump_down_allocator&& other ) = delete;
105 
106  //-----------------------------------------------------------------------
107  // Allocations / Deallocations
108  //-----------------------------------------------------------------------
109  public:
110 
118  owner<void*> try_allocate( std::size_t size,
119  std::size_t align,
120  std::size_t offset = 0 ) noexcept;
121 
126  void deallocate( owner<void*> p, std::size_t size );
127 
129  void deallocate_all() noexcept;
130 
131  //-----------------------------------------------------------------------
132  // Observers
133  //-----------------------------------------------------------------------
134  public:
135 
140  bool owns( const void* p ) const noexcept;
141 
148  allocator_info info() const noexcept;
149 
150  //-----------------------------------------------------------------------
151  // Private Members
152  //-----------------------------------------------------------------------
153  private:
154 
155  memory_block m_block;
156  void* m_current;
157  };
158 
159  //-------------------------------------------------------------------------
160  // Utilities
161  //-------------------------------------------------------------------------
162 
163  using named_bump_down_allocator = detail::named_allocator<bump_down_allocator>;
164 
165  } // namespace memory
166 } // namespace bit
167 
168 #include "detail/bump_down_allocator.inl"
169 
170 #endif /* BIT_MEMORY_ALLOCATORS_BUMP_DOWN_ALLOCATOR_HPP */
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.
bump_down_allocator(memory_block block) noexcept
Constructs a linear_allocator.
void deallocate_all() noexcept
Deallocates everything from this allocator.
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.
An allocator that allocates memory in contiguous memory by &#39;bumping&#39; the head pointer to lower memory...
allocator_info info() const noexcept
Gets the info about 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.