refactor(queue): remove namespace

This commit is contained in:
2025-09-08 19:36:38 +02:00
parent 47856a43d9
commit fe80f687de
3 changed files with 28 additions and 27 deletions

View File

@@ -3,8 +3,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
mcl_queue_s *mcl_queue_new(size_t queue_size, size_t elem_size) { queue_s *queue_new(size_t queue_size, size_t elem_size) {
mcl_queue_s *queue = malloc(sizeof(mcl_queue_s)); queue_s *queue = malloc(sizeof(queue_s));
if (queue == NULL) { if (queue == NULL) {
return NULL; return NULL;
} }
@@ -33,7 +33,7 @@ mcl_queue_s *mcl_queue_new(size_t queue_size, size_t elem_size) {
return queue; return queue;
} }
int mcl_queue_push(mcl_queue_s *queue, const void *elem) { int queue_push(queue_s *queue, const void *elem) {
int ret = mtx_lock(&queue->lock); int ret = mtx_lock(&queue->lock);
if (ret != thrd_success) { if (ret != thrd_success) {
return -1; return -1;
@@ -58,7 +58,7 @@ int mcl_queue_push(mcl_queue_s *queue, const void *elem) {
return 0; return 0;
} }
int mcl_queue_pop(mcl_queue_s *queue, void *out_elem) { int queue_pop(queue_s *queue, void *out_elem) {
int ret = mtx_lock(&queue->lock); int ret = mtx_lock(&queue->lock);
if (ret != thrd_success) { if (ret != thrd_success) {
return -1; return -1;
@@ -82,7 +82,7 @@ int mcl_queue_pop(mcl_queue_s *queue, void *out_elem) {
return 0; return 0;
} }
int mcl_queue_get_front(mcl_queue_s *queue, void *out) { int queue_get_front(queue_s *queue, void *out) {
int ret = mtx_lock(&queue->lock); int ret = mtx_lock(&queue->lock);
if (ret != thrd_success) { if (ret != thrd_success) {
return -1; return -1;
@@ -102,7 +102,7 @@ int mcl_queue_get_front(mcl_queue_s *queue, void *out) {
return 0; return 0;
} }
int mcl_queue_get_rear(mcl_queue_s *queue, void *out) { int queue_get_rear(queue_s *queue, void *out) {
int ret = mtx_lock(&queue->lock); int ret = mtx_lock(&queue->lock);
if (ret != thrd_success) { if (ret != thrd_success) {
return -1; return -1;
@@ -129,7 +129,7 @@ int mcl_queue_get_rear(mcl_queue_s *queue, void *out) {
return 0; return 0;
} }
void mcl_queue_free(mcl_queue_s *queue) { void queue_free(queue_s *queue) {
if (queue == NULL) { if (queue == NULL) {
return; return;
} }

View File

@@ -7,7 +7,7 @@
/** /**
* @brief A simple circular queue (ring buffer). * @brief A simple circular queue (ring buffer).
*/ */
typedef struct mcl_queue { typedef struct queue {
size_t front; /**< Index of the next element to read. */ size_t front; /**< Index of the next element to read. */
size_t rear; /**< Index where the next element will be written. */ size_t rear; /**< Index where the next element will be written. */
size_t size; /**< Current number of elements in the queue. */ size_t size; /**< Current number of elements in the queue. */
@@ -15,7 +15,7 @@ typedef struct mcl_queue {
size_t elem_size; /**< Size in bytes of each element. */ size_t elem_size; /**< Size in bytes of each element. */
void *buffer; /**< Memory buffer that holds the elements. */ void *buffer; /**< Memory buffer that holds the elements. */
mtx_t lock; /**< Mutex to protect concurrent access. */ mtx_t lock; /**< Mutex to protect concurrent access. */
} mcl_queue_s; } queue_s;
/** /**
* @brief Create and initialize a new queue. * @brief Create and initialize a new queue.
@@ -24,7 +24,7 @@ typedef struct mcl_queue {
* @param elem_size Size in bytes of each element. * @param elem_size Size in bytes of each element.
* @return Pointer to the new queue, or NULL on failure. * @return Pointer to the new queue, or NULL on failure.
*/ */
mcl_queue_s *mcl_queue_new(size_t queue_size, size_t elem_size); queue_s *queue_new(size_t queue_size, size_t elem_size);
/** /**
* @brief Add an element to the queue. * @brief Add an element to the queue.
@@ -33,7 +33,7 @@ mcl_queue_s *mcl_queue_new(size_t queue_size, size_t elem_size);
* @param elem Pointer to the data to add. * @param elem Pointer to the data to add.
* @return 0 on success, -1 if the queue is full or on error. * @return 0 on success, -1 if the queue is full or on error.
*/ */
int mcl_queue_push(mcl_queue_s *queue, const void *elem); int queue_push(queue_s *queue, const void *elem);
/** /**
* @brief Remove an element from the queue. * @brief Remove an element from the queue.
@@ -42,7 +42,7 @@ int mcl_queue_push(mcl_queue_s *queue, const void *elem);
* @param out_elem Pointer to memory where the removed element will be copied. * @param out_elem Pointer to memory where the removed element will be copied.
* @return 0 on success, -1 if the queue is empty or on error. * @return 0 on success, -1 if the queue is empty or on error.
*/ */
int mcl_queue_pop(mcl_queue_s *queue, void *out_elem); int queue_pop(queue_s *queue, void *out_elem);
/** /**
* @brief Copy the front element without removing it. * @brief Copy the front element without removing it.
@@ -51,7 +51,7 @@ int mcl_queue_pop(mcl_queue_s *queue, void *out_elem);
* @param out Pointer to memory where the element will be copied. * @param out Pointer to memory where the element will be copied.
* @return 0 on success, -1 if the queue is empty or on error. * @return 0 on success, -1 if the queue is empty or on error.
*/ */
int mcl_queue_get_front(mcl_queue_s *queue, void *out); int queue_get_front(queue_s *queue, void *out);
/** /**
* @brief Copy the last element without removing it. * @brief Copy the last element without removing it.
@@ -60,13 +60,13 @@ int mcl_queue_get_front(mcl_queue_s *queue, void *out);
* @param out Pointer to memory where the element will be copied. * @param out Pointer to memory where the element will be copied.
* @return 0 on success, -1 if the queue is empty or on error. * @return 0 on success, -1 if the queue is empty or on error.
*/ */
int mcl_queue_get_rear(mcl_queue_s *queue, void *out); int queue_get_rear(queue_s *queue, void *out);
/** /**
* @brief Free all resources used by the queue. * @brief Free all resources used by the queue.
* *
* @param queue Pointer to the queue to free. * @param queue Pointer to the queue to free.
*/ */
void mcl_queue_free(mcl_queue_s *queue); void queue_free(queue_s *queue);
#endif // MYCLIB_QUEUE_H #endif // MYCLIB_QUEUE_H

View File

@@ -4,45 +4,46 @@
void test_q1(void) { void test_q1(void) {
/* Allocate a new queue */ /* Allocate a new queue */
mcl_queue_s *queue = mcl_queue_new(3, sizeof(int)); queue_s *queue = queue_new(3, sizeof(int));
assert(queue != NULL); assert(queue != NULL);
int val, out; int val, out;
/* Push value to the ring buffer */ /* Push value to the ring buffer */
val = 1; val = 1;
assert(mcl_queue_push(queue, &val) == 0); assert(queue_push(queue, &val) == 0);
val = 2; val = 2;
assert(mcl_queue_push(queue, &val) == 0); assert(queue_push(queue, &val) == 0);
/* Retrieve values */ /* Retrieve values */
int front, rear; int front, rear;
assert(mcl_queue_get_front(queue, &front) == 0); assert(queue_get_front(queue, &front) == 0);
assert(mcl_queue_get_rear(queue, &rear) == 0); assert(queue_get_rear(queue, &rear) == 0);
assert(front == 1); assert(front == 1);
assert(rear == 2); assert(rear == 2);
/* Remove an element from the buffer */ /* Remove an element from the buffer */
assert(mcl_queue_pop(queue, &out) == 0); assert(queue_pop(queue, &out) == 0);
assert(out == 1); assert(out == 1);
assert(mcl_queue_get_front(queue, &front) == 0); assert(queue_get_front(queue, &front) == 0);
assert(front == 2); assert(front == 2);
val = 3; val = 3;
assert(mcl_queue_push(queue, &val) == 0); assert(queue_push(queue, &val) == 0);
assert(mcl_queue_get_rear(queue, &rear) == 0); assert(queue_get_rear(queue, &rear) == 0);
assert(rear == 3); assert(rear == 3);
val = 4; val = 4;
assert(mcl_queue_push(queue, &val) == 0); assert(queue_push(queue, &val) == 0);
/* Clear queue */ /* Clear queue */
while (mcl_queue_pop(queue, &out) == 0) {} while (queue_pop(queue, &out) == 0) {
}
assert(queue->size == 0); assert(queue->size == 0);
/* Deallocate memory */ /* Deallocate memory */
mcl_queue_free(queue); queue_free(queue);
} }