Accumulating BufferAccumulator.h
Accumulating buffer is very common paradigm to accumulate some kind of data, which is transferred via network sockets or pipes, and then to analyze it when all parts of message is received. This class implements such algorithm, and do necessary memory management to allow the buffer to grow more and more, until all the data are received. It may reallocate the buffer position during free space reservation. So, please, be careful when use old pointers to the buffer after calling the space reserving function.
Contents
- Constructor
- Copy constructor
- Destructor
- Accumulator functions
- Reserve space into the buffer
- Fill reservered space into the buffer
- Get pointer to the buffer
- Clear the buffer
- Accumulator properties
- Accumulator capacity
- Accumulator size
- Check if accumulator is empty
- Check if accumulator is initialized
Function list
C function name | Access type | C++ function name | Access type |
---|---|---|---|
Capacity | RO | (constructor) Accumulator | RW |
Clear | RW | (copy constructor) Accumulator | RW |
CopyAccumulator | RW | (destructor) ~Accumulator | RW |
Data | RO | Capacity | RO |
Fill | RW | Clear | RW |
FreeAccumulator | RW | Data | RO |
InitAccumulator | RW | Fill | RW |
IsEmpty | RO | IsEmpty | RO |
IsInit | RO | IsInit | RO |
Reserve | RW | Reserve | RW |
Size | RO | Size | RO |
C function name | Access type | C++ function name | Access type |
Constructor
Cvoid Accumulator_InitAccumulator (struct Accumulator *accumulator, size_t capacity);
C++Accumulator::Accumulator (size_t capacity);
Description: Init new accumulating buffer instance.
Access type: RW
Parameters:
- accumulator - pointer to the buffer structure
- capacity - initial buffer capacity (count of bytes). Buffer capacity is auto extended if required.
Return value: None.
Copy constructor
Cvoid Accumulator_CopyAccumulator (struct Accumulator *accumulator, const struct Accumulator *source);
C++Accumulator::Accumulator (const Accumulator &source);
Description: Init new instance of the accumulating buffer and copy all content from the source buffer inside the new one. New buffer has its own data space and doesn't share any memory blocks with the source buffer.
Access type: RW
Parameters:
- accumulator - pointer to target buffer structure
- source - pointer to source buffer structure
Return value: None.
Destructor
Cvoid Accumulator_FreeAccumulator (struct Accumulator *accumulator);
C++Accumulator::~Accumulator (void);
Description: Deinit the accumulating buffer instance and release all memory blocks are used by the object.
Access type: RW
Parameters:
- accumulator - pointer to the buffer structure
Return value: None.
Accumulator functions
Reserve space into the buffer
Cvoid* Accumulator_Reserve (struct Accumulator *accumulator, size_t size);
C++void* Accumulator::Reserve (size_t size);
Description: Check if the buffer has required amount of bytes to append a new portion of data to the buffer. If no space left inside the buffer, then function tries to extend the buffer, reserving required space.
Access type: RW
Parameters:
- accumulator - pointer to the buffer structure
- size - the number of bytes to reserve for a new portion of data
Return value:
- Pointer to the end of the buffer, before the reserved area.
- NULL if memory allocation is failed.
Warning: This function may reallocate buffer position. You should check that you do not use any old pointers to the buffer after invocation of this function.
Fill reservered space into the buffer
Cbool Accumulator_Fill (struct Accumulator *accumulator, size_t size);
C++bool Accumulator::Fill (size_t size);
Description: Marks previously reserved space of the buffer as filled with the data, decreasing free space by size bytes.
Access type: RW
Parameters:
- accumulator - pointer to the buffer structure
- size - the number of bytes were filled by the data
Return value:
- TRUE (1) if size is less than free space in the buffer.
- FALSE (0) if not.
Get pointer to the buffer
Cconst void* Accumulator_Data (const struct Accumulator *accumulator);
C++const void* Accumulator::Data (void) const;
Description: Return pointer to the beginning of the buffer data block (returns buffer content).
Access type: RO
Parameters:
- accumulator - pointer to the buffer structure
Return value: Pointer to buffered data.
Clear the buffer
Cvoid Accumulator_Clear (struct Accumulator *accumulator);
C++void Accumulator::Clear (void);
Description: Clear the accumulating buffer.
Access type: RW
Parameters:
- accumulator - pointer to the buffer structure
Return value: None.
Accumulator properties
Accumulator capacity
Csize_t Accumulator_Capacity (const struct Accumulator *accumulator);
C++size_t Accumulator::Capacity (void) const;
Description: Return buffer capacity.
Access type: RO
Parameters:
- accumulator - pointer to the buffer structure
Return value: Max count of bytes that buffer can hold now. Buffer capacity is auto extended if required.
Accumulator size
Csize_t Accumulator_Size (const struct Accumulator *accumulator);
C++size_t Accumulator::Size (void) const;
Description: Return current size of the buffer.
Access type: RO
Parameters:
- accumulator - pointer to the buffer structure
Return value: The number of bytes which are stored in the buffer.
Check if accumulator is empty
Cbool Accumulator_IsEmpty (const struct Accumulator *accumulator);
C++bool Accumulator::IsEmpty (void) const;
Description: Check if the buffer is empty.
Access type: RO
Parameters:
- accumulator - pointer to the buffer structure
Return value:
- TRUE (1) if buffer is empty.
- FALSE (0) if buffer is not empty.
Check if accumulator is initialized
Cbool Accumulator_IsInit (const struct Accumulator *accumulator);
C++bool Accumulator::IsInit (void) const;
Description: Check if the buffer is initialized.
Access type: RO
Parameters:
- accumulator - pointer to the buffer structure
Return value:
- TRUE (1) if buffer is initialized.
- FALSE (0) if buffer is not initialized.