Add missing linear resampler to the option setting list
[openal-soft.git] / alc / ringbuffer.h
blob32fb951c3ad74b48910cfa96905235279ad397bf
1 #ifndef RINGBUFFER_H
2 #define RINGBUFFER_H
4 #include <stddef.h>
6 #include <atomic>
7 #include <memory>
8 #include <utility>
10 #include "albyte.h"
11 #include "almalloc.h"
14 /* NOTE: This lockless ringbuffer implementation is copied from JACK, extended
15 * to include an element size. Consequently, parameters and return values for a
16 * size or count is in 'elements', not bytes. Additionally, it only supports
17 * single-consumer/single-provider operation.
20 struct ll_ringbuffer_data {
21 al::byte *buf;
22 size_t len;
24 using ll_ringbuffer_data_pair = std::pair<ll_ringbuffer_data,ll_ringbuffer_data>;
27 struct RingBuffer {
28 private:
29 std::atomic<size_t> mWritePtr{0u};
30 std::atomic<size_t> mReadPtr{0u};
31 size_t mWriteSize{0u};
32 size_t mSizeMask{0u};
33 size_t mElemSize{0u};
35 al::FlexArray<al::byte, 16> mBuffer;
37 public:
38 RingBuffer(const size_t count) : mBuffer{count} { }
40 /** Reset the read and write pointers to zero. This is not thread safe. */
41 void reset() noexcept;
43 /**
44 * The non-copying data reader. Returns two ringbuffer data pointers that
45 * hold the current readable data. If the readable data is in one segment
46 * the second segment has zero length.
48 ll_ringbuffer_data_pair getReadVector() const noexcept;
49 /**
50 * The non-copying data writer. Returns two ringbuffer data pointers that
51 * hold the current writeable data. If the writeable data is in one segment
52 * the second segment has zero length.
54 ll_ringbuffer_data_pair getWriteVector() const noexcept;
56 /**
57 * Return the number of elements available for reading. This is the number
58 * of elements in front of the read pointer and behind the write pointer.
60 size_t readSpace() const noexcept
62 const size_t w{mWritePtr.load(std::memory_order_acquire)};
63 const size_t r{mReadPtr.load(std::memory_order_acquire)};
64 return (w-r) & mSizeMask;
67 /**
68 * The copying data reader. Copy at most `cnt' elements into `dest'.
69 * Returns the actual number of elements copied.
71 size_t read(void *dest, size_t cnt) noexcept;
72 /**
73 * The copying data reader w/o read pointer advance. Copy at most `cnt'
74 * elements into `dest'. Returns the actual number of elements copied.
76 size_t peek(void *dest, size_t cnt) const noexcept;
77 /** Advance the read pointer `cnt' places. */
78 void readAdvance(size_t cnt) noexcept;
80 /**
81 * Return the number of elements available for writing. This is the number
82 * of elements in front of the write pointer and behind the read pointer.
84 size_t writeSpace() const noexcept
86 const size_t w{mWritePtr.load(std::memory_order_acquire)};
87 const size_t r{mReadPtr.load(std::memory_order_acquire) + mWriteSize - mSizeMask};
88 return (r-w-1) & mSizeMask;
91 /**
92 * The copying data writer. Copy at most `cnt' elements from `src'. Returns
93 * the actual number of elements copied.
95 size_t write(const void *src, size_t cnt) noexcept;
96 /** Advance the write pointer `cnt' places. */
97 void writeAdvance(size_t cnt) noexcept;
99 /**
100 * Create a new ringbuffer to hold at least `sz' elements of `elem_sz'
101 * bytes. The number of elements is rounded up to the next power of two
102 * (even if it is already a power of two, to ensure the requested amount
103 * can be written).
105 static std::unique_ptr<RingBuffer> Create(size_t sz, size_t elem_sz, int limit_writes);
107 DEF_FAM_NEWDEL(RingBuffer, mBuffer)
109 using RingBufferPtr = std::unique_ptr<RingBuffer>;
111 #endif /* RINGBUFFER_H */