PageRenderTime 72ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/external/immer/immer/memory_policy.hpp

https://github.com/bredelings/BAli-Phy
C++ Header | 142 lines | 77 code | 17 blank | 48 comment | 0 complexity | e17c90f2a7313e499caaa0ffad0b107c MD5 | raw file
  1. //
  2. // immer: immutable data structures for C++
  3. // Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
  4. //
  5. // This software is distributed under the Boost Software License, Version 1.0.
  6. // See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
  7. //
  8. #pragma once
  9. #include <immer/heap/cpp_heap.hpp>
  10. #include <immer/heap/heap_policy.hpp>
  11. #include <immer/lock/no_lock_policy.hpp>
  12. #include <immer/lock/spinlock_policy.hpp>
  13. #include <immer/refcount/no_refcount_policy.hpp>
  14. #include <immer/refcount/refcount_policy.hpp>
  15. #include <immer/refcount/unsafe_refcount_policy.hpp>
  16. #include <immer/transience/gc_transience_policy.hpp>
  17. #include <immer/transience/no_transience_policy.hpp>
  18. #include <type_traits>
  19. namespace immer {
  20. /*!
  21. * Metafunction that returns the best *transience policy* to use for a
  22. * given *refcount policy*.
  23. */
  24. template <typename RefcountPolicy>
  25. struct get_transience_policy
  26. : std::conditional<std::is_same<RefcountPolicy, no_refcount_policy>::value,
  27. gc_transience_policy,
  28. no_transience_policy>
  29. {};
  30. template <typename T>
  31. using get_transience_policy_t = typename get_transience_policy<T>::type;
  32. /*!
  33. * Metafunction that returns wether to *prefer fewer bigger objects*
  34. * to use for a given *heap policy*.
  35. */
  36. template <typename HeapPolicy>
  37. struct get_prefer_fewer_bigger_objects
  38. : std::integral_constant<
  39. bool,
  40. std::is_same<HeapPolicy, heap_policy<cpp_heap>>::value>
  41. {};
  42. template <typename T>
  43. constexpr auto get_prefer_fewer_bigger_objects_v =
  44. get_prefer_fewer_bigger_objects<T>::value;
  45. /*!
  46. * Metafunction that returns wether to use *transient R-Values*
  47. * for a given *refcount policy*.
  48. */
  49. template <typename RefcountPolicy>
  50. struct get_use_transient_rvalues
  51. : std::integral_constant<
  52. bool,
  53. !std::is_same<RefcountPolicy, no_refcount_policy>::value>
  54. {};
  55. template <typename T>
  56. constexpr auto get_use_transient_rvalues_v =
  57. get_use_transient_rvalues<T>::value;
  58. /*!
  59. * This is a default implementation of a *memory policy*. A memory
  60. * policy is just a bag of other policies plus some flags with hints
  61. * to the user about the best way to use these strategies.
  62. *
  63. * @tparam HeapPolicy A *heap policy*, for example, @ref heap_policy.
  64. * @tparam RefcountPolicy A *reference counting policy*, for example,
  65. * @ref refcount_policy.
  66. * @tparam TransiencePolicy A *transience policy*, for example,
  67. * @ref no_transience_policy.
  68. * @tparam PreferFewerBiggerObjects Boolean flag indicating whether
  69. * the user should prefer to allocate memory in bigger chunks
  70. * --e.g. by putting various objects in the same memory
  71. * region-- or not.
  72. * @tparam UseTransientRValues Boolean flag indicating whether
  73. * immutable containers should try to modify contents in-place
  74. * when manipulating an r-value reference.
  75. */
  76. template <typename HeapPolicy,
  77. typename RefcountPolicy,
  78. typename LockPolicy,
  79. typename TransiencePolicy = get_transience_policy_t<RefcountPolicy>,
  80. bool PreferFewerBiggerObjects =
  81. get_prefer_fewer_bigger_objects_v<HeapPolicy>,
  82. bool UseTransientRValues =
  83. get_use_transient_rvalues_v<RefcountPolicy>>
  84. struct memory_policy
  85. {
  86. using heap = HeapPolicy;
  87. using refcount = RefcountPolicy;
  88. using transience = TransiencePolicy;
  89. using lock = LockPolicy;
  90. static constexpr bool prefer_fewer_bigger_objects =
  91. PreferFewerBiggerObjects;
  92. static constexpr bool use_transient_rvalues = UseTransientRValues;
  93. using transience_t = typename transience::template apply<heap>::type;
  94. };
  95. /*!
  96. * The default *heap policy* just uses the standard heap with a
  97. * @ref free_list_heap_policy. If `IMMER_NO_FREE_LIST` is defined to `1`
  98. * then it just uses the standard heap.
  99. */
  100. #if IMMER_NO_FREE_LIST
  101. using default_heap_policy = heap_policy<debug_size_heap<cpp_heap>>;
  102. #else
  103. #if IMMER_NO_THREAD_SAFETY
  104. using default_heap_policy = unsafe_free_list_heap_policy<cpp_heap>;
  105. #else
  106. using default_heap_policy = free_list_heap_policy<cpp_heap>;
  107. #endif
  108. #endif
  109. /*!
  110. * By default we use thread safe reference counting.
  111. */
  112. #if IMMER_NO_THREAD_SAFETY
  113. using default_refcount_policy = unsafe_refcount_policy;
  114. using default_lock_policy = no_lock_policy;
  115. #else
  116. using default_refcount_policy = refcount_policy;
  117. using default_lock_policy = spinlock_policy;
  118. #endif
  119. /*!
  120. * The default memory policy.
  121. */
  122. using default_memory_policy = memory_policy<default_heap_policy,
  123. default_refcount_policy,
  124. default_lock_policy>;
  125. } // namespace immer