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"
32 RingBufferPtr
RingBuffer::Create(size_t sz
, size_t elem_sz
, int limit_writes
)
34 size_t power_of_two
{0u};
38 power_of_two
|= power_of_two
>>1;
39 power_of_two
|= power_of_two
>>2;
40 power_of_two
|= power_of_two
>>4;
41 power_of_two
|= power_of_two
>>8;
42 power_of_two
|= power_of_two
>>16;
43 #if SIZE_MAX > UINT_MAX
44 power_of_two
|= power_of_two
>>32;
48 if(power_of_two
<= sz
|| power_of_two
> std::numeric_limits
<size_t>::max()/elem_sz
)
49 throw std::overflow_error
{"Ring buffer size overflow"};
51 const size_t bufbytes
{power_of_two
* elem_sz
};
52 RingBufferPtr rb
{new(FamCount(bufbytes
)) RingBuffer
{bufbytes
}};
53 rb
->mWriteSize
= limit_writes
? sz
: (power_of_two
-1);
54 rb
->mSizeMask
= power_of_two
- 1;
55 rb
->mElemSize
= elem_sz
;
60 void RingBuffer::reset() noexcept
62 mWritePtr
.store(0, std::memory_order_relaxed
);
63 mReadPtr
.store(0, std::memory_order_relaxed
);
64 std::fill_n(mBuffer
.begin(), (mSizeMask
+1)*mElemSize
, al::byte
{});
68 size_t RingBuffer::read(void *dest
, size_t cnt
) noexcept
70 const size_t free_cnt
{readSpace()};
71 if(free_cnt
== 0) return 0;
73 const size_t to_read
{std::min(cnt
, free_cnt
)};
74 size_t read_ptr
{mReadPtr
.load(std::memory_order_relaxed
) & mSizeMask
};
77 const size_t cnt2
{read_ptr
+ to_read
};
78 if(cnt2
> mSizeMask
+1)
80 n1
= mSizeMask
+1 - read_ptr
;
81 n2
= cnt2
& mSizeMask
;
89 auto outiter
= std::copy_n(mBuffer
.begin() + read_ptr
*mElemSize
, n1
*mElemSize
,
90 static_cast<al::byte
*>(dest
));
94 std::copy_n(mBuffer
.begin(), n2
*mElemSize
, outiter
);
97 mReadPtr
.store(read_ptr
, std::memory_order_release
);
101 size_t RingBuffer::peek(void *dest
, size_t cnt
) const noexcept
103 const size_t free_cnt
{readSpace()};
104 if(free_cnt
== 0) return 0;
106 const size_t to_read
{std::min(cnt
, free_cnt
)};
107 size_t read_ptr
{mReadPtr
.load(std::memory_order_relaxed
) & mSizeMask
};
110 const size_t cnt2
{read_ptr
+ to_read
};
111 if(cnt2
> mSizeMask
+1)
113 n1
= mSizeMask
+1 - read_ptr
;
114 n2
= cnt2
& mSizeMask
;
122 auto outiter
= std::copy_n(mBuffer
.begin() + read_ptr
*mElemSize
, n1
*mElemSize
,
123 static_cast<al::byte
*>(dest
));
125 std::copy_n(mBuffer
.begin(), n2
*mElemSize
, outiter
);
129 size_t RingBuffer::write(const void *src
, size_t cnt
) noexcept
131 const size_t free_cnt
{writeSpace()};
132 if(free_cnt
== 0) return 0;
134 const size_t to_write
{std::min(cnt
, free_cnt
)};
135 size_t write_ptr
{mWritePtr
.load(std::memory_order_relaxed
) & mSizeMask
};
138 const size_t cnt2
{write_ptr
+ to_write
};
139 if(cnt2
> mSizeMask
+1)
141 n1
= mSizeMask
+1 - write_ptr
;
142 n2
= cnt2
& mSizeMask
;
150 auto srcbytes
= static_cast<const al::byte
*>(src
);
151 std::copy_n(srcbytes
, n1
*mElemSize
, mBuffer
.begin() + write_ptr
*mElemSize
);
155 std::copy_n(srcbytes
+ n1
*mElemSize
, n2
*mElemSize
, mBuffer
.begin());
158 mWritePtr
.store(write_ptr
, std::memory_order_release
);
163 auto RingBuffer::getReadVector() const noexcept
-> DataPair
167 size_t w
{mWritePtr
.load(std::memory_order_acquire
)};
168 size_t r
{mReadPtr
.load(std::memory_order_acquire
)};
171 const size_t free_cnt
{(w
-r
) & mSizeMask
};
173 const size_t cnt2
{r
+ free_cnt
};
174 if(cnt2
> mSizeMask
+1)
176 /* Two part vector: the rest of the buffer after the current read ptr,
177 * plus some from the start of the buffer. */
178 ret
.first
.buf
= const_cast<al::byte
*>(mBuffer
.data() + r
*mElemSize
);
179 ret
.first
.len
= mSizeMask
+1 - r
;
180 ret
.second
.buf
= const_cast<al::byte
*>(mBuffer
.data());
181 ret
.second
.len
= cnt2
& mSizeMask
;
185 /* Single part vector: just the rest of the buffer */
186 ret
.first
.buf
= const_cast<al::byte
*>(mBuffer
.data() + r
*mElemSize
);
187 ret
.first
.len
= free_cnt
;
188 ret
.second
.buf
= nullptr;
195 auto RingBuffer::getWriteVector() const noexcept
-> DataPair
199 size_t w
{mWritePtr
.load(std::memory_order_acquire
)};
200 size_t r
{mReadPtr
.load(std::memory_order_acquire
) + mWriteSize
- mSizeMask
};
203 const size_t free_cnt
{(r
-w
-1) & mSizeMask
};
205 const size_t cnt2
{w
+ free_cnt
};
206 if(cnt2
> mSizeMask
+1)
208 /* Two part vector: the rest of the buffer after the current write ptr,
209 * plus some from the start of the buffer. */
210 ret
.first
.buf
= const_cast<al::byte
*>(mBuffer
.data() + w
*mElemSize
);
211 ret
.first
.len
= mSizeMask
+1 - w
;
212 ret
.second
.buf
= const_cast<al::byte
*>(mBuffer
.data());
213 ret
.second
.len
= cnt2
& mSizeMask
;
217 ret
.first
.buf
= const_cast<al::byte
*>(mBuffer
.data() + w
*mElemSize
);
218 ret
.first
.len
= free_cnt
;
219 ret
.second
.buf
= nullptr;