casacore
Public Member Functions | Private Member Functions | Private Attributes | List of all members
casacore::DynBuffer Class Reference

More...

#include <DynBuffer.h>

Public Member Functions

 DynBuffer (uInt nrOfBytes=4096)
 Allocate a first buffer of the specified number of bytes (default 4096). More...
 
 ~DynBuffer ()
 Remove the whole buffer, i.e. More...
 
void allocstart ()
 Prepare for storing data (re-initialize the buffer) More...
 
uInt alloc (uInt nrOfValues, uInt valueSize, Char *&ptr)
 Allocate buffer space for nrOfValues values of size valueSize bytes, and return the pointer ptr to the buffer and the number of values that fit in the buffer. More...
 
void remove (uInt nrOfBuffer=1)
 Remove buffer nrOfBuffer and the buffers appended to it, and re-initialize the current buffer. More...
 
void nextstart ()
 Prepare for data retrieval (set up for looping through the buffers). More...
 
Bool next (uInt &usedLength, Char *&ptr)
 Get the pointer to the next buffer and its used length in bytes. More...
 

Private Member Functions

uInt newbuf (uInt nrOfValues, uInt valueSize)
 Get the next buffer for storing nrOfValues values of size valueSize bytes, and return the number of values that can be stored in the free space of that buffer (maybe less than nrOfValues). More...
 

Private Attributes

uInt bufsz_p
 size of 1st buffer and min. More...
 
Int nextbuf_p
 buffernr for next function More...
 
Int curbuf_p
 current buffernr More...
 
Int nrbuf_p
 nr of buffers allocated More...
 
Int maxnrbuf_p
 size of Blocks More...
 
Block< uIntuselen_p
 used length per buffer More...
 
Block< uInttotlen_p
 total length per buffer More...
 
PtrBlock< Char * > bufptr_p
 pointer to buffer More...
 
uInt curuselen_p
 used length of current buffer More...
 
uInt curtotlen_p
 total length of current buffer More...
 
Charcurbufptr_p
 pointer to current buffer More...
 

Detailed Description

Store data in dynamically allocated buffers

Intended use:

Public interface

Review Status

Reviewed By:
Friso Olnon
Date Reviewed:
1995/03/16
Test programs:
tDynBuffer

Synopsis

DynBuffer allows one to store data in dynamically allocated buffers. When a buffer is full, an additional buffer can be allocated and "linked" to the existing one; so, the data may not be stored contiguously You can loop through all the linked buffers and get their individual addresses and sizes, so that you can access the data.

Example

Example (without exception handling):

uInt nrOfValues, nrNeeded, nrAvailable;// nr of data values
float* pData = floatarr; // ptr to data to be handled
Char* pBuffer; // ptr to buffer
DynBuffer buffer; // create buffer
buffer.allocstart(); // prepare for storing
nrNeeded = nrOfValues; // nr of values to store
// copy data into dynamic buffer
while (nrNeeded > 0) {
nrAvailable = buffer.alloc (nrNeeded, sizeof(float), pBuffer);
// get buffer space:
// room for nrAvailable values
memcpy (pBuffer, pData, nrAvailable*sizeof(float));
// copy that many data values
nrNeeded -= nrAvailable; // how much more needed?
pData += nrAvailable; // pointer to as yet unstored data
}
// Maybe store more values
.
.
// Retrieve all the data values from the buffers and write them
buffer.nextstart(); // goto buffer start
while (buffer.next (nrAvailable, pBuffer)) {
// get next buffer
write (fd, nrAvailable, pBuffer); // write data from that buffer
}
DynBuffer(uInt nrOfBytes=4096)
Allocate a first buffer of the specified number of bytes (default 4096).
unsigned int uInt
Definition: aipstype.h:51
char Char
Definition: aipstype.h:46

Motivation

This class is developed as an intermediate buffer for class AipsIO, but it may serve other purposes as well.

Definition at line 92 of file DynBuffer.h.

Constructor & Destructor Documentation

◆ DynBuffer()

casacore::DynBuffer::DynBuffer ( uInt  nrOfBytes = 4096)

