CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Simple Buffers

Data Structures

struct  BufferHeader
 

Functions

Buffer bufCreate (size_t size)
 
Buffer bufTryCreate (size_t size)
 
void bufResize (Buffer *buf, size_t newsize)
 
bool bufTryResize (Buffer *buf, size_t newsize)
 
void bufDestroy (Buffer *buf)
 

Detailed Description

Simple dynamically-sized buffers with header metadata.

Buffers are heap-allocated structures that store arbitrary binary data along with size and length tracking. They support resizing and optional allocation.

Example:

Buffer buf = bufCreate(1024);
memcpy(buf->data, mydata, datalen);
buf->len = datalen;
bufResize(&buf, 2048);
bufDestroy(&buf);
void bufDestroy(Buffer *buf)
Buffer bufCreate(size_t size)
void bufResize(Buffer *buf, size_t newsize)
size_t len
Length of valid data currently in buffer.
Definition buffer.h:35
uint8 data[]
Buffer data (flexible array member)
Definition buffer.h:36

Function Documentation

◆ bufCreate()

Buffer bufCreate ( size_t  size)

Buffer bufCreate(size_t size)

Create a new buffer with the specified size.

Parameters
sizeThe size in bytes to allocate for the buffer
Returns
A newly allocated buffer (never NULL)

◆ bufDestroy()

void bufDestroy ( Buffer buf)

void bufDestroy(Buffer* buf)

Destroy a buffer and free its memory.

Sets the buffer pointer to NULL after freeing.

Parameters
bufPointer to buffer pointer to destroy

◆ bufResize()

void bufResize ( Buffer buf,
size_t  newsize 
)

void bufResize(Buffer* buf, size_t newsize)

Resize an existing buffer to a new size.

If the buffer pointer is NULL, creates a new buffer with the specified size. If resizing smaller than current length, the length is truncated.

Parameters
bufPointer to buffer pointer to resize (may be NULL)
newsizeNew size in bytes for the buffer

◆ bufTryCreate()

Buffer bufTryCreate ( size_t  size)

Buffer bufTryCreate(size_t size)

Create a new buffer with optional allocation (may fail).

Uses optional allocation which will return NULL on out-of-memory instead of terminating the program. Useful for large allocations that may fail.

Parameters
sizeThe size in bytes to allocate for the buffer
Returns
A newly allocated buffer, or NULL if allocation failed

◆ bufTryResize()

bool bufTryResize ( Buffer buf,
size_t  newsize 
)

bool bufTryResize(Buffer* buf, size_t newsize)

Resize an existing buffer with optional allocation (may fail).

Like bufResize() but uses optional allocation and returns false on failure instead of terminating the program.

Parameters
bufPointer to buffer pointer to resize (may be NULL)
newsizeNew size in bytes for the buffer
Returns
true if resize succeeded, false if allocation failed