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.

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