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.

203 lines
5.6KB

  1. /*
  2. * High-level, real-time safe, templated, C++ doubly-linked list
  3. * Copyright (C) 2013-2014 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 size_t minPreallocated, const size_t maxPreallocated) noexcept
  35. : fHandle(nullptr),
  36. fDataSize(sizeof(typename AbstractLinkedList<T>::Data))
  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. rtsafe_memory_pool_deallocate(fHandle, dataPtr);
  59. }
  60. void resize(const size_t minPreallocated, const size_t maxPreallocated) noexcept
  61. {
  62. if (fHandle != nullptr)
  63. {
  64. rtsafe_memory_pool_destroy(fHandle);
  65. fHandle = nullptr;
  66. }
  67. rtsafe_memory_pool_create(&fHandle, nullptr, fDataSize, minPreallocated, maxPreallocated);
  68. CARLA_SAFE_ASSERT(fHandle != nullptr);
  69. }
  70. bool operator==(const Pool& pool) const noexcept
  71. {
  72. return (fHandle == pool.fHandle && fDataSize == pool.fDataSize);
  73. }
  74. bool operator!=(const Pool& pool) const noexcept
  75. {
  76. return (fHandle != pool.fHandle || fDataSize != pool.fDataSize);
  77. }
  78. private:
  79. mutable RtMemPool_Handle fHandle;
  80. const size_t fDataSize;
  81. };
  82. // -------------------------------------------------------------------
  83. // Now the actual rt-linkedlist code
  84. RtLinkedList(Pool& memPool, const bool needsCopyCtr = false) noexcept
  85. : AbstractLinkedList<T>(needsCopyCtr),
  86. fMemPool(memPool) {}
  87. bool append_sleepy(const T& value) noexcept
  88. {
  89. return _add_sleepy(value, true);
  90. }
  91. bool insert_sleepy(const T& value) noexcept
  92. {
  93. return _add_sleepy(value, false);
  94. }
  95. void resize(const size_t minPreallocated, const size_t maxPreallocated) noexcept
  96. {
  97. this->clear();
  98. fMemPool.resize(minPreallocated, maxPreallocated);
  99. }
  100. void spliceAppend(RtLinkedList& list, const bool init = true) noexcept
  101. {
  102. CARLA_SAFE_ASSERT_RETURN(fMemPool == list.fMemPool,);
  103. AbstractLinkedList<T>::spliceAppend(list, init);
  104. }
  105. void spliceInsert(RtLinkedList& list, const bool init = true) noexcept
  106. {
  107. CARLA_SAFE_ASSERT_RETURN(fMemPool == list.fMemPool,);
  108. AbstractLinkedList<T>::spliceInsert(list, init);
  109. }
  110. protected:
  111. typename AbstractLinkedList<T>::Data* _allocate() noexcept override
  112. {
  113. return (typename AbstractLinkedList<T>::Data*)fMemPool.allocate_atomic();
  114. }
  115. typename AbstractLinkedList<T>::Data* _allocate_sleepy() noexcept
  116. {
  117. return (typename AbstractLinkedList<T>::Data*)fMemPool.allocate_sleepy();
  118. }
  119. void _deallocate(typename AbstractLinkedList<T>::Data* const dataPtr) noexcept override
  120. {
  121. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr,);
  122. fMemPool.deallocate(dataPtr);
  123. }
  124. private:
  125. Pool& fMemPool;
  126. bool _add_sleepy(const T& value, const bool inTail) noexcept
  127. {
  128. if (typename AbstractLinkedList<T>::Data* const data = _allocate_sleepy())
  129. {
  130. if (this->fNeedsCopyCtr)
  131. {
  132. try {
  133. new(data)typename AbstractLinkedList<T>::Data();
  134. }
  135. catch(...) {
  136. _deallocate(data);
  137. return false;
  138. }
  139. try {
  140. data->value = value;
  141. }
  142. catch(...) {
  143. data->~Data();
  144. _deallocate(data);
  145. return false;
  146. }
  147. }
  148. else
  149. {
  150. std::memcpy(&data->value, &value, this->fDataSize);
  151. }
  152. if (inTail)
  153. list_add_tail(&data->siblings, &this->fQueue);
  154. else
  155. list_add(&data->siblings, &this->fQueue);
  156. ++(this->fCount);
  157. return true;
  158. }
  159. return false;
  160. }
  161. LINKED_LIST_DECLARATIONS(RtLinkedList)
  162. };
  163. // -----------------------------------------------------------------------
  164. #endif // RT_LINKED_LIST_HPP_INCLUDED