UDPConnection/src/UDPC_Deque.h

115 lines
3.9 KiB
C
Raw Normal View History

2019-01-26 10:11:12 +00:00
#ifndef UDPC_DEQUE_H
#define UDPC_DEQUE_H
#include <stdint.h>
typedef struct
{
uint32_t head;
uint32_t tail;
uint32_t size;
uint32_t alloc_size;
char *buf;
} UDPC_Deque;
2019-01-26 11:44:31 +00:00
/*!
2019-01-27 09:30:35 +00:00
* \return non-null on success
2019-01-26 11:44:31 +00:00
*/
2019-01-27 09:30:35 +00:00
UDPC_Deque* UDPC_Deque_init(uint32_t alloc_size);
2019-01-26 11:44:31 +00:00
/*!
* Frees resources used by a UDPC_Deque
*/
2019-01-27 09:30:35 +00:00
void UDPC_Deque_destroy(UDPC_Deque **deque);
2019-01-26 11:44:31 +00:00
/*!
* Fails if new_size is smaller than current size of Deque.
* On failure, deque remains unchanged.
* \return non-zero on success
*/
int UDPC_Deque_realloc(UDPC_Deque *deque, uint32_t new_size);
2019-01-26 10:11:12 +00:00
/*!
* If there was not enough space in the Deque, then no data is inserted at all.
* \return non-zero on success (there was enough size to insert data)
*/
2019-01-27 06:09:38 +00:00
int UDPC_Deque_push_back(UDPC_Deque *deque, const void *data, uint32_t size);
2019-01-26 10:11:12 +00:00
/*!
* If there was not enough space in the Deque, then no data is inserted at all.
* \return non-zero on success (there was enough size to insert data)
*/
2019-01-27 06:09:38 +00:00
int UDPC_Deque_push_front(UDPC_Deque *deque, const void *data, uint32_t size);
2019-01-26 10:11:12 +00:00
2019-01-26 11:44:31 +00:00
/*!
* \return size in bytes of available data
*/
2019-01-26 10:11:12 +00:00
uint32_t UDPC_Deque_get_available(UDPC_Deque *deque);
2019-01-26 11:44:31 +00:00
/*!
* \return size in bytes of used data
*/
uint32_t UDPC_Deque_get_used(UDPC_Deque *deque);
2019-01-26 10:11:12 +00:00
/*!
* \brief Get data from back of deque
* Data must be free'd after use as it was allocated with malloc.
* When size is greater than deque size, partial data is allocated, size is
* updated to allocated amount. If deque is empty, no data is allocated.
* \return non-zero if full requested size was returned
*/
2019-01-27 06:09:38 +00:00
int UDPC_Deque_get_back(UDPC_Deque *deque, void **data, uint32_t *size);
2019-01-26 10:11:12 +00:00
/*!
* \brief Get data from front of deque
* Data must be free'd after use as it was allocated with malloc.
* When size is greater than deque size, partial data is allocated, size is
* updated to allocated amount. If deque is empty, no data is allocated.
* \return non-zero if full requested size was returned
*/
2019-01-27 06:09:38 +00:00
int UDPC_Deque_get_front(UDPC_Deque *deque, void **data, uint32_t *size);
2019-01-26 10:11:12 +00:00
/*!
* \brief "free" data from the back of the deque
* If size is greater than data used, then all data will be "free"d.
* Note that this doesn't actually deallocate data, but changes internal values
* that keep track of data positions in the internal buffer. The data will only
* actually be removed when it is overwritten or the Deque is free'd.
*/
void UDPC_Deque_pop_back(UDPC_Deque *deque, uint32_t size);
/*!
* \brief "free" data from the front of the deque
* If size is greater than data used, then all data will be "free"d.
* Note that this doesn't actually deallocate data, but changes internal values
* that keep track of data positions in the internal buffer. The data will only
* actually be removed when it is overwritten or the Deque is free'd.
*/
void UDPC_Deque_pop_front(UDPC_Deque *deque, uint32_t size);
2019-01-26 11:44:31 +00:00
/*!
* \brief Get a unitSize sized chunk of data at position unitSize * index
* The data will be indexed relative to the head of the Deque.
* The out pointer will be malloc'd with size unitSize and will have a copy of
* the data at the specified unitSize * index.
2019-01-27 06:09:38 +00:00
* Note that the out data must be free'd, but on fail nothing will be malloc'd
2019-01-26 11:44:31 +00:00
* and *out will be set to NULL.
2019-01-27 06:09:38 +00:00
* \return non-zero if unitSize * index < size
2019-01-26 11:44:31 +00:00
*/
2019-01-27 06:09:38 +00:00
int UDPC_Deque_index(UDPC_Deque *deque, uint32_t unitSize, uint32_t index, void **out);
2019-01-26 11:44:31 +00:00
/*!
* \brief Get a unitSize sized chunk of data at position relative to tail
* The out pointer will be malloc'd with size unitSize and will have a copy of
2019-01-27 06:09:38 +00:00
* the data at the specified unitSize * index relative to tail in reverse
2019-01-26 11:44:31 +00:00
* direction.
2019-01-27 06:09:38 +00:00
* Note that the out data must be free'd, but on fail nothing will be malloc'd
2019-01-26 11:44:31 +00:00
* and *out will be set to NULL.
2019-01-27 06:09:38 +00:00
* \return non-zero if unitSize * index < size
2019-01-26 11:44:31 +00:00
*/
2019-01-27 06:09:38 +00:00
int UDPC_Deque_index_rev(UDPC_Deque *deque, uint32_t unitSize, uint32_t index, void **out);
2019-01-26 11:44:31 +00:00
void UDPC_Deque_clear(UDPC_Deque *deque);
2019-01-26 10:11:12 +00:00
#endif