PMDK C++ bindings  1.13.0-git107.g7e59f08f
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-2021, 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 
30 template <typename T>
32 public:
33  /*
34  * Important typedefs.
35  */
36  using value_type = T;
39  using reference = value_type &;
40  using const_reference = const value_type &;
41 
45  template <class U>
46  struct rebind {
47  using other = object_traits<U>;
48  };
49 
53  object_traits() = default;
54 
58  ~object_traits() = default;
59 
63  template <typename U,
64  typename = typename std::enable_if<
65  std::is_convertible<U *, T *>::value>::type>
66  explicit object_traits(object_traits<U> const &)
67  {
68  }
69 
82  void
83  construct(pointer p, const_reference t)
84  {
85  if (pmemobj_tx_stage() != TX_STAGE_WORK)
87  "construct is called outside of transaction scope");
88 
89  /* construct called on newly allocated objects */
91  new (static_cast<void *>(p.get())) value_type(t);
92  }
93 
106  template <typename... Args>
107  void
108  construct(pointer p, Args &&... args)
109  {
110  if (pmemobj_tx_stage() != TX_STAGE_WORK)
112  "construct is called outside of transaction scope");
113 
115  new (static_cast<void *>(p.get()))
116  value_type(std::forward<Args>(args)...);
117  }
118 
126  void
128  {
129  /* XXX should we allow modifications outside of tx? */
130  if (pmemobj_tx_stage() == TX_STAGE_WORK) {
131  pmemobj_tx_add_range_direct((void *)p.get(), sizeof(p));
132  }
133 
134  detail::destroy<value_type>(*p);
135  }
136 };
137 
144 template <>
145 class object_traits<void> {
146 public:
147  /*
148  * Important typedefs.
149  */
150  using value_type = void;
152 
156  template <class U>
157  struct rebind {
158  using other = object_traits<U>;
159  };
160 
164  object_traits() = default;
165 
169  ~object_traits() = default;
170 
174  template <typename U>
175  explicit object_traits(object_traits<U> const &)
176  {
177  }
178 };
179 
187 template <typename T>
189 public:
190  /*
191  * Important typedefs.
192  */
193  using value_type = T;
196  using size_type = std::size_t;
197  using bool_type = bool;
198 
202  template <class U>
203  struct rebind {
205  };
206 
211 
216 
221  {
222  }
223 
227  template <typename U,
228  typename = typename std::enable_if<
229  std::is_convertible<U *, T *>::value>::type>
231  {
232  }
233 
246  pointer
247  allocate(size_type cnt, const_void_pointer = 0)
248  {
249  if (pmemobj_tx_stage() != TX_STAGE_WORK)
251  "refusing to allocate memory outside of transaction scope");
252 
253  /* allocate raw memory, no object construction */
254  pointer ptr = pmemobj_tx_alloc(sizeof(value_type) * cnt,
255  detail::type_num<value_type>());
256 
257  if (ptr == nullptr) {
258  const char *msg =
259  "Failed to allocate persistent memory object";
260  if (errno == ENOMEM) {
263  } else {
266  }
267  }
268 
269  return ptr;
270  }
271 
283  void
284  deallocate(pointer p, size_type = 0)
285  {
286  if (pmemobj_tx_stage() != TX_STAGE_WORK)
288  "refusing to free memory outside of transaction scope");
289 
290  if (pmemobj_tx_free(*p.raw_ptr()) != 0)
293  "failed to delete persistent memory object");
294  }
295 
301  size_type
302  max_size() const
303  {
304  return PMEMOBJ_MAX_ALLOC_SIZE / sizeof(value_type);
305  }
306 };
307 
312 template <>
314 public:
315  /*
316  * Important typedefs.
317  */
318  using value_type = void;
321  using reference = value_type;
322  using const_reference = const value_type;
323  using size_type = std::size_t;
324  using bool_type = bool;
325 
329  template <class U>
330  struct rebind {
332  };
333 
338 
343 
348  {
349  }
350 
354  template <typename U>
356  {
357  }
358 
370  pointer
371  allocate(size_type cnt, const_pointer = 0)
372  {
373  if (pmemobj_tx_stage() != TX_STAGE_WORK)
375  "refusing to allocate memory outside of transaction scope");
376 
377  /* allocate raw memory, no object construction */
378  pointer ptr = pmemobj_tx_alloc(1 /* void size */ * cnt, 0);
379 
380  if (ptr == nullptr) {
381  const char *msg =
382  "Failed to allocate persistent memory object";
383  if (errno == ENOMEM) {
386  } else {
389  }
390  }
391 
392  return ptr;
393  }
394 
406  void
407  deallocate(pointer p, size_type = 0)
408  {
409  if (pmemobj_tx_stage() != TX_STAGE_WORK)
411  "refusing to free memory outside of transaction scope");
412 
413  if (pmemobj_tx_free(p.raw()) != 0)
416  "failed to delete persistent memory object");
417  }
418 
424  size_type
425  max_size() const
426  {
427  return PMEMOBJ_MAX_ALLOC_SIZE;
428  }
429 };
430 
437 template <typename T, typename T2>
438 inline bool
440 {
441  return true;
442 }
443 
450 template <typename T, typename OtherAllocator>
451 inline bool
452 operator==(standard_alloc_policy<T> const &, OtherAllocator const &)
453 {
454  return false;
455 }
456 
465 template <typename T, typename Policy = standard_alloc_policy<T>,
466  typename Traits = object_traits<T>>
467 class allocator : public Policy, public Traits {
468 private:
469  /*
470  * private typedefs
471  */
472  using AllocationPolicy = Policy;
473  using TTraits = Traits;
474 
475 public:
476  /*
477  * Important typedefs.
478  */
479  using size_type = typename AllocationPolicy::size_type;
480  using pointer = typename AllocationPolicy::pointer;
481  using value_type = typename AllocationPolicy::value_type;
482 
486  template <typename U>
487  struct rebind {
488  using other = allocator<
489  U, typename AllocationPolicy::template rebind<U>::other,
490  typename TTraits::template rebind<U>::other>;
491  };
492 
496  allocator() = default;
497 
501  ~allocator() = default;
502 
506  allocator(allocator const &rhs) : Policy(rhs), Traits(rhs)
507  {
508  }
509 
513  template <typename U>
514  explicit allocator(allocator<U> const &)
515  {
516  }
517 
521  template <typename U, typename P, typename T2>
522  explicit allocator(allocator<U, P, T2> const &rhs)
523  : Policy(rhs), Traits(rhs)
524  {
525  }
526 };
527 
538 template <typename T, typename P, typename Tr, typename T2, typename P2,
539  typename Tr2>
540 inline bool
542 {
543  return operator==(static_cast<const P &>(lhs),
544  static_cast<const P2 &>(rhs));
545 }
546 
557 template <typename T, typename P, typename Tr, typename OtherAllocator>
558 inline bool
559 operator!=(const allocator<T, P, Tr> &lhs, const OtherAllocator &rhs)
560 {
561  return !operator==(lhs, rhs);
562 }
563 
564 } /* namespace obj */
565 
566 } /* namespace pmem */
567 
568 #endif /* LIBPMEMOBJ_CPP_ALLOCATOR_HPP */
(EXPERIMENTAL) Encapsulates the information about the persistent memory allocation model using PMDK's...
Definition: allocator.hpp:467
allocator()=default
Defaulted constructor.
~allocator()=default
Defaulted destructor.
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:559
bool operator==(const allocator< T, P, Tr > &lhs, const allocator< T2, P2, Tr2 > &rhs)
Determines if memory from another allocator can be deallocated from this one.
Definition: allocator.hpp:541
allocator(allocator< U, P, T2 > const &rhs)
Type converting constructor.
Definition: allocator.hpp:522
allocator(allocator< U > const &)
Type converting constructor.
Definition: allocator.hpp:514
allocator(allocator const &rhs)
Copy constructor.
Definition: allocator.hpp:506
object_traits()=default
Defaulted constructor.
~object_traits()=default
Defaulted destructor.
object_traits(object_traits< U > const &)
Type converting constructor.
Definition: allocator.hpp:175
Encapsulates object specific allocator functionality.
Definition: allocator.hpp:31
object_traits()=default
Defaulted constructor.
object_traits(object_traits< U > const &)
Type converting constructor.
Definition: allocator.hpp:66
void destroy(pointer p)
Destroy an object based on a pointer.
Definition: allocator.hpp:127
void construct(pointer p, const_reference t)
Create an object at a specific address.
Definition: allocator.hpp:83
~object_traits()=default
Defaulted destructor.
void construct(pointer p, Args &&... args)
Create an object at a specific address.
Definition: allocator.hpp:108
Resides on pmem class.
Definition: p.hpp:36
persistent_ptr const void specialization.
Definition: persistent_ptr.hpp:88
Persistent pointer class.
Definition: persistent_ptr.hpp:153
size_type max_size() const
The largest value that can meaningfully be passed to allocate().
Definition: allocator.hpp:425
standard_alloc_policy(standard_alloc_policy< U > const &)
Type converting constructor.
Definition: allocator.hpp:355
standard_alloc_policy(standard_alloc_policy const &)
Explicit copy constructor.
Definition: allocator.hpp:347
pointer allocate(size_type cnt, const_pointer=0)
Allocate storage for cnt bytes.
Definition: allocator.hpp:371
~standard_alloc_policy()=default
Defaulted destructor.
standard_alloc_policy()=default
Defaulted constructor.
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:407
The allocation policy template for a given type.
Definition: allocator.hpp:188
size_type max_size() const
The largest value that can meaningfully be passed to allocate().
Definition: allocator.hpp:302
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:439
~standard_alloc_policy()=default
Defaulted destructor.
pointer allocate(size_type cnt, const_void_pointer=0)
Allocate storage for cnt objects of type T.
Definition: allocator.hpp:247
bool operator==(standard_alloc_policy< T > const &, OtherAllocator const &)
Determines if memory from another allocator can be deallocated from this one.
Definition: allocator.hpp:452
standard_alloc_policy()=default
Defaulted constructor.
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:284
standard_alloc_policy(standard_alloc_policy< U > const &)
Type converting constructor.
Definition: allocator.hpp:230
standard_alloc_policy(standard_alloc_policy const &)
Explicit copy constructor.
Definition: allocator.hpp:220
Custom transaction error class.
Definition: pexceptions.hpp:132
Custom transaction error class.
Definition: pexceptions.hpp:156
Custom out of memory error class.
Definition: pexceptions.hpp:144
Custom transaction error class.
Definition: pexceptions.hpp:167
Commonly used functionality.
Functions for lifetime management.
void conditional_add_to_tx(const T *that, std::size_t count=1, uint64_t flags=0)
Conditionally add 'count' objects to a transaction.
Definition: common.hpp:176
ExcT exception_with_errormsg(Args &&... args)
Generic error message decorator for pmemobj-based exceptions.
Definition: pexceptions.hpp:69
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Persistent smart pointer.
Custom pmem exceptions.
Convenience extensions for the resides on pmem property template.
Rebind to a different type.
Definition: allocator.hpp:487
Rebind to a different type.
Definition: allocator.hpp:46
Rebind to a different type.
Definition: allocator.hpp:203