bit::memory
malloc_block_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains an implementation of a non-aligned, malloc
4  * block 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_BLOCK_ALLOCATORS_MALLOC_BLOCK_ALLOCATOR_HPP
31 #define BIT_MEMORY_BLOCK_ALLOCATORS_MALLOC_BLOCK_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/enum_types.hpp" // block_align_t, block_size_t
38 #include "detail/cached_block_allocator.hpp" // detail::cached_block_allocator
39 #include "detail/named_block_allocator.hpp" // detail::named_block_allocator
40 
41 #include "../utilities/ebo_storage.hpp" // ebo_storage
42 #include "../utilities/dynamic_size_type.hpp" // dynamic_size, etc
43 #include "../utilities/allocator_info.hpp" // allocator_info
44 #include "../utilities/macros.hpp" // BIT_MEMORY_UNLIKELY
45 #include "../utilities/memory_block.hpp" // memory_block
46 #include "../utilities/owner.hpp" // owner
47 
48 #include "../policies/growth_multipliers/no_growth.hpp" // no_growth
49 
50 #include <cstddef> // std::size_t, std::ptrdiff_t
51 #include <cstdlib> // std::malloc, std::free
52 #include <type_traits> // std::true_type, std::false_type, etc
53 
54 namespace bit {
55  namespace memory {
56 
67  template<std::size_t DefaultBlockSize,
68  typename GrowthMultiplier=no_growth_multiplier>
70  : ebo_storage<GrowthMultiplier>,
71  dynamic_size_type<0,DefaultBlockSize>
72  {
75 
76  //----------------------------------------------------------------------
77  // Public Member Types
78  //----------------------------------------------------------------------
79  public:
80 
81  using default_block_alignment = std::integral_constant<std::size_t,alignof(std::max_align_t)>;
82 
83  //----------------------------------------------------------------------
84  // Constructors
85  //----------------------------------------------------------------------
86  public:
87 
91  malloc_block_allocator() noexcept = default;
92 
100  template<std::size_t USize=DefaultBlockSize,
101  typename=std::enable_if_t<USize!=dynamic_size>>
102  explicit malloc_block_allocator( GrowthMultiplier growth );
103 
111  template<std::size_t USize=DefaultBlockSize,
112  typename=std::enable_if_t<USize==dynamic_size>>
113  explicit malloc_block_allocator( block_size_t block_size,
114  GrowthMultiplier growth = GrowthMultiplier{} );
115 
119  malloc_block_allocator( malloc_block_allocator&& other ) = default;
120 
124  malloc_block_allocator( const malloc_block_allocator& other ) = default;
125 
126  //-----------------------------------------------------------------------
127 
132  malloc_block_allocator& operator=( malloc_block_allocator&& other ) = default;
133 
138  malloc_block_allocator& operator=( const malloc_block_allocator& other ) = default;
139 
140  //----------------------------------------------------------------------
141  // Block Allocations
142  //----------------------------------------------------------------------
143  public:
144 
148  owner<memory_block> allocate_block() noexcept;
149 
153  void deallocate_block( owner<memory_block> block ) noexcept;
154 
155  //----------------------------------------------------------------------
156  // Observers
157  //----------------------------------------------------------------------
158  public:
159 
163  std::size_t next_block_size() const noexcept;
164 
171  allocator_info info() const noexcept;
172  };
173 
174  //-------------------------------------------------------------------------
175  // Utiltiies
176  //-------------------------------------------------------------------------
177 
178  template<std::size_t DefaultBlockSize, typename GrowthMultiplier=no_growth_multiplier>
179  using cached_malloc_block_allocator
180  = detail::cached_block_allocator<malloc_block_allocator<DefaultBlockSize,GrowthMultiplier>>;
181 
182  //-------------------------------------------------------------------------
183 
184  template<std::size_t DefaultBlockSize, typename GrowthMultiplier=no_growth_multiplier>
185  using named_malloc_block_allocator
186  = detail::named_block_allocator<malloc_block_allocator<DefaultBlockSize,GrowthMultiplier>>;
187 
188  //-------------------------------------------------------------------------
189 
190  template<std::size_t DefaultBlockSize, typename GrowthMultiplier=no_growth_multiplier>
191  using named_cached_malloc_block_allocator
192  = detail::named_block_allocator<cached_malloc_block_allocator<DefaultBlockSize,GrowthMultiplier>>;
193 
194  } // namespace memory
195 } // namespace bit
196 
197 #include "detail/malloc_block_allocator.inl"
198 
199 #endif /* BIT_MEMORY_BLOCK_ALLOCATORS_MALLOC_BLOCK_ALLOCATOR_HPP */
This type is used as a compile-time constant for storing size values.
owner< memory_block > allocate_block() noexcept
Allocates a malloc&#39;ed memory_block.
A block allocator that wraps around raw calls to std::malloc and std::free.
std::size_t next_block_size() const noexcept
Queries the next block size expected from this allocator.
allocator_info info() const noexcept
Gets the info about this allocator.
malloc_block_allocator & operator=(malloc_block_allocator &&other)=default
Move-assigns a malloc_block_allocator from another allocator.
void deallocate_block(owner< memory_block > block) noexcept
Deallocates a malloc&#39;ed memory_block.
This type is used to hold the generic information for a given allocator.
malloc_block_allocator() noexcept=default
Default-constructs this malloc_block_allocator.
A utility class used for leveraging empty-base optimization for a generic sequence of types...
Definition: ebo_storage.hpp:62