buf.c
April 3, 2026 ยท View on GitHub
Purpose
Three buffer families:
XByteBuffer_*: dynamic byte bufferXDataBuffer_*: pointer bufferXRingBuffer_*: circular queue of byte buffers
XByteBuffer_* API
uint8_t *XByteData_Dup(const uint8_t *pBuff, size_t nSize)
- Duplicates
nSizebytes and appends a trailing NUL. - Returns allocated buffer or
NULL.
xbyte_buffer_t *XByteBuffer_New(size_t nSize, int nFastAlloc)
int XByteBuffer_Init(xbyte_buffer_t *pBuffer, size_t nSize, int nFastAlloc)
nFastAllocenables geometric growth on reserve.Newallocates the struct and initializes it.Initreturns new reserved size or negative status.
void XByteBuffer_Clear(xbyte_buffer_t *pBuffer)
void XByteBuffer_Free(xbyte_buffer_t **pBuffer)
void XByteBuffer_Reset(xbyte_buffer_t *pBuffer)
Clear: frees owned storage and resets fields.Free: clears and frees the struct when it was heap-allocated.Reset: keeps allocated storage but sets used length to zero.
int XByteBuffer_Resize(xbyte_buffer_t *pBuffer, size_t nSize)
int XByteBuffer_Reserve(xbyte_buffer_t *pBuffer, size_t nSize)
Resizereallocates exact capacity.Reserveensures space fornUsed + nSize.- Return resulting size or negative error.
Resize(0)clears buffer and returnsXSTDNON.
int XByteBuffer_SetData(xbyte_buffer_t *pBuffer, uint8_t *pData, size_t nSize)
int XByteBuffer_Set(xbyte_buffer_t *pBuffer, xbyte_buffer_t *pSrc)
- Attach external storage without owning/resizing semantics.
- Return attached used length.
int XByteBuffer_OwnData(xbyte_buffer_t *pBuffer, uint8_t *pData, size_t nSize)
int XByteBuffer_Own(xbyte_buffer_t *pBuffer, xbyte_buffer_t *pSrc)
- Transfer ownership into
pBuffer. - Return resulting owned size.
int XByteBuffer_Add(xbyte_buffer_t *pBuffer, const uint8_t *pData, size_t nSize)
int XByteBuffer_AddByte(xbyte_buffer_t *pBuffer, uint8_t nByte)
int XByteBuffer_AddStr(xbyte_buffer_t *pBuffer, xstring_t *pStr)
int XByteBuffer_AddFmt(xbyte_buffer_t *pBuffer, const char *pFmt, ...)
int XByteBuffer_AddBuff(xbyte_buffer_t *pBuffer, xbyte_buffer_t *pSrc)
- Append bytes, one byte,
xstring_t, formatted text or another byte buffer. - Return new used length on success.
- Return
XSTDERR/XSTDNONon failure depending on path.
int XByteBuffer_Insert(xbyte_buffer_t *pBuffer, size_t nPosit, const uint8_t *pData, size_t nSize)
- Inserts bytes at position or appends when
nPosit >= nUsed. - Returns new used length or
XSTDERR.
int XByteBuffer_Remove(xbyte_buffer_t *pBuffer, size_t nPosit, size_t nSize)
- Removes bytes in place without shrinking allocation.
- Returns number of removed bytes or
0.
int XByteBuffer_Delete(xbyte_buffer_t *pBuffer, size_t nPosit, size_t nSize)
- Removes bytes and then resizes to exact
nUsed + 1. - Returns buffer status.
int XByteBuffer_Advance(xbyte_buffer_t *pBuffer, size_t nSize)
- Deletes from the front.
- Returns new used length.
int XByteBuffer_Terminate(xbyte_buffer_t *pBuffer, size_t nPosit)
int XByteBuffer_NullTerm(xbyte_buffer_t *pBuffer)
Terminatetruncates used length atnPosit.NullTermensures trailing NUL at current end.- Return
XSTDOK/used length orXSTDERR.
int XByteBuffer_ReadStdin(xbyte_buffer_t *pBuffer)
- Reads stdin until EOF and appends.
- Returns final used length or
XSTDERR.
uint8_t XByteBuffer_GetByte(xbyte_buffer_t *pBuffer, size_t nIndex)
- Returns byte at index or
0when out of range.
xbool_t XByteBuffer_HasData(xbyte_buffer_t *pBuffer)
- Returns
XTRUEwhen buffer exists andnUsed > 0.
XDataBuffer_* API
int XDataBuffer_Init(xdata_buffer_t *pBuffer, size_t nSize, int nFixed)
- Allocates pointer table of
nSize. - Returns capacity or
XSTDERR.
void XDataBuffer_Clear(xdata_buffer_t *pBuffer)
void XDataBuffer_Destroy(xdata_buffer_t *pBuffer)
ClearrunsclearCbfor every slot and resets usage.Destroyalso frees the pointer table.
int XDataBuffer_Add(xdata_buffer_t *pBuffer, void *pData)
- Appends pointer.
- Returns appended index on success.
- Returns
-2when reallocation failed after full buffer path, orXSTDERRwhen capacity is already full before growth.
void *XDataBuffer_Set(xdata_buffer_t *pBuffer, unsigned int nIndex, void *pData)
- Replaces pointer at index.
- Returns previous pointer.
- Caveat:
- current bounds condition is loose and should not be trusted blindly.
void *XDataBuffer_Get(xdata_buffer_t *pBuffer, unsigned int nIndex)
- Returns pointer at index.
- Returns
NULLwhennIndex >= nUsedornIndex == 0.
void *XDataBuffer_Pop(xdata_buffer_t *pBuffer, unsigned int nIndex)
- Removes pointer and shifts later items left.
- Returns removed pointer or
NULL.
XRingBuffer_* API
int XRingBuffer_Init(xring_buffer_t *pBuffer, size_t nSize)
- Allocates
nSizeinternalxbyte_buffer_tslots. - Returns capacity or
0.
void XRingBuffer_Reset(xring_buffer_t *pBuffer)
void XRingBuffer_Destroy(xring_buffer_t *pBuffer)
Resetclears/free each slot and resets indices.Destroyalso frees the slot table.
void XRingBuffer_Update(xring_buffer_t *pBuffer, int nAdd)
void XRingBuffer_Advance(xring_buffer_t *pBuffer)
- Internal index maintenance helpers.
Advancedrops the oldest item.
int XRingBuffer_AddData(xring_buffer_t *pBuffer, const uint8_t *pData, size_t nSize)
int XRingBuffer_AddDataAdv(xring_buffer_t *pBuffer, const uint8_t *pData, size_t nSize)
AddDataappends into current back slot when ring is not full.AddDataAdvauto-advances/drops oldest when full.- Return append status from
XByteBuffer_Addor0.
int XRingBuffer_GetData(xring_buffer_t *pBuffer, uint8_t **pData, size_t *pSize)
- Returns front slot pointer and size.
- Returns
1on success,0on failure. - Caveat:
- current full-buffer check is inverted; a full ring may be treated as unavailable.
int XRingBuffer_Pop(xring_buffer_t *pBuffer, uint8_t *pData, size_t nSize)
- Copies front slot bytes into caller buffer, clears that slot and advances.
- Returns copied byte count.