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.

RtLinkedList.hpp 6.6KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * High-level, real-time safe, templated, C++ doubly-linked list
  3. * Copyright (C) 2013-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef RT_LINKED_LIST_HPP_INCLUDED
  18. #define RT_LINKED_LIST_HPP_INCLUDED
  19. #include "LinkedList.hpp"
  20. extern "C" {
  21. #include "rtmempool/rtmempool.h"
  22. }
  23. // -----------------------------------------------------------------------
  24. // Realtime safe linkedlist
  25. template<typename T>
  26. class RtLinkedList : public AbstractLinkedList<T>
  27. {
  28. public:
  29. // -------------------------------------------------------------------
  30. // RtMemPool C++ class
  31. class Pool
  32. {
  33. public:
  34. Pool(const std::size_t minPreallocated, const std::size_t maxPreallocated) noexcept
  35. : kDataSize(sizeof(typename AbstractLinkedList<T>::Data)),
  36. fHandle(nullptr)
  37. {
  38. resize(minPreallocated, maxPreallocated);
  39. }
  40. ~Pool() noexcept
  41. {
  42. if (fHandle != nullptr)
  43. {
  44. rtsafe_memory_pool_destroy(fHandle);
  45. fHandle = nullptr;
  46. }
  47. }
  48. void* allocate_atomic() const noexcept
  49. {
  50. return rtsafe_memory_pool_allocate_atomic(fHandle);
  51. }
  52. void* allocate_sleepy() const noexcept
  53. {
  54. return rtsafe_memory_pool_allocate_sleepy(fHandle);
  55. }
  56. void deallocate(void* const dataPtr) const noexcept
  57. {
  58. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr,);
  59. rtsafe_memory_pool_deallocate(fHandle, dataPtr);
  60. }
  61. void resize(const std::size_t minPreallocated, const std::size_t maxPreallocated) noexcept
  62. {
  63. if (fHandle != nullptr)
  64. {
  65. rtsafe_memory_pool_destroy(fHandle);
  66. fHandle = nullptr;
  67. }
  68. rtsafe_memory_pool_create(&fHandle, nullptr, kDataSize, minPreallocated, maxPreallocated);
  69. CARLA_SAFE_ASSERT(fHandle != nullptr);
  70. }
  71. bool operator==(const Pool& pool) const noexcept
  72. {
  73. return (fHandle == pool.fHandle && kDataSize == pool.kDataSize);
  74. }
  75. bool operator!=(const Pool& pool) const noexcept
  76. {
  77. return (fHandle != pool.fHandle || kDataSize != pool.kDataSize);
  78. }
  79. private:
  80. const std::size_t kDataSize;
  81. mutable RtMemPool_Handle fHandle;
  82. CARLA_PREVENT_HEAP_ALLOCATION
  83. CARLA_DECLARE_NON_COPY_CLASS(Pool)
  84. };
  85. // -------------------------------------------------------------------
  86. // Now the actual rt-linkedlist code
  87. RtLinkedList(Pool& memPool) noexcept
  88. : fMemPool(memPool) {}
  89. #ifdef STOAT_TEST_BUILD
  90. // overridden for stoat
  91. bool append(const T& value) noexcept
  92. {
  93. if (typename AbstractLinkedList<T>::Data* const data = _allocate())
  94. return this->_add_internal(data, value, true, &this->fQueue);
  95. return false;
  96. }
  97. void clear() noexcept
  98. {
  99. if (this->fCount == 0)
  100. return;
  101. for (typename AbstractLinkedList<T>::ListHead *entry = this->fQueue.next, *entry2 = entry->next;
  102. entry != &this->fQueue; entry = entry2, entry2 = entry->next)
  103. {
  104. typename AbstractLinkedList<T>::Data* const data = list_entry(entry, typename AbstractLinkedList<T>::Data, siblings);
  105. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  106. this->_deallocate(data);
  107. }
  108. this->_init();
  109. }
  110. T getFirst(T& fallback, const bool removeObj) noexcept
  111. {
  112. CARLA_SAFE_ASSERT_RETURN(this->fCount > 0, fallback);
  113. typename AbstractLinkedList<T>::ListHead* const entry = this->fQueue.next;
  114. typename AbstractLinkedList<T>::Data* const data = list_entry(entry, typename AbstractLinkedList<T>::Data, siblings);
  115. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  116. if (! removeObj)
  117. return data->value;
  118. const T value(data->value);
  119. _deleteRT(entry, data);
  120. return value;
  121. }
  122. void _deleteRT(typename AbstractLinkedList<T>::ListHead* const entry, typename AbstractLinkedList<T>::Data* const data) noexcept
  123. {
  124. CARLA_SAFE_ASSERT_RETURN(entry != nullptr,);
  125. CARLA_SAFE_ASSERT_RETURN(entry->prev != nullptr,);
  126. CARLA_SAFE_ASSERT_RETURN(entry->next != nullptr,);
  127. --this->fCount;
  128. entry->next->prev = entry->prev;
  129. entry->prev->next = entry->next;
  130. entry->next = nullptr;
  131. entry->prev = nullptr;
  132. _deallocate(data);
  133. }
  134. #endif
  135. bool append_sleepy(const T& value) noexcept
  136. {
  137. return _add_sleepy(value, true);
  138. }
  139. bool insert_sleepy(const T& value) noexcept
  140. {
  141. return _add_sleepy(value, false);
  142. }
  143. void resize(const std::size_t minPreallocated, const std::size_t maxPreallocated) noexcept
  144. {
  145. CARLA_SAFE_ASSERT(this->fCount == 0);
  146. this->clear();
  147. fMemPool.resize(minPreallocated, maxPreallocated);
  148. }
  149. bool moveTo(RtLinkedList<T>& list, const bool inTail) noexcept
  150. {
  151. CARLA_SAFE_ASSERT_RETURN(fMemPool == list.fMemPool, false);
  152. return AbstractLinkedList<T>::moveTo(list, inTail);
  153. }
  154. protected:
  155. typename AbstractLinkedList<T>::Data* _allocate() noexcept override
  156. {
  157. return (typename AbstractLinkedList<T>::Data*)fMemPool.allocate_atomic();
  158. }
  159. typename AbstractLinkedList<T>::Data* _allocate_sleepy() noexcept
  160. {
  161. return (typename AbstractLinkedList<T>::Data*)fMemPool.allocate_sleepy();
  162. }
  163. void _deallocate(typename AbstractLinkedList<T>::Data* const dataPtr) noexcept override
  164. {
  165. fMemPool.deallocate(dataPtr);
  166. }
  167. private:
  168. Pool& fMemPool;
  169. bool _add_sleepy(const T& value, const bool inTail) noexcept
  170. {
  171. if (typename AbstractLinkedList<T>::Data* const data = _allocate_sleepy())
  172. return this->_add_internal(data, value, inTail, &this->fQueue);
  173. return false;
  174. }
  175. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  176. CARLA_DECLARE_NON_COPY_CLASS(RtLinkedList)
  177. };
  178. // -----------------------------------------------------------------------
  179. #endif // RT_LINKED_LIST_HPP_INCLUDED