bit::memory
memory_block_cache.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains definitions for an intrinsically linked list
4  * of available memory_blocks
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_UTILITIES_MEMORY_BLOCK_CACHE_HPP
31 #define BIT_MEMORY_UTILITIES_MEMORY_BLOCK_CACHE_HPP
32 
33 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
34 # pragma once
35 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
36 
37 #include "owner.hpp" // owner
38 #include "memory_block.hpp" // memory_block
39 #include "pointer_utilities.hpp" // align_of
40 #include "uninitialized_storage.hpp" // uninitialized_construct_at
41 
42 #include <cassert> // assert
43 
44 namespace bit {
45  namespace memory {
46 
61  {
62  //-----------------------------------------------------------------------
63  // Constructor / Assignment
64  //-----------------------------------------------------------------------
65  public:
66 
68  memory_block_cache() noexcept;
69 
73  memory_block_cache( memory_block_cache&& other ) noexcept = default;
74 
75  // Deleted copy constructor
76  memory_block_cache( const memory_block_cache& other ) = delete;
77 
78  //-----------------------------------------------------------------------
79 
80  // Deleted move assignment
81  memory_block_cache& operator=( memory_block_cache&& other ) = delete;
82 
83  // Deleted copy assignment
84  memory_block_cache& operator=( const memory_block_cache& other ) = delete;
85 
86  //-----------------------------------------------------------------------
87  // Observers
88  //-----------------------------------------------------------------------
89  public:
90 
94  bool empty() const noexcept;
95 
102  std::size_t size() const noexcept;
103 
107  std::size_t size_bytes() const noexcept;
108 
113  bool contains( const void* ptr ) const noexcept;
114 
115  //-----------------------------------------------------------------------
116  // Element Access
117  //-----------------------------------------------------------------------
118  public:
119 
128  const memory_block& peek() const noexcept;
129 
136  owner<memory_block> request_block() noexcept;
137 
142  template<typename BlockAllocator>
143  owner<memory_block> request_block( BlockAllocator& alloc );
144 
148  void steal_block( memory_block_cache& other ) noexcept;
149 
158  void store_block( owner<memory_block> block ) noexcept;
159 
160  //----------------------------------------------------------------------
161  // Modifiers
162  //----------------------------------------------------------------------
163  public:
164 
168  void swap( memory_block_cache& other ) noexcept;
169 
170  //----------------------------------------------------------------------
171  // Private Members
172  //----------------------------------------------------------------------
173  private:
174 
175  memory_block m_head;
176  };
177 
178  //------------------------------------------------------------------------
179  // Public Functions
180  //------------------------------------------------------------------------
181 
186  void swap( memory_block_cache& lhs, memory_block_cache& rhs ) noexcept;
187 
188  } // namespace memory
189 } // namespace bit
190 
191 #include "detail/memory_block_cache.inl"
192 
193 #endif /* BIT_MEMORY_UTILITIES_MEMORY_BLOCK_CACHE_HPP */
void store_block(owner< memory_block > block) noexcept
Stores an allocated block inside this memory_block_cache.
bool contains(const void *ptr) const noexcept
Evaluates whether ptr exists within this memory_block_cache.
void steal_block(memory_block_cache &other) noexcept
Acquires an entry from another cache.
const memory_block & peek() const noexcept
Views the front memory block of this cache.
owner< memory_block > request_block() noexcept
Requests a block from the current block cache.
memory_block_cache() noexcept
Default constructs a block cache.
bool empty() const noexcept
Returns whether or not this memory_block_cache is empty.
void swap(memory_block_cache &other) noexcept
Swaps this with another block cache.
A cache containing an intrinsically linked list of memory_blocks.
std::size_t size_bytes() const noexcept
Returns the size in bytes of all the memory_blocks in the cache.
std::size_t size() const noexcept
Returns the number of memory_blocks in this cache.
Wrapper around a block of memory, containing both the size and the address of the memory block...