-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRingBuffer.cpp
More file actions
47 lines (40 loc) · 1.57 KB
/
RingBuffer.cpp
File metadata and controls
47 lines (40 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "RingBuffer.h"
using namespace BA;
RingBuffer::RingBuffer (const size_t bufferSize,
const size_t readDistance)
:
bufferSize (bufferSize),
readDistance (readDistance),
readIndex(0),
writeIndex(0)
{
buffer.resize (bufferSize); // Resize the buffer vector to the capacity
for (auto& block : buffer) {
block.setSize (128000, true); // Preallocate each block
}
}
bool RingBuffer::push (const MemoryBlock& data)
{
size_t currentWriteIndex = writeIndex.load(std::memory_order_relaxed);
size_t nextWriteIndex = (currentWriteIndex + 1) % bufferSize;
// Ensure write does not catch up to the readIndex minus the readDistance
size_t expectedReadIndex = (readIndex.load(std::memory_order_acquire) + bufferSize - readDistance) % bufferSize;
if (nextWriteIndex != expectedReadIndex) {
buffer[currentWriteIndex].replaceAll(data.getData(), data.getSize());
writeIndex.store(nextWriteIndex, std::memory_order_release);
return true;
}
return false; // Buffer full
}
bool RingBuffer::pop (juce::MemoryBlock& data)
{
size_t currentReadIndex = readIndex.load(std::memory_order_relaxed);
size_t currentWriteIndex = writeIndex.load(std::memory_order_acquire);
// Allow read if there is enough data written ahead of the current read index
if (currentReadIndex != currentWriteIndex) {
data = buffer[currentReadIndex];
readIndex.store((currentReadIndex + 1) % bufferSize, std::memory_order_release);
return true;
}
return false; // No data available to read
}