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.
8  *
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.
13  *
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
19  */
20 
21 #include "config.h"
22 
23 #include "ringbuffer.h"
24 
25 #include <algorithm>
26 #include <climits>
27 #include <stdexcept>
28 
29 #include "almalloc.h"
30 
31 
Create(size_t sz,size_t elem_sz,int limit_writes)32 RingBufferPtr RingBuffer::Create(size_t sz, size_t elem_sz, int limit_writes)
33 {
34     size_t power_of_two{0u};
35     if(sz > 0)
36     {
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
46     }
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"};
50 
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;
56 
57     return rb;
58 }
59 
reset()60 void RingBuffer::reset() noexcept
61 {
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{});
65 }
66 
67 
read(void * dest,size_t cnt)68 size_t RingBuffer::read(void *dest, size_t cnt) noexcept
69 {
70     const size_t free_cnt{readSpace()};
71     if(free_cnt == 0) return 0;
72 
73     const size_t to_read{std::min(cnt, free_cnt)};
74     size_t read_ptr{mReadPtr.load(std::memory_order_relaxed) & mSizeMask};
75 
76     size_t n1, n2;
77     const size_t cnt2{read_ptr + to_read};
78     if(cnt2 > mSizeMask+1)
79     {
80         n1 = mSizeMask+1 - read_ptr;
81         n2 = cnt2 & mSizeMask;
82     }
83     else
84     {
85         n1 = to_read;
86         n2 = 0;
87     }
88 
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)
93     {
94         std::copy_n(mBuffer.begin(), n2*mElemSize, outiter);
95         read_ptr += n2;
96     }
97     mReadPtr.store(read_ptr, std::memory_order_release);
98     return to_read;
99 }
100 
peek(void * dest,size_t cnt) const101 size_t RingBuffer::peek(void *dest, size_t cnt) const noexcept
102 {
103     const size_t free_cnt{readSpace()};
104     if(free_cnt == 0) return 0;
105 
106     const size_t to_read{std::min(cnt, free_cnt)};
107     size_t read_ptr{mReadPtr.load(std::memory_order_relaxed) & mSizeMask};
108 
109     size_t n1, n2;
110     const size_t cnt2{read_ptr + to_read};
111     if(cnt2 > mSizeMask+1)
112     {
113         n1 = mSizeMask+1 - read_ptr;
114         n2 = cnt2 & mSizeMask;
115     }
116     else
117     {
118         n1 = to_read;
119         n2 = 0;
120     }
121 
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;
127 }
128 
write(const void * src,size_t cnt)129 size_t RingBuffer::write(const void *src, size_t cnt) noexcept
130 {
131     const size_t free_cnt{writeSpace()};
132     if(free_cnt == 0) return 0;
133 
134     const size_t to_write{std::min(cnt, free_cnt)};
135     size_t write_ptr{mWritePtr.load(std::memory_order_relaxed) & mSizeMask};
136 
137     size_t n1, n2;
138     const size_t cnt2{write_ptr + to_write};
139     if(cnt2 > mSizeMask+1)
140     {
141         n1 = mSizeMask+1 - write_ptr;
142         n2 = cnt2 & mSizeMask;
143     }
144     else
145     {
146         n1 = to_write;
147         n2 = 0;
148     }
149 
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)
154     {
155         std::copy_n(srcbytes + n1*mElemSize, n2*mElemSize, mBuffer.begin());
156         write_ptr += n2;
157     }
158     mWritePtr.store(write_ptr, std::memory_order_release);
159     return to_write;
160 }
161 
162 
getReadVector() const163 ll_ringbuffer_data_pair RingBuffer::getReadVector() const noexcept
164 {
165     ll_ringbuffer_data_pair ret;
166 
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};
172 
173     const size_t cnt2{r + free_cnt};
174     if(cnt2 > mSizeMask+1)
175     {
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;
182     }
183     else
184     {
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;
190     }
191 
192     return ret;
193 }
194 
getWriteVector() const195 ll_ringbuffer_data_pair RingBuffer::getWriteVector() const noexcept
196 {
197     ll_ringbuffer_data_pair ret;
198 
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};
204 
205     const size_t cnt2{w + free_cnt};
206     if(cnt2 > mSizeMask+1)
207     {
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;
214     }
215     else
216     {
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;
221     }
222 
223     return ret;
224 }
225