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.

417 lines
9.3KB

  1. /*
  2. * High-level, 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 LINKED_LIST_HPP_INCLUDED
  18. #define LINKED_LIST_HPP_INCLUDED
  19. #include "CarlaUtils.hpp"
  20. #include <new>
  21. extern "C" {
  22. #include "rtmempool/list.h"
  23. }
  24. // Declare non copyable and prevent heap allocation
  25. #ifdef CARLA_PROPER_CPP11_SUPPORT
  26. # define LINKED_LIST_DECLARATIONS(ClassName) \
  27. ClassName(ClassName&) = delete; \
  28. ClassName(const ClassName&) = delete; \
  29. ClassName& operator=(const ClassName&) = delete; \
  30. static void* operator new(size_t) = delete;
  31. #else
  32. # define LINKED_LIST_DECLARATIONS(ClassName) \
  33. ClassName(ClassName&); \
  34. ClassName(const ClassName&); \
  35. ClassName& operator=(const ClassName&);
  36. #endif
  37. typedef struct list_head k_list_head;
  38. // -----------------------------------------------------------------------
  39. // Abstract Linked List class
  40. // _allocate() and _deallocate are virtual calls provided by subclasses
  41. template<typename T>
  42. class AbstractLinkedList
  43. {
  44. protected:
  45. struct Data {
  46. T value;
  47. k_list_head siblings;
  48. };
  49. AbstractLinkedList()
  50. : fDataSize(sizeof(Data)),
  51. fCount(0)
  52. {
  53. _init();
  54. }
  55. public:
  56. virtual ~AbstractLinkedList()
  57. {
  58. CARLA_ASSERT(fCount == 0);
  59. }
  60. class Itenerator {
  61. public:
  62. Itenerator(const k_list_head* queue)
  63. : fData(nullptr),
  64. fEntry(queue->next),
  65. fEntry2(fEntry->next),
  66. fQueue(queue)
  67. {
  68. CARLA_ASSERT(fEntry != nullptr);
  69. CARLA_ASSERT(fEntry2 != nullptr);
  70. CARLA_ASSERT(fQueue != nullptr);
  71. }
  72. bool valid() const noexcept
  73. {
  74. return (fEntry != fQueue);
  75. }
  76. void next() noexcept
  77. {
  78. fEntry = fEntry2;
  79. fEntry2 = fEntry->next;
  80. }
  81. T& getValue()
  82. {
  83. fData = list_entry(fEntry, Data, siblings);
  84. CARLA_ASSERT(fData != nullptr);
  85. return fData->value;
  86. }
  87. private:
  88. Data* fData;
  89. k_list_head* fEntry;
  90. k_list_head* fEntry2;
  91. const k_list_head* const fQueue;
  92. friend class AbstractLinkedList;
  93. };
  94. Itenerator begin() const
  95. {
  96. return Itenerator(&fQueue);
  97. }
  98. void clear()
  99. {
  100. if (fCount != 0)
  101. {
  102. k_list_head* entry;
  103. k_list_head* entry2;
  104. list_for_each_safe(entry, entry2, &fQueue)
  105. {
  106. if (Data* data = list_entry(entry, Data, siblings))
  107. {
  108. data->~Data();
  109. _deallocate(data);
  110. }
  111. }
  112. }
  113. _init();
  114. }
  115. size_t count() const noexcept
  116. {
  117. return fCount;
  118. }
  119. bool isEmpty() const noexcept
  120. {
  121. return (fCount == 0);
  122. }
  123. bool append(const T& value)
  124. {
  125. if (Data* const data = _allocate())
  126. {
  127. new(data)Data();
  128. data->value = value;
  129. list_add_tail(&data->siblings, &fQueue);
  130. ++fCount;
  131. return true;
  132. }
  133. return false;
  134. }
  135. bool appendAt(const T& value, const Itenerator& it)
  136. {
  137. if (Data* const data = _allocate())
  138. {
  139. new(data)Data();
  140. data->value = value;
  141. list_add_tail(&data->siblings, it.fEntry->next);
  142. ++fCount;
  143. return true;
  144. }
  145. return false;
  146. }
  147. bool insert(const T& value)
  148. {
  149. if (Data* const data = _allocate())
  150. {
  151. new(data)Data();
  152. data->value = value;
  153. list_add(&data->siblings, &fQueue);
  154. ++fCount;
  155. return true;
  156. }
  157. return false;
  158. }
  159. bool insertAt(const T& value, const Itenerator& it)
  160. {
  161. if (Data* const data = _allocate())
  162. {
  163. new(data)Data();
  164. data->value = value;
  165. list_add(&data->siblings, it.fEntry->prev);
  166. ++fCount;
  167. return true;
  168. }
  169. return false;
  170. }
  171. T& getAt(const size_t index, const bool remove = false)
  172. {
  173. if (fCount == 0 || index >= fCount)
  174. return fRetValue;
  175. size_t i = 0;
  176. Data* data = nullptr;
  177. k_list_head* entry;
  178. k_list_head* entry2;
  179. list_for_each_safe(entry, entry2, &fQueue)
  180. {
  181. if (index != i++)
  182. continue;
  183. data = list_entry(entry, Data, siblings);
  184. if (data != nullptr)
  185. fRetValue = data->value;
  186. if (remove)
  187. {
  188. --fCount;
  189. list_del(entry);
  190. if (data != nullptr)
  191. {
  192. data->~Data();
  193. _deallocate(data);
  194. }
  195. }
  196. break;
  197. }
  198. return fRetValue;
  199. }
  200. T& getFirst(const bool remove = false)
  201. {
  202. return _getFirstOrLast(true, remove);
  203. }
  204. T& getLast(const bool remove = false)
  205. {
  206. return _getFirstOrLast(false, remove);
  207. }
  208. void remove(Itenerator& it)
  209. {
  210. CARLA_SAFE_ASSERT_RETURN(it.fEntry != nullptr,);
  211. --fCount;
  212. list_del(it.fEntry);
  213. CARLA_SAFE_ASSERT_RETURN(it.fData != nullptr,);
  214. it.fData->~Data();
  215. _deallocate(it.fData);
  216. }
  217. bool removeOne(const T& value)
  218. {
  219. Data* data = nullptr;
  220. k_list_head* entry;
  221. k_list_head* entry2;
  222. list_for_each_safe(entry, entry2, &fQueue)
  223. {
  224. data = list_entry(entry, Data, siblings);
  225. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  226. if (data->value == value)
  227. {
  228. --fCount;
  229. list_del(entry);
  230. data->~Data();
  231. _deallocate(data);
  232. break;
  233. }
  234. }
  235. return (data != nullptr);
  236. }
  237. void removeAll(const T& value)
  238. {
  239. Data* data;
  240. k_list_head* entry;
  241. k_list_head* entry2;
  242. list_for_each_safe(entry, entry2, &fQueue)
  243. {
  244. data = list_entry(entry, Data, siblings);
  245. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  246. if (data->value == value)
  247. {
  248. --fCount;
  249. list_del(entry);
  250. data->~Data();
  251. _deallocate(data);
  252. }
  253. }
  254. }
  255. void spliceAppend(AbstractLinkedList& list, const bool init = true)
  256. {
  257. if (init)
  258. {
  259. list_splice_tail_init(&fQueue, &list.fQueue);
  260. list.fCount += fCount;
  261. fCount = 0;
  262. }
  263. else
  264. {
  265. list_splice_tail(&fQueue, &list.fQueue);
  266. list.fCount += fCount;
  267. }
  268. }
  269. void spliceInsert(AbstractLinkedList& list, const bool init = true)
  270. {
  271. if (init)
  272. {
  273. list_splice_init(&fQueue, &list.fQueue);
  274. list.fCount += fCount;
  275. fCount = 0;
  276. }
  277. else
  278. {
  279. list_splice(&fQueue, &list.fQueue);
  280. list.fCount += fCount;
  281. }
  282. }
  283. protected:
  284. const size_t fDataSize;
  285. size_t fCount;
  286. k_list_head fQueue;
  287. virtual Data* _allocate() = 0;
  288. virtual void _deallocate(Data*& dataPtr) = 0;
  289. private:
  290. T fRetValue;
  291. void _init() noexcept
  292. {
  293. fCount = 0;
  294. INIT_LIST_HEAD(&fQueue);
  295. }
  296. T& _getFirstOrLast(const bool first, const bool remove)
  297. {
  298. if (fCount == 0)
  299. return fRetValue;
  300. k_list_head* const entry = first ? fQueue.next : fQueue.prev;
  301. Data* data = list_entry(entry, Data, siblings);
  302. if (data != nullptr)
  303. fRetValue = data->value;
  304. if (remove)
  305. {
  306. --fCount;
  307. list_del(entry);
  308. if (data != nullptr)
  309. {
  310. data->~Data();
  311. _deallocate(data);
  312. }
  313. }
  314. return fRetValue;
  315. }
  316. LINKED_LIST_DECLARATIONS(AbstractLinkedList)
  317. };
  318. // -----------------------------------------------------------------------
  319. // LinkedList
  320. template<typename T>
  321. class LinkedList : public AbstractLinkedList<T>
  322. {
  323. public:
  324. LinkedList() {}
  325. private:
  326. typename AbstractLinkedList<T>::Data* _allocate() override
  327. {
  328. return (typename AbstractLinkedList<T>::Data*)std::malloc(this->fDataSize);
  329. }
  330. void _deallocate(typename AbstractLinkedList<T>::Data*& dataPtr) override
  331. {
  332. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr,);
  333. std::free(dataPtr);
  334. dataPtr = nullptr;
  335. }
  336. LINKED_LIST_DECLARATIONS(LinkedList)
  337. };
  338. // -----------------------------------------------------------------------
  339. #endif // LINKED_LIST_HPP_INCLUDED