2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
23 #include "ringbuffer.h"
33 RingBufferPtr
CreateRingBuffer(size_t sz
, size_t elem_sz
, int limit_writes
)
35 size_t power_of_two
{0u};
39 power_of_two
|= power_of_two
>>1;
40 power_of_two
|= power_of_two
>>2;
41 power_of_two
|= power_of_two
>>4;
42 power_of_two
|= power_of_two
>>8;
43 power_of_two
|= power_of_two
>>16;
44 #if SIZE_MAX > UINT_MAX
45 power_of_two
|= power_of_two
>>32;
49 if(power_of_two
<= sz
|| power_of_two
> std::numeric_limits
<size_t>::max()/elem_sz
)
50 throw std::overflow_error
{"Ring buffer size overflow"};
52 const size_t bufbytes
{power_of_two
* elem_sz
};
53 RingBufferPtr rb
{new (FamCount
{bufbytes
}) RingBuffer
{bufbytes
}};
54 rb
->mWriteSize
= limit_writes
? sz
: (power_of_two
-1);
55 rb
->mSizeMask
= power_of_two
- 1;
56 rb
->mElemSize
= elem_sz
;
61 void RingBuffer::reset() noexcept
63 mWritePtr
.store(0, std::memory_order_relaxed
);
64 mReadPtr
.store(0, std::memory_order_relaxed
);
65 std::fill_n(mBuffer
.begin(), (mSizeMask
+1)*mElemSize
, al::byte
{});
69 size_t RingBuffer::readSpace() const noexcept
71 size_t w
= mWritePtr
.load(std::memory_order_acquire
);
72 size_t r
= mReadPtr
.load(std::memory_order_acquire
);
73 return (w
-r
) & mSizeMask
;
76 size_t RingBuffer::writeSpace() const noexcept
78 size_t w
= mWritePtr
.load(std::memory_order_acquire
);
79 size_t r
= mReadPtr
.load(std::memory_order_acquire
) + mWriteSize
- mSizeMask
;
80 return (r
-w
-1) & mSizeMask
;
84 size_t RingBuffer::read(void *dest
, size_t cnt
) noexcept
86 const size_t free_cnt
{readSpace()};
87 if(free_cnt
== 0) return 0;
89 const size_t to_read
{std::min(cnt
, free_cnt
)};
90 size_t read_ptr
{mReadPtr
.load(std::memory_order_relaxed
) & mSizeMask
};
93 const size_t cnt2
{read_ptr
+ to_read
};
94 if(cnt2
> mSizeMask
+1)
96 n1
= mSizeMask
+1 - read_ptr
;
97 n2
= cnt2
& mSizeMask
;
105 auto outiter
= std::copy_n(mBuffer
.begin() + read_ptr
*mElemSize
, n1
*mElemSize
,
106 static_cast<al::byte
*>(dest
));
110 std::copy_n(mBuffer
.begin(), n2
*mElemSize
, outiter
);
113 mReadPtr
.store(read_ptr
, std::memory_order_release
);
117 size_t RingBuffer::peek(void *dest
, size_t cnt
) const noexcept
119 const size_t free_cnt
{readSpace()};
120 if(free_cnt
== 0) return 0;
122 const size_t to_read
{std::min(cnt
, free_cnt
)};
123 size_t read_ptr
{mReadPtr
.load(std::memory_order_relaxed
) & mSizeMask
};
126 const size_t cnt2
{read_ptr
+ to_read
};
127 if(cnt2
> mSizeMask
+1)
129 n1
= mSizeMask
+1 - read_ptr
;
130 n2
= cnt2
& mSizeMask
;
138 auto outiter
= std::copy_n(mBuffer
.begin() + read_ptr
*mElemSize
, n1
*mElemSize
,
139 static_cast<al::byte
*>(dest
));
141 std::copy_n(mBuffer
.begin(), n2
*mElemSize
, outiter
);
145 size_t RingBuffer::write(const void *src
, size_t cnt
) noexcept
147 const size_t free_cnt
{writeSpace()};
148 if(free_cnt
== 0) return 0;
150 const size_t to_write
{std::min(cnt
, free_cnt
)};
151 size_t write_ptr
{mWritePtr
.load(std::memory_order_relaxed
) & mSizeMask
};
154 const size_t cnt2
{write_ptr
+ to_write
};
155 if(cnt2
> mSizeMask
+1)
157 n1
= mSizeMask
+1 - write_ptr
;
158 n2
= cnt2
& mSizeMask
;
166 auto srcbytes
= static_cast<const al::byte
*>(src
);
167 std::copy_n(srcbytes
, n1
*mElemSize
, mBuffer
.begin() + write_ptr
*mElemSize
);
171 std::copy_n(srcbytes
+ n1
*mElemSize
, n2
*mElemSize
, mBuffer
.begin());
174 mWritePtr
.store(write_ptr
, std::memory_order_release
);
179 void RingBuffer::readAdvance(size_t cnt
) noexcept
181 mReadPtr
.fetch_add(cnt
, std::memory_order_acq_rel
);
184 void RingBuffer::writeAdvance(size_t cnt
) noexcept
186 mWritePtr
.fetch_add(cnt
, std::memory_order_acq_rel
);
190 ll_ringbuffer_data_pair
RingBuffer::getReadVector() const noexcept
192 ll_ringbuffer_data_pair ret
;
194 size_t w
{mWritePtr
.load(std::memory_order_acquire
)};
195 size_t r
{mReadPtr
.load(std::memory_order_acquire
)};
198 const size_t free_cnt
{(w
-r
) & mSizeMask
};
200 const size_t cnt2
{r
+ free_cnt
};
201 if(cnt2
> mSizeMask
+1)
203 /* Two part vector: the rest of the buffer after the current read ptr,
204 * plus some from the start of the buffer. */
205 ret
.first
.buf
= const_cast<al::byte
*>(mBuffer
.data() + r
*mElemSize
);
206 ret
.first
.len
= mSizeMask
+1 - r
;
207 ret
.second
.buf
= const_cast<al::byte
*>(mBuffer
.data());
208 ret
.second
.len
= cnt2
& mSizeMask
;
212 /* Single part vector: just the rest of the buffer */
213 ret
.first
.buf
= const_cast<al::byte
*>(mBuffer
.data() + r
*mElemSize
);
214 ret
.first
.len
= free_cnt
;
215 ret
.second
.buf
= nullptr;
222 ll_ringbuffer_data_pair
RingBuffer::getWriteVector() const noexcept
224 ll_ringbuffer_data_pair ret
;
226 size_t w
{mWritePtr
.load(std::memory_order_acquire
)};
227 size_t r
{mReadPtr
.load(std::memory_order_acquire
) + mWriteSize
- mSizeMask
};
230 const size_t free_cnt
{(r
-w
-1) & mSizeMask
};
232 const size_t cnt2
{w
+ free_cnt
};
233 if(cnt2
> mSizeMask
+1)
235 /* Two part vector: the rest of the buffer after the current write ptr,
236 * plus some from the start of the buffer. */
237 ret
.first
.buf
= const_cast<al::byte
*>(mBuffer
.data() + w
*mElemSize
);
238 ret
.first
.len
= mSizeMask
+1 - w
;
239 ret
.second
.buf
= const_cast<al::byte
*>(mBuffer
.data());
240 ret
.second
.len
= cnt2
& mSizeMask
;
244 ret
.first
.buf
= const_cast<al::byte
*>(mBuffer
.data() + w
*mElemSize
);
245 ret
.first
.len
= free_cnt
;
246 ret
.second
.buf
= nullptr;