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.

508 lines
13KB

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