bit::memory
memory_block.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains implementation for both the memory_block and
4  * the memory_block_cache.
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_HPP
31 #define BIT_MEMORY_UTILITIES_MEMORY_BLOCK_HPP
32 
33 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
34 # pragma once
35 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
36 
37 #include <cstdlib> // std::size_t
38 #include <utility> // std::swap
39 
40 namespace bit {
41  namespace memory {
42  namespace detail {
43  struct memory_block_arg{};
44  } // namespace detail
45 
47 #ifndef BIT_DOXYGEN_BUILD
48  inline void nullblock( detail::memory_block_arg& ){}
49 #else
50  void nullblock( /* implementation defined */ );
51 #endif
52 
54  using nullblock_t = decltype(*nullblock);
55 
61  {
62  //----------------------------------------------------------------------
63  // Constructors / Assignment
64  //----------------------------------------------------------------------
65  public:
66 
68  constexpr memory_block() noexcept;
69 
71  constexpr memory_block( nullblock_t ) noexcept;
72 
78  constexpr memory_block( void* data, std::size_t size ) noexcept;
79 
83  constexpr memory_block( const memory_block& other ) noexcept = default;
84 
88  constexpr memory_block( memory_block&& other ) noexcept = default;
89 
90  //----------------------------------------------------------------------
91 
96  memory_block& operator=( const memory_block& other ) noexcept = default;
97 
102  memory_block& operator=( memory_block&& other ) noexcept = default;
103 
104  //----------------------------------------------------------------------
105  // Element Access
106  //----------------------------------------------------------------------
107  public:
108 
112  constexpr void* data() const noexcept;
113 
119  constexpr void* start_address() const noexcept;
120 
126  constexpr void* end_address() const noexcept;
127 
128  //----------------------------------------------------------------------
129  // Observers
130  //----------------------------------------------------------------------
131  public:
132 
136  constexpr std::size_t size() const noexcept;
137 
142  bool contains( const void* ptr ) const noexcept;
143 
145  constexpr explicit operator bool() const noexcept;
146 
147  //----------------------------------------------------------------------
148  // Mutators
149  //----------------------------------------------------------------------
150  public:
151 
155  void swap( memory_block& other ) noexcept;
156 
157  //----------------------------------------------------------------------
158  // Private Member Types
159  //----------------------------------------------------------------------
160  private:
161 
162  void* m_data;
163  std::size_t m_size;
164  };
165 
166  //------------------------------------------------------------------------
167  // Free Functions
168  //------------------------------------------------------------------------
169 
174  void swap( memory_block& lhs, memory_block& rhs ) noexcept;
175 
176  constexpr bool operator == ( const memory_block& lhs, const memory_block& rhs ) noexcept;
177  constexpr bool operator == ( nullblock_t, const memory_block& rhs ) noexcept;
178  constexpr bool operator == ( const memory_block& lhs, nullblock_t ) noexcept;
179 
180  constexpr bool operator != ( const memory_block& lhs, const memory_block& rhs ) noexcept;
181  constexpr bool operator != ( nullblock_t, const memory_block& rhs ) noexcept;
182  constexpr bool operator != ( const memory_block& lhs, nullblock_t ) noexcept;
183 
184  //------------------------------------------------------------------------
185  // Utilities
186  //------------------------------------------------------------------------
187 
192  constexpr bool is_null( memory_block block ) noexcept;
193 
198  constexpr bool is_null( nullblock_t ) noexcept;
199 
200  } // namespace memory
201 } // namespace bit
202 
203 #include "detail/memory_block.inl"
204 
205 #endif /* BIT_MEMORY_UTILITIES_MEMORY_BLOCK_HPP */
Wrapper around a block of memory, containing both the size and the address of the memory block...