PMDK C++ bindings  1.10
This is the C++ bindings documentation for PMDK's libpmemobj.
allocator.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2016-2020, Intel Corporation */
3 
9 #ifndef LIBPMEMOBJ_CPP_ALLOCATOR_HPP
10 #define LIBPMEMOBJ_CPP_ALLOCATOR_HPP
11 
16 #include <libpmemobj++/pext.hpp>
17 #include <libpmemobj/tx_base.h>
18 
19 namespace pmem
20 {
21 
22 namespace obj
23 {
24 
29 template <typename T>
31 public:
32  /*
33  * Important typedefs.
34  */
35  using value_type = T;
38  using reference = value_type &;
39  using const_reference = const value_type &;
40 
44  template <class U>
45  struct rebind {
46  using other = object_traits<U>;
47  };
48 
52  object_traits() = default;
53 
57  ~object_traits() = default;
58 
62  template <typename U,
63  typename = typename std::enable_if<
64  std::is_convertible<U *, T *>::value>::type>
65  explicit object_traits(object_traits<U> const &)
66  {
67  }
68 
81  void
82  construct(pointer p, const_reference t)
83  {
84  if (pmemobj_tx_stage() != TX_STAGE_WORK)
86  "construct is called outside of transaction scope");
87 
88  /* construct called on newly allocated objects */
89  detail::conditional_add_to_tx(p.get());
90  new (static_cast<void *>(p.get())) value_type(t);
91  }
92 
105  template <typename... Args>
106  void
107  construct(pointer p, Args &&... args)
108  {
109  if (pmemobj_tx_stage() != TX_STAGE_WORK)
111  "construct is called outside of transaction scope");
112 
113  detail::conditional_add_to_tx(p.get());
114  new (static_cast<void *>(p.get()))
115  value_type(std::forward<Args>(args)...);
116  }
117 
125  void
127  {
128  /* XXX should we allow modifications outside of tx? */
129  if (pmemobj_tx_stage() == TX_STAGE_WORK) {
130  pmemobj_tx_add_range_direct((void *)p.get(), sizeof(p));
131  }
132 
133  detail::destroy<value_type>(*p);
134  }
135 };
136 
141 template <>
142 class object_traits<void> {
143 public:
144  /*
145  * Important typedefs.
146  */
147  using value_type = void;
149 
153  template <class U>
154  struct rebind {
155  using other = object_traits<U>;
156  };
157 
161  object_traits() = default;
162 
166  ~object_traits() = default;
167 
171  template <typename U>
172  explicit object_traits(object_traits<U> const &)
173  {
174  }
175 };
176 
183 template <typename T>
185 public:
186  /*
187  * Important typedefs.
188  */
189  using value_type = T;
192  using size_type = std::size_t;
193  using bool_type = bool;
194 
198  template <class U>
199  struct rebind {
201  };
202 
207 
212 
217  {
218  }
219 
223  template <typename U,
224  typename = typename std::enable_if<
225  std::is_convertible<U *, T *>::value>::type>
227  {
228  }
229 
241  pointer
242  allocate(size_type cnt, const_void_pointer = 0)
243  {
244  if (pmemobj_tx_stage() != TX_STAGE_WORK)
246  "refusing to allocate memory outside of transaction scope");
247 
248  /* allocate raw memory, no object construction */
249  pointer ptr = pmemobj_tx_alloc(sizeof(value_type) * cnt,
250  detail::type_num<value_type>());
251 
252  if (ptr == nullptr) {
253  if (errno == ENOMEM) {
255  "Failed to allocate persistent memory object")
256  .with_pmemobj_errormsg();
257  } else {
259  "Failed to allocate persistent memory object")
260  .with_pmemobj_errormsg();
261  }
262  }
263 
264  return ptr;
265  }
266 
274  void
275  deallocate(pointer p, size_type = 0)
276  {
277  if (pmemobj_tx_stage() != TX_STAGE_WORK)
279  "refusing to free memory outside of transaction scope");
280 
281  if (pmemobj_tx_free(*p.raw_ptr()) != 0)
283  "failed to delete persistent memory object")
284  .with_pmemobj_errormsg();
285  }
286 
292  size_type
293  max_size() const
294  {
295  return PMEMOBJ_MAX_ALLOC_SIZE / sizeof(value_type);
296  }
297 };
298 
302 template <>
304 public:
305  /*
306  * Important typedefs.
307  */
308  using value_type = void;
311  using reference = value_type;
312  using const_reference = const value_type;
313  using size_type = std::size_t;
314  using bool_type = bool;
315 
319  template <class U>
320  struct rebind {
322  };
323 
328 
333 
338  {
339  }
340 
344  template <typename U>
346  {
347  }
348 
356  pointer
357  allocate(size_type cnt, const_pointer = 0)
358  {
359  if (pmemobj_tx_stage() != TX_STAGE_WORK)
361  "refusing to allocate memory outside of transaction scope");
362 
363  /* allocate raw memory, no object construction */
364  return pmemobj_tx_alloc(1 /* void size */ * cnt, 0);
365  }
366 
374  void
375  deallocate(pointer p, size_type = 0)
376  {
377  if (pmemobj_tx_stage() != TX_STAGE_WORK)
379  "refusing to free memory outside of transaction scope");
380 
381  if (pmemobj_tx_free(p.raw()) != 0)
383  "failed to delete persistent memory object")
384  .with_pmemobj_errormsg();
385  }
386 
392  size_type
393  max_size() const
394  {
395  return PMEMOBJ_MAX_ALLOC_SIZE;
396  }
397 };
398 
404 template <typename T, typename T2>
405 inline bool
407 {
408  return true;
409 }
410 
416 template <typename T, typename OtherAllocator>
417 inline bool
418 operator==(standard_alloc_policy<T> const &, OtherAllocator const &)
419 {
420  return false;
421 }
422 
430 template <typename T, typename Policy = standard_alloc_policy<T>,
431  typename Traits = object_traits<T>>
432 class allocator : public Policy, public Traits {
433 private:
434  /*
435  * private typedefs
436  */
437  using AllocationPolicy = Policy;
438  using TTraits = Traits;
439 
440 public:
441  /*
442  * Important typedefs.
443  */
444  using size_type = typename AllocationPolicy::size_type;
445  using pointer = typename AllocationPolicy::pointer;
446  using value_type = typename AllocationPolicy::value_type;
447 
451  template <typename U>
452  struct rebind {
453  using other = allocator<
454  U, typename AllocationPolicy::template rebind<U>::other,
455  typename TTraits::template rebind<U>::other>;
456  };
457 
461  allocator() = default;
462 
466  ~allocator() = default;
467 
471  allocator(allocator const &rhs) : Policy(rhs), Traits(rhs)
472  {
473  }
474 
478  template <typename U>
479  explicit allocator(allocator<U> const &)
480  {
481  }
482 
486  template <typename U, typename P, typename T2>
487  explicit allocator(allocator<U, P, T2> const &rhs)
488  : Policy(rhs), Traits(rhs)
489  {
490  }
491 };
492 
502 template <typename T, typename P, typename Tr, typename T2, typename P2,
503  typename Tr2>
504 inline bool
506 {
507  return operator==(static_cast<const P &>(lhs),
508  static_cast<const P2 &>(rhs));
509 }
510 
520 template <typename T, typename P, typename Tr, typename OtherAllocator>
521 inline bool
522 operator!=(const allocator<T, P, Tr> &lhs, const OtherAllocator &rhs)
523 {
524  return !operator==(lhs, rhs);
525 }
526 
527 } /* namespace obj */
528 
529 } /* namespace pmem */
530 
531 #endif /* LIBPMEMOBJ_CPP_ALLOCATOR_HPP */
pmem::obj::standard_alloc_policy< void >::standard_alloc_policy
standard_alloc_policy(standard_alloc_policy< U > const &)
Type converting constructor.
Definition: allocator.hpp:345
pmem::obj::object_traits::construct
void construct(pointer p, Args &&... args)
Create an object at a specific address.
Definition: allocator.hpp:107
pmem::transaction_free_error
Custom transaction error class.
Definition: pexceptions.hpp:140
pmem::obj::allocator::allocator
allocator()=default
Defaulted constructor.
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:15
pmem::obj::persistent_ptr< const void >
persistent_ptr const void specialization.
Definition: persistent_ptr.hpp:88
pmem::transaction_out_of_memory
Custom out of memory error class.
Definition: pexceptions.hpp:120
common.hpp
Commonly used functionality.
pmem::obj::allocator
(EXPERIMENTAL) Encapsulates the information about the persistent memory allocation model using PMDK's...
Definition: allocator.hpp:432
pmem::obj::allocator::allocator
allocator(allocator< U > const &)
Type converting constructor.
Definition: allocator.hpp:479
pmem::obj::object_traits< void >::~object_traits
~object_traits()=default
Defaulted destructor.
pexceptions.hpp
Custom exceptions.
pmem::obj::operator==
bool operator==(standard_alloc_policy< T > const &, standard_alloc_policy< T2 > const &)
Determines if memory from another allocator can be deallocated from this one.
Definition: allocator.hpp:406
pmem::obj::p
Resides on pmem class.
Definition: p.hpp:35
pmem::obj::standard_alloc_policy::rebind
Rebind to a different type.
Definition: allocator.hpp:199
pmem::obj::object_traits::object_traits
object_traits(object_traits< U > const &)
Type converting constructor.
Definition: allocator.hpp:65
pmem::obj::object_traits< void >::object_traits
object_traits()=default
Defaulted constructor.
pmem::obj::object_traits::destroy
void destroy(pointer p)
Destroy an object based on a pointer.
Definition: allocator.hpp:126
pmem::obj::operator!=
bool operator!=(const allocator< T, P, Tr > &lhs, const OtherAllocator &rhs)
Determines if memory from another allocator can be deallocated from this one.
Definition: allocator.hpp:522
pmem::obj::standard_alloc_policy::~standard_alloc_policy
~standard_alloc_policy()=default
Defaulted destructor.
pmem::obj::standard_alloc_policy< void >::standard_alloc_policy
standard_alloc_policy(standard_alloc_policy const &)
Explicit copy constructor.
Definition: allocator.hpp:337
pmem::obj::standard_alloc_policy< void >::~standard_alloc_policy
~standard_alloc_policy()=default
Defaulted destructor.
pmem::obj::standard_alloc_policy::standard_alloc_policy
standard_alloc_policy()=default
Defaulted constructor.
pmem::obj::standard_alloc_policy::deallocate
void deallocate(pointer p, size_type=0)
Deallocates storage pointed to p, which must be a value returned by a previous call to allocate that ...
Definition: allocator.hpp:275
pmem::obj::object_traits
Encapsulates object specific allocator functionality.
Definition: allocator.hpp:30
pmem::obj::object_traits::rebind
Rebind to a different type.
Definition: allocator.hpp:45
pmem::obj::persistent_ptr
Persistent pointer class.
Definition: persistent_ptr.hpp:183
pmem::obj::allocator::allocator
allocator(allocator< U, P, T2 > const &rhs)
Type converting constructor.
Definition: allocator.hpp:487
pmem::obj::standard_alloc_policy::standard_alloc_policy
standard_alloc_policy(standard_alloc_policy< U > const &)
Type converting constructor.
Definition: allocator.hpp:226
pmem::obj::object_traits::object_traits
object_traits()=default
Defaulted constructor.
pmem::obj::object_traits::~object_traits
~object_traits()=default
Defaulted destructor.
pext.hpp
Convenience extensions for the resides on pmem property template.
pmem::obj::standard_alloc_policy
The allocation policy template for a given type.
Definition: allocator.hpp:184
life.hpp
Functions for destroying arrays.
pmem::transaction_scope_error
Custom transaction error class.
Definition: pexceptions.hpp:158
pmem::obj::object_traits< void >::object_traits
object_traits(object_traits< U > const &)
Type converting constructor.
Definition: allocator.hpp:172
pmem::obj::standard_alloc_policy< void >::allocate
pointer allocate(size_type cnt, const_pointer=0)
Allocate storage for cnt bytes.
Definition: allocator.hpp:357
pmem::obj::allocator::rebind
Rebind to a different type.
Definition: allocator.hpp:452
pmem::obj::allocator::~allocator
~allocator()=default
Defaulted destructor.
pmem::obj::object_traits::construct
void construct(pointer p, const_reference t)
Create an object at a specific address.
Definition: allocator.hpp:82
pmem::obj::standard_alloc_policy< void >::max_size
size_type max_size() const
The largest value that can meaningfully be passed to allocate().
Definition: allocator.hpp:393
persistent_ptr.hpp
Persistent smart pointer.
pmem::obj::standard_alloc_policy< void >::standard_alloc_policy
standard_alloc_policy()=default
Defaulted constructor.
pmem::obj::allocator::allocator
allocator(allocator const &rhs)
Copy constructor.
Definition: allocator.hpp:471
pmem::transaction_alloc_error
Custom transaction error class.
Definition: pexceptions.hpp:101
pmem::obj::standard_alloc_policy::allocate
pointer allocate(size_type cnt, const_void_pointer=0)
Allocate storage for cnt objects of type T.
Definition: allocator.hpp:242
pmem::obj::standard_alloc_policy< void >::deallocate
void deallocate(pointer p, size_type=0)
Deallocates storage pointed to p, which must be a value returned by a previous call to allocate that ...
Definition: allocator.hpp:375
pmem::obj::standard_alloc_policy::max_size
size_type max_size() const
The largest value that can meaningfully be passed to allocate().
Definition: allocator.hpp:293
pmem::obj::standard_alloc_policy::standard_alloc_policy
standard_alloc_policy(standard_alloc_policy const &)
Explicit copy constructor.
Definition: allocator.hpp:216