RingBuffer.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * RingBuffer.h
  3. * simple_mp3_playback
  4. *
  5. * Created by Ben Allison on 11/10/07.
  6. * Copyright 2007 Nullsoft, Inc. All rights reserved.
  7. *
  8. * Ring Buffer class
  9. * Thread safety:
  10. * This class can be used from exactly two simultaneous threads without locking
  11. * as long as one thread only writes and the other thread only reads
  12. * the writer thread may call empty(), avail(), size(), write(), fill(
  13. * the reader thread my call empty(), avail(), size(), read(), peek(), advance()
  14. *
  15. * two (or more) readers or two (or more) writers requires external locking
  16. *
  17. * Reset(), reserve(), clear(), LockBuffer(), UnlockBuffer() are not thread-safe
  18. */
  19. #pragma once
  20. #include <stddef.h>
  21. class Filler
  22. {
  23. public:
  24. virtual size_t Read(void *dest, size_t len)=0;
  25. };
  26. class Drainer
  27. {
  28. public:
  29. virtual size_t Write(const void *dest, size_t len)=0;
  30. };
  31. class RingBuffer
  32. {
  33. public:
  34. RingBuffer() {}
  35. ~RingBuffer();
  36. void Reset();
  37. bool reserve( size_t bytes ); // destructive.
  38. int expand( size_t bytes ); // like reserve, but only expands upward. non-destructive. returns an NError
  39. bool empty() const;
  40. size_t avail() const; // how much available for writing
  41. size_t size() const; // how much available for reading
  42. void clear();
  43. size_t read( void *dest, size_t len ); // returns amount actually read
  44. size_t advance( size_t len ); // same as read() but doesn't write the data any where.
  45. size_t peek( void *dest, size_t len ) const; // same as read() but doesn't advance the read pointer
  46. size_t write( const void *src, size_t len );
  47. size_t fill( Filler *filler, size_t max_bytes );
  48. size_t drain( Drainer *drainer, size_t max_bytes );
  49. size_t at( size_t offset, void *dest, size_t len ) const; // peeks() from offset. returns bytes read
  50. size_t write_position() const; // returns an integer representing a write position
  51. size_t read_position() const; // returns an integer representing the read position
  52. 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 */
  53. /* DO NOT USING THIS UNLESS YOU KNOW WHAT YOU'RE DOING
  54. you should only use it when the ring buffer is empty
  55. 1) call clear() beforehand - very important!
  56. 2) call LockBuffer(), it'll give you a buffer
  57. 3) call UnlockBufer() with how much you've written
  58. 4) you catch the man
  59. */
  60. void *LockBuffer();
  61. void UnlockBuffer( size_t written );
  62. private:
  63. volatile size_t ringBufferUsed = 0;
  64. size_t ringBufferSize = 0;
  65. char *ringBuffer = 0;
  66. char *ringWritePosition = 0;
  67. char *ringReadPosition = 0;
  68. };