|
CX Framework
Cross-platform C utility framework
|
Data Structures | |
| struct | BufferHeader |
| struct | BufIov |
Typedefs | |
| typedef struct BufIov | BufIov |
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) |
| size_t | bufLen (Buffer buf) |
| void | bufClear (Buffer buf) |
| uint8 * | bufReserve (Buffer *buf, size_t len) |
| void | bufAppendBytes (Buffer *buf, _In_reads_bytes_opt_(len) const void *data, size_t len) |
| void | bufAppend (Buffer *buf, Buffer src) |
| void | bufAppendC (Buffer *buf, Buffer *src) |
| void | bufDestroy (Buffer *buf) |
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:
Platform-neutral scatter/gather vector.
Describes a single contiguous region of memory as part of a vectored I/O operation. This is not the same layout as the OS's own type (struct iovec on Unix, WSABUF on Windows) – translate into the platform's array immediately before the syscall that needs it.
void bufAppend(Buffer* buf, Buffer src)
Appends the contents of one buffer to another.
| buf | Pointer to buffer pointer to append to (may be NULL) |
| src | Buffer to copy the bytes from; NULL or empty appends nothing |
Example:
| void bufAppendBytes | ( | Buffer * | buf, |
| _In_reads_bytes_opt_(len) const void * | data, | ||
| size_t | len | ||
| ) |
void bufAppendBytes(Buffer* buf, const void* data, size_t len)
Appends raw bytes to the end of a buffer.
The buffer grows if it has to, and creates one if the pointer is NULL.
| buf | Pointer to buffer pointer to append to (may be NULL) |
| data | Bytes to append |
| len | Number of bytes |
Example:
void bufAppendC(Buffer* buf, Buffer* src)
Appends one buffer to another and destroys the source.
The source is destroyed and its pointer set to NULL, so it must not be used again. When the destination is empty this hands its memory over instead of copying the bytes, which is what makes chaining these cheap.
| buf | Pointer to buffer pointer to append to (may be NULL) |
| src | Pointer to the buffer to append and destroy |
Example:
|
inline |
void bufClear(Buffer buf)
Discards a buffer's contents without freeing its memory.
The allocation is kept, so a buffer used over and over as scratch space stops reallocating once it has grown to the size it needs.
| buf | Buffer to empty; NULL is ignored |
| Buffer bufCreate | ( | size_t | size | ) |
Buffer bufCreate(size_t size)
Create a new buffer with the specified size.
| size | The size in bytes to allocate for the buffer |
| void bufDestroy | ( | Buffer * | buf | ) |
Destroy a buffer and free its memory.
Sets the buffer pointer to NULL after freeing.
| buf | Pointer to buffer pointer to destroy |
|
inline |
size_t bufLen(Buffer buf)
Number of valid bytes in a buffer.
| buf | Buffer to measure; NULL counts as empty |
| uint8 * bufReserve | ( | Buffer * | buf, |
| size_t | len | ||
| ) |
uint8* bufReserve(Buffer* buf, size_t len)
Makes room for len more bytes and returns where to write them.
The buffer grows if it has to, and creates one if the pointer is NULL. The length is left alone, so add to it yourself once the bytes are written.
| buf | Pointer to buffer pointer to reserve space in (may be NULL) |
| len | Number of bytes to make room for |
Example:
| 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.
| buf | Pointer to buffer pointer to resize (may be NULL) |
| newsize | New size in bytes for the buffer |
| 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.
| size | The size in bytes to allocate for the buffer |
| 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.
| buf | Pointer to buffer pointer to resize (may be NULL) |
| newsize | New size in bytes for the buffer |