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.

217 lines
6.3KB

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