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.

558 lines
12KB

  1. /*
  2. * High-level, real-time safe, templated, C++ doubly-linked list
  3. * Copyright (C) 2013 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 GPL.txt file
  16. */
  17. #ifndef __RT_LIST_HPP__
  18. #define __RT_LIST_HPP__
  19. #include "CarlaUtils.hpp"
  20. extern "C" {
  21. #include "rtmempool/list.h"
  22. #include "rtmempool/rtmempool.h"
  23. }
  24. // Declare non copyable and prevent heap allocation
  25. #define LIST_DECLARATIONS(className) \
  26. className(const className&); \
  27. className& operator= (const className&); \
  28. static void* operator new (size_t) { return nullptr; } \
  29. static void operator delete (void*) {}
  30. typedef struct list_head k_list_head;
  31. // -----------------------------------------------------------------------
  32. // Abstract List class
  33. // _allocate() and _deallocate are virtual calls provided by subclasses
  34. template<typename T>
  35. class List
  36. {
  37. protected:
  38. struct Data {
  39. T value;
  40. k_list_head siblings;
  41. };
  42. class Itenerator {
  43. public:
  44. Itenerator(k_list_head* queue)
  45. : kQueue(queue),
  46. fEntry(queue->next),
  47. fData(nullptr)
  48. {
  49. CARLA_ASSERT(kQueue != nullptr);
  50. CARLA_ASSERT(fEntry != nullptr);
  51. }
  52. bool valid()
  53. {
  54. prefetch(fEntry->next);
  55. return (fEntry != kQueue);
  56. }
  57. void next()
  58. {
  59. fEntry = fEntry->next;
  60. }
  61. T& operator*()
  62. {
  63. fData = list_entry(fEntry, Data, siblings);
  64. CARLA_ASSERT(fData != nullptr);
  65. return fData->value;
  66. }
  67. private:
  68. k_list_head* const kQueue;
  69. k_list_head* fEntry;
  70. Data* fData;
  71. friend class List;
  72. };
  73. List()
  74. : kDataSize(sizeof(Data)),
  75. fCount(0)
  76. {
  77. _init();
  78. }
  79. public:
  80. virtual ~List()
  81. {
  82. CARLA_ASSERT(fCount == 0);
  83. }
  84. Itenerator begin()
  85. {
  86. return Itenerator(&fQueue);
  87. }
  88. void clear()
  89. {
  90. if (fCount != 0)
  91. {
  92. k_list_head* entry;
  93. list_for_each(entry, &fQueue)
  94. {
  95. if (Data* data = list_entry(entry, Data, siblings))
  96. _deallocate(data);
  97. }
  98. }
  99. _init();
  100. }
  101. size_t count() const
  102. {
  103. return fCount;
  104. }
  105. bool isEmpty() const
  106. {
  107. return (fCount == 0);
  108. }
  109. bool append(const T& value)
  110. {
  111. if (Data* const data = _allocate())
  112. {
  113. std::memcpy(&data->value, &value, sizeof(T));
  114. list_add_tail(&data->siblings, &fQueue);
  115. fCount++;
  116. return true;
  117. }
  118. return false;
  119. }
  120. bool appendAt(const T& value, const Itenerator& it)
  121. {
  122. if (Data* const data = _allocate())
  123. {
  124. std::memcpy(&data->value, &value, sizeof(T));
  125. list_add_tail(&data->siblings, it.fEntry->next);
  126. fCount++;
  127. return true;
  128. }
  129. return false;
  130. }
  131. bool insert(const T& value)
  132. {
  133. if (Data* const data = _allocate())
  134. {
  135. std::memcpy(&data->value, &value, sizeof(T));
  136. list_add(&data->siblings, &fQueue);
  137. fCount++;
  138. return true;
  139. }
  140. return false;
  141. }
  142. bool insertAt(const T& value, const Itenerator& it)
  143. {
  144. if (Data* const data = _allocate())
  145. {
  146. std::memcpy(&data->value, &value, sizeof(T));
  147. list_add(&data->siblings, it.fEntry->prev);
  148. fCount++;
  149. return true;
  150. }
  151. return false;
  152. }
  153. T& getAt(const size_t index, const bool remove = false)
  154. {
  155. if (fCount == 0 || index >= fCount)
  156. return _getEmpty();
  157. size_t i = 0;
  158. Data* data = nullptr;
  159. k_list_head* entry;
  160. list_for_each(entry, &fQueue)
  161. {
  162. if (index != i++)
  163. continue;
  164. data = list_entry(entry, Data, siblings);
  165. if (remove)
  166. {
  167. fCount--;
  168. list_del(entry);
  169. if (data != nullptr)
  170. _deallocate(data);
  171. }
  172. break;
  173. }
  174. CARLA_ASSERT(data != nullptr);
  175. return (data != nullptr) ? data->value : _getEmpty();
  176. }
  177. T& getFirst(const bool remove = false)
  178. {
  179. return _getFirstOrLast(true, remove);
  180. }
  181. T& getLast(const bool remove = false)
  182. {
  183. return _getFirstOrLast(false, remove);
  184. }
  185. bool removeOne(const T& value)
  186. {
  187. Data* data = nullptr;
  188. k_list_head* entry;
  189. list_for_each(entry, &fQueue)
  190. {
  191. data = list_entry(entry, Data, siblings);
  192. CARLA_ASSERT(data != nullptr);
  193. if (data != nullptr && data->value == value)
  194. {
  195. fCount--;
  196. list_del(entry);
  197. _deallocate(data);
  198. break;
  199. }
  200. }
  201. return (data != nullptr);
  202. }
  203. void removeAll(const T& value)
  204. {
  205. Data* data;
  206. k_list_head* entry;
  207. k_list_head* tmp;
  208. list_for_each_safe(entry, tmp, &fQueue)
  209. {
  210. data = list_entry(entry, Data, siblings);
  211. CARLA_ASSERT(data != nullptr);
  212. if (data != nullptr && data->value == value)
  213. {
  214. fCount--;
  215. list_del(entry);
  216. _deallocate(data);
  217. }
  218. }
  219. }
  220. virtual void spliceAppend(List& list, const bool init = false)
  221. {
  222. if (init)
  223. {
  224. list_splice_tail_init(&fQueue, &list.fQueue);
  225. list.fCount += fCount;
  226. fCount = 0;
  227. }
  228. else
  229. {
  230. list_splice_tail(&fQueue, &list.fQueue);
  231. list.fCount += fCount;
  232. }
  233. }
  234. virtual void spliceInsert(List& list, const bool init = false)
  235. {
  236. if (init)
  237. {
  238. list_splice_init(&fQueue, &list.fQueue);
  239. list.fCount += fCount;
  240. fCount = 0;
  241. }
  242. else
  243. {
  244. list_splice(&fQueue, &list.fQueue);
  245. list.fCount += fCount;
  246. }
  247. }
  248. protected:
  249. const size_t kDataSize;
  250. size_t fCount;
  251. k_list_head fQueue;
  252. virtual Data* _allocate() = 0;
  253. virtual void _deallocate(Data* const dataPtr) = 0;
  254. private:
  255. void _init()
  256. {
  257. fCount = 0;
  258. INIT_LIST_HEAD(&fQueue);
  259. }
  260. T& _getFirstOrLast(const bool first, const bool remove)
  261. {
  262. if (fCount == 0)
  263. return _getEmpty();
  264. k_list_head* const entry = first ? fQueue.next : fQueue.prev;
  265. Data* const data = list_entry(entry, Data, siblings);
  266. CARLA_ASSERT(data != nullptr);
  267. if (data == nullptr)
  268. return _getEmpty();
  269. T& ret = data->value;
  270. if (data != nullptr && remove)
  271. {
  272. fCount--;
  273. list_del(entry);
  274. _deallocate(data);
  275. }
  276. return ret;
  277. }
  278. T& _getEmpty()
  279. {
  280. // FIXME ?
  281. static T value;
  282. static bool reset = true;
  283. if (reset)
  284. {
  285. reset = false;
  286. carla_zeroStruct<T>(value);
  287. }
  288. return value;
  289. }
  290. LIST_DECLARATIONS(List)
  291. };
  292. // -----------------------------------------------------------------------
  293. // Realtime safe list
  294. template<typename T>
  295. class RtList : public List<T>
  296. {
  297. public:
  298. // -------------------------------------------------------------------
  299. // RtMemPool C++ class
  300. class Pool
  301. {
  302. public:
  303. Pool(const size_t minPreallocated, const size_t maxPreallocated)
  304. : fHandle(nullptr),
  305. kDataSize(sizeof(typename List<T>::Data))
  306. {
  307. rtsafe_memory_pool_create(&fHandle, nullptr, kDataSize, minPreallocated, maxPreallocated);
  308. CARLA_ASSERT(fHandle != nullptr);
  309. }
  310. ~Pool()
  311. {
  312. if (fHandle != nullptr)
  313. rtsafe_memory_pool_destroy(fHandle);
  314. }
  315. void* allocate_atomic()
  316. {
  317. return rtsafe_memory_pool_allocate_atomic(fHandle);
  318. }
  319. void* allocate_sleepy()
  320. {
  321. return rtsafe_memory_pool_allocate_sleepy(fHandle);
  322. }
  323. void deallocate(void* const dataPtr)
  324. {
  325. rtsafe_memory_pool_deallocate(fHandle, dataPtr);
  326. }
  327. void resize(const size_t minPreallocated, const size_t maxPreallocated)
  328. {
  329. if (fHandle != nullptr)
  330. {
  331. rtsafe_memory_pool_destroy(fHandle);
  332. fHandle = nullptr;
  333. }
  334. rtsafe_memory_pool_create(&fHandle, nullptr, kDataSize, minPreallocated, maxPreallocated);
  335. CARLA_ASSERT(fHandle != nullptr);
  336. }
  337. private:
  338. RtMemPool_Handle fHandle;
  339. const size_t kDataSize;
  340. };
  341. // -------------------------------------------------------------------
  342. // Now the actual list code
  343. RtList(Pool* const memPool)
  344. : kMemPool(memPool)
  345. {
  346. CARLA_ASSERT(kMemPool != nullptr);
  347. }
  348. ~RtList()
  349. {
  350. }
  351. void append_sleepy(const T& value)
  352. {
  353. if (typename List<T>::Data* const data = _allocate_sleepy())
  354. {
  355. std::memcpy(&data->value, &value, sizeof(T));
  356. list_add_tail(&data->siblings, &this->fQueue);
  357. this->fCount++;
  358. }
  359. }
  360. void insert_sleepy(const T& value)
  361. {
  362. if (typename List<T>::Data* const data = _allocate_sleepy())
  363. {
  364. std::memcpy(&data->value, &value, sizeof(T));
  365. list_add(&data->siblings, &this->fQueue);
  366. this->fCount++;
  367. }
  368. }
  369. void resize(const size_t minPreallocated, const size_t maxPreallocated)
  370. {
  371. this->clear();
  372. kMemPool->resize(minPreallocated, maxPreallocated);
  373. }
  374. void spliceAppend(RtList& list, const bool init = false)
  375. {
  376. CARLA_ASSERT(kMemPool == list.kMemPool);
  377. List<T>::spliceAppend(list, init);
  378. }
  379. void spliceInsert(RtList& list, const bool init = false)
  380. {
  381. CARLA_ASSERT(kMemPool == list.kMemPool);
  382. List<T>::spliceInsert(list, init);
  383. }
  384. private:
  385. Pool* const kMemPool;
  386. typename List<T>::Data* _allocate()
  387. {
  388. return _allocate_atomic();
  389. }
  390. typename List<T>::Data* _allocate_atomic()
  391. {
  392. return (typename List<T>::Data*)kMemPool->allocate_atomic();
  393. }
  394. typename List<T>::Data* _allocate_sleepy()
  395. {
  396. return (typename List<T>::Data*)kMemPool->allocate_sleepy();
  397. }
  398. void _deallocate(typename List<T>::Data* const dataPtr)
  399. {
  400. kMemPool->deallocate(dataPtr);
  401. }
  402. LIST_DECLARATIONS(RtList)
  403. };
  404. // -----------------------------------------------------------------------
  405. // Non-Realtime list, using malloc/free methods
  406. template<typename T>
  407. class NonRtList : public List<T>
  408. {
  409. public:
  410. NonRtList()
  411. {
  412. }
  413. ~NonRtList()
  414. {
  415. }
  416. private:
  417. typename List<T>::Data* _allocate()
  418. {
  419. return (typename List<T>::Data*)malloc(this->kDataSize);
  420. }
  421. void _deallocate(typename List<T>::Data* const dataPtr)
  422. {
  423. free(dataPtr);
  424. }
  425. LIST_DECLARATIONS(NonRtList)
  426. };
  427. // -----------------------------------------------------------------------
  428. // Non-Realtime list, using new/delete methods
  429. template<typename T>
  430. class NonRtListNew : public List<T>
  431. {
  432. public:
  433. NonRtListNew()
  434. {
  435. }
  436. ~NonRtListNew()
  437. {
  438. }
  439. private:
  440. typename List<T>::Data* _allocate()
  441. {
  442. return new typename List<T>::Data;
  443. }
  444. void _deallocate(typename List<T>::Data* const dataPtr)
  445. {
  446. delete dataPtr;
  447. }
  448. LIST_DECLARATIONS(NonRtListNew)
  449. };
  450. // -----------------------------------------------------------------------
  451. #endif // __RT_LIST_HPP__