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.

622 lines
16KB

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