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.

Lv2AtomQueue.hpp 4.5KB

11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Simple Queue, specially developed for Atom types
  3. * Copyright (C) 2012-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 __LV2_ATOM_QUEUE_HPP__
  18. #define __LV2_ATOM_QUEUE_HPP__
  19. #include "CarlaMutex.hpp"
  20. #include <cstring> // memcpy, memset
  21. #include "lv2/atom.h"
  22. class Lv2AtomQueue
  23. {
  24. public:
  25. Lv2AtomQueue()
  26. {
  27. index = indexPool = 0;
  28. empty = true;
  29. full = false;
  30. std::memset(dataPool, 0, sizeof(unsigned char)*MAX_POOL_SIZE);
  31. }
  32. void copyDataFrom(Lv2AtomQueue* const queue)
  33. {
  34. // lock mutexes
  35. queue->lock();
  36. lock();
  37. // copy data from queue
  38. std::memcpy(data, queue->data, sizeof(datatype)*MAX_SIZE);
  39. std::memcpy(dataPool, queue->dataPool, sizeof(unsigned char)*MAX_POOL_SIZE);
  40. index = queue->index;
  41. indexPool = queue->indexPool;
  42. empty = queue->empty;
  43. full = queue->full;
  44. // unlock our mutex, no longer needed
  45. unlock();
  46. // reset queque
  47. std::memset(queue->data, 0, sizeof(datatype)*MAX_SIZE);
  48. std::memset(queue->dataPool, 0, sizeof(unsigned char)*MAX_POOL_SIZE);
  49. queue->index = queue->indexPool = 0;
  50. queue->empty = true;
  51. queue->full = false;
  52. // unlock queque mutex
  53. queue->unlock();
  54. }
  55. bool isEmpty()
  56. {
  57. return empty;
  58. }
  59. bool isFull()
  60. {
  61. return full;
  62. }
  63. void lock()
  64. {
  65. mutex.lock();
  66. }
  67. bool tryLock()
  68. {
  69. return mutex.tryLock();
  70. }
  71. void unlock()
  72. {
  73. mutex.unlock();
  74. }
  75. void put(const uint32_t portIndex, const LV2_Atom* const atom)
  76. {
  77. CARLA_ASSERT(atom && atom->size > 0);
  78. CARLA_ASSERT(indexPool + atom->size < MAX_POOL_SIZE); // overflow
  79. if (full || atom->size == 0 || indexPool + atom->size >= MAX_POOL_SIZE)
  80. return;
  81. lock();
  82. for (unsigned short i=0; i < MAX_SIZE; i++)
  83. {
  84. if (data[i].size == 0)
  85. {
  86. data[i].portIndex = portIndex;
  87. data[i].size = atom->size;
  88. data[i].type = atom->type;
  89. data[i].poolOffset = indexPool;
  90. std::memcpy(dataPool + indexPool, (const unsigned char*)LV2_ATOM_BODY_CONST(atom), atom->size);
  91. empty = false;
  92. full = (i == MAX_SIZE-1);
  93. indexPool += atom->size;
  94. break;
  95. }
  96. }
  97. unlock();
  98. }
  99. // needs to be locked first!
  100. bool get(uint32_t* const portIndex, const LV2_Atom** const atom)
  101. {
  102. CARLA_ASSERT(portIndex && atom);
  103. if (empty || ! (portIndex && atom))
  104. return false;
  105. full = false;
  106. if (data[index].size == 0)
  107. {
  108. index = indexPool = 0;
  109. empty = true;
  110. unlock();
  111. return false;
  112. }
  113. retAtom.atom.size = data[index].size;
  114. retAtom.atom.type = data[index].type;
  115. std::memcpy(retAtom.data, dataPool + data[index].poolOffset, data[index].size);
  116. *portIndex = data[index].portIndex;
  117. *atom = (LV2_Atom*)&retAtom;
  118. data[index].portIndex = 0;
  119. data[index].size = 0;
  120. data[index].type = 0;
  121. data[index].poolOffset = 0;
  122. index++;
  123. empty = false;
  124. return true;
  125. }
  126. private:
  127. struct datatype {
  128. size_t size;
  129. uint32_t type;
  130. uint32_t portIndex;
  131. uint32_t poolOffset;
  132. datatype()
  133. : size(0),
  134. type(0),
  135. portIndex(0),
  136. poolOffset(0) {}
  137. };
  138. static const unsigned short MAX_SIZE = 128;
  139. static const unsigned short MAX_POOL_SIZE = 8192;
  140. datatype data[MAX_SIZE];
  141. unsigned char dataPool[MAX_POOL_SIZE];
  142. struct {
  143. LV2_Atom atom;
  144. unsigned char data[MAX_POOL_SIZE];
  145. } retAtom;
  146. unsigned short index, indexPool;
  147. bool empty, full;
  148. CarlaMutex mutex;
  149. };
  150. #endif // __LV2_ATOM_QUEUE_HPP__