bit::memory
new_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains the definition for the RawAllocator,
4  * new_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_NEW_ALLOCATOR_HPP
31 #define BIT_MEMORY_ALLOCATORS_NEW_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/allocator_info.hpp" // allocator_info
40 #include "../utilities/macros.hpp" // BIT_MEMORY_UNUSED
41 #include "../utilities/owner.hpp" // owner
42 
43 #include <cstddef> // std::size_t, std::max_align_t
44 #include <new> // ::operator new, ::operator delete, std::nothrow
45 #include <type_traits> // std::true_type
46 
47 namespace bit {
48  namespace memory {
49 
60  {
61  //-----------------------------------------------------------------------
62  // Public Static Members
63  //-----------------------------------------------------------------------
64  public:
65 
66  using default_alignment = std::integral_constant<std::size_t,alignof(std::max_align_t)>;
67 
68  //-----------------------------------------------------------------------
69  // Constructor / Assignment
70  //-----------------------------------------------------------------------
71  public:
72 
74  new_allocator() = default;
75 
79  new_allocator( new_allocator&& other ) noexcept = default;
80 
84  new_allocator( const new_allocator& other ) noexcept = default;
85 
86  //-----------------------------------------------------------------------
87 
92  new_allocator& operator=( new_allocator&& other ) noexcept = default;
93 
98  new_allocator& operator=( const new_allocator& other ) noexcept = default;
99 
100  //-----------------------------------------------------------------------
101  // Allocations
102  //-----------------------------------------------------------------------
103  public:
104 
114  owner<void*> try_allocate( std::size_t size, std::size_t align ) noexcept;
115 
120  void deallocate( owner<void*> p, std::size_t size );
121 
122  //-----------------------------------------------------------------------
123  // Observers
124  //-----------------------------------------------------------------------
125  public:
126 
133  allocator_info info() const noexcept;
134  };
135 
136  //-------------------------------------------------------------------------
137  // Utilities
138  //-------------------------------------------------------------------------
139 
140  using named_new_allocator = detail::named_allocator<new_allocator>;
141 
142  } // namespace memory
143 } // namespace bit
144 
145 #include "detail/new_allocator.inl"
146 
147 #endif /* BIT_MEMORY_ALLOCATORS_NEW_ALLOCATOR_HPP */
owner< void * > try_allocate(std::size_t size, std::size_t align) noexcept
Attempts to allocate memory of size size, returning nullptr on failure.
This stateless allocator performs all of its allocation calls using raw calls to &#39;new&#39;, and frees memory with &#39;delete&#39;.
allocator_info info() const noexcept
Gets the info about this allocator.
void deallocate(owner< void *> p, std::size_t size)
Deallocates a pointer p with the allocation size of size.
new_allocator & operator=(new_allocator &&other) noexcept=default
Move-assigns a new_allocator from another allocator.
new_allocator()=default
Default-constructs a new_allocator.
This type is used to hold the generic information for a given allocator.