59 lines
1.1 KiB
C
59 lines
1.1 KiB
C
#ifdef __TESTING__
|
|
#include <kernel/_kernel.h>
|
|
#endif
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
|
|
#include <queue.h>
|
|
|
|
void init_queue(queue *q)
|
|
{
|
|
q->front = -1;
|
|
q->rear = 0;
|
|
}
|
|
|
|
bool queue_is_empty(queue *q)
|
|
{
|
|
bool empty = q->front == q->rear - 1;
|
|
if (empty) // This allows us to continuously reuse the queue when it empties. Ideally i'd like to switch to a heap implementation... TODO
|
|
init_queue(q);
|
|
return empty;
|
|
}
|
|
bool queue_is_full(queue *q)
|
|
{return (q->rear == MAX_QUEUE_SIZE);} // TODO: this is an error for SURE
|
|
|
|
void enqueue(queue *q, uint8_t val)
|
|
{
|
|
if (queue_is_full(q)) {
|
|
#ifdef __TESTING__
|
|
kwarn("Queue being added to is full!!"); // TODO add a way to print like code lines and addresses to stack trace this
|
|
#endif
|
|
return;
|
|
}
|
|
q->items[q->rear++] = val;
|
|
}
|
|
|
|
void dequeue(queue* q)
|
|
{
|
|
if (queue_is_empty(q)) {
|
|
#ifdef __TESTING__
|
|
kwarn("Queue thats already empty is trying to be removed from!!!"); // TODO same as above
|
|
#endif
|
|
return;
|
|
}
|
|
q->front++;
|
|
}
|
|
|
|
uint8_t queue_peek(queue* q)
|
|
{
|
|
if (queue_is_empty(q)) {
|
|
#ifdef __TESTING__
|
|
kwarn("Peeking an empty queue!");
|
|
#endif
|
|
return -1;
|
|
|
|
}
|
|
return q->items[q->front + 1];
|
|
}
|