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.

RtList.hpp 4.7KB

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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * High-level, real-time safe, templated, C++ doubly-linked list
  3. * Copyright (C) 2013 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_LIST_HPP_INCLUDED
  18. #define RT_LIST_HPP_INCLUDED
  19. #include "List.hpp"
  20. extern "C" {
  21. #include "rtmempool/rtmempool.h"
  22. }
  23. // -----------------------------------------------------------------------
  24. // Realtime safe list
  25. template<typename T>
  26. class RtList : public AbstractList<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)
  35. : fHandle(nullptr),
  36. fDataSize(sizeof(typename AbstractList<T>::Data))
  37. {
  38. resize(minPreallocated, maxPreallocated);
  39. }
  40. ~Pool()
  41. {
  42. if (fHandle != nullptr)
  43. {
  44. rtsafe_memory_pool_destroy(fHandle);
  45. fHandle = nullptr;
  46. }
  47. }
  48. void* allocate_atomic() const
  49. {
  50. return rtsafe_memory_pool_allocate_atomic(fHandle);
  51. }
  52. void* allocate_sleepy() const
  53. {
  54. return rtsafe_memory_pool_allocate_sleepy(fHandle);
  55. }
  56. void deallocate(void* const dataPtr) const
  57. {
  58. rtsafe_memory_pool_deallocate(fHandle, dataPtr);
  59. }
  60. void resize(const size_t minPreallocated, const size_t maxPreallocated)
  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_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-list code
  84. RtList(Pool& memPool)
  85. : fMemPool(memPool)
  86. {
  87. }
  88. void append_sleepy(const T& value)
  89. {
  90. if (typename AbstractList<T>::Data* const data = _allocate_sleepy())
  91. {
  92. new(data)typename AbstractList<T>::Data();
  93. data->value = value;
  94. list_add_tail(&data->siblings, &this->fQueue);
  95. ++(this->fCount);
  96. }
  97. }
  98. void insert_sleepy(const T& value)
  99. {
  100. if (typename AbstractList<T>::Data* const data = _allocate_sleepy())
  101. {
  102. new(data)typename AbstractList<T>::Data();
  103. data->value = value;
  104. list_add(&data->siblings, &this->fQueue);
  105. ++(this->fCount);
  106. }
  107. }
  108. void resize(const size_t minPreallocated, const size_t maxPreallocated)
  109. {
  110. this->clear();
  111. fMemPool.resize(minPreallocated, maxPreallocated);
  112. }
  113. void spliceAppend(RtList& list, const bool init = true)
  114. {
  115. CARLA_ASSERT(fMemPool == list.fMemPool);
  116. AbstractList<T>::spliceAppend(list, init);
  117. }
  118. void spliceInsert(RtList& list, const bool init = true)
  119. {
  120. CARLA_ASSERT(fMemPool == list.fMemPool);
  121. AbstractList<T>::spliceInsert(list, init);
  122. }
  123. private:
  124. Pool& fMemPool;
  125. typename AbstractList<T>::Data* _allocate() override
  126. {
  127. return (typename AbstractList<T>::Data*)fMemPool.allocate_atomic();
  128. }
  129. typename AbstractList<T>::Data* _allocate_sleepy()
  130. {
  131. return (typename AbstractList<T>::Data*)fMemPool.allocate_sleepy();
  132. }
  133. void _deallocate(typename AbstractList<T>::Data*& dataPtr) override
  134. {
  135. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr,);
  136. fMemPool.deallocate(dataPtr);
  137. dataPtr = nullptr;
  138. }
  139. LIST_DECLARATIONS(RtList)
  140. };
  141. // -----------------------------------------------------------------------
  142. #endif // RT_LIST_HPP_INCLUDED