Allocate a first buffer of the specified number of bytes (default 4096).

When the allocation fails, an exception is thrown.

◆ ~DynBuffer()

casacore::DynBuffer::~DynBuffer ( )

Remove the whole buffer, i.e.

the first buffer and all the buffers appended to it.

Member Function Documentation

◆ alloc()

uInt casacore::DynBuffer::alloc ( uInt  nrOfValues,
uInt  valueSize,
Char *&  ptr 
)
inline

Allocate buffer space for nrOfValues values of size valueSize bytes, and return the pointer ptr to the buffer and the number of values that fit in the buffer.

When not all values fit in the current buffer, new buffer space is added (probably non-contiguous). If that allocation fails an exception is thrown.

Definition at line 177 of file DynBuffer.h.

References curbufptr_p, curtotlen_p, curuselen_p, and newbuf().

◆ allocstart()

void casacore::DynBuffer::allocstart ( )

Prepare for storing data (re-initialize the buffer)

◆ newbuf()

uInt casacore::DynBuffer::newbuf ( uInt  nrOfValues,
uInt  valueSize 
)
private

Get the next buffer for storing nrOfValues values of size valueSize bytes, and return the number of values that can be stored in the free space of that buffer (maybe less than nrOfValues).

The new current buffer can be the present one (if it has free space), the next buffer already allocated (if there is one), or a newly allocated and linked-in buffer. If, in the last case, the allocation fails an exception is thrown.

Referenced by alloc().

◆ next()

Bool casacore::DynBuffer::next ( uInt usedLength,
Char *&  ptr 
)

Get the pointer to the next buffer and its used length in bytes.

The function returns a False value if there are no more buffers.

◆ nextstart()

void casacore::DynBuffer::nextstart ( )

Prepare for data retrieval (set up for looping through the buffers).

◆ remove()

void casacore::DynBuffer::remove ( uInt  nrOfBuffer = 1)

Remove buffer nrOfBuffer and the buffers appended to it, and re-initialize the current buffer.

By default we keep the first buffer (i.e. the one numbered 0).

The idea is that you may want to free intermediate storage space taken up by data that you no longer need, and that the first buffer is often big enough to hold further data. So, you only remove the first buffer in special cases.

Member Data Documentation

◆ bufptr_p

PtrBlock<Char*> casacore::DynBuffer::bufptr_p
private

pointer to buffer

Definition at line 162 of file DynBuffer.h.

◆ bufsz_p

uInt casacore::DynBuffer::bufsz_p
private

size of 1st buffer and min.

bufsize

Definition at line 148 of file DynBuffer.h.

◆ curbuf_p

Int casacore::DynBuffer::curbuf_p
private

current buffernr

Definition at line 152 of file DynBuffer.h.

◆ curbufptr_p

Char* casacore::DynBuffer::curbufptr_p
private

pointer to current buffer

Definition at line 168 of file DynBuffer.h.

Referenced by alloc().

◆ curtotlen_p

uInt casacore::DynBuffer::curtotlen_p
private

total length of current buffer

Definition at line 166 of file DynBuffer.h.

Referenced by alloc().

◆ curuselen_p

uInt casacore::DynBuffer::curuselen_p
private

used length of current buffer

Definition at line 164 of file DynBuffer.h.

Referenced by alloc().

◆ maxnrbuf_p

Int casacore::DynBuffer::maxnrbuf_p
private

size of Blocks

Definition at line 156 of file DynBuffer.h.

◆ nextbuf_p

Int casacore::DynBuffer::nextbuf_p
private

buffernr for next function

Definition at line 150 of file DynBuffer.h.

◆ nrbuf_p

Int casacore::DynBuffer::nrbuf_p
private

nr of buffers allocated

Definition at line 154 of file DynBuffer.h.

◆ totlen_p

Block<uInt> casacore::DynBuffer::totlen_p
private

total length per buffer

Definition at line 160 of file DynBuffer.h.

◆ uselen_p

Block<uInt> casacore::DynBuffer::uselen_p
private

used length per buffer

Definition at line 158 of file DynBuffer.h.


The documentation for this class was generated from the following file: