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.

213 lines
6.1KB

  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. /* full path as IDE Helper */
  22. #include "../modules/rtmempool/rtmempool.h"
  23. }
  24. // -----------------------------------------------------------------------
  25. // Realtime safe linkedlist
  26. template<typename T>
  27. class RtLinkedList : public AbstractLinkedList<T>
  28. {
  29. public:
  30. // -------------------------------------------------------------------
  31. // RtMemPool C++ class
  32. class Pool
  33. {
  34. public:
  35. Pool(const std::size_t minPreallocated, const std::size_t maxPreallocated) noexcept
  36. : kDataSize(sizeof(typename AbstractLinkedList<T>::Data)),
  37. fHandle(nullptr)
  38. {
  39. rtsafe_memory_pool_create(&fHandle, nullptr, kDataSize, minPreallocated, maxPreallocated);
  40. CARLA_SAFE_ASSERT(fHandle != nullptr);
  41. }
  42. ~Pool() noexcept
  43. {
  44. if (fHandle != nullptr)
  45. {
  46. rtsafe_memory_pool_destroy(fHandle);
  47. fHandle = nullptr;
  48. }
  49. }
  50. void* allocate_atomic() const noexcept
  51. {
  52. return rtsafe_memory_pool_allocate_atomic(fHandle);
  53. }
  54. void* allocate_sleepy() const noexcept
  55. {
  56. return rtsafe_memory_pool_allocate_sleepy(fHandle);
  57. }
  58. void deallocate(void* const dataPtr) const noexcept
  59. {
  60. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr,);
  61. rtsafe_memory_pool_deallocate(fHandle, dataPtr);
  62. }
  63. bool operator==(const Pool& pool) const noexcept
  64. {
  65. return (fHandle == pool.fHandle && kDataSize == pool.kDataSize);
  66. }
  67. bool operator!=(const Pool& pool) const noexcept
  68. {
  69. return (fHandle != pool.fHandle || kDataSize != pool.kDataSize);
  70. }
  71. private:
  72. const std::size_t kDataSize;
  73. mutable RtMemPool_Handle fHandle;
  74. CARLA_PREVENT_HEAP_ALLOCATION
  75. CARLA_DECLARE_NON_COPY_CLASS(Pool)
  76. };
  77. // -------------------------------------------------------------------
  78. // Now the actual rt-linkedlist code
  79. RtLinkedList(Pool& memPool) noexcept
  80. : fMemPool(memPool) {}
  81. #ifdef STOAT_TEST_BUILD
  82. // overridden for stoat
  83. bool append(const T& value) noexcept
  84. {
  85. if (typename AbstractLinkedList<T>::Data* const data = _allocate())
  86. return this->_add_internal(data, value, true, &this->fQueue);
  87. return false;
  88. }
  89. void clear() noexcept
  90. {
  91. if (this->fCount == 0)
  92. return;
  93. for (typename AbstractLinkedList<T>::ListHead *entry = this->fQueue.next, *entry2 = entry->next;
  94. entry != &this->fQueue; entry = entry2, entry2 = entry->next)
  95. {
  96. typename AbstractLinkedList<T>::Data* const data = list_entry(entry, typename AbstractLinkedList<T>::Data, siblings);
  97. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  98. this->_deallocate(data);
  99. }
  100. this->_init();
  101. }
  102. T getFirst(T& fallback, const bool removeObj) noexcept
  103. {
  104. CARLA_SAFE_ASSERT_RETURN(this->fCount > 0, fallback);
  105. typename AbstractLinkedList<T>::ListHead* const entry = this->fQueue.next;
  106. typename AbstractLinkedList<T>::Data* const data = list_entry(entry, typename AbstractLinkedList<T>::Data, siblings);
  107. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  108. if (! removeObj)
  109. return data->value;
  110. const T value(data->value);
  111. _deleteRT(entry, data);
  112. return value;
  113. }
  114. void _deleteRT(typename AbstractLinkedList<T>::ListHead* const entry, typename AbstractLinkedList<T>::Data* const data) noexcept
  115. {
  116. CARLA_SAFE_ASSERT_RETURN(entry != nullptr,);
  117. CARLA_SAFE_ASSERT_RETURN(entry->prev != nullptr,);
  118. CARLA_SAFE_ASSERT_RETURN(entry->next != nullptr,);
  119. --this->fCount;
  120. entry->next->prev = entry->prev;
  121. entry->prev->next = entry->next;
  122. entry->next = nullptr;
  123. entry->prev = nullptr;
  124. _deallocate(data);
  125. }
  126. #endif
  127. bool append_sleepy(const T& value) noexcept
  128. {
  129. return _add_sleepy(value, true);
  130. }
  131. bool insert_sleepy(const T& value) noexcept
  132. {
  133. return _add_sleepy(value, false);
  134. }
  135. bool moveTo(AbstractLinkedList<T>& list, const bool inTail) noexcept override
  136. {
  137. CARLA_SAFE_ASSERT_RETURN(((RtLinkedList&)list).fMemPool == fMemPool, false);
  138. return AbstractLinkedList<T>::moveTo(list, inTail);
  139. }
  140. protected:
  141. typename AbstractLinkedList<T>::Data* _allocate() noexcept override
  142. {
  143. return (typename AbstractLinkedList<T>::Data*)fMemPool.allocate_atomic();
  144. }
  145. typename AbstractLinkedList<T>::Data* _allocate_sleepy() noexcept
  146. {
  147. return (typename AbstractLinkedList<T>::Data*)fMemPool.allocate_sleepy();
  148. }
  149. void _deallocate(typename AbstractLinkedList<T>::Data* const dataPtr) noexcept override
  150. {
  151. fMemPool.deallocate(dataPtr);
  152. }
  153. private:
  154. Pool& fMemPool;
  155. bool _add_sleepy(const T& value, const bool inTail) noexcept
  156. {
  157. if (typename AbstractLinkedList<T>::Data* const data = _allocate_sleepy())
  158. return this->_add_internal(data, value, inTail, &this->fQueue);
  159. return false;
  160. }
  161. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  162. CARLA_DECLARE_NON_COPY_CLASS(RtLinkedList)
  163. };
  164. // -----------------------------------------------------------------------
  165. #endif // RT_LINKED_LIST_HPP_INCLUDED