forked from marcdinkum/ringbuffer
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathringbuffer.cpp
More file actions
Latest commit
155 lines (111 loc) · 3.6 KB
/
Copy pathringbuffer.cpp
File metadata and controls
155 lines (111 loc) · 3.6 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include<iostream>
#include"ringbuffer.h"
#include<unistd.h>
#include<string.h>
// Size is specified as #items, not bytes.
template<typename FloatType>
RingBuffer<FloatType>::RingBuffer(const uint64 size, const std::string& name) :
size(size),
buffer(new FloatType[size]),
tail(0),
head(0),
name(name),
blockingPush(false),
blockingPop(false)
{
}
template<typename FloatType>
RingBuffer<FloatType>::~RingBuffer()
{
delete[] buffer;
}
template<typename FloatType>
auto RingBuffer<FloatType>::numItemsAvailableForWrite() const -> uint64
{
// signed space between head and tail index
constlong pointerSpace = head.load() - tail.load();
// NB: > 0 so NOT including 0
return pointerSpace > 0 ? pointerSpace : pointerSpace + size;
}
template<typename FloatType>
auto RingBuffer<FloatType>::numItemsAvailableForRead() const -> uint64
{
// signed space between tail and head index
constlong pointerSpace = tail.load() - head.load();
// NB: >= 0 so including 0
return pointerSpace >= 0 ? pointerSpace : pointerSpace + size;
}
template<typename FloatType>
auto RingBuffer<FloatType>::pushMayBlock(bool block) -> void
{
blockingPush = block;
}
template<typename FloatType>
auto RingBuffer<FloatType>::popMayBlock(bool block) -> void
{
blockingPop = block;
}
template<typename FloatType>
auto RingBuffer<FloatType>::setBlockingNap(const uint64 newBlockingNap) -> void
{
blockingNap = newBlockingNap;
}
// Try to write as many items as possible and return the number actually written
template<typename FloatType>
auto RingBuffer<FloatType>::push(FloatType* data, const uint64 numSamples) -> uint64
{
auto space = size;
if(blockingPush)
while((space = numItemsAvailableForWrite()) < numSamples)
usleep(static_cast<useconds_t>(blockingNap));
if(space == 0) return0;
constauto numToWrite = numSamples <= space ? numSamples : space;
constauto currentTail = tail.load();
// wrap if needed
if(currentTail + numToWrite <= size)
{
memcpy(buffer + currentTail, data, numToWrite * itemSize);
}
else
{
constauto firstChunk = size - currentTail;
memcpy(buffer + currentTail, data, firstChunk * itemSize);
memcpy(buffer, data + firstChunk, (numToWrite - firstChunk) * itemSize);
}
tail.store((currentTail + numToWrite) % size);
return numToWrite;
}
// Try to read as many items as possible and return the number actually read
template<typename FloatType>
auto RingBuffer<FloatType>::pop(FloatType* data, const uint64 numSamples) -> uint64
{
auto space = size;
if(blockingPop)
while((space = numItemsAvailableForRead()) < numSamples)
usleep(static_cast<useconds_t>(blockingNap));
if(space == 0) return0;
constauto numToRead = numSamples <= space ? numSamples : space;
constauto currentHead = head.load();
//wrap if needed
if(currentHead + numToRead <= size)
{
memcpy(data, buffer + currentHead, numToRead * itemSize);
}
else
{
constauto firstChunk = size - currentHead;
memcpy(data, buffer + currentHead, firstChunk * itemSize);
memcpy(data + firstChunk, buffer, (numToRead - firstChunk) * itemSize);
}
head.store((currentHead + numToRead) % size);
return numToRead;
}
template<typename FloatType>
auto RingBuffer<FloatType>::isLockFree() const -> bool
{
return (tail.is_lock_free() && head.is_lock_free());
}
//three available types as of now...
template classRingBuffer<float>;
template classRingBuffer<double>;
template classRingBuffer<longdouble>;