Improved error handling in queue.c

This commit is contained in:
2022-08-23 12:56:45 -05:00
parent 9c37167c5e
commit 2f7d39e679
2 changed files with 17 additions and 5 deletions

View File

@@ -1,5 +1,7 @@
#pragma once #pragma once
#include "types/status.h"
struct queue_node_t; struct queue_node_t;
/** /**
@@ -38,15 +40,16 @@ void queue_construct(struct queue_t *queue);
* @brief Inserts a new item at the end of the queue. * @brief Inserts a new item at the end of the queue.
* *
* @param queue * @param queue
* @param ptr * @param ptr A pointer to some user-defined data to store on the queue.
* @return enum error_t
*/ */
void queue_insert(struct queue_t *queue, void *ptr); enum error_t queue_insert(struct queue_t *queue, void *ptr);
/** /**
* @brief Removes the next item from the queue and returns it. * @brief Removes the next item from the queue and returns it.
* *
* @param queue * @param queue
* @return void* * @return void* The pointer stored at the front of the queue.
*/ */
void *queue_get_next(struct queue_t *queue); void *queue_get_next(struct queue_t *queue);
@@ -54,7 +57,7 @@ void *queue_get_next(struct queue_t *queue);
* @brief Returns the next item on the queue without removing it. * @brief Returns the next item on the queue without removing it.
* *
* @param queue * @param queue
* @return void* * @return void* The pointer stored at the front of the queue.
*/ */
void *queue_peek(struct queue_t *queue); void *queue_peek(struct queue_t *queue);

View File

@@ -23,9 +23,13 @@ void queue_construct(struct queue_t *queue)
queue->count = 0; queue->count = 0;
} }
void queue_insert(struct queue_t *queue, void *ptr) enum error_t queue_insert(struct queue_t *queue, void *ptr)
{ {
struct queue_node_t *node = kmalloc(sizeof(struct queue_node_t)); struct queue_node_t *node = kmalloc(sizeof(struct queue_node_t));
if(node == NULL)
{
return ENOMEM;
}
node->ptr = ptr; node->ptr = ptr;
node->next = NULL; node->next = NULL;
if(queue->last == NULL) if(queue->last == NULL)
@@ -38,11 +42,16 @@ void queue_insert(struct queue_t *queue, void *ptr)
queue->last = node; queue->last = node;
} }
queue->count++; queue->count++;
return ENONE;
} }
void *queue_get_next(struct queue_t *queue) void *queue_get_next(struct queue_t *queue)
{ {
struct queue_node_t *node = queue->first; struct queue_node_t *node = queue->first;
if(node == NULL)
{
return NULL;
}
queue->first = node->next; queue->first = node->next;
if(queue->first == NULL) if(queue->first == NULL)
{ {