Sources Pipelines Documentation

Allocators  
bucketizer.ipp
1 // Copyright (c) 2016 Lukasz Laszko
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #pragma once
6 
7 
8 namespace boost { namespace memory {
9 
10 template <typename bucketizer_type>
11 bucketizer_helper<bucketizer_type>::bucketizer_helper(bucketizer_type& instance)
12  :
13  instance_(instance)
14 {
15 
16 }
17 
18 template <typename bucketizer_type>
19 template <std::size_t bucket>
20 inline auto& bucketizer_helper<bucketizer_type>::get() noexcept
21 {
22  static const auto min = bucketizer_type::min_size;
23  static const auto max = bucketizer_type::max_size;
24  static const auto step = bucketizer_type::step_size;
25 
26  static_assert(bucket < (max - min) / step, "Wrong bucket number!");
27 
28  return instance_.buckets_[bucket];
29 }
30 
31 template <
32  typename allocator,
33  std::size_t min,
34  std::size_t max,
35  std::size_t step>
36 inline memory_block bucketizer<
37  allocator,
38  min,
39  max,
40  step>::allocate(std::size_t size)
41 {
42  if (min <= size && size <= max)
43  {
44  auto bucket_index = (size - min) / step;
45  auto& bucket = buckets_[bucket_index];
46 
47  return bucket.allocate(size);
48  }
49  else
50  {
51  return { nullptr, 0ul };
52  }
53 }
54 
55 template <
56  typename allocator,
57  std::size_t min,
58  std::size_t max,
59  std::size_t step>
60 inline void bucketizer<
61  allocator,
62  min,
63  max,
64  step>::deallocate(memory_block& block)
65 {
66  if (min <= block.size && block.size <= max)
67  {
68  auto bucket_index = (block.size - min) / step;
69  auto& bucket = buckets_[bucket_index];
70 
71  return bucket.deallocate(block);
72  }
73 }
74 
75 template <
76  typename allocator,
77  std::size_t min,
78  std::size_t max,
79  std::size_t step>
80 inline bool bucketizer<
81  allocator,
82  min,
83  max,
84  step>::owns(memory_block& block)
85 {
86  if (min <= block.size && block.size <= max)
87  {
88  auto bucket_index = (block.size - min) / step;
89  auto& bucket = buckets_[bucket_index];
90 
91  return bucket.owns(block);
92  }
93  else
94  {
95  return false;
96  }
97 }
98 
99 } }