An implementation of a ring buffer with a BDataIO interface. More...
Inherits BDataIO.
Public Member Functions | |
BMemoryRingIO (size_t size) | |
Creates a new BMemoryRingIO object with the given buffer size. | |
virtual | ~BMemoryRingIO () |
Free up resources held by the object. | |
size_t | BufferSize () |
Get the total capacity of the object. | |
size_t | BytesAvailable () |
Get the amount of data stored in the object. | |
void | Clear () |
Discard all data in the object. | |
status_t | InitCheck () const |
Whether the object has been initialized correctly. | |
virtual ssize_t | Read (void *buffer, size_t size) |
Reads data from the object into a buffer. | |
status_t | SetSize (size_t size) |
Change the ring buffer capacity. | |
void | SetWriteDisabled (bool disabled) |
Set whether writes to the ring buffer is disabled. | |
size_t | SpaceAvailable () |
Get the amount of space left in the object. | |
status_t | WaitForRead (bigtime_t timeout=B_INFINITE_TIMEOUT) |
Wait for data to be available for reading. | |
status_t | WaitForWrite (bigtime_t timeout=B_INFINITE_TIMEOUT) |
Wait for space to be available for writing. | |
virtual ssize_t | Write (const void *buffer, size_t size) |
Writes data from a buffer to the object. | |
bool | WriteDisabled () |
Indicates whether writes to the ring buffer is disabled. | |
Public Member Functions inherited from BDataIO | |
BDataIO () | |
This constructor does nothing. | |
virtual | ~BDataIO () |
This destructor does nothing. | |
virtual status_t | Flush () |
Writes pending data to underlying storage. | |
virtual ssize_t | Read (void *buffer, size_t size) |
Reads data from the object into a buffer. | |
status_t | ReadExactly (void *buffer, size_t size, size_t *_bytesRead=NULL) |
Reads an exact amount of data from the object into a buffer. | |
virtual ssize_t | Write (const void *buffer, size_t size) |
Writes data from a buffer to the object. | |
status_t | WriteExactly (const void *buffer, size_t size, size_t *_bytesWritten=NULL) |
Writes an exact amount of data from a buffer to the object. | |
An implementation of a ring buffer with a BDataIO interface.
BMemoryRingIO::BMemoryRingIO | ( | size_t | size | ) |
Creates a new BMemoryRingIO object with the given buffer size.
Call InitCheck() to verify that the buffer has been successfully created.
size | The initial size of the buffer. |
|
virtual |
Free up resources held by the object.
size_t BMemoryRingIO::BufferSize | ( | ) |
Get the total capacity of the object.
size_t BMemoryRingIO::BytesAvailable | ( | ) |
Get the amount of data stored in the object.
void BMemoryRingIO::Clear | ( | ) |
Discard all data in the object.
This method will discard all data within the buffer. However it does not free the memory held by the buffer. If this is desired, use in combination with SetSize() with 0
as the new capacity.
status_t BMemoryRingIO::InitCheck | ( | ) | const |
Whether the object has been initialized correctly.
B_OK | Everything is initialized. |
B_NO_INIT | The internal buffer couldn't be created or the buffer capacity is 0 . |
|
virtual |
Reads data from the object into a buffer.
If the ring buffer is empty, this method blocks until some data is made available. 0
is returned if size
is 0
or the buffer is empty and was set to have write disabled.
B_BAD_VALUE | buffer is NULL |
Reimplemented from BDataIO.
status_t BMemoryRingIO::SetSize | ( | size_t | size | ) |
Change the ring buffer capacity.
size | The new capacity for the ring buffer. This value will be rounded up to the nearest power of two. The new capacity must be larger or equal to BytesAvailable(). If set to 0 when there are no the bytes available to be read, the buffer will be freed. |
B_OK | The operation was successful. |
B_BAD_VALUE | The new capacity is smaller than BytesAvailable(). |
B_NO_MEMORY | Memory couldn't be allocated to grow/create the buffer. The buffer remains unchanged. |
void BMemoryRingIO::SetWriteDisabled | ( | bool | disabled | ) |
Set whether writes to the ring buffer is disabled.
This method controls whether further writes to the ring buffer is allowed. If writing is disabled, any further writes will error with B_READ_ONLY_DEVICE
, and read will no longer block on an empty buffer and instead return 0
. In addition, WaitForRead() and WaitForWrite() will return B_READ_ONLY_DEVICE
.
This method is usually used to notify the writer/reader of the pipe to not write more and/or to wait for more data.
disabled | Whether writes should be disabled. |
size_t BMemoryRingIO::SpaceAvailable | ( | ) |
Get the amount of space left in the object.
Wait for data to be available for reading.
This method will block the current thread until there's data ready to be Read() from the object or until timeout has been reached.
timeout | The minimum amount of time to wait in microseconds. If the value is B_INFINITE_TIMEOUT , this method will not return until data can be read from the object. |
B_OK | There's data ready to be read. |
B_TIMED_OUT | Timeout reached but no data has been made available. |
B_READ_ONLY_DEVICE | The buffer has write disabled. |
Wait for space to be available for writing.
This method will block the current thread until there are storage space available for a Write() operation or until timeout has been reached.
timeout | The minimum amount of time to wait in microseconds. If the value is B_INFINITE_TIMEOUT , this method will not return until data can be written into the object. |
B_OK | There's storage space to write to. |
B_TIMED_OUT | Timeout reached but no additional storage space has been made available. |
B_READ_ONLY_DEVICE | The buffer has write disabled. |
|
virtual |
Writes data from a buffer to the object.
If the ring buffer is full, this method blocks until some space is made available.
size
is 0
, 0
will be returned.B_BAD_VALUE | buffer is NULL |
B_READ_ONLY_DEVICE | Writes to the buffer has been disabled. |
Reimplemented from BDataIO.
bool BMemoryRingIO::WriteDisabled | ( | ) |
Indicates whether writes to the ring buffer is disabled.
This method indicates whether further writes to the ring buffer is allowed. See SetWriteDisabled() for more information.
true
if writes to the ring buffer is disabled, false
if not.