AkRingBuffer.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. #pragma once
  21. #include <AK/SoundEngine/Common/AkTypes.h>
  22. #include <AK/Tools/Common/AkObject.h>
  23. #include <AK/Tools/Common/AkPlatformFuncs.h>
  24. template <AkMemID T_MEMID>
  25. struct AkRingBufferAllocatorNoAlign
  26. {
  27. static AkForceInline void* Alloc(size_t in_uSize)
  28. {
  29. return AkAlloc(T_MEMID, in_uSize);
  30. }
  31. static AkForceInline void Free(void * in_pAddress)
  32. {
  33. AkFree(T_MEMID, in_pAddress);
  34. }
  35. };
  36. template <AkMemID T_MEMID>
  37. struct AkRingBufferAllocatorAligned
  38. {
  39. static AkForceInline void* Alloc(size_t in_uSize)
  40. {
  41. return AkMalign(T_MEMID, in_uSize, AK_SIMD_ALIGNMENT);
  42. }
  43. static AkForceInline void Free(void * in_pAddress)
  44. {
  45. AkFree(T_MEMID, in_pAddress);
  46. }
  47. };
  48. typedef AkRingBufferAllocatorNoAlign<AkMemID_Object> AkRingBufferAllocatorDefault;
  49. typedef AkRingBufferAllocatorNoAlign<AkMemID_Processing> AkRingBufferAllocatorLEngine;
  50. typedef AkRingBufferAllocatorAligned<AkMemID_Processing> AkRingBufferAllocatorLEngineAligned;
  51. template <class T, class TAlloc = AkRingBufferAllocatorDefault>
  52. //Single producer, single consumer pattern implementation.
  53. class AkRingBuffer
  54. {
  55. public:
  56. AkRingBuffer()
  57. : m_nbItems(0)
  58. , m_readIndex(0)
  59. , m_writeIndex(0)
  60. , m_nbReadableItems(0)
  61. {
  62. }
  63. AKRESULT Init(AkUInt32 nbItems)
  64. {
  65. m_nbItems = nbItems;
  66. m_readIndex = 0;
  67. m_writeIndex = 0;
  68. m_nbReadableItems = 0;
  69. m_data = reinterpret_cast<T*>(TAlloc::Alloc(m_nbItems * sizeof(T)));
  70. if (m_data == NULL)
  71. {
  72. return AK_InsufficientMemory;
  73. }
  74. return AK_Success;
  75. }
  76. void Term()
  77. {
  78. m_nbItems = 0;
  79. m_readIndex = 0;
  80. m_writeIndex = 0;
  81. m_nbReadableItems = 0;
  82. if (m_data != NULL)
  83. {
  84. TAlloc::Free(reinterpret_cast<void*>(m_data));
  85. m_data = NULL;
  86. }
  87. }
  88. // Reset ringbuffer to initial state without freeing memory. Not thread-safe.
  89. void Reset()
  90. {
  91. m_readIndex = 0;
  92. m_writeIndex = 0;
  93. m_nbReadableItems = 0;
  94. }
  95. // ---- Producer ---- //
  96. AkUInt32 GetWriteIndex() const
  97. {
  98. return m_writeIndex;
  99. }
  100. T* GetWritePtr()
  101. {
  102. return &m_data[m_writeIndex];
  103. }
  104. void IncrementWriteIndex(AkUInt32 nbItems)
  105. {
  106. AKASSERT(GetNbWritableItems() >= nbItems);
  107. m_writeIndex = (m_writeIndex + nbItems) % m_nbItems;
  108. AkAtomicAdd32(&m_nbReadableItems, nbItems);
  109. }
  110. // ---- Consumer ----
  111. AkUInt32 GetReadIndex() const
  112. {
  113. return m_readIndex;
  114. }
  115. const T* GetReadPtr() const
  116. {
  117. return &m_data[m_readIndex];
  118. }
  119. // Peek at any item between the read and write pointer without advancing the read pointer.
  120. const T* Peek(AkUInt32 uOffset) const
  121. {
  122. AKASSERT((AkUInt32)m_nbReadableItems > uOffset);
  123. AkUInt32 uReadIndex = (m_readIndex + uOffset) % m_nbItems;
  124. return &m_data[uReadIndex];
  125. }
  126. void IncrementReadIndex(AkUInt32 nbItems)
  127. {
  128. AKASSERT((AkUInt32)m_nbReadableItems >= nbItems);
  129. m_readIndex = (m_readIndex + nbItems) % m_nbItems;
  130. AkAtomicSub32(&m_nbReadableItems, nbItems);
  131. }
  132. AkUInt32 GetNbReadableItems() const
  133. {
  134. return m_nbReadableItems;
  135. }
  136. AkUInt32 GetNbWritableItems() const
  137. {
  138. return m_nbItems - (AkUInt32)m_nbReadableItems;
  139. }
  140. AkUInt32 Size() const
  141. {
  142. return m_nbItems;
  143. }
  144. // Warning: requires external locking to prevent concurrent Grow+Read in a multi-threaded scenario.
  145. // Like the rest of the class, assumes a single writing thread.
  146. bool Grow(AkUInt32 in_uGrowBy)
  147. {
  148. AkUInt32 uTargetItems = m_nbItems + in_uGrowBy;
  149. if (T* pNewData = reinterpret_cast<T*>(TAlloc::Alloc(uTargetItems * sizeof(T))))
  150. {
  151. if (m_nbReadableItems)
  152. {
  153. if (m_readIndex >= m_writeIndex)
  154. {
  155. // insert new free space in the middle of the buffer.
  156. if (m_writeIndex)
  157. memcpy(pNewData, m_data, sizeof(T) * m_writeIndex);
  158. memcpy(pNewData + m_readIndex + in_uGrowBy, m_data + m_readIndex, sizeof(T) * (m_nbItems - m_readIndex));
  159. m_readIndex += in_uGrowBy;
  160. }
  161. else
  162. {
  163. // insert new free space at the end of the buffer.
  164. memcpy(pNewData + m_readIndex, m_data + m_readIndex, sizeof(T) * (AkUInt32)m_nbReadableItems);
  165. }
  166. }
  167. TAlloc::Free(reinterpret_cast<void*>(m_data));
  168. m_data = pNewData;
  169. m_nbItems = uTargetItems;
  170. return true;
  171. }
  172. else
  173. {
  174. return false;
  175. }
  176. }
  177. private:
  178. T* m_data{nullptr};
  179. AkUInt32 m_nbItems;
  180. AkUInt32 m_readIndex;
  181. AkUInt32 m_writeIndex;
  182. AkAtomic32 m_nbReadableItems;
  183. };