Veritable Lasagna
An Allocator & Data Structure Library for C.
Loading...
Searching...
No Matches
vl_buffer.c File Reference
#include "vl_buffer.h"
#include <stdlib.h>
#include <memory.h>
+ Include dependency graph for vl_buffer.c:

Functions

vl_buffervlBufferNew (void)
 Allocates a new buffer, and initializes it with the default capacity. The buffer must be deleted with vlBufferDelete(...) when it is no longer being used to avoid leaking memory.
 
vl_buffervlBufferNewSz (vl_memsize_t initialSize)
 Allocates a new buffer, and initializes it with a capacity of N bytes. The buffer must be deleted with vlBufferDelete(...) when it is no longer being used to avoid leaking memory.
 
void vlBufferInitSz (vl_buffer *buffer, vl_memsize_t initialSize)
 Initializes a buffer instance for the first time. This will create the initial allocation for the specified capacity.
 
void vlBufferReset (vl_buffer *buffer)
 Resets the state of the specified buffer, setting the offset integer to zero. This will re-allocate the buffer to hold the default capacity.
 
void vlBufferResetSz (vl_buffer *buffer, vl_memsize_t initialCapacity)
 Resets the state of the specified buffer, setting the offset integer to zero. This will re-allocate the buffer to hold the specified capacity.
 
void vlBufferClear (vl_buffer *buffer)
 Sets the entirety of the buffer to zero. Also resets offset and computed size.
 
void vlBufferShrinkToFit (vl_buffer *buffer)
 Resizes the specified buffer to hold a capacity equal to the current size.
 
vl_buffervlBufferClone (const vl_buffer *src, vl_buffer *dest)
 Clones the source buffer to the destination buffer.
 
vl_memsize_t vlBufferCopy (vl_buffer *src, vl_buffer *dest, vl_memsize_t len)
 Copies a series of bytes from one buffer to another.
 
vl_uintptr_t vlBufferWrite (vl_buffer *buffer, vl_memsize_t size, const void *src)
 Performs a copy from the specified source pointer into the buffer. The bytes are written at the current offset integer belonging to the buffer state. This function will resize the buffer when necessary. The buffer offset will be incremented by the total number of bytes written.
 
vl_memsize_t vlBufferRead (vl_buffer *buffer, vl_memsize_t size, void *dest)
 Copies bytes from the buffer to the specified destination. Performs a copy from the buffer to the specified destination pointer. This will increment the buffer offset.
 
void vlBufferSeek (vl_buffer *buffer, vl_uintptr_t offset)
 Sets the buffer offset relative to the beginning of the allocation.
 
void vlBufferSeekRelative (vl_buffer *buffer, vl_intptr_t offset)
 Seeks the internal offset relative to the current offset. Adds the specified offset to the offset integer belonging to the buffer state.
 
void vlBufferSeekBegin (vl_buffer *buffer)
 Seeks the internal offset to the beginning of the buffer. Moves the offset integer belonging to the buffer state to the beginning of the allocation (e.g, zero)
 
void vlBufferSeekEnd (vl_buffer *buffer)
 Seeks the internal offset to the end of the buffer. Moves the offset integer belonging to the buffer state to the end of the allocation (e.g, the buffer size)
 
vl_transientvlBufferBegin (vl_buffer *buffer)
 Returns a pointer to the beginning of the buffer allocation.
 
vl_transientvlBufferEnd (vl_buffer *buffer)
 Returns a pointer to the end of the buffer allocation.
 
void vlBufferDelete (vl_buffer *buffer)
 Deletes the specified buffer.
 
void vlBufferFree (vl_buffer *buffer)
 Frees the specified buffer.
 

Function Documentation

◆ vlBufferBegin()

vl_transient * vlBufferBegin ( vl_buffer * buffer)

Returns a pointer to the beginning of the buffer allocation.

Complexity O(1) constant
Parameters
bufferstruct pointer
Returns
pointer to the first byte of the allocation

◆ vlBufferClear()

void vlBufferClear ( vl_buffer * buffer)

Sets the entirety of the buffer to zero. Also resets offset and computed size.

Complexity O(n) where n = buffer capacity
Parameters
bufferstruct pointer
+ Here is the call graph for this function:

◆ vlBufferClone()

vl_buffer * vlBufferClone ( const vl_buffer * src,
vl_buffer * dest )

Clones the source buffer to the destination buffer.

This is a 'complete' clone of the entirety of the buffer, and thus includes all related state alongside a copy of the underlying block of memory.

When simply copying data from one buffer to another, consider the more stateful vlBufferCopy.

The 'src' buffer must be non-null and point to an initialized buffer. The 'dest' buffer must be either null, or a pointer to an initialized buffer.

If the 'dest' buffer is null, a new instance is created via vlBufferNew. It must later be disposed using vlBufferDelete if this is the case.

See also
vlBufferNew
vlBufferCopy
Parameters
srcsource buffer pointer
destdestination buffer pointer, or NULL.
Complexity O(n) where n = buffer capacity.
Returns
'dest' buffer, or buffer created via vlBufferNew.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlBufferCopy()

vl_memsize_t vlBufferCopy ( vl_buffer * src,
vl_buffer * dest,
vl_memsize_t len )

Copies a series of bytes from one buffer to another.

The read from the src buffer and the write to the dest buffer are relative to their current respective offsets.

If there are less bytes available in the source buffer than those specified by len, the copy is performed up to the end of the source.

Parameters
srcsource buffer pointer
destdestination buffer pointer
lentotal number of bytes to attempt to copy.
Complexity O(n) where n = len
Returns
total number of bytes copied from src to dest.
+ Here is the call graph for this function:

