bit::memory
aligned_offset_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains the implementation of an allocator used for
4  * offset 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_OFFSET_ALLOCATOR_HPP
31 #define BIT_MEMORY_ALLOCATORS_ALIGNED_OFFSET_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_offset_allocator() = default;
74 
78  aligned_offset_allocator( aligned_offset_allocator&& other ) noexcept = default;
79 
83  aligned_offset_allocator( const aligned_offset_allocator& other ) noexcept = default;
84 
85  //-----------------------------------------------------------------------
86 
91  aligned_offset_allocator& operator=( aligned_offset_allocator&& other ) noexcept = default;
92 
97  aligned_offset_allocator& operator=( const aligned_offset_allocator& other ) noexcept = default;
98 
99  //-----------------------------------------------------------------------
100  // Allocations / Deallocation
101  //-----------------------------------------------------------------------
102  public:
103 
111  owner<void*> try_allocate( std::size_t size,
112  std::size_t align,
113  std::size_t offset = 0 ) noexcept;
114 
119  void deallocate( owner<void*> p, std::size_t size );
120 
121  //---------------------------------------------------------------------
122  // Observers
123  //---------------------------------------------------------------------
124  public:
125 
132  allocator_info info() const noexcept;
133  };
134 
135  //-------------------------------------------------------------------------
136  // Utilities
137  //-------------------------------------------------------------------------
138 
139  using named_aligned_allocator = detail::named_allocator<aligned_offset_allocator>;
140 
141  } // namespace memory
142 } // namespace bit
143 
144 #include "detail/aligned_offset_allocator.inl"
145 
146 #endif /* BIT_MEMORY_ALLOCATORS_ALIGNED_OFFSET_ALLOCATOR_HPP */
void deallocate(owner< void *> p, std::size_t size)
Deallocates a pointer p with the allocation size of size.
This stateless allocator performs all of its allocation calls using aligned memory invocations aligne...
allocator_info info() const noexcept
Gets the info about this allocator.
owner< void * > try_allocate(std::size_t size, std::size_t align, std::size_t offset=0) noexcept
Allocates aligned memory of size size, with alignment to a boundary of at least align, aligned to an offset.
aligned_offset_allocator & operator=(aligned_offset_allocator &&other) noexcept=default
Move-assigns an aligned_offset_allocator from another allocator.
aligned_offset_allocator()=default
Default-constructs a aligned_offset_allocator.
This type is used to hold the generic information for a given allocator.