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.

534 lines
14KB

  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. // -----------------------------------------------------------------------
  21. // Define list_entry and list_entry_const
  22. #ifndef offsetof
  23. # define offsetof(TYPE, MEMBER) ((std::size_t) &((TYPE*)nullptr)->MEMBER)
  24. #endif
  25. #if (defined(__GNUC__) || defined(__clang__)) && ! defined(__STRICT_ANSI__)
  26. # define list_entry(ptr, type, member) ({ \
  27. typeof( ((type*)nullptr)->member ) *__mptr = (ptr); \
  28. (type*)( (char*)__mptr - offsetof(type, member) );})
  29. # define list_entry_const(ptr, type, member) ({ \
  30. const typeof( ((type*)nullptr)->member ) *__mptr = (ptr); \
  31. (const type*)( (const char*)__mptr - offsetof(type, member) );})
  32. #else
  33. # define list_entry(ptr, type, member) \
  34. ((type*)((char*)(ptr)-offsetof(type, member)))
  35. # define list_entry_const(ptr, type, member) \
  36. ((const type*)((const char*)(ptr)-offsetof(type, member)))
  37. #endif
  38. // -----------------------------------------------------------------------
  39. // Abstract Linked List class
  40. // _allocate() and _deallocate are virtual calls provided by subclasses
  41. // NOTE: this class is meant for non-polymorphic data types only!
  42. template<typename T>
  43. class AbstractLinkedList
  44. {
  45. protected:
  46. struct ListHead {
  47. ListHead* next;
  48. ListHead* prev;
  49. };
  50. struct Data {
  51. T value;
  52. ListHead siblings;
  53. };
  54. AbstractLinkedList() noexcept
  55. : kDataSize(sizeof(Data)),
  56. #ifdef CARLA_PROPER_CPP11_SUPPORT
  57. fQueue({&fQueue, &fQueue}),
  58. #endif
  59. fCount(0)
  60. {
  61. #ifndef CARLA_PROPER_CPP11_SUPPORT
  62. fQueue.next = &fQueue;
  63. fQueue.prev = &fQueue;
  64. #endif
  65. }
  66. public:
  67. virtual ~AbstractLinkedList() noexcept
  68. {
  69. CARLA_SAFE_ASSERT(fCount == 0);
  70. }
  71. class Itenerator {
  72. public:
  73. Itenerator(const ListHead& queue) noexcept
  74. : fEntry(queue.next),
  75. fEntry2(fEntry->next),
  76. kQueue(queue)
  77. {
  78. CARLA_SAFE_ASSERT(fEntry != nullptr);
  79. CARLA_SAFE_ASSERT(fEntry2 != nullptr);
  80. }
  81. bool valid() const noexcept
  82. {
  83. return (fEntry != nullptr && fEntry != &kQueue);
  84. }
  85. void next() noexcept
  86. {
  87. fEntry = fEntry2;
  88. fEntry2 = (fEntry != nullptr) ? fEntry->next : nullptr;
  89. }
  90. T& getValue(T& fallback) const noexcept
  91. {
  92. Data* const data(list_entry(fEntry, Data, siblings));
  93. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  94. return data->value;
  95. }
  96. const T& getValue(const T& fallback) const noexcept
  97. {
  98. const Data* const data(list_entry_const(fEntry, Data, siblings));
  99. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  100. return data->value;
  101. }
  102. void setValue(const T& value) noexcept
  103. {
  104. Data* const data(list_entry(fEntry, Data, siblings));
  105. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  106. data->value = value;
  107. }
  108. private:
  109. ListHead* fEntry;
  110. ListHead* fEntry2;
  111. const ListHead& kQueue;
  112. friend class AbstractLinkedList;
  113. };
  114. class AutoItenerator {
  115. public:
  116. AutoItenerator(const ListHead* entry) noexcept
  117. : fEntry(entry),
  118. fEntry2(entry != nullptr ? entry->next : nullptr)
  119. {
  120. CARLA_SAFE_ASSERT(fEntry != nullptr);
  121. CARLA_SAFE_ASSERT(fEntry2 != nullptr);
  122. }
  123. bool operator!=(AutoItenerator& it) const noexcept
  124. {
  125. CARLA_SAFE_ASSERT_RETURN(fEntry != nullptr, false);
  126. CARLA_SAFE_ASSERT_RETURN(it.fEntry != nullptr, false);
  127. return fEntry != it.fEntry;
  128. }
  129. AutoItenerator& operator++() noexcept
  130. {
  131. fEntry = fEntry2;
  132. fEntry2 = (fEntry != nullptr) ? fEntry->next : nullptr;
  133. return *this;
  134. }
  135. T& operator*() noexcept
  136. {
  137. static T& fallback(_getFallback());
  138. Data* const data(list_entry(fEntry, Data, siblings));
  139. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  140. return data->value;
  141. }
  142. const T& operator*() const noexcept
  143. {
  144. static const T& fallback(_getFallback());
  145. const Data* const data(list_entry_const(fEntry, Data, siblings));
  146. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  147. return data->value;
  148. }
  149. private:
  150. ListHead* fEntry;
  151. ListHead* fEntry2;
  152. static T& _getFallback()
  153. {
  154. static T data;
  155. carla_zeroStruct(data);
  156. return data;
  157. }
  158. };
  159. Itenerator begin2() const noexcept
  160. {
  161. return Itenerator(fQueue);
  162. }
  163. AutoItenerator begin() const noexcept
  164. {
  165. return AutoItenerator(fQueue.next);
  166. }
  167. AutoItenerator end() const noexcept
  168. {
  169. return AutoItenerator(&fQueue);
  170. }
  171. void clear() noexcept
  172. {
  173. if (fCount == 0)
  174. return;
  175. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  176. {
  177. Data* const data(list_entry(entry, Data, siblings));
  178. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  179. _deallocate(data);
  180. }
  181. _init();
  182. }
  183. std::size_t count() const noexcept
  184. {
  185. return fCount;
  186. }
  187. bool isEmpty() const noexcept
  188. {
  189. return (fCount == 0);
  190. }
  191. bool append(const T& value) noexcept
  192. {
  193. return _add(value, true, &fQueue);
  194. }
  195. bool appendAt(const T& value, const Itenerator& it) noexcept
  196. {
  197. return _add(value, true, it.fEntry->next);
  198. }
  199. bool insert(const T& value) noexcept
  200. {
  201. return _add(value, false, &fQueue);
  202. }
  203. bool insertAt(const T& value, const Itenerator& it) noexcept
  204. {
  205. return _add(value, false, it.fEntry->prev);
  206. }
  207. // NOTE: do not use this function unless strictly needed. it can be very expensive if the list is big
  208. const T& getAt(const std::size_t index, const T& fallback) const noexcept
  209. {
  210. CARLA_SAFE_ASSERT_RETURN(fCount > 0 && index < fCount, fallback);
  211. std::size_t i = 0;
  212. ListHead* entry = fQueue.next;
  213. for (; i++ != index; entry = entry->next) {}
  214. return _get(entry, fallback);
  215. }
  216. T getFirst(T& fallback, const bool removeObj) noexcept
  217. {
  218. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  219. return _get(fQueue.next, fallback, removeObj);
  220. }
  221. T& getFirst(T& fallback) const noexcept
  222. {
  223. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  224. return _get(fQueue.next, fallback);
  225. }
  226. const T& getFirst(const T& fallback) const noexcept
  227. {
  228. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  229. return _get(fQueue.next, fallback);
  230. }
  231. T getLast(T& fallback, const bool removeObj) noexcept
  232. {
  233. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  234. return _get(fQueue.prev, fallback, removeObj);
  235. }
  236. T& getLast(T& fallback) const noexcept
  237. {
  238. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  239. return _get(fQueue.prev, fallback);
  240. }
  241. const T& getLast(const T& fallback) const noexcept
  242. {
  243. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  244. return _get(fQueue.prev, fallback);
  245. }
  246. void remove(Itenerator& it) noexcept
  247. {
  248. CARLA_SAFE_ASSERT_RETURN(it.fEntry != nullptr,);
  249. Data* const data(list_entry(it.fEntry, Data, siblings));
  250. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  251. _delete(it.fEntry, data);
  252. }
  253. bool removeOne(const T& value) noexcept
  254. {
  255. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  256. {
  257. Data* const data = list_entry(entry, Data, siblings);
  258. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  259. if (data->value != value)
  260. continue;
  261. _delete(entry, data);
  262. return true;
  263. }
  264. return false;
  265. }
  266. void removeAll(const T& value) noexcept
  267. {
  268. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  269. {
  270. Data* const data = list_entry(entry, Data, siblings);
  271. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  272. if (data->value != value)
  273. continue;
  274. _delete(entry, data);
  275. }
  276. }
  277. // move data to a new list, and clear ourselves
  278. bool moveTo(AbstractLinkedList<T>& list, const bool inTail = true) noexcept
  279. {
  280. CARLA_SAFE_ASSERT_RETURN(fCount > 0, false);
  281. if (inTail)
  282. __list_splice_tail(&fQueue, &list.fQueue);
  283. else
  284. __list_splice(&fQueue, &list.fQueue);
  285. //! @a list gets our items
  286. list.fCount += fCount;
  287. //! and we get nothing
  288. _init();
  289. return true;
  290. }
  291. protected:
  292. const std::size_t kDataSize;
  293. ListHead fQueue;
  294. std::size_t fCount;
  295. virtual Data* _allocate() noexcept = 0;
  296. virtual void _deallocate(Data* const dataPtr) noexcept = 0;
  297. private:
  298. void _init() noexcept
  299. {
  300. fCount = 0;
  301. fQueue.next = &fQueue;
  302. fQueue.prev = &fQueue;
  303. }
  304. bool _add(const T& value, const bool inTail, ListHead* const queue) noexcept
  305. {
  306. if (Data* const data = _allocate())
  307. return _add_internal(data, value, inTail, queue);
  308. return false;
  309. }
  310. bool _add_internal(Data* const data, const T& value, const bool inTail, ListHead* const queue) noexcept
  311. {
  312. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  313. CARLA_SAFE_ASSERT_RETURN(queue != nullptr, false);
  314. CARLA_SAFE_ASSERT_RETURN(queue->prev != nullptr, false);
  315. CARLA_SAFE_ASSERT_RETURN(queue->next != nullptr, false);
  316. data->value = value;
  317. ListHead* const siblings(&data->siblings);
  318. if (inTail)
  319. {
  320. siblings->prev = queue->prev;
  321. siblings->next = queue;
  322. queue->prev->next = siblings;
  323. queue->prev = siblings;
  324. }
  325. else
  326. {
  327. siblings->prev = queue;
  328. siblings->next = queue->next;
  329. queue->next->prev = siblings;
  330. queue->next = siblings;
  331. }
  332. ++fCount;
  333. return true;
  334. }
  335. void _delete(ListHead* const entry, Data* const data) noexcept
  336. {
  337. CARLA_SAFE_ASSERT_RETURN(entry != nullptr,);
  338. CARLA_SAFE_ASSERT_RETURN(entry->prev != nullptr,);
  339. CARLA_SAFE_ASSERT_RETURN(entry->next != nullptr,);
  340. --fCount;
  341. entry->next->prev = entry->prev;
  342. entry->prev->next = entry->next;
  343. entry->next = nullptr;
  344. entry->prev = nullptr;
  345. _deallocate(data);
  346. }
  347. T _get(ListHead* const entry, T& fallback, const bool removeObj) noexcept
  348. {
  349. Data* const data(list_entry(entry, Data, siblings));
  350. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  351. if (! removeObj)
  352. return data->value;
  353. const T value(data->value);
  354. _delete(entry, data);
  355. return value;
  356. }
  357. T& _get(ListHead* const entry, T& fallback) const noexcept
  358. {
  359. Data* const data(list_entry(entry, Data, siblings));
  360. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  361. return data->value;
  362. }
  363. const T& _get(const ListHead* const entry, const T& fallback) const noexcept
  364. {
  365. const Data* const data(list_entry_const(entry, Data, siblings));
  366. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  367. return data->value;
  368. }
  369. static void __list_splice(ListHead* const list, ListHead* const head) noexcept
  370. {
  371. ListHead* const first = list->next;
  372. ListHead* const last = list->prev;
  373. ListHead* const at = head->next;
  374. first->prev = head;
  375. head->next = first;
  376. last->next = at;
  377. at->prev = last;
  378. }
  379. static void __list_splice_tail(ListHead* const list, ListHead* const head) noexcept
  380. {
  381. ListHead* const first = list->next;
  382. ListHead* const last = list->prev;
  383. ListHead* const at = head->prev;
  384. first->prev = at;
  385. at->next = first;
  386. last->next = head;
  387. head->prev = last;
  388. }
  389. template<typename> friend class RtLinkedList;
  390. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  391. CARLA_DECLARE_NON_COPY_CLASS(AbstractLinkedList)
  392. };
  393. // -----------------------------------------------------------------------
  394. // LinkedList
  395. template<typename T>
  396. class LinkedList : public AbstractLinkedList<T>
  397. {
  398. public:
  399. LinkedList() noexcept {}
  400. protected:
  401. typename AbstractLinkedList<T>::Data* _allocate() noexcept override
  402. {
  403. return (typename AbstractLinkedList<T>::Data*)std::malloc(this->kDataSize);
  404. }
  405. void _deallocate(typename AbstractLinkedList<T>::Data* const dataPtr) noexcept override
  406. {
  407. std::free(dataPtr);
  408. }
  409. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  410. CARLA_DECLARE_NON_COPY_CLASS(LinkedList)
  411. };
  412. // -----------------------------------------------------------------------
  413. #endif // LINKED_LIST_HPP_INCLUDED