bit::memory
null_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header defines the null allocator policy, null_allocator
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_ALLOCATORS_NULL_ALLOCATOR_HPP
30 #define BIT_MEMORY_ALLOCATORS_NULL_ALLOCATOR_HPP
31 
32 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
33 # pragma once
34 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
35 
36 #include "detail/named_allocator.hpp" // detail::named_allocator
37 
38 #include "../utilities/allocator_info.hpp" // allocator_info
39 #include "../utilities/macros.hpp" // BIT_MEMORY_UNUSED
40 #include "../utilities/owner.hpp" // owner
41 
42 #include <cstddef> // std::size_t
43 #include <type_traits> // std::true_type
44 
45 namespace bit {
46  namespace memory {
47 
62  {
63  //-----------------------------------------------------------------------
64  // Public Static Members
65  //-----------------------------------------------------------------------
66  public:
67 
68  using default_alignment = std::integral_constant<std::size_t,1>;
69 
70  //-----------------------------------------------------------------------
71  // Constructor / Assignment
72  //-----------------------------------------------------------------------
73  public:
74 
76  null_allocator() = default;
77 
81  null_allocator( null_allocator&& other ) noexcept = default;
82 
86  null_allocator( const null_allocator& other ) noexcept = default;
87 
88  //-----------------------------------------------------------------------
89 
94  null_allocator& operator=( null_allocator&& other ) noexcept = default;
95 
100  null_allocator& operator=( const null_allocator& other ) noexcept = default;
101 
102  //----------------------------------------------------------------------
103  // Allocation
104  //----------------------------------------------------------------------
105  public:
106 
112  owner<void*> try_allocate( std::size_t size,
113  std::size_t align,
114  std::size_t offset = 0 ) noexcept;
115 
121  void deallocate( owner<void*> p, std::size_t n ) noexcept;
122 
123  //----------------------------------------------------------------------
124  // Observers
125  //----------------------------------------------------------------------
126  public:
127 
132  bool owns( const void* p ) const noexcept;
133 
137  bool owns( std::nullptr_t ) const noexcept;
138 
139  //----------------------------------------------------------------------
140 
147  allocator_info info() const noexcept;
148  };
149 
150  //-------------------------------------------------------------------------
151  // Utilities
152  //-------------------------------------------------------------------------
153 
154  using named_null_allocator = detail::named_allocator<null_allocator>;
155 
156  } // namespace memory
157 } // namespace bit
158 
159 #include "detail/null_allocator.inl"
160 
161 #endif /* BIT_MEMORY_ALLOCATORS_NULL_ALLOCATOR_HPP */
null_allocator & operator=(null_allocator &&other) noexcept=default
Move-assigns a null_allocator from another allocator.
owner< void * > try_allocate(std::size_t size, std::size_t align, std::size_t offset=0) noexcept
Allocates a null pointer.
An allocator that only ever serves nullptr.
allocator_info info() const noexcept
Gets the info about this allocator.
null_allocator()=default
Default-constructs a null_allocator.
void deallocate(owner< void *> p, std::size_t n) noexcept
Deallocates a pointer previously allocated with a call to allocate.
bool owns(const void *p) const noexcept
Checks if the pointer p is contained in the null_allocator.
This type is used to hold the generic information for a given allocator.