Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
3.3KB

  1. //
  2. // detail/object_pool.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_DETAIL_OBJECT_POOL_HPP
  11. #define ASIO_DETAIL_OBJECT_POOL_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/noncopyable.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. namespace asio {
  18. namespace detail {
  19. template <typename Object>
  20. class object_pool;
  21. class object_pool_access
  22. {
  23. public:
  24. template <typename Object>
  25. static Object* create()
  26. {
  27. return new Object;
  28. }
  29. template <typename Object, typename Arg>
  30. static Object* create(Arg arg)
  31. {
  32. return new Object(arg);
  33. }
  34. template <typename Object>
  35. static void destroy(Object* o)
  36. {
  37. delete o;
  38. }
  39. template <typename Object>
  40. static Object*& next(Object* o)
  41. {
  42. return o->next_;
  43. }
  44. template <typename Object>
  45. static Object*& prev(Object* o)
  46. {
  47. return o->prev_;
  48. }
  49. };
  50. template <typename Object>
  51. class object_pool
  52. : private noncopyable
  53. {
  54. public:
  55. // Constructor.
  56. object_pool()
  57. : live_list_(0),
  58. free_list_(0)
  59. {
  60. }
  61. // Destructor destroys all objects.
  62. ~object_pool()
  63. {
  64. destroy_list(live_list_);
  65. destroy_list(free_list_);
  66. }
  67. // Get the object at the start of the live list.
  68. Object* first()
  69. {
  70. return live_list_;
  71. }
  72. // Allocate a new object.
  73. Object* alloc()
  74. {
  75. Object* o = free_list_;
  76. if (o)
  77. free_list_ = object_pool_access::next(free_list_);
  78. else
  79. o = object_pool_access::create<Object>();
  80. object_pool_access::next(o) = live_list_;
  81. object_pool_access::prev(o) = 0;
  82. if (live_list_)
  83. object_pool_access::prev(live_list_) = o;
  84. live_list_ = o;
  85. return o;
  86. }
  87. // Allocate a new object with an argument.
  88. template <typename Arg>
  89. Object* alloc(Arg arg)
  90. {
  91. Object* o = free_list_;
  92. if (o)
  93. free_list_ = object_pool_access::next(free_list_);
  94. else
  95. o = object_pool_access::create<Object>(arg);
  96. object_pool_access::next(o) = live_list_;
  97. object_pool_access::prev(o) = 0;
  98. if (live_list_)
  99. object_pool_access::prev(live_list_) = o;
  100. live_list_ = o;
  101. return o;
  102. }
  103. // Free an object. Moves it to the free list. No destructors are run.
  104. void free(Object* o)
  105. {
  106. if (live_list_ == o)
  107. live_list_ = object_pool_access::next(o);
  108. if (object_pool_access::prev(o))
  109. {
  110. object_pool_access::next(object_pool_access::prev(o))
  111. = object_pool_access::next(o);
  112. }
  113. if (object_pool_access::next(o))
  114. {
  115. object_pool_access::prev(object_pool_access::next(o))
  116. = object_pool_access::prev(o);
  117. }
  118. object_pool_access::next(o) = free_list_;
  119. object_pool_access::prev(o) = 0;
  120. free_list_ = o;
  121. }
  122. private:
  123. // Helper function to destroy all elements in a list.
  124. void destroy_list(Object* list)
  125. {
  126. while (list)
  127. {
  128. Object* o = list;
  129. list = object_pool_access::next(o);
  130. object_pool_access::destroy(o);
  131. }
  132. }
  133. // The list of live objects.
  134. Object* live_list_;
  135. // The free list.
  136. Object* free_list_;
  137. };
  138. } // namespace detail
  139. } // namespace asio
  140. #include "asio/detail/pop_options.hpp"
  141. #endif // ASIO_DETAIL_OBJECT_POOL_HPP