Collection of tools useful for audio production
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.

192 lines
4.6KB

  1. /*
  2. * Simple Queue, specially developed for Atom types
  3. * Copyright (C) 2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #ifndef LV2_ATOM_QUEUE_HPP
  18. #define LV2_ATOM_QUEUE_HPP
  19. #include "lv2/atom.h"
  20. #include <cstring>
  21. #include <QtCore/QMutex>
  22. class Lv2AtomQueue
  23. {
  24. public:
  25. Lv2AtomQueue()
  26. {
  27. index = indexPool = 0;
  28. empty = true;
  29. full = false;
  30. ::memset(dataPool, 0, sizeof(unsigned char)*MAX_POOL_SIZE);
  31. }
  32. void copyDataFrom(Lv2AtomQueue* const queue)
  33. {
  34. // lock mutexes
  35. queue->mutex.lock();
  36. mutex.lock();
  37. // copy data from queue
  38. ::memcpy(data, queue->data, sizeof(datatype)*MAX_SIZE);
  39. ::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. mutex.unlock();
  46. // reset queque
  47. ::memset(queue->data, 0, sizeof(datatype)*MAX_SIZE);
  48. ::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->mutex.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. void unlock()
  68. {
  69. mutex.unlock();
  70. }
  71. void put(const uint32_t portIndex, const LV2_Atom* const atom, const bool lock = true)
  72. {
  73. CARLA_ASSERT(atom && atom->size > 0);
  74. CARLA_ASSERT(indexPool + atom->size < MAX_POOL_SIZE); // overflow
  75. if (full || atom->size == 0 || indexPool + atom->size >= MAX_POOL_SIZE)
  76. return;
  77. if (lock)
  78. mutex.lock();
  79. for (unsigned short i=0; i < MAX_SIZE; i++)
  80. {
  81. if (data[i].size == 0)
  82. {
  83. data[i].portIndex = portIndex;
  84. data[i].size = atom->size;
  85. data[i].type = atom->type;
  86. data[i].poolOffset = indexPool;
  87. ::memcpy(dataPool + indexPool, (const unsigned char*)LV2_ATOM_BODY_CONST(atom), atom->size);
  88. empty = false;
  89. full = (i == MAX_SIZE-1);
  90. indexPool += atom->size;
  91. break;
  92. }
  93. }
  94. if (lock)
  95. mutex.unlock();
  96. }
  97. bool get(uint32_t* const portIndex, const LV2_Atom** const atom, const bool lock = true)
  98. {
  99. CARLA_ASSERT(portIndex && atom);
  100. if (empty || ! (portIndex && atom))
  101. return false;
  102. if (lock)
  103. mutex.lock();
  104. full = false;
  105. if (data[index].size == 0)
  106. {
  107. index = indexPool = 0;
  108. empty = true;
  109. if (lock)
  110. mutex.unlock();
  111. return false;
  112. }
  113. retAtom.atom.size = data[index].size;
  114. retAtom.atom.type = data[index].type;
  115. ::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. if (lock)
  125. mutex.unlock();
  126. return true;
  127. }
  128. private:
  129. struct datatype {
  130. size_t size;
  131. uint32_t type;
  132. uint32_t portIndex;
  133. uint32_t poolOffset;
  134. datatype()
  135. : size(0),
  136. type(0),
  137. portIndex(0),
  138. poolOffset(0) {}
  139. };
  140. static const unsigned short MAX_SIZE = 128;
  141. static const unsigned short MAX_POOL_SIZE = 8192;
  142. datatype data[MAX_SIZE];
  143. unsigned char dataPool[MAX_POOL_SIZE];
  144. struct {
  145. LV2_Atom atom;
  146. unsigned char data[MAX_POOL_SIZE];
  147. } retAtom;
  148. unsigned short index, indexPool;
  149. bool empty, full;
  150. QMutex mutex;
  151. };
  152. #endif // LV2_ATOM_QUEUE_HPP