PMDK C++ bindings  1.11.1
This is the C++ bindings documentation for PMDK's libpmemobj.
segment_vector_policies.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2019, Intel Corporation */
3 
9 #ifndef LIBPMEMOBJ_SEGMENT_VECTOR_POLICIES_HPP
10 #define LIBPMEMOBJ_SEGMENT_VECTOR_POLICIES_HPP
11 
15 #include <vector>
16 
17 namespace pmem
18 {
19 namespace obj
20 {
21 namespace segment_vector_internal
22 {
23 template <typename T>
24 using array_64 = array<T, 64>;
25 
26 template <typename Container>
27 using resize_method =
28  decltype(std::declval<Container>().resize(std::declval<size_t>()));
29 
30 template <typename Container>
31 using container_has_resize = detail::supports<Container, resize_method>;
32 
33 template <typename Container, bool = container_has_resize<Container>::value>
34 struct segment_vector_resize {
35  using segment_vector_type = Container;
36 
37  static void
38  resize(segment_vector_type &c, size_t n)
39  {
40  c.resize(n);
41  }
42 };
43 
44 template <typename Container>
45 struct segment_vector_resize<Container, false> {
46  using segment_vector_type = Container;
47 
48  static void
49  resize(segment_vector_type &c, size_t n)
50  {
51  }
52 };
53 
54 template <template <typename> class SegmentVectorType,
55  template <typename> class SegmentType, size_t SegmentSize>
56 class fixed_size_policy {
57 public:
58  /* Traits */
59  template <typename T>
60  using segment_vector_type = SegmentVectorType<SegmentType<T>>;
61 
62  template <typename T>
63  using segment_type = SegmentType<T>;
64 
65  template <typename T>
66  using value_type = typename segment_type<T>::value_type;
67 
68  using size_type = std::size_t;
69 
70  template <typename T>
71  using segment_vector_resize_type =
72  segment_vector_resize<segment_vector_type<T>>;
73 
74  static constexpr size_type Size = SegmentSize;
75 
76  template <typename T>
77  static void
78  resize(segment_vector_type<T> &c, size_type n)
79  {
80  segment_vector_resize_type<T>::resize(c, n);
81  }
82 
88  static size_type
89  get_segment(size_type index)
90  {
91  return index / Size;
92  }
98  static size_type
99  segment_top(size_type segment_index)
100  {
101  return segment_index * Size;
102  }
108  static size_type
109  segment_size(size_type segment_index)
110  {
111  return Size;
112  }
118  static size_type
119  index_in_segment(size_type index)
120  {
121  return index % Size;
122  }
123 
127  template <typename T>
128  static size_type
129  max_size(const segment_vector_type<T> &seg_storage)
130  {
131  return seg_storage.max_size() * SegmentSize;
132  }
133 
137  static size_type
138  capacity(size_type segment_index)
139  {
140  return (segment_index + 1) * Size;
141  }
142 };
143 
144 template <template <typename> class SegmentVectorType,
145  template <typename> class SegmentType>
146 class exponential_size_policy {
147 public:
148  /* Traits */
149  template <typename T>
150  using segment_vector_type = SegmentVectorType<SegmentType<T>>;
151 
152  template <typename T>
153  using segment_type = SegmentType<T>;
154 
155  template <typename T>
156  using value_type = typename segment_type<T>::value_type;
157 
158  using size_type = std::size_t;
159 
160  template <typename T>
161  using segment_vector_resize_type =
162  segment_vector_resize<segment_vector_type<T>>;
163 
164  template <typename T>
165  static void
166  resize(segment_vector_type<T> &c, size_type n)
167  {
168  segment_vector_resize_type<T>::resize(c, n);
169  }
170 
176  static size_type
177  get_segment(size_type index)
178  {
179  return static_cast<size_type>(detail::Log2(index | 1));
180  }
186  static size_type
187  segment_top(size_type segment_index)
188  {
189  return (size_type(1) << segment_index) & ~size_type(1);
190  }
196  static size_type
197  segment_size(size_type segment_index)
198  {
199  return (segment_index == 0) ? 2 : segment_top(segment_index);
200  }
206  static size_type
207  index_in_segment(size_type index)
208  {
209  return index - segment_top(get_segment(index));
210  }
211 
215  template <typename T>
216  static size_t
217  max_size(const segment_vector_type<T> &)
218  {
219  return segment_size(get_segment(PMEMOBJ_MAX_ALLOC_SIZE /
220  sizeof(value_type<T>)) +
221  1);
222  }
223 
227  static size_type
228  capacity(size_type segment_index)
229  {
230  if (segment_index == 0)
231  return 2;
232  return segment_size(segment_index) * 2;
233  }
234 };
235 
236 } /* segment_vector_internal namespace */
237 } /* namespace obj */
238 } /* namespace pmem */
239 
240 #endif /* LIBPMEMOBJ_SEGMENT_VECTOR_POLICIES_HPP */
vector.hpp
Vector container with std::vector compatible interface.
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:15
template_helpers.hpp
Commonly used SFINAE helpers.
array.hpp
Array container with std::array compatible interface.
pmem::obj::array
pmem::obj::array - persistent container with std::array compatible interface.
Definition: array.hpp:46