dynamic_buffer.h v0.2.2
Reference-counted byte buffer library for efficient I/O operations
Loading...
Searching...
No Matches
Functions
Buffer Lifecycle

Functions for creating and destroying buffers. More...

Functions

DB_DEF db_buffer db_new (size_t capacity)
 Create a new empty buffer with specified capacity.
 
DB_DEF db_buffer db_new_with_data (const void *data, size_t size)
 Create a new buffer initialized with data (copies the data)
 
DB_DEF db_buffer db_new_from_owned_data (void *data, size_t size, size_t capacity)
 Create a new buffer by copying existing data.
 
DB_DEF db_buffer db_retain (db_buffer buf)
 Increase reference count (share ownership)
 
DB_DEF void db_release (db_buffer *buf_ptr)
 Decrease reference count and potentially free buffer.
 

Detailed Description

Functions for creating and destroying buffers.

Function Documentation

◆ db_new()

DB_DEF db_buffer db_new ( size_t  capacity)

Create a new empty buffer with specified capacity.

Parameters
capacityInitial capacity in bytes (0 for minimal allocation)
Returns
New buffer instance (asserts on allocation failure)

◆ db_new_from_owned_data()

DB_DEF db_buffer db_new_from_owned_data ( void *  data,
size_t  size,
size_t  capacity 
)

Create a new buffer by copying existing data.

Parameters
dataPointer to data to copy
sizeSize of the data in bytes
capacityTotal allocated capacity (must be >= size)
Returns
New buffer instance or NULL on invalid parameters (asserts on allocation failure)
Note
This function copies the data into the new buffer. You are responsible for freeing the original data if needed.

◆ db_new_with_data()

DB_DEF db_buffer db_new_with_data ( const void *  data,
size_t  size 
)

Create a new buffer initialized with data (copies the data)

Parameters
dataPointer to source data (can be NULL if size is 0)
sizeNumber of bytes to copy
Returns
New buffer instance (asserts on allocation failure)
Example:
db_buffer buf = db_new_with_data("Hello", 5);
if (buf) {
printf("Buffer size: %zu\n", db_size(buf));
db_release(&buf);
}
char * db_buffer
Buffer handle - points directly to buffer data.
Definition dynamic_buffer.h:173
DB_DEF size_t db_size(db_buffer buf)
Get current size of buffer in bytes.
DB_DEF db_buffer db_new_with_data(const void *data, size_t size)
Create a new buffer initialized with data (copies the data)
DB_DEF void db_release(db_buffer *buf_ptr)
Decrease reference count and potentially free buffer.

◆ db_release()

DB_DEF void db_release ( db_buffer buf_ptr)

Decrease reference count and potentially free buffer.

Parameters
buf_ptrPointer to buffer variable (will be set to NULL)

◆ db_retain()

DB_DEF db_buffer db_retain ( db_buffer  buf)

Increase reference count (share ownership)

Parameters
bufBuffer to retain (can be NULL)
Returns
The same buffer for convenience