return buf;
}
-void buf_free(buf_t* buf, int free_contents)
+void buf_free(buf_t* buf, bool free_contents)
{
buf_clear(buf,free_contents);
free( buf->buffer );
return (size_t)buf->size;
}
-int buf_empty(buf_t* buf)
+bool buf_empty(buf_t* buf)
{
return (buf->reads == buf->writes);
}
-int buf_full(buf_t* buf)
+bool buf_full(buf_t* buf)
{
int full = !buf_empty(buf);
full &= ((buf->reads % buf->size) == (buf->writes % buf->size));
return full;
}
-void buf_clear(buf_t* buf, int free_contents)
+void buf_clear(buf_t* buf, bool free_contents)
{
if (free_contents)
{
return data;
}
-int buf_write(buf_t* buf, void* data)
+bool buf_write(buf_t* buf, void* data)
{
- int success = 0;
+ bool success = false;
if (!buf_full(buf))
{
buf->buffer[ buf->writes % buf->size ] = data;
buf->writes++;
- success = 1;
+ success = true;
}
return success;
}
extern "C" {
#endif
+#include <stdbool.h>
+
/** A structure defining a circular buffer */
typedef struct {
void** buffer; /**< Pointer to the buffer */
* @param buf The buffer to free.
* @param free_contents Whether unread contents should also be freed.
*/
-void buf_free(buf_t* buf, int free_contents);
+void buf_free(buf_t* buf, bool free_contents);
/**
* @brief Returns the size of the provided buffer.
*
* @return 1 if the buffer is empty 0 otherwise.
*/
-int buf_empty(buf_t* buf);
+bool buf_empty(buf_t* buf);
/**
* @brief Returns whether the buffer is full.
*
* @return 1 if the buffer is full 0 otherwise.
*/
-int buf_full(buf_t* buf);
+bool buf_full(buf_t* buf);
/**
* @brief Clears all unread data from the provided buffer.
* @param buf The buffer to clear.
* @param free_contents Whether the unread contents should also be freed.
*/
-void buf_clear(buf_t* buf, int free_contents);
+void buf_clear(buf_t* buf, bool free_contents);
/**
* @brief Reads an item from the provided buffer.
*
* @return 1 on successful write 0 otherwise.
*/
-int buf_write(buf_t* buf, void* data);
+bool buf_write(buf_t* buf, void* data);
#ifdef __cplusplus
}
return node;
}
-void sll_free(sll_t* list, int free_contents)
+void sll_free(sll_t* list, bool free_contents)
{
sll_node_t* node = list->head;
while( NULL != node )
free( list );
}
-void sll_free_node(sll_node_t* node, int free_contents)
+void sll_free_node(sll_node_t* node, bool free_contents)
{
if( free_contents )
{
return list->tail;
}
-unsigned int sll_size(sll_t* list)
+size_t sll_size(sll_t* list)
{
- unsigned int length = 0;
+ size_t length = 0;
sll_node_t* node = list->head;
while( NULL != node )
{
return length;
}
-int sll_empty(sll_t* list)
+bool sll_empty(sll_t* list)
{
return ((NULL == list->head) && (NULL == list->tail));
}
-sll_node_t* sll_at(sll_t* list, unsigned int index)
+sll_node_t* sll_at(sll_t* list, size_t index)
{
sll_node_t* node = NULL;
- unsigned int cur_index = 0;
+ size_t cur_index = 0;
sll_node_t* cur_node = list->head;
while( NULL != cur_node )
{
return node;
}
-sll_node_t* sll_insert( sll_t* list, unsigned int index, void* contents)
+sll_node_t* sll_insert( sll_t* list, size_t index, void* contents)
{
sll_node_t* new_node = NULL;
if( 0 == index )
return new_node;
}
-sll_node_t* sll_delete( sll_t* list, unsigned int index, int free_contents)
+sll_node_t* sll_delete( sll_t* list, size_t index, bool free_contents)
{
sll_node_t* node = NULL;
return node;
}
-sll_t* sll_clear(sll_t* list, int free_contents)
+sll_t* sll_clear(sll_t* list, bool free_contents)
{
sll_node_t* node = list->head;
while(NULL != node)
extern "C" {
#endif
+#include <stdbool.h>
+
/** A linked list node. */
typedef struct sll_node_t
{
* @param list The list to be freed.
* @param free_contents Whether or not to also free the contents of each node.
**/
-void sll_free(sll_t* list, int free_contents);
+void sll_free(sll_t* list, bool free_contents);
/**
* @brief Frees all memory used by a node.
* @param node
* @param free_contents
*/
-void sll_free_node(sll_node_t* node, int free_contents);
+void sll_free_node(sll_node_t* node, bool free_contents);
/**
* @brief Returns pointer to first node in the list
*
* @return The number of elements in the list.
**/
-unsigned int sll_size(sll_t* list);
+size_t sll_size(sll_t* list);
/**
* @brief Returns whether the list is empty or not.
*
* @return Whether the list is empty, 1 for true and 0 for false.
*/
-int sll_empty(sll_t* list);
+bool sll_empty(sll_t* list);
/**
* @brief Return the node at the specified index in a linked list.
*
* @return A pointer to the node at the supplied index, NULL if out of range.
**/
-sll_node_t* sll_at(sll_t* list, unsigned int index);
+sll_node_t* sll_at(sll_t* list, size_t index);
/**
* @brief Adds a new node to the front of an existing linked list.
*
* @return Pointer to the newly inserted node, NULL if index is out of range.
**/
-sll_node_t* sll_insert( sll_t* list, unsigned int index, void* contents);
+sll_node_t* sll_insert( sll_t* list, size_t index, void* contents);
/**
* @brief Deletes a node from the supplied list.
*
* @return Pointer to the node that is now at the supplied index.
**/
-sll_node_t* sll_delete( sll_t* list, unsigned int index, int free_contents);
+sll_node_t* sll_delete( sll_t* list, size_t index, bool free_contents);
/**
* @brief Deletes all elements in the provided list
*
* @return A pointer to the cleared list.
*/
-sll_t* sll_clear(sll_t* list, int free_contents);
+sll_t* sll_clear(sll_t* list, bool free_contents);
#ifdef __cplusplus
}