aboutsummaryrefslogtreecommitdiff
path: root/Src/replicant/nu/RingBuffer.h
diff options
context:
space:
mode:
authorJef <jef@targetspot.com>2024-09-24 08:54:57 -0400
committerJef <jef@targetspot.com>2024-09-24 08:54:57 -0400
commit20d28e80a5c861a9d5f449ea911ab75b4f37ad0d (patch)
tree12f17f78986871dd2cfb0a56e5e93b545c1ae0d0 /Src/replicant/nu/RingBuffer.h
parent537bcbc86291b32fc04ae4133ce4d7cac8ebe9a7 (diff)
downloadwinamp-20d28e80a5c861a9d5f449ea911ab75b4f37ad0d.tar.gz
Initial community commit
Diffstat (limited to 'Src/replicant/nu/RingBuffer.h')
-rw-r--r--Src/replicant/nu/RingBuffer.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/Src/replicant/nu/RingBuffer.h b/Src/replicant/nu/RingBuffer.h
new file mode 100644
index 00000000..bff147d1
--- /dev/null
+++ b/Src/replicant/nu/RingBuffer.h
@@ -0,0 +1,61 @@
+/*
+* Not Thread Safe !
+*/
+
+#pragma once
+#include <stddef.h>
+
+class Filler
+{
+public:
+ virtual size_t Read(void *dest, size_t len)=0;
+};
+
+class Drainer
+{
+public:
+ virtual size_t Write(const void *dest, size_t len)=0;
+};
+
+class RingBuffer
+{
+public:
+ RingBuffer();
+ ~RingBuffer();
+
+ void Reset();
+ bool reserve( size_t bytes ); // destructive.
+ int expand( size_t bytes ); // like reserve, but only expands upward. non-destructive. returns an NError
+ bool empty() const;
+ size_t avail() const; // how much available for writing
+ size_t size() const; // how much available for reading
+ void clear();
+ size_t read( void *dest, size_t len ); // returns amount actually read
+ size_t advance( size_t len ); // same as read() but doesn't write the data any where.
+ size_t peek( void *dest, size_t len ) const; // same as read() but doesn't advance the read pointer
+ size_t write( const void *src, size_t len );
+ size_t fill( Filler *filler, size_t max_bytes );
+ size_t drain( Drainer *drainer, size_t max_bytes );
+ size_t at( size_t offset, void *dest, size_t len ) const; // peeks() from offset. returns bytes read
+
+ size_t write_position() const; // returns an integer representing a write position
+ size_t read_position() const; // returns an integer representing the read position
+
+ void get_read_buffer( size_t bytes, const void **buffer, size_t *bytes_available ) const; /* returns a pointer that you can read data from, call advance() when you are done */
+ /* DO NOT USING THIS UNLESS YOU KNOW WHAT YOU'RE DOING
+ you should only use it when the ring buffer is empty
+ 1) call clear() beforehand - very important!
+ 2) call LockBuffer(), it'll give you a buffer
+ 3) call UnlockBufer() with how much you've written
+ 4) you catch the man
+ */
+ void *LockBuffer();
+ void UnlockBuffer( size_t written );
+
+private:
+ volatile size_t ringBufferUsed;
+ size_t ringBufferSize;
+ char *ringBuffer;
+ char *ringWritePosition;
+ char *ringReadPosition;
+};