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 #include "alnumeric.h"
35 RingBufferPtr
RingBuffer::Create(std::size_t sz
, std::size_t elem_sz
, bool limit_writes
)
37 std::size_t power_of_two
{0u};
41 power_of_two
|= power_of_two
>>1;
42 power_of_two
|= power_of_two
>>2;
43 power_of_two
|= power_of_two
>>4;
44 power_of_two
|= power_of_two
>>8;
45 power_of_two
|= power_of_two
>>16;
46 if constexpr(sizeof(size_t) > sizeof(uint32_t))
47 power_of_two
|= power_of_two
>>32;
50 if(power_of_two
<= sz
|| power_of_two
> std::numeric_limits
<std::size_t>::max()/elem_sz
)
51 throw std::overflow_error
{"Ring buffer size overflow"};
53 const std::size_t bufbytes
{power_of_two
* elem_sz
};
54 RingBufferPtr rb
{new(FamCount(bufbytes
)) RingBuffer
{bufbytes
}};
55 rb
->mWriteSize
= limit_writes
? sz
: (power_of_two
-1);
56 rb
->mSizeMask
= power_of_two
- 1;
57 rb
->mElemSize
= elem_sz
;
62 void RingBuffer::reset() noexcept
64 mWritePtr
.store(0, std::memory_order_relaxed
);
65 mReadPtr
.store(0, std::memory_order_relaxed
);
66 std::fill_n(mBuffer
.begin(), (mSizeMask
+1)*mElemSize
, std::byte
{});
70 std::size_t RingBuffer::read(void *dest
, std::size_t cnt
) noexcept
72 const std::size_t free_cnt
{readSpace()};
73 if(free_cnt
== 0) return 0;
75 const std::size_t to_read
{std::min(cnt
, free_cnt
)};
76 std::size_t read_ptr
{mReadPtr
.load(std::memory_order_relaxed
) & mSizeMask
};
78 const std::size_t cnt2
{read_ptr
+ to_read
};
79 const auto [n1
, n2
] = (cnt2
<= mSizeMask
+1) ? std::make_tuple(to_read
, 0_uz
)
80 : std::make_tuple(mSizeMask
+1 - read_ptr
, cnt2
&mSizeMask
);
82 auto outiter
= std::copy_n(mBuffer
.begin() + read_ptr
*mElemSize
, n1
*mElemSize
,
83 static_cast<std::byte
*>(dest
));
87 std::copy_n(mBuffer
.begin(), n2
*mElemSize
, outiter
);
90 mReadPtr
.store(read_ptr
, std::memory_order_release
);
94 std::size_t RingBuffer::peek(void *dest
, std::size_t cnt
) const noexcept
96 const std::size_t free_cnt
{readSpace()};
97 if(free_cnt
== 0) return 0;
99 const std::size_t to_read
{std::min(cnt
, free_cnt
)};
100 std::size_t read_ptr
{mReadPtr
.load(std::memory_order_relaxed
) & mSizeMask
};
102 const std::size_t cnt2
{read_ptr
+ to_read
};
103 const auto [n1
, n2
] = (cnt2
<= mSizeMask
+1) ? std::make_tuple(to_read
, 0_uz
)
104 : std::make_tuple(mSizeMask
+1 - read_ptr
, cnt2
&mSizeMask
);
106 auto outiter
= std::copy_n(mBuffer
.begin() + read_ptr
*mElemSize
, n1
*mElemSize
,
107 static_cast<std::byte
*>(dest
));
109 std::copy_n(mBuffer
.begin(), n2
*mElemSize
, outiter
);
113 std::size_t RingBuffer::write(const void *src
, std::size_t cnt
) noexcept
115 const std::size_t free_cnt
{writeSpace()};
116 if(free_cnt
== 0) return 0;
118 const std::size_t to_write
{std::min(cnt
, free_cnt
)};
119 std::size_t write_ptr
{mWritePtr
.load(std::memory_order_relaxed
) & mSizeMask
};
121 const std::size_t cnt2
{write_ptr
+ to_write
};
122 const auto [n1
, n2
] = (cnt2
<= mSizeMask
+1) ? std::make_tuple(to_write
, 0_uz
)
123 : std::make_tuple(mSizeMask
+1 - write_ptr
, cnt2
&mSizeMask
);
125 auto srcbytes
= static_cast<const std::byte
*>(src
);
126 std::copy_n(srcbytes
, n1
*mElemSize
, mBuffer
.begin() + write_ptr
*mElemSize
);
130 std::copy_n(srcbytes
+ n1
*mElemSize
, n2
*mElemSize
, mBuffer
.begin());
133 mWritePtr
.store(write_ptr
, std::memory_order_release
);
138 auto RingBuffer::getReadVector() noexcept
-> DataPair
142 std::size_t w
{mWritePtr
.load(std::memory_order_acquire
)};
143 std::size_t r
{mReadPtr
.load(std::memory_order_acquire
)};
146 const std::size_t free_cnt
{(w
-r
) & mSizeMask
};
148 const std::size_t cnt2
{r
+ free_cnt
};
149 if(cnt2
> mSizeMask
+1)
151 /* Two part vector: the rest of the buffer after the current read ptr,
152 * plus some from the start of the buffer. */
153 ret
.first
.buf
= mBuffer
.data() + r
*mElemSize
;
154 ret
.first
.len
= mSizeMask
+1 - r
;
155 ret
.second
.buf
= mBuffer
.data();
156 ret
.second
.len
= cnt2
& mSizeMask
;
160 /* Single part vector: just the rest of the buffer */
161 ret
.first
.buf
= mBuffer
.data() + r
*mElemSize
;
162 ret
.first
.len
= free_cnt
;
163 ret
.second
.buf
= nullptr;
170 auto RingBuffer::getWriteVector() noexcept
-> DataPair
174 std::size_t w
{mWritePtr
.load(std::memory_order_acquire
)};
175 std::size_t r
{mReadPtr
.load(std::memory_order_acquire
) + mWriteSize
- mSizeMask
};
178 const std::size_t free_cnt
{(r
-w
-1) & mSizeMask
};
180 const std::size_t cnt2
{w
+ free_cnt
};
181 if(cnt2
> mSizeMask
+1)
183 /* Two part vector: the rest of the buffer after the current write ptr,
184 * plus some from the start of the buffer. */
185 ret
.first
.buf
= mBuffer
.data() + w
*mElemSize
;
186 ret
.first
.len
= mSizeMask
+1 - w
;
187 ret
.second
.buf
= mBuffer
.data();
188 ret
.second
.len
= cnt2
& mSizeMask
;
192 ret
.first
.buf
= mBuffer
.data() + w
*mElemSize
;
193 ret
.first
.len
= free_cnt
;
194 ret
.second
.buf
= nullptr;