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.

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