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.

541 lines
14KB

  1. /*
  2. * High-level, templated, C++ doubly-linked list
  3. * Copyright (C) 2013-2020 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. #if 0
  136. T& operator*() noexcept
  137. {
  138. static T& fallback(_getFallback());
  139. Data* const data(list_entry(fEntry, Data, siblings));
  140. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  141. return data->value;
  142. }
  143. #endif
  144. const T& operator*() const noexcept
  145. {
  146. static const T& fallback(_getFallback());
  147. const Data* const data(list_entry_const(fEntry, Data, siblings));
  148. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  149. return data->value;
  150. }
  151. private:
  152. const ListHead* fEntry;
  153. const ListHead* fEntry2;
  154. static T& _getFallback()
  155. {
  156. static T data;
  157. carla_zeroStruct(data);
  158. return data;
  159. }
  160. };
  161. Itenerator begin2() const noexcept
  162. {
  163. return Itenerator(fQueue);
  164. }
  165. AutoItenerator begin() const noexcept
  166. {
  167. return AutoItenerator(fQueue.next);
  168. }
  169. AutoItenerator end() const noexcept
  170. {
  171. return AutoItenerator(&fQueue);
  172. }
  173. void clear() noexcept
  174. {
  175. if (fCount == 0)
  176. return;
  177. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  178. {
  179. Data* const data(list_entry(entry, Data, siblings));
  180. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  181. _deallocate(data);
  182. }
  183. _init();
  184. }
  185. inline std::size_t count() const noexcept
  186. {
  187. return fCount;
  188. }
  189. inline bool isEmpty() const noexcept
  190. {
  191. return fCount == 0;
  192. }
  193. inline bool isNotEmpty() const noexcept
  194. {
  195. return fCount != 0;
  196. }
  197. bool append(const T& value) noexcept
  198. {
  199. return _add(value, true, &fQueue);
  200. }
  201. bool appendAt(const T& value, const Itenerator& it) noexcept
  202. {
  203. return _add(value, true, it.fEntry->next);
  204. }
  205. bool insert(const T& value) noexcept
  206. {
  207. return _add(value, false, &fQueue);
  208. }
  209. bool insertAt(const T& value, const Itenerator& it) noexcept
  210. {
  211. return _add(value, false, it.fEntry->prev);
  212. }
  213. // NOTE: do not use this function unless strictly needed. it can be very expensive if the list is big
  214. const T& getAt(const std::size_t index, const T& fallback) const noexcept
  215. {
  216. CARLA_SAFE_ASSERT_RETURN(fCount > 0 && index < fCount, fallback);
  217. std::size_t i = 0;
  218. ListHead* entry = fQueue.next;
  219. for (; i++ != index; entry = entry->next) {}
  220. return _get(entry, fallback);
  221. }
  222. T getFirst(T& fallback, const bool removeObj) noexcept
  223. {
  224. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  225. return _get(fQueue.next, fallback, removeObj);
  226. }
  227. T& getFirst(T& fallback) const noexcept
  228. {
  229. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  230. return _get(fQueue.next, fallback);
  231. }
  232. const T& getFirst(const T& fallback) const noexcept
  233. {
  234. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  235. return _get(fQueue.next, fallback);
  236. }
  237. T getLast(T& fallback, const bool removeObj) noexcept
  238. {
  239. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  240. return _get(fQueue.prev, fallback, removeObj);
  241. }
  242. T& getLast(T& fallback) const noexcept
  243. {
  244. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  245. return _get(fQueue.prev, fallback);
  246. }
  247. const T& getLast(const T& fallback) const noexcept
  248. {
  249. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  250. return _get(fQueue.prev, fallback);
  251. }
  252. void remove(Itenerator& it) noexcept
  253. {
  254. CARLA_SAFE_ASSERT_RETURN(it.fEntry != nullptr,);
  255. Data* const data(list_entry(it.fEntry, Data, siblings));
  256. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  257. _delete(it.fEntry, data);
  258. }
  259. bool removeOne(const T& value) noexcept
  260. {
  261. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  262. {
  263. Data* const data = list_entry(entry, Data, siblings);
  264. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  265. if (data->value != value)
  266. continue;
  267. _delete(entry, data);
  268. return true;
  269. }
  270. return false;
  271. }
  272. void removeAll(const T& value) noexcept
  273. {
  274. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  275. {
  276. Data* const data = list_entry(entry, Data, siblings);
  277. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  278. if (data->value != value)
  279. continue;
  280. _delete(entry, data);
  281. }
  282. }
  283. // move data to a new list, and clear ourselves
  284. virtual bool moveTo(AbstractLinkedList<T>& list, const bool inTail = true) noexcept
  285. {
  286. CARLA_SAFE_ASSERT_RETURN(fCount > 0, false);
  287. if (inTail)
  288. __list_splice_tail(&fQueue, &list.fQueue);
  289. else
  290. __list_splice(&fQueue, &list.fQueue);
  291. //! @a list gets our items
  292. list.fCount += fCount;
  293. //! and we get nothing
  294. _init();
  295. return true;
  296. }
  297. protected:
  298. const std::size_t kDataSize;
  299. ListHead fQueue;
  300. std::size_t fCount;
  301. virtual Data* _allocate() noexcept = 0;
  302. virtual void _deallocate(Data* const dataPtr) noexcept = 0;
  303. private:
  304. void _init() noexcept
  305. {
  306. fCount = 0;
  307. fQueue.next = &fQueue;
  308. fQueue.prev = &fQueue;
  309. }
  310. bool _add(const T& value, const bool inTail, ListHead* const queue) noexcept
  311. {
  312. if (Data* const data = _allocate())
  313. return _add_internal(data, value, inTail, queue);
  314. return false;
  315. }
  316. bool _add_internal(Data* const data, const T& value, const bool inTail, ListHead* const queue) noexcept
  317. {
  318. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  319. CARLA_SAFE_ASSERT_RETURN(queue != nullptr, false);
  320. CARLA_SAFE_ASSERT_RETURN(queue->prev != nullptr, false);
  321. CARLA_SAFE_ASSERT_RETURN(queue->next != nullptr, false);
  322. data->value = value;
  323. ListHead* const siblings(&data->siblings);
  324. if (inTail)
  325. {
  326. siblings->prev = queue->prev;
  327. siblings->next = queue;
  328. queue->prev->next = siblings;
  329. queue->prev = siblings;
  330. }
  331. else
  332. {
  333. siblings->prev = queue;
  334. siblings->next = queue->next;
  335. queue->next->prev = siblings;
  336. queue->next = siblings;
  337. }
  338. ++fCount;
  339. return true;
  340. }
  341. void _delete(ListHead* const entry, Data* const data) noexcept
  342. {
  343. CARLA_SAFE_ASSERT_RETURN(entry != nullptr,);
  344. CARLA_SAFE_ASSERT_RETURN(entry->prev != nullptr,);
  345. CARLA_SAFE_ASSERT_RETURN(entry->next != nullptr,);
  346. --fCount;
  347. entry->next->prev = entry->prev;
  348. entry->prev->next = entry->next;
  349. entry->next = nullptr;
  350. entry->prev = nullptr;
  351. _deallocate(data);
  352. }
  353. T _get(ListHead* const entry, T& fallback, const bool removeObj) noexcept
  354. {
  355. Data* const data(list_entry(entry, Data, siblings));
  356. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  357. if (! removeObj)
  358. return data->value;
  359. const T value(data->value);
  360. _delete(entry, data);
  361. return value;
  362. }
  363. T& _get(ListHead* const entry, T& fallback) const noexcept
  364. {
  365. Data* const data(list_entry(entry, Data, siblings));
  366. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  367. return data->value;
  368. }
  369. const T& _get(const ListHead* const entry, const T& fallback) const noexcept
  370. {
  371. const Data* const data(list_entry_const(entry, Data, siblings));
  372. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  373. return data->value;
  374. }
  375. static void __list_splice(ListHead* const list, ListHead* const head) noexcept
  376. {
  377. ListHead* const first = list->next;
  378. ListHead* const last = list->prev;
  379. ListHead* const at = head->next;
  380. first->prev = head;
  381. head->next = first;
  382. last->next = at;
  383. at->prev = last;
  384. }
  385. static void __list_splice_tail(ListHead* const list, ListHead* const head) noexcept
  386. {
  387. ListHead* const first = list->next;
  388. ListHead* const last = list->prev;
  389. ListHead* const at = head->prev;
  390. first->prev = at;
  391. at->next = first;
  392. last->next = head;
  393. head->prev = last;
  394. }
  395. template<typename> friend class RtLinkedList;
  396. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  397. CARLA_DECLARE_NON_COPY_CLASS(AbstractLinkedList)
  398. };
  399. // -----------------------------------------------------------------------
  400. // LinkedList
  401. template<typename T>
  402. class LinkedList : public AbstractLinkedList<T>
  403. {
  404. public:
  405. LinkedList() noexcept {}
  406. protected:
  407. typename AbstractLinkedList<T>::Data* _allocate() noexcept override
  408. {
  409. return (typename AbstractLinkedList<T>::Data*)std::malloc(this->kDataSize);
  410. }
  411. void _deallocate(typename AbstractLinkedList<T>::Data* const dataPtr) noexcept override
  412. {
  413. std::free(dataPtr);
  414. }
  415. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  416. CARLA_DECLARE_NON_COPY_CLASS(LinkedList)
  417. };
  418. // -----------------------------------------------------------------------
  419. #endif // LINKED_LIST_HPP_INCLUDED