All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Public Member Functions | List of all members
BMemoryRingIO Class Reference

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.
 

Detailed Description

An implementation of a ring buffer with a BDataIO interface.

Since
Haiku R1

Constructor & Destructor Documentation

◆ BMemoryRingIO()

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.

Parameters
sizeThe initial size of the buffer.
Since
Haiku R1
See also
SetSize()

◆ ~BMemoryRingIO()

BMemoryRingIO::~BMemoryRingIO ( )
virtual

Free up resources held by the object.

Since
Haiku R1

Member Function Documentation

◆ BufferSize()

size_t BMemoryRingIO::BufferSize ( )

Get the total capacity of the object.

Returns
The total capacity of the object in bytes.
Since
Haiku R1
See also
SpaceAvailable() for the amount of space left for writing in the object.
BytesAvailable() for the amount of data ready to be read.

◆ BytesAvailable()

size_t BMemoryRingIO::BytesAvailable ( )

Get the amount of data stored in the object.

Returns
The amount of bytes ready to be read from the object.
Since
Haiku R1
See also
BufferSize() for the total buffer size.
SpaceAvailable() for the amount of space left for writing in the object.

◆ Clear()

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.

Since
Haiku R1
See also
SetSize() for freeing memory held by the buffer, or for growing the buffer's size.

◆ InitCheck()

status_t BMemoryRingIO::InitCheck ( ) const

Whether the object has been initialized correctly.

Return values
B_OKEverything is initialized.
B_NO_INITThe internal buffer couldn't be created or the buffer capacity is 0.
Since
Haiku R1

◆ Read()

ssize_t BMemoryRingIO::Read ( void *  buffer,
size_t  size 
)
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.

Returns
The amount of bytes read.
Return values
B_BAD_VALUEbuffer is NULL
Since
Haiku R1
See also
SetWriteDisabled()

Reimplemented from BDataIO.

◆ SetSize()

status_t BMemoryRingIO::SetSize ( size_t  size)

Change the ring buffer capacity.

Parameters
sizeThe 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.
Return values
B_OKThe operation was successful.
B_BAD_VALUEThe new capacity is smaller than BytesAvailable().
B_NO_MEMORYMemory couldn't be allocated to grow/create the buffer. The buffer remains unchanged.
Since
Haiku R1
See also
BytesAvailable() for the amount of data to be read.

◆ SetWriteDisabled()

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.

Parameters
disabledWhether writes should be disabled.
See also
WriteDisabled() to see whether writes to the ring buffer is currently disabled.

◆ SpaceAvailable()

size_t BMemoryRingIO::SpaceAvailable ( )

Get the amount of space left in the object.

Returns
The remaining storage capacity of the object in bytes.
Since
Haiku R1
See also
BufferSize() for the total buffer size.
BytesAvailable() for the amount of data ready to be read.

◆ WaitForRead()

status_t BMemoryRingIO::WaitForRead ( bigtime_t  timeout = B_INFINITE_TIMEOUT)

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.

Parameters
timeoutThe 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.
Return values
B_OKThere's data ready to be read.
B_TIMED_OUTTimeout reached but no data has been made available.
B_READ_ONLY_DEVICEThe buffer has write disabled.
See also
Read()
SetWriteDisabled()

◆ WaitForWrite()

status_t BMemoryRingIO::WaitForWrite ( bigtime_t  timeout = B_INFINITE_TIMEOUT)

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.

Parameters
timeoutThe 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.
Return values
B_OKThere's storage space to write to.
B_TIMED_OUTTimeout reached but no additional storage space has been made available.
B_READ_ONLY_DEVICEThe buffer has write disabled.
See also
Write()
SetWriteDisabled()

◆ Write()

ssize_t BMemoryRingIO::Write ( const void *  buffer,
size_t  size 
)
virtual

Writes data from a buffer to the object.

If the ring buffer is full, this method blocks until some space is made available.

Returns
The amount of bytes written. If the ring buffer is filled or size is 0, 0 will be returned.
Return values
B_BAD_VALUEbuffer is NULL
B_READ_ONLY_DEVICEWrites to the buffer has been disabled.
Since
Haiku R1
See also
SetWriteDisabled()

Reimplemented from BDataIO.

◆ WriteDisabled()

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.

See also
SetWriteDisabled() to control whether writes to the ring buffer is disabled.
Returns
true if writes to the ring buffer is disabled, false if not.