bit::memory
bump_up_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains the definition of the ExtendedAllocator class,
4  * bump_up_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_UP_ALLOCATOR_HPP
31 #define BIT_MEMORY_ALLOCATORS_BUMP_UP_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_forward
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 
63  {
64  //-----------------------------------------------------------------------
65  // Public Member Types
66  //-----------------------------------------------------------------------
67  public:
68 
69  using max_alignment = std::integral_constant<std::size_t,(1 << (sizeof(std::size_t)-1))>;
70 
71  //-----------------------------------------------------------------------
72  // Constructors
73  //-----------------------------------------------------------------------
74  public:
75 
77  explicit bump_up_allocator( memory_block block ) noexcept;
78 
82  bump_up_allocator( bump_up_allocator&& other ) noexcept = default;
83 
84  // Deleted copy constructor
85  bump_up_allocator( const bump_up_allocator& other ) = delete;
86 
87  // Deleted nullblock constructor
88  bump_up_allocator( nullblock_t ) = delete;
89 
90  //----------------------------------------------------------------------
91 
92  // Deleted copy assignment
93  bump_up_allocator& operator=( const bump_up_allocator& ) = delete;
94 
95  // Deleted move assignment
96  bump_up_allocator& operator=( bump_up_allocator&& other ) = delete;
97 
98  //-----------------------------------------------------------------------
99  // Allocations / Deallocations
100  //-----------------------------------------------------------------------
101  public:
102 
110  owner<void*> try_allocate( std::size_t size,
111  std::size_t align,
112  std::size_t offset = 0 ) noexcept;
113 
118  void deallocate( owner<void*> p, std::size_t size );
119 
121  void deallocate_all() noexcept;
122 
123  //-----------------------------------------------------------------------
124  // Observers
125  //-----------------------------------------------------------------------
126  public:
127 
132  bool owns( const void* p ) const noexcept;
133 
140  allocator_info info() const noexcept;
141 
142  //-----------------------------------------------------------------------
143  // Private Members
144  //-----------------------------------------------------------------------
145  private:
146 
147  memory_block m_block;
148  void* m_current;
149  };
150 
151  //-------------------------------------------------------------------------
152  // Utilities
153  //-------------------------------------------------------------------------
154 
155  using named_bump_up_allocator = detail::named_allocator<bump_up_allocator>;
156 
157  } // namespace memory
158 } // namespace bit
159 
160 #include "detail/bump_up_allocator.inl"
161 
162 #endif /* BIT_MEMORY_ALLOCATORS_BUMP_UP_ALLOCATOR_HPP */
void deallocate(owner< void *> p, std::size_t size)
Does nothing for bump_up_allocator. Use deallocate_all.
An allocator that allocates memory in contiguous memory by &#39;bumping&#39; the head pointer to higher memor...
bool owns(const void *p) const noexcept
Checks whether bump_up_allocator contains the pointer p.
bump_up_allocator(memory_block block) noexcept
Constructs a bump_up_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.
Wrapper around a block of memory, containing both the size and the address of the memory block...
allocator_info info() const noexcept
Gets the info about this allocator.
This type is used to hold the generic information for a given allocator.