DynamicBuffer rename
This commit is contained in:
parent
a0da4ce1a3
commit
1a330b3c9b
2 changed files with 29 additions and 29 deletions
|
@ -19,12 +19,12 @@
|
||||||
*/
|
*/
|
||||||
#include "dynamicbuffer.h"
|
#include "dynamicbuffer.h"
|
||||||
|
|
||||||
int DynamicBuffer_Create(dynamic_buffer_t *destination, size_t initial_capacity)
|
int DynamicBuffer_Create(DynamicBuffer *destination, size_t initial_capacity)
|
||||||
{
|
{
|
||||||
return DynamicBuffer_CreateWithAllocator(destination, initial_capacity, NULL);
|
return DynamicBuffer_CreateWithAllocator(destination, initial_capacity, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
int DynamicBuffer_CreateWithAllocator(dynamic_buffer_t *destination, size_t initial_capacity, allocator_t* allocator)
|
int DynamicBuffer_CreateWithAllocator(DynamicBuffer *destination, size_t initial_capacity, allocator_t* allocator)
|
||||||
{
|
{
|
||||||
if (destination == NULL) {
|
if (destination == NULL) {
|
||||||
return EDESTADDRREQ;
|
return EDESTADDRREQ;
|
||||||
|
@ -34,7 +34,7 @@ int DynamicBuffer_CreateWithAllocator(dynamic_buffer_t *destination, size_t init
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
dynamic_buffer_t local;
|
DynamicBuffer local;
|
||||||
|
|
||||||
local.allocator = allocator;
|
local.allocator = allocator;
|
||||||
local.capacity = initial_capacity;
|
local.capacity = initial_capacity;
|
||||||
|
@ -54,7 +54,7 @@ int DynamicBuffer_CreateWithAllocator(dynamic_buffer_t *destination, size_t init
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DynamicBuffer_EnsureUnusedCapacity(dynamic_buffer_t* buffer, size_t needed_unused)
|
int DynamicBuffer_EnsureUnusedCapacity(DynamicBuffer* buffer, size_t needed_unused)
|
||||||
{
|
{
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
|
@ -77,7 +77,7 @@ int DynamicBuffer_EnsureUnusedCapacity(dynamic_buffer_t* buffer, size_t needed_u
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DynamicBuffer_EnsureCapacity(dynamic_buffer_t* buffer, size_t minimal_capacity)
|
int DynamicBuffer_EnsureCapacity(DynamicBuffer* buffer, size_t minimal_capacity)
|
||||||
{
|
{
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
|
@ -96,7 +96,7 @@ int DynamicBuffer_EnsureCapacity(dynamic_buffer_t* buffer, size_t minimal_capaci
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DynamicBuffer_Resize(dynamic_buffer_t* buffer, size_t new_capacity)
|
int DynamicBuffer_Resize(DynamicBuffer* buffer, size_t new_capacity)
|
||||||
{
|
{
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
|
@ -121,7 +121,7 @@ int DynamicBuffer_Resize(dynamic_buffer_t* buffer, size_t new_capacity)
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DynamicBuffer_Prune(dynamic_buffer_t* buffer)
|
int DynamicBuffer_Prune(DynamicBuffer* buffer)
|
||||||
{
|
{
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
|
@ -130,7 +130,7 @@ int DynamicBuffer_Prune(dynamic_buffer_t* buffer)
|
||||||
return DynamicBuffer_Resize(buffer, buffer->used);
|
return DynamicBuffer_Resize(buffer, buffer->used);
|
||||||
}
|
}
|
||||||
|
|
||||||
int DynamicBuffer_Reset(dynamic_buffer_t* buffer)
|
int DynamicBuffer_Reset(DynamicBuffer* buffer)
|
||||||
{
|
{
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
|
@ -141,7 +141,7 @@ int DynamicBuffer_Reset(dynamic_buffer_t* buffer)
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DynamicBuffer_RewindBytes(dynamic_buffer_t* buffer, size_t bytes)
|
int DynamicBuffer_RewindBytes(DynamicBuffer* buffer, size_t bytes)
|
||||||
{
|
{
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
|
@ -155,7 +155,7 @@ int DynamicBuffer_RewindBytes(dynamic_buffer_t* buffer, size_t bytes)
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DynamicBuffer_Store(dynamic_buffer_t* buffer, const void* data, size_t data_size)
|
int DynamicBuffer_Store(DynamicBuffer* buffer, const void* data, size_t data_size)
|
||||||
{
|
{
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
|
@ -180,12 +180,12 @@ int DynamicBuffer_Store(dynamic_buffer_t* buffer, const void* data, size_t data_
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t DynamicBuffer_GetBlockCount(dynamic_buffer_t* buffer, size_t block_size)
|
size_t DynamicBuffer_GetBlockCount(DynamicBuffer* buffer, size_t block_size)
|
||||||
{
|
{
|
||||||
return buffer->used / block_size;
|
return buffer->used / block_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* DynamicBuffer_ReadAt(dynamic_buffer_t* buffer, size_t offset)
|
void* DynamicBuffer_ReadAt(DynamicBuffer* buffer, size_t offset)
|
||||||
{
|
{
|
||||||
if (offset >= buffer->used) {
|
if (offset >= buffer->used) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -194,12 +194,12 @@ void* DynamicBuffer_ReadAt(dynamic_buffer_t* buffer, size_t offset)
|
||||||
return (void*) (((char*) buffer->array) + offset);
|
return (void*) (((char*) buffer->array) + offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* DynamicBuffer_ReadBlockAt(dynamic_buffer_t* buffer, size_t block_size, size_t index)
|
void* DynamicBuffer_ReadBlockAt(DynamicBuffer* buffer, size_t block_size, size_t index)
|
||||||
{
|
{
|
||||||
return DynamicBuffer_ReadAt(buffer, block_size * index);
|
return DynamicBuffer_ReadAt(buffer, block_size * index);
|
||||||
}
|
}
|
||||||
|
|
||||||
int DynamicBuffer_Destroy(dynamic_buffer_t* buffer)
|
int DynamicBuffer_Destroy(DynamicBuffer* buffer)
|
||||||
{
|
{
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
|
|
|
@ -27,90 +27,90 @@
|
||||||
|
|
||||||
#include "../allocator-interface/allocator-interface.h"
|
#include "../allocator-interface/allocator-interface.h"
|
||||||
|
|
||||||
typedef struct DynamicBuffer {
|
typedef struct DynamicBuffer_s {
|
||||||
void* array;
|
void* array;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
size_t used;
|
size_t used;
|
||||||
|
|
||||||
allocator_t* allocator;
|
allocator_t* allocator;
|
||||||
} dynamic_buffer_t;
|
} DynamicBuffer;
|
||||||
|
|
||||||
/// @brief Create a new Dynamic Buffer at destination with initialCapacity initialCapacity
|
/// @brief Create a new Dynamic Buffer at destination with initialCapacity initialCapacity
|
||||||
/// @param destination where the buffer will be stored
|
/// @param destination where the buffer will be stored
|
||||||
/// @param initialCapacity what it's initialCapacity will be
|
/// @param initialCapacity what it's initialCapacity will be
|
||||||
/// @return EINVAL, EINVAL, ENOMEM, EXIT_SUCCESS
|
/// @return EINVAL, EINVAL, ENOMEM, EXIT_SUCCESS
|
||||||
int DynamicBuffer_Create(dynamic_buffer_t* destination, size_t initialCapacity);
|
int DynamicBuffer_Create(DynamicBuffer* destination, size_t initialCapacity);
|
||||||
|
|
||||||
|
|
||||||
/// @brief Create a new Dynamic Buffer at destination with initialCapacity initialCapacity
|
/// @brief Create a new Dynamic Buffer at destination with initialCapacity initialCapacity
|
||||||
/// @param destination where the buffer will be stored
|
/// @param destination where the buffer will be stored
|
||||||
/// @param initialCapacity what it's initialCapacity will be
|
/// @param initialCapacity what it's initialCapacity will be
|
||||||
/// @return EINVAL, EINVAL, ENOMEM, EXIT_SUCCESS
|
/// @return EINVAL, EINVAL, ENOMEM, EXIT_SUCCESS
|
||||||
int DynamicBuffer_CreateWithAllocator(dynamic_buffer_t* destination, size_t initialCapacity, allocator_t* allocator);
|
int DynamicBuffer_CreateWithAllocator(DynamicBuffer* destination, size_t initialCapacity, allocator_t* allocator);
|
||||||
|
|
||||||
/// @brief This function checks that there are at least needed_unused free bytes in the allocated area
|
/// @brief This function checks that there are at least needed_unused free bytes in the allocated area
|
||||||
/// @param buffer buffer to check
|
/// @param buffer buffer to check
|
||||||
/// @param needed_unused required free array size
|
/// @param needed_unused required free array size
|
||||||
/// @return EINVAL, ENOMEM, EXIT_SUCCESS
|
/// @return EINVAL, ENOMEM, EXIT_SUCCESS
|
||||||
int DynamicBuffer_EnsureUnusedCapacity(dynamic_buffer_t *buffer, size_t needed_unused);
|
int DynamicBuffer_EnsureUnusedCapacity(DynamicBuffer *buffer, size_t needed_unused);
|
||||||
|
|
||||||
/// @brief This function resizes the buffer to minimal_capacity, if necessary
|
/// @brief This function resizes the buffer to minimal_capacity, if necessary
|
||||||
/// @param buffer buffer to check
|
/// @param buffer buffer to check
|
||||||
/// @param minimal_capacity minimal capacity of the buffer array
|
/// @param minimal_capacity minimal capacity of the buffer array
|
||||||
/// @return EINVAL, ENOMEM, EXIT_SUCCESS
|
/// @return EINVAL, ENOMEM, EXIT_SUCCESS
|
||||||
int DynamicBuffer_EnsureCapacity(dynamic_buffer_t *buffer, size_t minimal_capacity);
|
int DynamicBuffer_EnsureCapacity(DynamicBuffer *buffer, size_t minimal_capacity);
|
||||||
|
|
||||||
/// @brief This function resizes the buffers array via realloc to new_capacity
|
/// @brief This function resizes the buffers array via realloc to new_capacity
|
||||||
/// @param buffer buffer to resize
|
/// @param buffer buffer to resize
|
||||||
/// @param new_capacity capacity of the new buffer array
|
/// @param new_capacity capacity of the new buffer array
|
||||||
/// @return EINVAL, ENOMEM, EXIT_SUCCESS
|
/// @return EINVAL, ENOMEM, EXIT_SUCCESS
|
||||||
int DynamicBuffer_Resize(dynamic_buffer_t *buffer, size_t new_capacity);
|
int DynamicBuffer_Resize(DynamicBuffer *buffer, size_t new_capacity);
|
||||||
|
|
||||||
/// @brief This function sets the buffers capacity to what it uses
|
/// @brief This function sets the buffers capacity to what it uses
|
||||||
/// @param buffer buffer to prune
|
/// @param buffer buffer to prune
|
||||||
/// @return EINVAL, ENOMEM, EXIT_SUCCESS
|
/// @return EINVAL, ENOMEM, EXIT_SUCCESS
|
||||||
int DynamicBuffer_Prune(dynamic_buffer_t* buffer);
|
int DynamicBuffer_Prune(DynamicBuffer* buffer);
|
||||||
|
|
||||||
/// @brief Resets the count of used bytes to "clear" the buffer
|
/// @brief Resets the count of used bytes to "clear" the buffer
|
||||||
/// @param buffer buffer to reset
|
/// @param buffer buffer to reset
|
||||||
/// @return EINVAL, EXIT_SUCCESS
|
/// @return EINVAL, EXIT_SUCCESS
|
||||||
int DynamicBuffer_Reset(dynamic_buffer_t *buffer);
|
int DynamicBuffer_Reset(DynamicBuffer *buffer);
|
||||||
|
|
||||||
/// @brief Rewind the buffer pointer by bytes bytes, losing the bytes stored
|
/// @brief Rewind the buffer pointer by bytes bytes, losing the bytes stored
|
||||||
/// @param buffer buffer to rewind
|
/// @param buffer buffer to rewind
|
||||||
/// @param bytes How many bytes the buffer will lose
|
/// @param bytes How many bytes the buffer will lose
|
||||||
/// @return EINVAL, EBOUNDS, EXIT_SUCCESS
|
/// @return EINVAL, EBOUNDS, EXIT_SUCCESS
|
||||||
int DynamicBuffer_RewindBytes(dynamic_buffer_t* buffer, size_t bytes);
|
int DynamicBuffer_RewindBytes(DynamicBuffer* buffer, size_t bytes);
|
||||||
|
|
||||||
/// @brief Stores data[.data_size] at the end of the buffer array and resizes if necessary
|
/// @brief Stores data[.data_size] at the end of the buffer array and resizes if necessary
|
||||||
/// @param buffer buffer in which the data will be stored
|
/// @param buffer buffer in which the data will be stored
|
||||||
/// @param data data to store
|
/// @param data data to store
|
||||||
/// @param data_size how many bytes will be copied from data
|
/// @param data_size how many bytes will be copied from data
|
||||||
/// @return EINVAL, EINVAL, ENOMEM, EXIT_SUCCESS
|
/// @return EINVAL, EINVAL, ENOMEM, EXIT_SUCCESS
|
||||||
int DynamicBuffer_Store(dynamic_buffer_t *buffer, const void *data, size_t data_size);
|
int DynamicBuffer_Store(DynamicBuffer *buffer, const void *data, size_t data_size);
|
||||||
|
|
||||||
/// @brief Calculate how many blocks are currently in the buffer
|
/// @brief Calculate how many blocks are currently in the buffer
|
||||||
/// @param buffer buffer to query
|
/// @param buffer buffer to query
|
||||||
/// @param block_size what's the size of a single block
|
/// @param block_size what's the size of a single block
|
||||||
/// @return How many of these block_sizes does the buffer currently hold
|
/// @return How many of these block_sizes does the buffer currently hold
|
||||||
size_t DynamicBuffer_GetBlockCount(dynamic_buffer_t* buffer, size_t block_size);
|
size_t DynamicBuffer_GetBlockCount(DynamicBuffer* buffer, size_t block_size);
|
||||||
|
|
||||||
/// @brief Get a pointer reference to the buffer contents at offset
|
/// @brief Get a pointer reference to the buffer contents at offset
|
||||||
/// @param buffer buffer to query
|
/// @param buffer buffer to query
|
||||||
/// @param offset offset from the buffer start
|
/// @param offset offset from the buffer start
|
||||||
/// @return Pointer to the address in the buffer at offset or NULL if out of Bounds
|
/// @return Pointer to the address in the buffer at offset or NULL if out of Bounds
|
||||||
void* DynamicBuffer_ReadAt(dynamic_buffer_t* buffer, size_t offset);
|
void* DynamicBuffer_ReadAt(DynamicBuffer* buffer, size_t offset);
|
||||||
|
|
||||||
/// @brief Get a pointer reference to indexn'th block in buffer
|
/// @brief Get a pointer reference to indexn'th block in buffer
|
||||||
/// @param buffer buffer to query
|
/// @param buffer buffer to query
|
||||||
/// @param block_size size of a single block
|
/// @param block_size size of a single block
|
||||||
/// @param index which block to read
|
/// @param index which block to read
|
||||||
/// @return Pointer to the block at index or NULL if out of bounds
|
/// @return Pointer to the block at index or NULL if out of bounds
|
||||||
void* DynamicBuffer_ReadBlockAt(dynamic_buffer_t* buffer, size_t block_size, size_t index);
|
void* DynamicBuffer_ReadBlockAt(DynamicBuffer* buffer, size_t block_size, size_t index);
|
||||||
|
|
||||||
/// @brief Destroys the dynamic buffer and releases all resources held, the struct will not hold anything it did before
|
/// @brief Destroys the dynamic buffer and releases all resources held, the struct will not hold anything it did before
|
||||||
/// @param buffer buffer that shall be destroyed
|
/// @param buffer buffer that shall be destroyed
|
||||||
/// @return EINVAL, EXIT_SUCCESS
|
/// @return EINVAL, EXIT_SUCCESS
|
||||||
int DynamicBuffer_Destroy(dynamic_buffer_t *buffer);
|
int DynamicBuffer_Destroy(DynamicBuffer *buffer);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue