bit::memory
aligned_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains the implementation of a RawAllocator used for
4  * aligned allocations
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_ALIGNED_ALLOCATOR_HPP
31 #define BIT_MEMORY_ALLOCATORS_ALIGNED_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 "../regions/aligned_heap_memory.hpp" // aligned_malloc, aligned_free
44 
45 #include <cstddef> // std::max_align_t
46 #include <type_traits> // std::true_type
47 
48 namespace bit {
49  namespace memory {
50 
59  {
60  //-----------------------------------------------------------------------
61  // Public Static Members
62  //-----------------------------------------------------------------------
63  public:
64 
65  using default_alignment = std::integral_constant<std::size_t,1>;
66 
67  //-----------------------------------------------------------------------
68  // Constructor / Assignment
69  //-----------------------------------------------------------------------
70  public:
71 
73  aligned_allocator() = default;
74 
78  aligned_allocator( aligned_allocator&& other ) noexcept = default;
79 
83  aligned_allocator( const aligned_allocator& other ) noexcept = default;
84 
85  //-----------------------------------------------------------------------
86 
91  aligned_allocator& operator=( aligned_allocator&& other ) noexcept = default;
92 
97  aligned_allocator& operator=( const aligned_allocator& other ) noexcept = default;
98 
99  //-----------------------------------------------------------------------
100  // Allocations / Deallocation
101  //-----------------------------------------------------------------------
102  public:
103 
110  owner<void*> try_allocate( std::size_t size, std::size_t align ) noexcept;
111 
116  void deallocate( owner<void*> p, std::size_t size );
117 
118  //---------------------------------------------------------------------
119  // Observers
120  //---------------------------------------------------------------------
121  public:
122 
129  allocator_info info() const noexcept;
130  };
131 
132  //-------------------------------------------------------------------------
133  // Utilities
134  //-------------------------------------------------------------------------
135 
136  using named_aligned_allocator = detail::named_allocator<aligned_allocator>;
137 
138  } // namespace memory
139 } // namespace bit
140 
141 #include "detail/aligned_allocator.inl"
142 
143 #endif /* BIT_MEMORY_ALLOCATORS_ALIGNED_ALLOCATOR_HPP */
allocator_info info() const noexcept
Gets the info about this allocator.
This stateless allocator performs all of its allocation calls using aligned memory invocations...
void deallocate(owner< void *> p, std::size_t size)
Deallocates a pointer p with the allocation size of size.
aligned_allocator & operator=(aligned_allocator &&other) noexcept=default
Move-assigns an aligned_allocator from another allocator.
owner< void * > try_allocate(std::size_t size, std::size_t align) noexcept
Allocates aligned memory of size size, with alignment to a boundary of at least align.
aligned_allocator()=default
Default-constructs a aligned_allocator.
This type is used to hold the generic information for a given allocator.