]> git.tdb.fi Git - ext/openal.git/blob - common/ringbuffer.h
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / common / ringbuffer.h
1 #ifndef RINGBUFFER_H
2 #define RINGBUFFER_H
3
4 #include <atomic>
5 #include <memory>
6 #include <stddef.h>
7 #include <utility>
8
9 #include "albyte.h"
10 #include "almalloc.h"
11
12
13 /* NOTE: This lockless ringbuffer implementation is copied from JACK, extended
14  * to include an element size. Consequently, parameters and return values for a
15  * size or count is in 'elements', not bytes. Additionally, it only supports
16  * single-consumer/single-provider operation.
17  */
18
19 struct RingBuffer {
20 private:
21     std::atomic<size_t> mWritePtr{0u};
22     std::atomic<size_t> mReadPtr{0u};
23     size_t mWriteSize{0u};
24     size_t mSizeMask{0u};
25     size_t mElemSize{0u};
26
27     al::FlexArray<al::byte, 16> mBuffer;
28
29 public:
30     struct Data {
31         al::byte *buf;
32         size_t len;
33     };
34     using DataPair = std::pair<Data,Data>;
35
36
37     RingBuffer(const size_t count) : mBuffer{count} { }
38
39     /** Reset the read and write pointers to zero. This is not thread safe. */
40     void reset() noexcept;
41
42     /**
43      * The non-copying data reader. Returns two ringbuffer data pointers that
44      * hold the current readable data. If the readable data is in one segment
45      * the second segment has zero length.
46      */
47     DataPair getReadVector() const noexcept;
48     /**
49      * The non-copying data writer. Returns two ringbuffer data pointers that
50      * hold the current writeable data. If the writeable data is in one segment
51      * the second segment has zero length.
52      */
53     DataPair getWriteVector() const noexcept;
54
55     /**
56      * Return the number of elements available for reading. This is the number
57      * of elements in front of the read pointer and behind the write pointer.
58      */
59     size_t readSpace() const noexcept
60     {
61         const size_t w{mWritePtr.load(std::memory_order_acquire)};
62         const size_t r{mReadPtr.load(std::memory_order_acquire)};
63         return (w-r) & mSizeMask;
64     }
65
66     /**
67      * The copying data reader. Copy at most `cnt' elements into `dest'.
68      * Returns the actual number of elements copied.
69      */
70     size_t read(void *dest, size_t cnt) noexcept;
71     /**
72      * The copying data reader w/o read pointer advance. Copy at most `cnt'
73      * elements into `dest'. Returns the actual number of elements copied.
74      */
75     size_t peek(void *dest, size_t cnt) const noexcept;
76     /** Advance the read pointer `cnt' places. */
77     void readAdvance(size_t cnt) noexcept
78     { mReadPtr.fetch_add(cnt, std::memory_order_acq_rel); }
79
80
81     /**
82      * Return the number of elements available for writing. This is the number
83      * of elements in front of the write pointer and behind the read pointer.
84      */
85     size_t writeSpace() const noexcept
86     {
87         const size_t w{mWritePtr.load(std::memory_order_acquire)};
88         const size_t r{mReadPtr.load(std::memory_order_acquire) + mWriteSize - mSizeMask};
89         return (r-w-1) & mSizeMask;
90     }
91
92     /**
93      * The copying data writer. Copy at most `cnt' elements from `src'. Returns
94      * the actual number of elements copied.
95      */
96     size_t write(const void *src, size_t cnt) noexcept;
97     /** Advance the write pointer `cnt' places. */
98     void writeAdvance(size_t cnt) noexcept
99     { mWritePtr.fetch_add(cnt, std::memory_order_acq_rel); }
100
101     size_t getElemSize() const noexcept { return mElemSize; }
102
103     /**
104      * Create a new ringbuffer to hold at least `sz' elements of `elem_sz'
105      * bytes. The number of elements is rounded up to the next power of two
106      * (even if it is already a power of two, to ensure the requested amount
107      * can be written).
108      */
109     static std::unique_ptr<RingBuffer> Create(size_t sz, size_t elem_sz, int limit_writes);
110
111     DEF_FAM_NEWDEL(RingBuffer, mBuffer)
112 };
113 using RingBufferPtr = std::unique_ptr<RingBuffer>;
114
115 #endif /* RINGBUFFER_H */