Version
menu_open
link
Wwise SDK 2021.1.14
AkRingBuffer.h
Go to the documentation of this file.
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  Version: v2021.1.14 Build: 6590
25  Copyright (c) 2006-2023 Audiokinetic Inc.
26 *******************************************************************************/
27 
28 #pragma once
29 
33 
34 template <AkMemID T_MEMID>
36 {
37  static AkForceInline void* Alloc(size_t in_uSize)
38  {
39  return AkAlloc(T_MEMID, in_uSize);
40  }
41 
42  static AkForceInline void Free(void * in_pAddress)
43  {
44  AkFree(T_MEMID, in_pAddress);
45  }
46 };
47 
48 template <AkMemID T_MEMID>
50 {
51  static AkForceInline void* Alloc(size_t in_uSize)
52  {
53  return AkMalign(T_MEMID, in_uSize, AK_SIMD_ALIGNMENT);
54  }
55 
56  static AkForceInline void Free(void * in_pAddress)
57  {
58  AkFree(T_MEMID, in_pAddress);
59  }
60 };
61 
65 
66 template <class T, class TAlloc = AkRingBufferAllocatorDefault>
67 
68 //Single producer, single consumer pattern implementation.
70 {
71 public:
73  : m_nbItems(0)
74  , m_readIndex(0)
75  , m_writeIndex(0)
76  , m_nbReadableItems(0)
77  {
78  }
79 
81  {
82  m_nbItems = nbItems;
83  m_readIndex = 0;
84  m_writeIndex = 0;
85  m_nbReadableItems = 0;
86 
87  m_data = reinterpret_cast<T*>(TAlloc::Alloc(m_nbItems * sizeof(T)));
88  if (m_data == NULL)
89  {
90  return AK_InsufficientMemory;
91  }
92  return AK_Success;
93  }
94 
95  void Term()
96  {
97  m_nbItems = 0;
98  m_readIndex = 0;
99  m_writeIndex = 0;
100  m_nbReadableItems = 0;
101 
102  if (m_data != NULL)
103  {
104  TAlloc::Free(reinterpret_cast<void*>(m_data));
105  m_data = NULL;
106  }
107  }
108 
109  // Reset ringbuffer to initial state without freeing memory. Not thread-safe.
110  void Reset()
111  {
112  m_readIndex = 0;
113  m_writeIndex = 0;
114  m_nbReadableItems = 0;
115  }
116 
117  // ---- Producer ---- //
118 
120  {
121  return m_writeIndex;
122  }
123 
125  {
126  return &m_data[m_writeIndex];
127  }
128 
130  {
131  AKASSERT(GetNbWritableItems() >= nbItems);
132 
133  m_writeIndex = (m_writeIndex + nbItems) % m_nbItems;
134 
135  AkAtomicAdd32(&m_nbReadableItems, nbItems);
136  }
137 
138  // ---- Consumer ----
139 
141  {
142  return m_readIndex;
143  }
144 
145  const T* GetReadPtr() const
146  {
147  return &m_data[m_readIndex];
148  }
149 
151  {
152  AKASSERT((AkUInt32)m_nbReadableItems >= nbItems);
153 
154  m_readIndex = (m_readIndex + nbItems) % m_nbItems;
155 
156  AkAtomicSub32(&m_nbReadableItems, nbItems);
157  }
158 
160  {
161  return m_nbReadableItems;
162  }
163 
165  {
166  return m_nbItems - m_nbReadableItems;
167  }
168 
169  AkUInt32 Size() const
170  {
171  return m_nbItems;
172  }
173 
174 private:
175  T* m_data{nullptr};
176 
177  AkUInt32 m_nbItems;
178  AkUInt32 m_readIndex;
179  AkUInt32 m_writeIndex;
180  AkAtomic32 m_nbReadableItems;
181 };
__forceinline long AkAtomicAdd32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:63
AkRingBuffer()
Definition: AkRingBuffer.h:72
AkUInt32 GetReadIndex() const
Definition: AkRingBuffer.h:140
const T * GetReadPtr() const
Definition: AkRingBuffer.h:145
AkUInt32 GetWriteIndex() const
Definition: AkRingBuffer.h:119
#define AkFree(_pool, _pvmem)
Definition: AkObject.h:83
static AkForceInline void Free(void *in_pAddress)
Definition: AkRingBuffer.h:56
AKSOUNDENGINE_API void Free(AkMemPoolId in_poolId, void *in_pMemAddress)
static AkForceInline void * Alloc(size_t in_uSize)
Definition: AkRingBuffer.h:37
AKRESULT
Standard function call result.
Definition: AkTypes.h:132
AkRingBufferAllocatorAligned< AkMemID_Processing > AkRingBufferAllocatorLEngineAligned
Definition: AkRingBuffer.h:64
void Term()
Definition: AkRingBuffer.h:95
static AkForceInline void * Alloc(size_t in_uSize)
Definition: AkRingBuffer.h:51
AkRingBufferAllocatorNoAlign< AkMemID_Object > AkRingBufferAllocatorDefault
Definition: AkRingBuffer.h:62
#define AkAlloc(_pool, _size)
Definition: AkObject.h:76
#define NULL
Definition: AkTypes.h:47
@ AK_Success
The operation was successful.
Definition: AkTypes.h:134
static AkForceInline void Free(void *in_pAddress)
Definition: AkRingBuffer.h:42
void IncrementReadIndex(AkUInt32 nbItems)
Definition: AkRingBuffer.h:150
void Reset()
Definition: AkRingBuffer.h:110
AkUInt32 GetNbReadableItems() const
Definition: AkRingBuffer.h:159
AkUInt32 Size() const
Definition: AkRingBuffer.h:169
AkUInt32 GetNbWritableItems() const
Definition: AkRingBuffer.h:164
#define AKASSERT(Condition)
Definition: AkAssert.h:76
AKRESULT Init(AkUInt32 nbItems)
Definition: AkRingBuffer.h:80
T * GetWritePtr()
Definition: AkRingBuffer.h:124
#define AK_SIMD_ALIGNMENT
Platform-specific alignment requirement for SIMD data.
Definition: AkTypes.h:50
#define AkMalign(_pool, _size, _align)
Definition: AkObject.h:77
volatile long AkAtomic32
Definition: AkAtomic.h:40
void IncrementWriteIndex(AkUInt32 nbItems)
Definition: AkRingBuffer.h:129
__forceinline long AkAtomicSub32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:64
uint32_t AkUInt32
Unsigned 32-bit integer.
Definition: AkTypes.h:59
@ AK_InsufficientMemory
Memory error.
Definition: AkTypes.h:162
#define AkForceInline
Definition: AkTypes.h:60
AkRingBufferAllocatorNoAlign< AkMemID_Processing > AkRingBufferAllocatorLEngine
Definition: AkRingBuffer.h:63

Was this page helpful?

Need Support?

Questions? Problems? Need more info? Contact us, and we can help!

Visit our Support page

Tell us about your project. We're here to help.

Register your project and we'll help you get started with no strings attached!

Get started with Wwise