版本

menu_open
Wwise SDK 2023.1.9
AkRingBuffer.h
浏览该文件的文档.
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 
5 Commercial License Usage
6 
7 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
8 may use this file in accordance with the end user license agreement provided
9 with the software or, alternatively, in accordance with the terms contained in a
10 written agreement between you and Audiokinetic Inc.
11 
12 Apache License Usage
13 
14 Alternatively, this file may be used under the Apache License, Version 2.0 (the
15 "Apache License"); you may not use this file except in compliance with the
16 Apache License. You may obtain a copy of the Apache License at
17 http://www.apache.org/licenses/LICENSE-2.0.
18 
19 Unless required by applicable law or agreed to in writing, software distributed
20 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
21 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
22 the specific language governing permissions and limitations under the License.
23 
24  Copyright (c) 2024 Audiokinetic Inc.
25 *******************************************************************************/
26 
27 #pragma once
28 
32 
33 template <AkMemID T_MEMID>
35 {
36  static AkForceInline void* Alloc(size_t in_uSize)
37  {
38  return AkAlloc(T_MEMID, in_uSize);
39  }
40 
41  static AkForceInline void Free(void * in_pAddress)
42  {
43  AkFree(T_MEMID, in_pAddress);
44  }
45 };
46 
47 template <AkMemID T_MEMID>
49 {
50  static AkForceInline void* Alloc(size_t in_uSize)
51  {
52  return AkMalign(T_MEMID, in_uSize, AK_SIMD_ALIGNMENT);
53  }
54 
55  static AkForceInline void Free(void * in_pAddress)
56  {
57  AkFree(T_MEMID, in_pAddress);
58  }
59 };
60 
64 
65 template <class T, class TAlloc = AkRingBufferAllocatorDefault>
66 
67 //Single producer, single consumer pattern implementation.
69 {
70 public:
72  : m_nbItems(0)
73  , m_readIndex(0)
74  , m_writeIndex(0)
75  , m_nbReadableItems(0)
76  {
77  }
78 
80  {
81  m_nbItems = nbItems;
82  m_readIndex = 0;
83  m_writeIndex = 0;
84  m_nbReadableItems = 0;
85 
86  m_data = reinterpret_cast<T*>(TAlloc::Alloc(m_nbItems * sizeof(T)));
87  if (m_data == NULL)
88  {
89  return AK_InsufficientMemory;
90  }
91  return AK_Success;
92  }
93 
94  void Term()
95  {
96  m_nbItems = 0;
97  m_readIndex = 0;
98  m_writeIndex = 0;
99  m_nbReadableItems = 0;
100 
101  if (m_data != NULL)
102  {
103  TAlloc::Free(reinterpret_cast<void*>(m_data));
104  m_data = NULL;
105  }
106  }
107 
108  // Reset ringbuffer to initial state without freeing memory. Not thread-safe.
109  void Reset()
110  {
111  m_readIndex = 0;
112  m_writeIndex = 0;
113  m_nbReadableItems = 0;
114  }
115 
116  // ---- Producer ---- //
117 
119  {
120  return m_writeIndex;
121  }
122 
124  {
125  return &m_data[m_writeIndex];
126  }
127 
129  {
130  AKASSERT(GetNbWritableItems() >= nbItems);
131 
132  m_writeIndex = (m_writeIndex + nbItems) % m_nbItems;
133 
134  AkAtomicAdd32(&m_nbReadableItems, nbItems);
135  }
136 
137  // ---- Consumer ----
138 
140  {
141  return m_readIndex;
142  }
143 
144  const T* GetReadPtr() const
145  {
146  return &m_data[m_readIndex];
147  }
148 
149  // Peek at any item between the read and write pointer without advancing the read pointer.
150  const T* Peek(AkUInt32 uOffset) const
151  {
152  AKASSERT(GetNbReadableItems() > uOffset);
153  AkUInt32 uReadIndex = (m_readIndex + uOffset) % m_nbItems;
154  return &m_data[uReadIndex];
155  }
156 
158  {
159  AKASSERT(GetNbReadableItems() >= nbItems);
160 
161  m_readIndex = (m_readIndex + nbItems) % m_nbItems;
162 
163  AkAtomicSub32(&m_nbReadableItems, nbItems);
164  }
165 
167  {
168  return (AkUInt32)AkAtomicLoad32(const_cast<AkAtomic32*>(&m_nbReadableItems));
169  }
170 
172  {
173  return m_nbItems - GetNbReadableItems();
174  }
175 
176  AkUInt32 Size() const
177  {
178  return m_nbItems;
179  }
180 
181  // Warning: requires external locking to prevent concurrent Grow+Read in a multi-threaded scenario.
182  // Like the rest of the class, assumes a single writing thread.
183  bool Grow(AkUInt32 in_uGrowBy)
184  {
185  AkUInt32 uTargetItems = m_nbItems + in_uGrowBy;
186  if (T* pNewData = reinterpret_cast<T*>(TAlloc::Alloc(uTargetItems * sizeof(T))))
187  {
188  AkUInt32 uReadableItems = GetNbReadableItems();
189  if (uReadableItems)
190  {
191  if (m_readIndex >= m_writeIndex)
192  {
193  // insert new free space in the middle of the buffer.
194 
195  if (m_writeIndex)
196  memcpy(pNewData, m_data, sizeof(T) * m_writeIndex);
197 
198  memcpy(pNewData + m_readIndex + in_uGrowBy, m_data + m_readIndex, sizeof(T) * (m_nbItems - m_readIndex));
199  m_readIndex += in_uGrowBy;
200  }
201  else
202  {
203  // insert new free space at the end of the buffer.
204 
205  memcpy(pNewData + m_readIndex, m_data + m_readIndex, sizeof(T) * uReadableItems);
206  }
207  }
208 
209  TAlloc::Free(reinterpret_cast<void*>(m_data));
210  m_data = pNewData;
211  m_nbItems = uTargetItems;
212 
213  return true;
214  }
215  else
216  {
217  return false;
218  }
219  }
220 
221 private:
222  T* m_data{nullptr};
223 
224  AkUInt32 m_nbItems;
225  AkUInt32 m_readIndex;
226  AkUInt32 m_writeIndex;
227  AkAtomic32 m_nbReadableItems;
228 };
const T * Peek(AkUInt32 uOffset) const
Definition: AkRingBuffer.h:150
__forceinline long AkAtomicAdd32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:64
AkUInt32 GetReadIndex() const
Definition: AkRingBuffer.h:139
const T * GetReadPtr() const
Definition: AkRingBuffer.h:144
AkUInt32 GetWriteIndex() const
Definition: AkRingBuffer.h:118
#define AkFree(_pool, _pvmem)
Definition: AkObject.h:82
static AkForceInline void Free(void *in_pAddress)
Definition: AkRingBuffer.h:55
AKSOUNDENGINE_API void Free(AkMemPoolId in_poolId, void *in_pMemAddress)
static AkForceInline void * Alloc(size_t in_uSize)
Definition: AkRingBuffer.h:36
AKRESULT
Standard function call result.
Definition: AkTypes.h:131
AkRingBufferAllocatorAligned< AkMemID_Processing > AkRingBufferAllocatorLEngineAligned
Definition: AkRingBuffer.h:63
static AkForceInline void * Alloc(size_t in_uSize)
Definition: AkRingBuffer.h:50
AkRingBufferAllocatorNoAlign< AkMemID_Object > AkRingBufferAllocatorDefault
Definition: AkRingBuffer.h:61
#define AkAlloc(_pool, _size)
Definition: AkObject.h:75
#define NULL
Definition: AkTypes.h:46
@ AK_Success
The operation was successful.
Definition: AkTypes.h:133
static AkForceInline void Free(void *in_pAddress)
Definition: AkRingBuffer.h:41
void IncrementReadIndex(AkUInt32 nbItems)
Definition: AkRingBuffer.h:157
volatile uint32_t AkAtomic32
Definition: AkAtomic.h:45
AkUInt32 GetNbReadableItems() const
Definition: AkRingBuffer.h:166
AkUInt32 Size() const
Definition: AkRingBuffer.h:176
AkUInt32 GetNbWritableItems() const
Definition: AkRingBuffer.h:171
#define AKASSERT(Condition)
Definition: AkAssert.h:67
AKRESULT Init(AkUInt32 nbItems)
Definition: AkRingBuffer.h:79
T * GetWritePtr()
Definition: AkRingBuffer.h:123
#define AK_SIMD_ALIGNMENT
Platform-specific alignment requirement for SIMD data
Definition: AkTypes.h:52
#define AkMalign(_pool, _size, _align)
Definition: AkObject.h:76
void IncrementWriteIndex(AkUInt32 nbItems)
Definition: AkRingBuffer.h:128
__forceinline long AkAtomicSub32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:65
uint32_t AkUInt32
Unsigned 32-bit integer
@ AK_InsufficientMemory
Memory error.
Definition: AkTypes.h:161
#define AkForceInline
Definition: AkTypes.h:63
__forceinline long AkAtomicLoad32(AkAtomic32 *pSrc)
Definition: AkAtomic.h:58
bool Grow(AkUInt32 in_uGrowBy)
Definition: AkRingBuffer.h:183
AkRingBufferAllocatorNoAlign< AkMemID_Processing > AkRingBufferAllocatorLEngine
Definition: AkRingBuffer.h:62

此页面对您是否有帮助?

需要技术支持?

仍有疑问?或者问题?需要更多信息?欢迎联系我们,我们可以提供帮助!

查看我们的“技术支持”页面

介绍一下自己的项目。我们会竭力为您提供帮助。

来注册自己的项目,我们帮您快速入门,不带任何附加条件!

开始 Wwise 之旅