Remove another gratuitous [[likely]]
[openal-soft.git] / common / ringbuffer.cpp
blob0aec1d4970de57d59bf7a72ad2c06370dac67186
1 /**
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
21 #include "config.h"
23 #include "ringbuffer.h"
25 #include <algorithm>
26 #include <climits>
27 #include <stdexcept>
29 #include "almalloc.h"
32 RingBufferPtr RingBuffer::Create(size_t sz, size_t elem_sz, int limit_writes)
34 size_t power_of_two{0u};
35 if(sz > 0)
37 power_of_two = sz;
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;
45 #endif
47 ++power_of_two;
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;
57 return rb;
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};
76 size_t n1, n2;
77 const size_t cnt2{read_ptr + to_read};
78 if(cnt2 > mSizeMask+1)
80 n1 = mSizeMask+1 - read_ptr;
81 n2 = cnt2 & mSizeMask;
83 else
85 n1 = to_read;
86 n2 = 0;
89 auto outiter = std::copy_n(mBuffer.begin() + read_ptr*mElemSize, n1*mElemSize,
90 static_cast<al::byte*>(dest));
91 read_ptr += n1;
92 if(n2 > 0)
94 std::copy_n(mBuffer.begin(), n2*mElemSize, outiter);
95 read_ptr += n2;
97 mReadPtr.store(read_ptr, std::memory_order_release);
98 return to_read;
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};
109 size_t n1, n2;
110 const size_t cnt2{read_ptr + to_read};
111 if(cnt2 > mSizeMask+1)
113 n1 = mSizeMask+1 - read_ptr;
114 n2 = cnt2 & mSizeMask;
116 else
118 n1 = to_read;
119 n2 = 0;
122 auto outiter = std::copy_n(mBuffer.begin() + read_ptr*mElemSize, n1*mElemSize,
123 static_cast<al::byte*>(dest));
124 if(n2 > 0)
125 std::copy_n(mBuffer.begin(), n2*mElemSize, outiter);
126 return to_read;
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};
137 size_t n1, n2;
138 const size_t cnt2{write_ptr + to_write};
139 if(cnt2 > mSizeMask+1)
141 n1 = mSizeMask+1 - write_ptr;
142 n2 = cnt2 & mSizeMask;
144 else
146 n1 = to_write;
147 n2 = 0;
150 auto srcbytes = static_cast<const al::byte*>(src);
151 std::copy_n(srcbytes, n1*mElemSize, mBuffer.begin() + write_ptr*mElemSize);
152 write_ptr += n1;
153 if(n2 > 0)
155 std::copy_n(srcbytes + n1*mElemSize, n2*mElemSize, mBuffer.begin());
156 write_ptr += n2;
158 mWritePtr.store(write_ptr, std::memory_order_release);
159 return to_write;
163 auto RingBuffer::getReadVector() const noexcept -> DataPair
165 DataPair ret;
167 size_t w{mWritePtr.load(std::memory_order_acquire)};
168 size_t r{mReadPtr.load(std::memory_order_acquire)};
169 w &= mSizeMask;
170 r &= mSizeMask;
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;
183 else
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;
189 ret.second.len = 0;
192 return ret;
195 auto RingBuffer::getWriteVector() const noexcept -> DataPair
197 DataPair ret;
199 size_t w{mWritePtr.load(std::memory_order_acquire)};
200 size_t r{mReadPtr.load(std::memory_order_acquire) + mWriteSize - mSizeMask};
201 w &= mSizeMask;
202 r &= 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;
215 else
217 ret.first.buf = const_cast<al::byte*>(mBuffer.data() + w*mElemSize);
218 ret.first.len = free_cnt;
219 ret.second.buf = nullptr;
220 ret.second.len = 0;
223 return ret;