Clean up some unnecessary includes
[openal-soft.git] / alc / ringbuffer.cpp
blobd61f6129a05428bbe80db4fd1911a2e50472d2ce
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 <cstdint>
29 #include "almalloc.h"
32 RingBufferPtr CreateRingBuffer(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) return nullptr;
50 const size_t bufbytes{power_of_two * elem_sz};
51 RingBufferPtr rb{new (FamCount{bufbytes}) RingBuffer{bufbytes}};
52 rb->mWriteSize = limit_writes ? sz : (power_of_two-1);
53 rb->mSizeMask = power_of_two - 1;
54 rb->mElemSize = elem_sz;
56 return rb;
59 void RingBuffer::reset() noexcept
61 mWritePtr.store(0, std::memory_order_relaxed);
62 mReadPtr.store(0, std::memory_order_relaxed);
63 std::fill_n(mBuffer.begin(), (mSizeMask+1)*mElemSize, al::byte{});
67 size_t RingBuffer::readSpace() const noexcept
69 size_t w = mWritePtr.load(std::memory_order_acquire);
70 size_t r = mReadPtr.load(std::memory_order_acquire);
71 return (w-r) & mSizeMask;
74 size_t RingBuffer::writeSpace() const noexcept
76 size_t w = mWritePtr.load(std::memory_order_acquire);
77 size_t r = mReadPtr.load(std::memory_order_acquire) + mWriteSize - mSizeMask;
78 return (r-w-1) & mSizeMask;
82 size_t RingBuffer::read(void *dest, size_t cnt) noexcept
84 const size_t free_cnt{readSpace()};
85 if(free_cnt == 0) return 0;
87 const size_t to_read{std::min(cnt, free_cnt)};
88 size_t read_ptr{mReadPtr.load(std::memory_order_relaxed) & mSizeMask};
90 size_t n1, n2;
91 const size_t cnt2{read_ptr + to_read};
92 if(cnt2 > mSizeMask+1)
94 n1 = mSizeMask+1 - read_ptr;
95 n2 = cnt2 & mSizeMask;
97 else
99 n1 = to_read;
100 n2 = 0;
103 auto outiter = std::copy_n(mBuffer.begin() + read_ptr*mElemSize, n1*mElemSize,
104 static_cast<al::byte*>(dest));
105 read_ptr += n1;
106 if(n2 > 0)
108 std::copy_n(mBuffer.begin(), n2*mElemSize, outiter);
109 read_ptr += n2;
111 mReadPtr.store(read_ptr, std::memory_order_release);
112 return to_read;
115 size_t RingBuffer::peek(void *dest, size_t cnt) const noexcept
117 const size_t free_cnt{readSpace()};
118 if(free_cnt == 0) return 0;
120 const size_t to_read{std::min(cnt, free_cnt)};
121 size_t read_ptr{mReadPtr.load(std::memory_order_relaxed) & mSizeMask};
123 size_t n1, n2;
124 const size_t cnt2{read_ptr + to_read};
125 if(cnt2 > mSizeMask+1)
127 n1 = mSizeMask+1 - read_ptr;
128 n2 = cnt2 & mSizeMask;
130 else
132 n1 = to_read;
133 n2 = 0;
136 auto outiter = std::copy_n(mBuffer.begin() + read_ptr*mElemSize, n1*mElemSize,
137 static_cast<al::byte*>(dest));
138 if(n2 > 0)
139 std::copy_n(mBuffer.begin(), n2*mElemSize, outiter);
140 return to_read;
143 size_t RingBuffer::write(const void *src, size_t cnt) noexcept
145 const size_t free_cnt{writeSpace()};
146 if(free_cnt == 0) return 0;
148 const size_t to_write{std::min(cnt, free_cnt)};
149 size_t write_ptr{mWritePtr.load(std::memory_order_relaxed) & mSizeMask};
151 size_t n1, n2;
152 const size_t cnt2{write_ptr + to_write};
153 if(cnt2 > mSizeMask+1)
155 n1 = mSizeMask+1 - write_ptr;
156 n2 = cnt2 & mSizeMask;
158 else
160 n1 = to_write;
161 n2 = 0;
164 auto srcbytes = static_cast<const al::byte*>(src);
165 std::copy_n(srcbytes, n1*mElemSize, mBuffer.begin() + write_ptr*mElemSize);
166 write_ptr += n1;
167 if(n2 > 0)
169 std::copy_n(srcbytes + n1*mElemSize, n2*mElemSize, mBuffer.begin());
170 write_ptr += n2;
172 mWritePtr.store(write_ptr, std::memory_order_release);
173 return to_write;
177 void RingBuffer::readAdvance(size_t cnt) noexcept
179 mReadPtr.fetch_add(cnt, std::memory_order_acq_rel);
182 void RingBuffer::writeAdvance(size_t cnt) noexcept
184 mWritePtr.fetch_add(cnt, std::memory_order_acq_rel);
188 ll_ringbuffer_data_pair RingBuffer::getReadVector() const noexcept
190 ll_ringbuffer_data_pair ret;
192 size_t w{mWritePtr.load(std::memory_order_acquire)};
193 size_t r{mReadPtr.load(std::memory_order_acquire)};
194 w &= mSizeMask;
195 r &= mSizeMask;
196 const size_t free_cnt{(w-r) & mSizeMask};
198 const size_t cnt2{r + free_cnt};
199 if(cnt2 > mSizeMask+1)
201 /* Two part vector: the rest of the buffer after the current read ptr,
202 * plus some from the start of the buffer. */
203 ret.first.buf = const_cast<al::byte*>(mBuffer.data() + r*mElemSize);
204 ret.first.len = mSizeMask+1 - r;
205 ret.second.buf = const_cast<al::byte*>(mBuffer.data());
206 ret.second.len = cnt2 & mSizeMask;
208 else
210 /* Single part vector: just the rest of the buffer */
211 ret.first.buf = const_cast<al::byte*>(mBuffer.data() + r*mElemSize);
212 ret.first.len = free_cnt;
213 ret.second.buf = nullptr;
214 ret.second.len = 0;
217 return ret;
220 ll_ringbuffer_data_pair RingBuffer::getWriteVector() const noexcept
222 ll_ringbuffer_data_pair ret;
224 size_t w{mWritePtr.load(std::memory_order_acquire)};
225 size_t r{mReadPtr.load(std::memory_order_acquire) + mWriteSize - mSizeMask};
226 w &= mSizeMask;
227 r &= mSizeMask;
228 const size_t free_cnt{(r-w-1) & mSizeMask};
230 const size_t cnt2{w + free_cnt};
231 if(cnt2 > mSizeMask+1)
233 /* Two part vector: the rest of the buffer after the current write ptr,
234 * plus some from the start of the buffer. */
235 ret.first.buf = const_cast<al::byte*>(mBuffer.data() + w*mElemSize);
236 ret.first.len = mSizeMask+1 - w;
237 ret.second.buf = const_cast<al::byte*>(mBuffer.data());
238 ret.second.len = cnt2 & mSizeMask;
240 else
242 ret.first.buf = const_cast<al::byte*>(mBuffer.data() + w*mElemSize);
243 ret.first.len = free_cnt;
244 ret.second.buf = nullptr;
245 ret.second.len = 0;
248 return ret;