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.

LinkedList.hpp 13KB

10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 *)0)->MEMBER)
  24. #endif
  25. #if (defined(__GNUC__) || defined(__clang__)) && ! defined(__STRICT_ANSI__)
  26. # define container_of(ptr, type, member) ({ \
  27. typeof( ((type *)0)->member ) *__mptr = (ptr); \
  28. (type *)( (char *)__mptr - offsetof(type, member) );})
  29. # define container_of_const(ptr, type, member) ({ \
  30. const typeof( ((type *)0)->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. #else
  64. {
  65. fQueue.next = &fQueue;
  66. fQueue.prev = &fQueue;
  67. }
  68. #endif
  69. public:
  70. virtual ~AbstractLinkedList() noexcept
  71. {
  72. CARLA_SAFE_ASSERT(fCount == 0);
  73. }
  74. class Itenerator {
  75. public:
  76. Itenerator(const ListHead& queue) noexcept
  77. : fData(nullptr),
  78. fEntry(queue.next),
  79. fEntry2(fEntry->next),
  80. kQueue(queue)
  81. {
  82. CARLA_SAFE_ASSERT(fEntry != nullptr);
  83. CARLA_SAFE_ASSERT(fEntry2 != nullptr);
  84. }
  85. bool valid() const noexcept
  86. {
  87. return (fEntry != nullptr && fEntry != &kQueue);
  88. }
  89. void next() noexcept
  90. {
  91. fEntry = fEntry2;
  92. fEntry2 = (fEntry != nullptr) ? fEntry->next : nullptr;
  93. }
  94. T& getValue() noexcept
  95. {
  96. fData = list_entry(fEntry, Data, siblings);
  97. return fData->value;
  98. }
  99. void setValue(const T& value) noexcept
  100. {
  101. fData = list_entry(fEntry, Data, siblings);
  102. fData->value = value;
  103. }
  104. private:
  105. Data* fData;
  106. ListHead* fEntry;
  107. ListHead* fEntry2;
  108. const ListHead& kQueue;
  109. friend class AbstractLinkedList;
  110. };
  111. Itenerator begin() const noexcept
  112. {
  113. return Itenerator(fQueue);
  114. }
  115. void clear() noexcept
  116. {
  117. if (fCount == 0)
  118. return;
  119. ListHead* entry;
  120. ListHead* entry2;
  121. for (entry = fQueue.next, entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  122. {
  123. Data* const data = list_entry(entry, Data, siblings);
  124. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  125. _deallocate(data);
  126. }
  127. _init();
  128. }
  129. std::size_t count() const noexcept
  130. {
  131. return fCount;
  132. }
  133. bool isEmpty() const noexcept
  134. {
  135. return (fCount == 0);
  136. }
  137. bool append(const T& value) noexcept
  138. {
  139. return _add(value, true, &fQueue);
  140. }
  141. bool appendAt(const T& value, const Itenerator& it) noexcept
  142. {
  143. return _add(value, true, it.fEntry->next);
  144. }
  145. bool insert(const T& value) noexcept
  146. {
  147. return _add(value, false, &fQueue);
  148. }
  149. bool insertAt(const T& value, const Itenerator& it) noexcept
  150. {
  151. return _add(value, false, it.fEntry->prev);
  152. }
  153. const T& getAt(const std::size_t index, const T& fallback) const noexcept
  154. {
  155. CARLA_SAFE_ASSERT_RETURN(fCount > 0 && index < fCount, fallback);
  156. std::size_t i = 0;
  157. ListHead* entry;
  158. ListHead* entry2;
  159. for (entry = fQueue.next, entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  160. {
  161. if (index != i++)
  162. continue;
  163. return _get(entry, fallback);
  164. }
  165. return fallback;
  166. }
  167. T& getAt(const std::size_t index, T& fallback) const noexcept
  168. {
  169. CARLA_SAFE_ASSERT_RETURN(fCount > 0 && index < fCount, fallback);
  170. std::size_t i = 0;
  171. ListHead* entry;
  172. ListHead* entry2;
  173. for (entry = fQueue.next, entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  174. {
  175. if (index != i++)
  176. continue;
  177. return _get(entry, fallback);
  178. }
  179. return fallback;
  180. }
  181. T getAt(const std::size_t index, T& fallback, const bool removeObj) noexcept
  182. {
  183. CARLA_SAFE_ASSERT_RETURN(fCount > 0 && index < fCount, fallback);
  184. std::size_t i = 0;
  185. ListHead* entry;
  186. ListHead* entry2;
  187. for (entry = fQueue.next, entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  188. {
  189. if (index != i++)
  190. continue;
  191. return _get(entry, fallback, removeObj);
  192. }
  193. return fallback;
  194. }
  195. const T& getFirst(const T& fallback) const noexcept
  196. {
  197. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  198. return _get(fQueue.next, fallback);
  199. }
  200. T& getFirst(T& fallback) const noexcept
  201. {
  202. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  203. return _get(fQueue.next, fallback);
  204. }
  205. T getFirst(T& fallback, const bool removeObj) noexcept
  206. {
  207. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  208. return _get(fQueue.next, fallback, removeObj);
  209. }
  210. const T& getLast(const T& fallback) const noexcept
  211. {
  212. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  213. return _get(fQueue.prev, fallback);
  214. }
  215. T& getLast(T& fallback) const noexcept
  216. {
  217. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  218. return _get(fQueue.prev, fallback);
  219. }
  220. T getLast(T& fallback, const bool removeObj) noexcept
  221. {
  222. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  223. return _get(fQueue.prev, fallback, removeObj);
  224. }
  225. void remove(Itenerator& it) noexcept
  226. {
  227. CARLA_SAFE_ASSERT_RETURN(it.fEntry != nullptr,);
  228. CARLA_SAFE_ASSERT_RETURN(it.fData != nullptr,);
  229. _delete(it.fEntry, it.fData);
  230. }
  231. bool removeOne(const T& value) noexcept
  232. {
  233. ListHead* entry;
  234. ListHead* entry2;
  235. for (entry = fQueue.next, entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  236. {
  237. Data* const data = list_entry(entry, Data, siblings);
  238. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  239. if (data->value != value)
  240. continue;
  241. _delete(entry, data);
  242. return true;
  243. }
  244. return false;
  245. }
  246. void removeAll(const T& value) noexcept
  247. {
  248. ListHead* entry;
  249. ListHead* entry2;
  250. for (entry = fQueue.next, entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  251. {
  252. Data* const data = list_entry(entry, Data, siblings);
  253. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  254. if (data->value != value)
  255. continue;
  256. _delete(entry, data);
  257. }
  258. }
  259. void spliceAppendTo(AbstractLinkedList<T>& list) noexcept
  260. {
  261. if (fQueue.next == &fQueue)
  262. return;
  263. __list_splice_tail(&fQueue, &list.fQueue);
  264. list.fCount += fCount;
  265. _init();
  266. }
  267. void spliceInsertInto(AbstractLinkedList<T>& list) noexcept
  268. {
  269. if (fQueue.next == &fQueue)
  270. return;
  271. __list_splice(&fQueue, &list.fQueue);
  272. list.fCount += fCount;
  273. _init();
  274. }
  275. protected:
  276. const std::size_t kDataSize;
  277. std::size_t fCount;
  278. ListHead fQueue;
  279. virtual Data* _allocate() noexcept = 0;
  280. virtual void _deallocate(Data* const dataPtr) noexcept = 0;
  281. private:
  282. void _init() noexcept
  283. {
  284. fCount = 0;
  285. fQueue.next = &fQueue;
  286. fQueue.prev = &fQueue;
  287. }
  288. void _createData(Data* const data, const T& value) noexcept
  289. {
  290. ++fCount;
  291. data->value = value;
  292. //std::memcpy(data->value, value, kDataSize);
  293. }
  294. bool _add(const T& value, const bool inTail, ListHead* const queue) noexcept
  295. {
  296. if (Data* const data = _allocate())
  297. {
  298. _createData(data, value);
  299. if (inTail)
  300. __list_add(data->siblings, queue->prev, queue);
  301. else
  302. __list_add(data->siblings, queue, queue->next);
  303. return true;
  304. }
  305. return false;
  306. }
  307. void _delete(ListHead* const entry, Data* const data) noexcept
  308. {
  309. __list_del(entry->prev, entry->next);
  310. entry->next = nullptr;
  311. entry->prev = nullptr;
  312. _deallocate(data);
  313. --fCount;
  314. }
  315. const T& _get(ListHead* const entry, const T& fallback) const noexcept
  316. {
  317. const Data* const data = list_entry_const(entry, Data, siblings);
  318. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  319. return data->value;
  320. }
  321. T& _get(ListHead* const entry, T& fallback) const noexcept
  322. {
  323. Data* const data = list_entry(entry, Data, siblings);
  324. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  325. return data->value;
  326. }
  327. T _get(ListHead* const entry, T& fallback, const bool removeObj) noexcept
  328. {
  329. Data* const data = list_entry(entry, Data, siblings);
  330. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  331. if (! removeObj)
  332. return data->value;
  333. const T value(data->value);
  334. _delete(entry, data);
  335. return value;
  336. }
  337. /*
  338. * Insert a new entry between two known consecutive entries.
  339. */
  340. static void __list_add(ListHead& newl, ListHead* const prev, ListHead* const next) noexcept
  341. {
  342. next->prev = &newl;
  343. newl.next = next;
  344. newl.prev = prev;
  345. prev->next = &newl;
  346. }
  347. /*
  348. * Delete a list entry by making the prev/next entries
  349. * point to each other.
  350. */
  351. static void __list_del(ListHead* const prev, ListHead* const next) noexcept
  352. {
  353. next->prev = prev;
  354. prev->next = next;
  355. }
  356. static void __list_splice(ListHead* const list, ListHead* const head) noexcept
  357. {
  358. ListHead* const first = list->next;
  359. ListHead* const last = list->prev;
  360. ListHead* const at = head->next;
  361. first->prev = head;
  362. head->next = first;
  363. last->next = at;
  364. at->prev = last;
  365. }
  366. static void __list_splice_tail(ListHead* const list, ListHead* const head) noexcept
  367. {
  368. ListHead* const first = list->next;
  369. ListHead* const last = list->prev;
  370. ListHead* const at = head->prev;
  371. first->prev = at;
  372. at->next = first;
  373. last->next = head;
  374. head->prev = last;
  375. }
  376. template<typename> friend class RtLinkedList;
  377. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  378. CARLA_DECLARE_NON_COPY_CLASS(AbstractLinkedList)
  379. };
  380. // -----------------------------------------------------------------------
  381. // LinkedList
  382. template<typename T>
  383. class LinkedList : public AbstractLinkedList<T>
  384. {
  385. public:
  386. LinkedList() noexcept {}
  387. protected:
  388. typename AbstractLinkedList<T>::Data* _allocate() noexcept override
  389. {
  390. return (typename AbstractLinkedList<T>::Data*)std::malloc(this->kDataSize);
  391. }
  392. void _deallocate(typename AbstractLinkedList<T>::Data* const dataPtr) noexcept override
  393. {
  394. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr,);
  395. std::free(dataPtr);
  396. }
  397. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  398. CARLA_DECLARE_NON_COPY_CLASS(LinkedList)
  399. };
  400. // -----------------------------------------------------------------------
  401. #endif // LINKED_LIST_HPP_INCLUDED