2007-12-16 18:36:06 -08:00
|
|
|
/**
|
|
|
|
|
* OpenAL cross platform audio library
|
|
|
|
|
* Copyright (C) 1999-2007 by authors.
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
2014-08-18 14:11:03 +02:00
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-16 18:36:06 -08:00
|
|
|
* Or go to http://www.gnu.org/copyleft/lgpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
2008-01-16 14:09:04 -08:00
|
|
|
#include "config.h"
|
|
|
|
|
|
2019-07-28 21:29:59 -07:00
|
|
|
#include "ringbuffer.h"
|
2007-12-16 18:36:06 -08:00
|
|
|
|
2018-12-26 21:22:17 -08:00
|
|
|
#include <algorithm>
|
2023-12-10 22:15:17 -08:00
|
|
|
#include <cstdint>
|
2023-12-08 10:11:08 -08:00
|
|
|
#include <limits>
|
2019-10-08 21:52:08 -07:00
|
|
|
#include <stdexcept>
|
2024-02-13 10:45:15 -08:00
|
|
|
#include <tuple>
|
2018-12-26 21:22:17 -08:00
|
|
|
|
2024-01-19 06:49:39 -08:00
|
|
|
#include "alnumeric.h"
|
2007-12-16 18:36:06 -08:00
|
|
|
|
|
|
|
|
|
2024-02-23 06:57:08 -08:00
|
|
|
auto RingBuffer::Create(std::size_t sz, std::size_t elem_sz, bool limit_writes) -> RingBufferPtr
|
2014-12-23 19:24:41 -08:00
|
|
|
{
|
2023-05-04 11:39:13 -07:00
|
|
|
std::size_t power_of_two{0u};
|
2018-01-11 10:03:26 -08:00
|
|
|
if(sz > 0)
|
|
|
|
|
{
|
2024-02-23 07:49:09 -08:00
|
|
|
power_of_two = sz - 1;
|
2018-01-11 10:03:26 -08:00
|
|
|
power_of_two |= power_of_two>>1;
|
|
|
|
|
power_of_two |= power_of_two>>2;
|
|
|
|
|
power_of_two |= power_of_two>>4;
|
|
|
|
|
power_of_two |= power_of_two>>8;
|
|
|
|
|
power_of_two |= power_of_two>>16;
|
2023-12-15 10:00:23 -08:00
|
|
|
if constexpr(sizeof(size_t) > sizeof(uint32_t))
|
2023-05-04 11:39:13 -07:00
|
|
|
power_of_two |= power_of_two>>32;
|
2018-01-11 10:03:26 -08:00
|
|
|
}
|
2018-12-27 12:55:43 -08:00
|
|
|
++power_of_two;
|
2024-03-09 23:39:56 -08:00
|
|
|
if(power_of_two < sz || power_of_two > std::numeric_limits<std::size_t>::max()>>1
|
2024-02-23 07:49:09 -08:00
|
|
|
|| power_of_two > std::numeric_limits<std::size_t>::max()/elem_sz)
|
2019-10-08 21:52:08 -07:00
|
|
|
throw std::overflow_error{"Ring buffer size overflow"};
|
2014-12-23 19:24:41 -08:00
|
|
|
|
2023-05-04 11:39:13 -07:00
|
|
|
const std::size_t bufbytes{power_of_two * elem_sz};
|
2024-02-23 07:49:09 -08:00
|
|
|
RingBufferPtr rb{new(FamCount(bufbytes)) RingBuffer{limit_writes ? sz : power_of_two,
|
2024-02-23 06:57:08 -08:00
|
|
|
power_of_two-1, elem_sz, bufbytes}};
|
2018-12-27 12:55:43 -08:00
|
|
|
|
2014-12-23 19:24:41 -08:00
|
|
|
return rb;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 21:22:17 -08:00
|
|
|
void RingBuffer::reset() noexcept
|
2014-12-23 19:24:41 -08:00
|
|
|
{
|
2024-02-23 23:24:30 -08:00
|
|
|
mWriteCount.store(0, std::memory_order_relaxed);
|
|
|
|
|
mReadCount.store(0, std::memory_order_relaxed);
|
2023-05-04 11:39:13 -07:00
|
|
|
std::fill_n(mBuffer.begin(), (mSizeMask+1)*mElemSize, std::byte{});
|
2014-12-23 19:24:41 -08:00
|
|
|
}
|
|
|
|
|
|
2018-03-02 12:46:31 -08:00
|
|
|
|
2024-02-23 23:24:30 -08:00
|
|
|
auto RingBuffer::read(void *dest, std::size_t count) noexcept -> std::size_t
|
2014-12-23 19:24:41 -08:00
|
|
|
{
|
2024-02-23 23:24:30 -08:00
|
|
|
const std::size_t w{mWriteCount.load(std::memory_order_acquire)};
|
|
|
|
|
const std::size_t r{mReadCount.load(std::memory_order_relaxed)};
|
|
|
|
|
const std::size_t readable{w - r};
|
|
|
|
|
if(readable == 0) return 0;
|
2014-12-23 19:24:41 -08:00
|
|
|
|
2024-02-23 23:24:30 -08:00
|
|
|
const std::size_t to_read{std::min(count, readable)};
|
|
|
|
|
const std::size_t read_idx{r & mSizeMask};
|
2016-11-03 00:47:22 -07:00
|
|
|
|
2024-02-23 23:24:30 -08:00
|
|
|
const std::size_t rdend{read_idx + to_read};
|
|
|
|
|
const auto [n1, n2] = (rdend <= mSizeMask+1) ? std::make_tuple(to_read, 0_uz)
|
|
|
|
|
: std::make_tuple(mSizeMask+1 - read_idx, rdend&mSizeMask);
|
2014-12-23 19:24:41 -08:00
|
|
|
|
2024-03-12 08:12:37 -07:00
|
|
|
auto dstbytes = al::span{static_cast<std::byte*>(dest), count*mElemSize};
|
2024-03-16 07:51:23 -07:00
|
|
|
auto outiter = std::copy_n(mBuffer.begin() + ptrdiff_t(read_idx*mElemSize), n1*mElemSize,
|
2024-03-12 08:12:37 -07:00
|
|
|
dstbytes.begin());
|
2019-01-03 15:54:18 -08:00
|
|
|
if(n2 > 0)
|
2019-06-05 23:29:13 -07:00
|
|
|
std::copy_n(mBuffer.begin(), n2*mElemSize, outiter);
|
2024-02-23 23:24:30 -08:00
|
|
|
mReadCount.store(r+n1+n2, std::memory_order_release);
|
2014-12-23 19:24:41 -08:00
|
|
|
return to_read;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-23 23:24:30 -08:00
|
|
|
auto RingBuffer::peek(void *dest, std::size_t count) const noexcept -> std::size_t
|
2014-12-23 19:24:41 -08:00
|
|
|
{
|
2024-02-23 23:24:30 -08:00
|
|
|
const std::size_t w{mWriteCount.load(std::memory_order_acquire)};
|
|
|
|
|
const std::size_t r{mReadCount.load(std::memory_order_relaxed)};
|
|
|
|
|
const std::size_t readable{w - r};
|
|
|
|
|
if(readable == 0) return 0;
|
2014-12-23 19:24:41 -08:00
|
|
|
|
2024-02-23 23:24:30 -08:00
|
|
|
const std::size_t to_read{std::min(count, readable)};
|
|
|
|
|
const std::size_t read_idx{r & mSizeMask};
|
2016-11-03 00:47:22 -07:00
|
|
|
|
2024-02-23 23:24:30 -08:00
|
|
|
const std::size_t rdend{read_idx + to_read};
|
|
|
|
|
const auto [n1, n2] = (rdend <= mSizeMask+1) ? std::make_tuple(to_read, 0_uz)
|
|
|
|
|
: std::make_tuple(mSizeMask+1 - read_idx, rdend&mSizeMask);
|
2014-12-23 19:24:41 -08:00
|
|
|
|
2024-03-12 08:12:37 -07:00
|
|
|
auto dstbytes = al::span{static_cast<std::byte*>(dest), count*mElemSize};
|
2024-03-16 07:51:23 -07:00
|
|
|
auto outiter = std::copy_n(mBuffer.begin() + ptrdiff_t(read_idx*mElemSize), n1*mElemSize,
|
2024-03-12 08:12:37 -07:00
|
|
|
dstbytes.begin());
|
2019-01-03 15:54:18 -08:00
|
|
|
if(n2 > 0)
|
2019-06-05 23:29:13 -07:00
|
|
|
std::copy_n(mBuffer.begin(), n2*mElemSize, outiter);
|
2014-12-23 19:24:41 -08:00
|
|
|
return to_read;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-23 23:24:30 -08:00
|
|
|
auto RingBuffer::write(const void *src, std::size_t count) noexcept -> std::size_t
|
2014-12-23 19:24:41 -08:00
|
|
|
{
|
2024-02-23 23:24:30 -08:00
|
|
|
const std::size_t w{mWriteCount.load(std::memory_order_relaxed)};
|
|
|
|
|
const std::size_t r{mReadCount.load(std::memory_order_acquire)};
|
|
|
|
|
const std::size_t writable{mWriteSize - (w - r)};
|
|
|
|
|
if(writable == 0) return 0;
|
2014-12-23 19:24:41 -08:00
|
|
|
|
2024-02-23 23:24:30 -08:00
|
|
|
const std::size_t to_write{std::min(count, writable)};
|
|
|
|
|
const std::size_t write_idx{w & mSizeMask};
|
2016-11-03 00:47:22 -07:00
|
|
|
|
2024-02-23 23:24:30 -08:00
|
|
|
const std::size_t wrend{write_idx + to_write};
|
|
|
|
|
const auto [n1, n2] = (wrend <= mSizeMask+1) ? std::make_tuple(to_write, 0_uz)
|
|
|
|
|
: std::make_tuple(mSizeMask+1 - write_idx, wrend&mSizeMask);
|
2014-12-23 19:24:41 -08:00
|
|
|
|
2024-03-12 08:12:37 -07:00
|
|
|
auto srcbytes = al::span{static_cast<const std::byte*>(src), count*mElemSize};
|
2024-03-16 07:51:23 -07:00
|
|
|
std::copy_n(srcbytes.cbegin(), n1*mElemSize, mBuffer.begin() + ptrdiff_t(write_idx*mElemSize));
|
2019-01-03 15:54:18 -08:00
|
|
|
if(n2 > 0)
|
2024-03-16 07:51:23 -07:00
|
|
|
std::copy_n(srcbytes.cbegin() + ptrdiff_t(n1*mElemSize), n2*mElemSize, mBuffer.begin());
|
2024-02-23 23:24:30 -08:00
|
|
|
mWriteCount.store(w+n1+n2, std::memory_order_release);
|
2014-12-23 19:24:41 -08:00
|
|
|
return to_write;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-02 12:46:31 -08:00
|
|
|
|
2024-01-03 19:10:15 -08:00
|
|
|
auto RingBuffer::getReadVector() noexcept -> DataPair
|
2014-12-23 19:24:41 -08:00
|
|
|
{
|
2024-02-23 23:24:30 -08:00
|
|
|
const std::size_t w{mWriteCount.load(std::memory_order_acquire)};
|
|
|
|
|
const std::size_t r{mReadCount.load(std::memory_order_relaxed)};
|
|
|
|
|
const std::size_t readable{w - r};
|
|
|
|
|
const std::size_t read_idx{r & mSizeMask};
|
2014-12-23 19:24:41 -08:00
|
|
|
|
2024-02-23 23:24:30 -08:00
|
|
|
const std::size_t rdend{read_idx + readable};
|
|
|
|
|
if(rdend > mSizeMask+1)
|
2014-12-23 19:24:41 -08:00
|
|
|
{
|
2018-12-27 15:05:12 -08:00
|
|
|
/* Two part vector: the rest of the buffer after the current read ptr,
|
2024-02-23 23:24:30 -08:00
|
|
|
* plus some from the start of the buffer.
|
|
|
|
|
*/
|
|
|
|
|
return DataPair{{mBuffer.data() + read_idx*mElemSize, mSizeMask+1 - read_idx},
|
|
|
|
|
{mBuffer.data(), rdend&mSizeMask}};
|
2014-12-23 19:24:41 -08:00
|
|
|
}
|
2024-02-23 23:24:30 -08:00
|
|
|
return DataPair{{mBuffer.data() + read_idx*mElemSize, readable}, {}};
|
2014-12-23 19:24:41 -08:00
|
|
|
}
|
|
|
|
|
|
2024-01-03 19:10:15 -08:00
|
|
|
auto RingBuffer::getWriteVector() noexcept -> DataPair
|
2014-12-23 19:24:41 -08:00
|
|
|
{
|
2024-02-23 23:24:30 -08:00
|
|
|
const std::size_t w{mWriteCount.load(std::memory_order_relaxed)};
|
|
|
|
|
const std::size_t r{mReadCount.load(std::memory_order_acquire)};
|
|
|
|
|
const std::size_t writable{mWriteSize - (w - r)};
|
|
|
|
|
const std::size_t write_idx{w & mSizeMask};
|
2014-12-23 19:24:41 -08:00
|
|
|
|
2024-02-23 23:24:30 -08:00
|
|
|
const std::size_t wrend{write_idx + writable};
|
|
|
|
|
if(wrend > mSizeMask+1)
|
2014-12-23 19:24:41 -08:00
|
|
|
{
|
|
|
|
|
/* Two part vector: the rest of the buffer after the current write ptr,
|
2024-02-23 23:24:30 -08:00
|
|
|
* plus some from the start of the buffer.
|
|
|
|
|
*/
|
|
|
|
|
return DataPair{{mBuffer.data() + write_idx*mElemSize, mSizeMask+1 - write_idx},
|
|
|
|
|
{mBuffer.data(), wrend&mSizeMask}};
|
2014-12-23 19:24:41 -08:00
|
|
|
}
|
2024-02-23 23:24:30 -08:00
|
|
|
return DataPair{{mBuffer.data() + write_idx*mElemSize, writable}, {}};
|
2014-12-23 19:24:41 -08:00
|
|
|
}
|