bit::memory
freelist.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains the definition of a simple freelist utility
4  *****************************************************************************/
5 
6 /*
7  The MIT License (MIT)
8 
9  Copyright (c) 2018 Matthew Rodusek
10 
11  Permission is hereby granted, free of charge, to any person obtaining a copy
12  of this software and associated documentation files (the "Software"), to deal
13  in the Software without restriction, including without limitation the rights
14  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  copies of the Software, and to permit persons to whom the Software is
16  furnished to do so, subject to the following conditions:
17 
18  The above copyright notice and this permission notice shall be included in
19  all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  SOFTWARE.
28 */
29 #ifndef BIT_MEMORY_UTILITIES_FREELIST_HPP
30 #define BIT_MEMORY_UTILITIES_FREELIST_HPP
31 
32 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
33 # pragma once
34 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
35 
36 #include "pointer_utilities.hpp" // align_of
37 #include "uninitialized_storage.hpp" // uninitialized_construct_at
38 
39 #include <cstddef> // std::size_t
40 #include <cassert> // assert
41 
42 namespace bit {
43  namespace memory {
44 
53  class freelist
54  {
55  //-----------------------------------------------------------------------
56  // Constructors
57  //-----------------------------------------------------------------------
58  public:
59 
61  constexpr freelist() noexcept;
62 
64  freelist( freelist&& other ) noexcept = default;
65 
66  // Deleted copy construction
67  freelist( const freelist& other ) = delete;
68 
69  //-----------------------------------------------------------------------
70 
71  // Deleted move assignment
72  freelist& operator=( freelist&& other ) = delete;
73 
74  // Deleted copy construction
75  freelist& operator=( const freelist& other ) = delete;
76 
77  //----------------------------------------------------------------------
78  // Modifiers
79  //----------------------------------------------------------------------
80  public:
81 
85  void swap( freelist& other ) noexcept;
86 
87  //----------------------------------------------------------------------
88  // Observers
89  //----------------------------------------------------------------------
90  public:
91 
95  bool empty() const noexcept;
96 
100  std::size_t size() const noexcept;
101 
102  //----------------------------------------------------------------------
103  // Modifiers
104  //----------------------------------------------------------------------
105  public:
106 
108  void clear();
109 
110  //----------------------------------------------------------------------
111  // Caching
112  //----------------------------------------------------------------------
113  public:
114 
120  void* request() noexcept;
121 
123  void steal( freelist& other ) noexcept;
124 
132  void store( void* p ) noexcept;
133 
134  //-----------------------------------------------------------------------
135  // Private Members
136  //-----------------------------------------------------------------------
137  private:
138 
139  void* m_head;
140 
141  friend bool operator==( const freelist&, const freelist& ) noexcept;
142  };
143 
144  //-------------------------------------------------------------------------
145  // Comparisons
146  //-------------------------------------------------------------------------
147 
148  bool operator==( const freelist& lhs, const freelist& rhs ) noexcept;
149  bool operator!=( const freelist& lhs, const freelist& rhs ) noexcept;
150 
151  } // namespace memory
152 } // namespace bit
153 
154 #include "detail/freelist.inl"
155 
156 #endif /* BIT_MEMORY_UTILITIES_FREELIST_HPP */
void clear()
Empties the freelist cache.
bool empty() const noexcept
Returns whether or not this memory_block_cache is empty.
void * request() noexcept
Requests raw memory from the freelist, if any exists.
void store(void *p) noexcept
Stores raw memory into this freelist.
void steal(freelist &other) noexcept
Steals raw meory from an existing freelist.
A simple freelist that uses intrusive pointers inside raw memory.
Definition: freelist.hpp:53
constexpr freelist() noexcept
Default constructs an empty freelist.
std::size_t size() const noexcept
Returns the number of entries in this freelist.
void swap(freelist &other) noexcept
Swaps this with another block cache.