Dynamic buffers

Overview

Definition of functions to work with dynamic buffers. More…

// typedefs

typedef struct te_dbuf te_dbuf;

// structs

struct te_dbuf;

// global functions

static void te_dbuf_reset(te_dbuf* dbuf);
te_errno te_dbuf_append(te_dbuf* dbuf, const void* data, size_t data_len);
te_errno te_dbuf_expand(te_dbuf* dbuf, size_t n);
void te_dbuf_cut(te_dbuf* dbuf, size_t start_index, size_t count);
void te_dbuf_free(te_dbuf* dbuf);
void te_dbuf_print(const te_dbuf* dbuf);

// macros

#define TE_DBUF_INIT(_grow_factor)

Detailed Documentation

Definition of functions to work with dynamic buffers.

Copyright (C) 2004-2022 OKTET Labs Ltd. All rights reserved.

Example of using:

// Init the dynamic buffer with 100% overlength.
te_dbuf dbuf = TE_DBUF_INIT(100);
...
// Put the "foo\0" into the dbuf
if (te_dbuf_append(&dbuf, "foo", 4) == -1) {
    ERROR("Memory allocation error");
}
...
// Reserve 4 bytes into the dbuf
size_t pos = dbuf.len;   // Save current position in the buffer.
if (te_dbuf_append(&dbuf, NULL, 4) == -1) {
    ERROR("Memory allocation error");
}
(uint32_t *)&dbuf.ptr[pos] = 5;  // Put the number to reserved place.
...
// Reset the buffer to start filling it from the beginning again.
te_dbuf_reset(&dbuf);
...
// Finish work with dynamic buffer, free the memory.
te_dbuf_free(&dbuf);

Typedefs

typedef struct te_dbuf te_dbuf

Dynamically allocated buffer.

Global Functions

static void te_dbuf_reset(te_dbuf* dbuf)

Reset dynamic buffer (makes its empty).

Parameters:

dbuf

Dynamic buffer.

te_errno te_dbuf_append(te_dbuf* dbuf, const void* data, size_t data_len)

Append additional data to the dynamic buffer. Allocate/reallocate the memory for the buffer if needed. dbuf should be released with te_dbuf_free when it is no longer needed.

Parameters:

dbuf

Dynamic buffer.

data

Data to append to the buffer pointed by dbuf. May be NULL if needs only reserve the area without placing any data.

data_len

Length of the data.

Returns:

Status code.

See also:

te_dbuf_free

te_errno te_dbuf_expand(te_dbuf* dbuf, size_t n)

Increase the size of dynamic buffer on n bytes. It reallocates the te_dbuf container with new size. dbuf should be released with te_dbuf_free when it is no longer needed.

Parameters:

dbuf

Dynamic buffer.

n

Number of bytes to add to the buffer size to expand it.

Returns:

Status code.

void te_dbuf_cut(te_dbuf* dbuf, size_t start_index, size_t count)

Cut a region of a dynamic buffer starting from start_index of length count

Function does not reallocate memory

Parameters:

dbuf

Dynamic buffer

start_index

Starting index of a region

count

Length of a region

void te_dbuf_free(te_dbuf* dbuf)

Free dynamic buffer that was allocated with te_dbuf_append or te_dbuf_expand.

Parameters:

dbuf

Dynamic buffer.

See also:

te_dbuf_append, te_dbuf_expand

void te_dbuf_print(const te_dbuf* dbuf)

Prints the dbuf info and its data using VERB function. This function should be used for debugging purpose.

Parameters:

dbuf

Dynamic buffer.

Macros

#define TE_DBUF_INIT(_grow_factor)

On-stack te_dbuf initializer.

Parameters:

_grow_factor

Buffer extra size in percentages.