◆ vlBufferDelete()

void vlBufferDelete ( vl_buffer * buffer)

Deletes the specified buffer.

The buffer should have been initialized via vlBufferNew(Sz) or vlBufferClone.

See also
vlBufferNew
vlBufferClone
Parameters
bufferstruct pointer
Complexity O(1) constant.
+ Here is the call graph for this function:

◆ vlBufferEnd()

vl_transient * vlBufferEnd ( vl_buffer * buffer)

Returns a pointer to the end of the buffer allocation.

Complexity O(1) constant
Parameters
bufferstruct pointer
Returns
pointer to one past the last byte of the allocation

◆ vlBufferFree()

void vlBufferFree ( vl_buffer * buffer)

Frees the specified buffer.

The buffer should have been initialized via vlBufferInit(Sz).

See also
vlBufferInit
vlBufferInitSz
Parameters
bufferstruct pointer
Complexity O(1) constant.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlBufferInitSz()

void vlBufferInitSz ( vl_buffer * buffer,
vl_memsize_t initialCapacity )

Initializes a buffer instance for the first time. This will create the initial allocation for the specified capacity.

This buffer should be deleted via vlBufferFree.

See also
vlBufferFree
Parameters
bufferstruct pointer
initialCapacitytotal number of initial bytes the buffer will have the capacity to hold.
Complexity O(1) constant
+ Here is the call graph for this function:

◆ vlBufferNew()

vl_buffer * vlBufferNew ( void )

Allocates a new buffer, and initializes it with the default capacity. The buffer must be deleted with vlBufferDelete(...) when it is no longer being used to avoid leaking memory.

Complexity O(1) constant
Returns
pointer to buffer.
+ Here is the call graph for this function:

◆ vlBufferNewSz()

vl_buffer * vlBufferNewSz ( vl_memsize_t initialCapacity)

Allocates a new buffer, and initializes it with a capacity of N bytes. The buffer must be deleted with vlBufferDelete(...) when it is no longer being used to avoid leaking memory.

Complexity O(1) constant
Parameters
initialCapacityinitial capacity N
Returns
pointer to buffer.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlBufferRead()

vl_memsize_t vlBufferRead ( vl_buffer * buffer,
vl_memsize_t size,
void * dest )

Copies bytes from the buffer to the specified destination. Performs a copy from the buffer to the specified destination pointer. This will increment the buffer offset.

Complexity O(n) where n = param size
Parameters
bufferstruct pointer
sizetotal number of bytes to attempt to copy
destpointer to the memory that will be copied to
Returns
actual number of bytes copied

◆ vlBufferReset()

void vlBufferReset ( vl_buffer * buffer)

Resets the state of the specified buffer, setting the offset integer to zero. This will re-allocate the buffer to hold the default capacity.

Complexity O(1) constant
Parameters
bufferstruct pointer
+ Here is the caller graph for this function:

◆ vlBufferResetSz()

void vlBufferResetSz ( vl_buffer * buffer,
vl_memsize_t newCapacity )

Resets the state of the specified buffer, setting the offset integer to zero. This will re-allocate the buffer to hold the specified capacity.

Complexity O(1) constant
Parameters
bufferstruct pointer
newCapacitynew capacity, in bytes
+ Here is the call graph for this function:

◆ vlBufferSeek()

void vlBufferSeek ( vl_buffer * buffer,
vl_uintptr_t offset )

Sets the buffer offset relative to the beginning of the allocation.

Complexity O(1) constant
Parameters
bufferstruct pointer
offsettotal number of bytes, positive, to seek forward.

◆ vlBufferSeekBegin()

void vlBufferSeekBegin ( vl_buffer * buffer)

Seeks the internal offset to the beginning of the buffer. Moves the offset integer belonging to the buffer state to the beginning of the allocation (e.g, zero)

Complexity O(1) constant
Parameters
bufferstruct pointer

◆ vlBufferSeekEnd()

void vlBufferSeekEnd ( vl_buffer * buffer)

Seeks the internal offset to the end of the buffer. Moves the offset integer belonging to the buffer state to the end of the allocation (e.g, the buffer size)

Complexity O(1) constant
See also
vlBufferEnd
Parameters
bufferstruct pointer

◆ vlBufferSeekRelative()

void vlBufferSeekRelative ( vl_buffer * buffer,
vl_intptr_t offset )

Seeks the internal offset relative to the current offset. Adds the specified offset to the offset integer belonging to the buffer state.

Complexity O(1) constant
Parameters
bufferstruct pointer
offsettotal number of bytes, positive or negative, to seek.

◆ vlBufferShrinkToFit()

void vlBufferShrinkToFit ( vl_buffer * buffer)

Resizes the specified buffer to hold a capacity equal to the current size.

Complexity O(n) where n = new buffer capacity
Parameters
bufferstruct pointer
+ Here is the call graph for this function:

◆ vlBufferWrite()

vl_uintptr_t vlBufferWrite ( vl_buffer * buffer,
vl_memsize_t size,
const void * src )

Performs a copy from the specified source pointer into the buffer. The bytes are written at the current offset integer belonging to the buffer state. This function will resize the buffer when necessary. The buffer offset will be incremented by the total number of bytes written.

Complexity O(n) where n = param size
Parameters
bufferstruct pointer
sizetotal number of bytes to write
srcpointer to the memory that will be copied from
Returns
the offset at which the bytes were written into the buffer.
+ Here is the call graph for this function:
+ Here is the caller graph for this function: