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.

147 lines
2.8KB

  1. //
  2. // detail/object_pool.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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>
  30. static void destroy(Object* o)
  31. {
  32. delete o;
  33. }
  34. template <typename Object>
  35. static Object*& next(Object* o)
  36. {
  37. return o->next_;
  38. }
  39. template <typename Object>
  40. static Object*& prev(Object* o)
  41. {
  42. return o->prev_;
  43. }
  44. };
  45. template <typename Object>
  46. class object_pool
  47. : private noncopyable
  48. {
  49. public:
  50. // Constructor.
  51. object_pool()
  52. : live_list_(0),
  53. free_list_(0)
  54. {
  55. }
  56. // Destructor destroys all objects.
  57. ~object_pool()
  58. {
  59. destroy_list(live_list_);
  60. destroy_list(free_list_);
  61. }
  62. // Get the object at the start of the live list.
  63. Object* first()
  64. {
  65. return live_list_;
  66. }
  67. // Allocate a new object.
  68. Object* alloc()
  69. {
  70. Object* o = free_list_;
  71. if (o)
  72. free_list_ = object_pool_access::next(free_list_);
  73. else
  74. o = object_pool_access::create<Object>();
  75. object_pool_access::next(o) = live_list_;
  76. object_pool_access::prev(o) = 0;
  77. if (live_list_)
  78. object_pool_access::prev(live_list_) = o;
  79. live_list_ = o;
  80. return o;
  81. }
  82. // Free an object. Moves it to the free list. No destructors are run.
  83. void free(Object* o)
  84. {
  85. if (live_list_ == o)
  86. live_list_ = object_pool_access::next(o);
  87. if (object_pool_access::prev(o))
  88. {
  89. object_pool_access::next(object_pool_access::prev(o))
  90. = object_pool_access::next(o);
  91. }
  92. if (object_pool_access::next(o))
  93. {
  94. object_pool_access::prev(object_pool_access::next(o))
  95. = object_pool_access::prev(o);
  96. }
  97. object_pool_access::next(o) = free_list_;
  98. object_pool_access::prev(o) = 0;
  99. free_list_ = o;
  100. }
  101. private:
  102. // Helper function to destroy all elements in a list.
  103. void destroy_list(Object* list)
  104. {
  105. while (list)
  106. {
  107. Object* o = list;
  108. list = object_pool_access::next(o);
  109. object_pool_access::destroy(o);
  110. }
  111. }
  112. // The list of live objects.
  113. Object* live_list_;
  114. // The free list.
  115. Object* free_list_;
  116. };
  117. } // namespace detail
  118. } // namespace asio
  119. #include "asio/detail/pop_options.hpp"
  120. #endif // ASIO_DETAIL_OBJECT_POOL_HPP