PMDK C++ bindings  1.5.2
This is the C++ bindings documentation for PMDK's libpmemobj.
allocator.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2019, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
38 #ifndef LIBPMEMOBJ_CPP_ALLOCATOR_HPP
39 #define LIBPMEMOBJ_CPP_ALLOCATOR_HPP
40 
45 #include <libpmemobj++/pext.hpp>
46 #include <libpmemobj/tx_base.h>
47 
48 namespace pmem
49 {
50 
51 namespace obj
52 {
53 
58 template <typename T>
60 public:
61  /*
62  * Important typedefs.
63  */
64  using value_type = T;
67  using reference = value_type &;
68  using const_reference = const value_type &;
69 
73  template <class U>
74  struct rebind {
75  using other = object_traits<U>;
76  };
77 
81  object_traits() = default;
82 
86  ~object_traits() = default;
87 
91  template <typename U,
92  typename = typename std::enable_if<
93  std::is_convertible<U *, T *>::value>::type>
94  explicit object_traits(object_traits<U> const &)
95  {
96  }
97 
106  void
107  construct(pointer p, const_reference t)
108  {
109  /* construct called on newly allocated objects */
110  detail::conditional_add_to_tx(p.get());
111  new (static_cast<void *>(p.get())) value_type(t);
112  }
113 
122  template <typename... Args>
123  void
124  construct(pointer p, Args &&... args)
125  {
126  detail::conditional_add_to_tx(p.get());
127  new (static_cast<void *>(p.get()))
128  value_type(std::forward<Args>(args)...);
129  }
130 
138  void
140  {
141  /* XXX should we allow modifications outside of tx? */
142  if (pmemobj_tx_stage() == TX_STAGE_WORK) {
143  pmemobj_tx_add_range_direct((void *)p.get(), sizeof(p));
144  }
145 
146  detail::destroy<value_type>(*p);
147  }
148 };
149 
154 template <>
155 class object_traits<void> {
156 public:
157  /*
158  * Important typedefs.
159  */
160  using value_type = void;
162 
166  template <class U>
167  struct rebind {
168  using other = object_traits<U>;
169  };
170 
174  object_traits() = default;
175 
179  ~object_traits() = default;
180 
184  template <typename U>
185  explicit object_traits(object_traits<U> const &)
186  {
187  }
188 };
189 
196 template <typename T>
198 public:
199  /*
200  * Important typedefs.
201  */
202  using value_type = T;
204  using const_void_pointer = persistent_ptr<const void>;
205  using size_type = std::size_t;
206  using bool_type = bool;
207 
211  template <class U>
212  struct rebind {
214  };
215 
220 
225 
230  {
231  }
232 
236  template <typename U,
237  typename = typename std::enable_if<
238  std::is_convertible<U *, T *>::value>::type>
240  {
241  }
242 
251  pointer
252  allocate(size_type cnt, const_void_pointer = 0)
253  {
254  if (pmemobj_tx_stage() != TX_STAGE_WORK)
256  "refusing to allocate "
257  "memory outside of transaction scope");
258 
259  /* allocate raw memory, no object construction */
260  return pmemobj_tx_alloc(sizeof(value_type) * cnt,
261  detail::type_num<T>());
262  }
263 
271  void
272  deallocate(pointer p, size_type = 0)
273  {
274  if (pmemobj_tx_stage() != TX_STAGE_WORK)
276  "refusing to free "
277  "memory outside of transaction scope");
278 
279  if (pmemobj_tx_free(*p.raw_ptr()) != 0)
281  "failed to delete "
282  "persistent memory object");
283  }
284 
290  size_type
291  max_size() const
292  {
293  return PMEMOBJ_MAX_ALLOC_SIZE / sizeof(value_type);
294  }
295 };
296 
300 template <>
302 public:
303  /*
304  * Important typedefs.
305  */
306  using value_type = void;
309  using reference = value_type;
310  using const_reference = const value_type;
311  using size_type = std::size_t;
312  using bool_type = bool;
313 
317  template <class U>
318  struct rebind {
320  };
321 
326 
331 
336  {
337  }
338 
342  template <typename U>
344  {
345  }
346 
354  pointer
355  allocate(size_type cnt, const_pointer = 0)
356  {
357  if (pmemobj_tx_stage() != TX_STAGE_WORK)
359  "refusing to allocate "
360  "memory outside of transaction scope");
361 
362  /* allocate raw memory, no object construction */
363  return pmemobj_tx_alloc(1 /* void size */ * cnt, 0);
364  }
365 
373  void
374  deallocate(pointer p, size_type = 0)
375  {
376  if (pmemobj_tx_stage() != TX_STAGE_WORK)
378  "refusing to free "
379  "memory outside of transaction scope");
380 
381  if (pmemobj_tx_free(p.raw()) != 0)
383  "failed to delete "
384  "persistent memory object");
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  explicit 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:343
pmem::obj::object_traits::construct
void construct(pointer p, Args &&... args)
Create an object at a specific address.
Definition: allocator.hpp:124
pmem::transaction_free_error
Custom transaction error class.
Definition: pexceptions.hpp:94
pmem::obj::allocator::allocator
allocator()=default
Defaulted constructor.
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::p
Resides on pmem class.
Definition: p.hpp:64
pmem::obj::standard_alloc_policy::rebind
Rebind to a different type.
Definition: allocator.hpp:212
pmem::obj::object_traits::object_traits
object_traits(object_traits< U > const &)
Type converting constructor.
Definition: allocator.hpp:94
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::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:139
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:335
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:272
pmem::obj::object_traits
Encapsulates object specific allocator functionality.
Definition: allocator.hpp:59
pmem::obj::object_traits::rebind
Rebind to a different type.
Definition: allocator.hpp:74
pmem::obj::persistent_ptr
Persistent pointer class.
Definition: persistent_ptr.hpp:132
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:239
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::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::standard_alloc_policy
The allocation policy template for a given type.
Definition: allocator.hpp:197
life.hpp
Functions for destroying arrays.
pmem::transaction_scope_error
Custom transaction error class.
Definition: pexceptions.hpp:104
pmem::obj::object_traits< void >::object_traits
object_traits(object_traits< U > const &)
Type converting constructor.
Definition: allocator.hpp:185
pmem::obj::standard_alloc_policy< void >::allocate
pointer allocate(size_type cnt, const_pointer=0)
Allocate storage for cnt bytes.
Definition: allocator.hpp:355
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:107
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)
Explicit copy constructor.
Definition: allocator.hpp:471
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:252
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:374
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:291
pmem::obj::standard_alloc_policy::standard_alloc_policy
standard_alloc_policy(standard_alloc_policy const &)
Explicit copy constructor.
Definition: allocator.hpp:229