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.

581 lines
13KB

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