Loading...
Searching...
No Matches
queue.c
Go to the documentation of this file.
#include "arc/std/queue.h"
#include "arc/std/errno.h"
#include <stdlib.h>
typedef struct ARC_QueueNode ARC_QueueNode;
struct ARC_QueueNode {
ARC_QueueNode *next;
void *data;
};
struct ARC_Queue {
uint32_t currentSize;
ARC_QueueNode *node;
};
void ARC_Queue_Create(ARC_Queue **queue){
*queue = (ARC_Queue *)malloc(sizeof(ARC_Queue));
(*queue)->currentSize = 0;
(*queue)->node = NULL;
}
void ARC_Queue_Destroy(ARC_Queue *queue){
if(queue->currentSize != 0 || queue->node != NULL){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_Queue_Destroy(queue) called, but queue was not empty");
return;
}
free(queue);
}
void ARC_Queue_Push(ARC_Queue *queue, void *data){
if(queue->currentSize == ~(uint32_t)0){
arc_errno = ARC_ERRNO_OVERFLOW;
ARC_DEBUG_LOG_ERROR("ARC_Queue_Push(queue) called, size of queue is maxed, cannot add another node");
return;
}
if(queue->currentSize == 0){
ARC_QueueNode *node = (ARC_QueueNode *)malloc(sizeof(ARC_QueueNode));
node->data = data;
node->next = NULL;
queue->node = node;
queue->currentSize++;
return;
}
ARC_QueueNode *end = queue->node;
for(uint32_t i = 1; i < queue->currentSize; i++){
if(end->next == NULL){
arc_errno = ARC_ERRNO_NULL;
ARC_DEBUG_LOG_ERROR("ARC_Queue_Push(queue), when getting end node encountered a NULL node");
return;
}
end = end->next;
}
ARC_QueueNode *node = (ARC_QueueNode *)malloc(sizeof(ARC_QueueNode));
node->data = data;
node->next = NULL;
end->next = node;
queue->currentSize++;
}
void *ARC_Queue_Pop(ARC_Queue *queue){
if(queue->currentSize == 0){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_Queue_Pop(queue) called, but queue was empty");
return NULL;
}
ARC_QueueNode *node = queue->node;
if(node == NULL){
arc_errno = ARC_ERRNO_NULL;
ARC_DEBUG_LOG_ERROR("ARC_Queue_Pop(queue) called, but node was NULL");
return NULL;
}
queue->node = node->next;
void *nodeData = node->data;
free(node);
queue->currentSize--;
return nodeData;
}
uint32_t ARC_Queue_Size(ARC_Queue *queue){
return queue->currentSize;
}
errno.h
arc_errno
int32_t arc_errno
Definition errno.c:5
ARC_ERRNO_OVERFLOW
#define ARC_ERRNO_OVERFLOW
Definition errno.h:10
ARC_ERRNO_NULL
#define ARC_ERRNO_NULL
Definition errno.h:6
ARC_DEBUG_LOG_ERROR
#define ARC_DEBUG_LOG_ERROR(STR)
Definition errno.h:39
ARC_ERRNO_DATA
#define ARC_ERRNO_DATA
Definition errno.h:7
ARC_Queue_Push
void ARC_Queue_Push(ARC_Queue *queue, void *data)
pushes value to end of queue
Definition queue.c:34
ARC_Queue_Destroy
void ARC_Queue_Destroy(ARC_Queue *queue)
destroyes ARC_Queue type
Definition queue.c:24
ARC_Queue_Pop
void * ARC_Queue_Pop(ARC_Queue *queue)
pops the front off of the ARC_Queue
Definition queue.c:72
ARC_Queue_Size
uint32_t ARC_Queue_Size(ARC_Queue *queue)
gets size of queue
Definition queue.c:95
ARC_Queue_Create
void ARC_Queue_Create(ARC_Queue **queue)
creates ARC_Queue type
Definition queue.c:18
queue.h
ARC_QueueNode
Definition queue.c:8
ARC_QueueNode::next
ARC_QueueNode * next
Definition queue.c:9
ARC_QueueNode::data
void * data
Definition queue.c:10
ARC_Queue
Definition queue.c:13
ARC_Queue::currentSize
uint32_t currentSize
Definition queue.c:14
ARC_Queue::node
ARC_QueueNode * node
Definition queue.c:15