Public Member Functions | List of all members
BMallocIO Class Reference

A BPositionIO derived class that creates a memory buffer. More...

Inherits BPositionIO.

Public Member Functions

 BMallocIO ()
 Create a new memory buffer with block size 256. More...
 
virtual ~BMallocIO ()
 Destroy the object and free the internal buffer. More...
 
const void * Buffer () const
 Return a pointer to the internal buffer. More...
 
size_t BufferLength () const
 Return the number of bytes in the buffer. More...
 
virtual off_t Position () const
 Return the position of the cursor. More...
 
virtual ssize_t ReadAt (off_t position, void *buffer, size_t size)
 Read data at a certain position. More...
 
virtual off_t Seek (off_t position, uint32 seekMode)
 Move the cursor to a given position. More...
 
void SetBlockSize (size_t blockSize)
 Change the block size to a certain value. More...
 
virtual status_t SetSize (off_t size)
 Change the size of the buffer. More...
 
virtual ssize_t WriteAt (off_t position, const void *buffer, size_t size)
 Write data to a certain position. More...
 
- Public Member Functions inherited from BPositionIO
 BPositionIO ()
 This constructor does nothing. More...
 
virtual ~BPositionIO ()
 This destructor does nothing. More...
 
virtual status_t GetSize (off_t *size) const
 Get the size of the object or data. More...
 
virtual ssize_t Read (void *buffer, size_t size)
 Read data from current position. More...
 
status_t ReadAtExactly (off_t position, void *buffer, size_t size, size_t *_bytesRead=NULL)
 Reads an exact amount of data from the object at the specified position into a buffer. More...
 
virtual ssize_t Write (const void *buffer, size_t size)
 Write data to the current position. More...
 
status_t WriteAtExactly (off_t position, const void *buffer, size_t size, size_t *_bytesWritten=NULL)
 Writes an exact amount of data from a buffer to the object at the specified position. More...
 
- Public Member Functions inherited from BDataIO
 BDataIO ()
 This constructor does nothing. More...
 
virtual ~BDataIO ()
 This destructor does nothing. More...
 
virtual status_t Flush ()
 Writes pending data to underlying storage. More...
 
status_t ReadExactly (void *buffer, size_t size, size_t *_bytesRead=NULL)
 Reads an exact amount of data from the object into a buffer. More...
 
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. More...
 

Detailed Description

A BPositionIO derived class that creates a memory buffer.

This class creates a memory buffer and provides a BPositionIO interface to work on it. The memory buffer grows and shrinks automatically. This is especially useful if you want to use a method or function that works on an object derived from BPositionIO and you want to do something with the resulting data, or it could be useful if you want to read and write to memory in a safe way, since this class has boundary checking.

BMallocIO allocates a buffer based on a certain block size. This provides a mechanism that will prevent it from needing to allocate new memory too often. The default block size is 256 bytes, you can change it with SetBlockSize(). If you are sure you are going to use a bigger buffer, change the block size so that you won't have to allocate more memory too often, especially if you use this class in performance-critical code.

If you require a BPositionIO derived object that works on buffers you provide, have a look at BMemoryIO.

Since
BeOS R3

Constructor & Destructor Documentation

◆ BMallocIO()

BMallocIO::BMallocIO ( )

Create a new memory buffer with block size 256.

See also
SetBlockSize()
Since
BeOS R3

◆ ~BMallocIO()

BMallocIO::~BMallocIO ( )
virtual

Destroy the object and free the internal buffer.

Since
BeOS R3

Member Function Documentation

◆ Buffer()

const void * BMallocIO::Buffer ( ) const

Return a pointer to the internal buffer.

As with any pointer to internal buffers the Haiku API exposes, make sure you don't change anything since it doesn't belong to you.

Since
BeOS R3

◆ BufferLength()

size_t BMallocIO::BufferLength ( ) const

Return the number of bytes in the buffer.

This number doesn't have to be the same size as the buffer is. Because memory is allocated in blocks the actual size of the buffer may be greater, but this method only returns the number of bytes that are actually used.

Since
BeOS R3

◆ Position()

off_t BMallocIO::Position ( ) const
virtual

Return the position of the cursor.

Since
BeOS R3

Implements BPositionIO.

◆ ReadAt()

ssize_t BMallocIO::ReadAt ( off_t  pos,
void *  buffer,
size_t  size 
)
virtual

Read data at a certain position.

Parameters
[in]posOffset into the data where to read from.
[out]bufferThe buffer to copy the read bytes in.
[in]sizeSize of the buffer.
Returns
The number of read bytes, or B_BAD_VALUE if the provided buffer is invalid.
Since
BeOS R3

Implements BPositionIO.

◆ Seek()

off_t BMallocIO::Seek ( off_t  position,
uint32  seekMode 
)
virtual

Move the cursor to a given position.

Parameters
positionThe position to move the cursor to.
seekModeThe mode determines where the cursor is placed. Possibilities:
  • SEEK_SET The cursor is set to position.
  • SEEK_CUR The position is added to the current position of the cursor.
  • SEEK_END The cursor is put at the end of the data, plus position added to it.
Returns
The new position as an off_t.
Since
BeOS R3

Implements BPositionIO.

◆ SetBlockSize()

void BMallocIO::SetBlockSize ( size_t  blockSize)

Change the block size to a certain value.

This class allocates memory in blocks. If you are in performance-critical code you might want to tweak this setting to create a better performance in case you know you are going to allocate more than the default block size of 256.

Parameters
blockSizeThe new block size.
Since
BeOS R3

◆ SetSize()

status_t BMallocIO::SetSize ( off_t  size)
virtual

Change the size of the buffer.

This method changes the size of the current buffer. If size is smaller than the current size, the data will be cleared.

Parameters
sizeThe new size of the buffer.
Returns
A status code.
Return values
B_OKResizing the data succeeded.
B_NO_MEMORYFailed to allocate the necessary memory.
Since
BeOS R3

Reimplemented from BPositionIO.

◆ WriteAt()

ssize_t BMallocIO::WriteAt ( off_t  pos,
const void *  buffer,
size_t  size 
)
virtual

Write data to a certain position.

Parameters
posOffset into the data where to write to.
bufferThe buffer to copy from.
sizeThe size of the buffer.
Returns
The number of bytes written or B_BAD_VALUE if the provided. buffer is invalid.
Since
BeOS R3

Implements BPositionIO.