LibJuno 0.23.0
LibJuno is a lightweight C99 library designed specifically for embedded systems.
Loading...
Searching...
No Matches
buff_queue_api.h
Go to the documentation of this file.
1/*
2 MIT License
3
4 Copyright (c) 2025 Robin A. Onsay
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files
8 (the "Software"), to deal in the Software without restriction,
9 including without limitation the rights to use, copy, modify, merge,
10 publish, distribute, sublicense, and/or sell copies of the Software,
11 and to permit persons to whom the Software is furnished to do so,
12 subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16*/
17
27#ifndef JUNO_BUFF_QUEUE_API_H
28#define JUNO_BUFF_QUEUE_API_H
29#include "juno/macros.h"
31#include "juno/status.h"
32#include "juno/module.h"
33#ifdef __cplusplus
34extern "C"
35{
36#endif
37
38typedef struct JUNO_BUFF_QUEUE_API_TAG JUNO_BUFF_QUEUE_API_T;
40
41
50
51static inline JUNO_STATUS_T JunoBuff_QueueInit(JUNO_BUFF_QUEUE_T *ptQueue, size_t zCapacity, JUNO_FAILURE_HANDLER_T pfcnFailureHdlr, JUNO_USER_DATA_T *pvFailureUserData)
52{
53 ASSERT_EXISTS(ptQueue);
54 ptQueue->iStartIndex = 0;
55 ptQueue->zLength = 0;
56 ptQueue->zCapacity = zCapacity;
57 ptQueue->_pfcnFailureHandler = pfcnFailureHdlr;
58 ptQueue->_pvFailurUserData = pvFailureUserData;
60}
61
62static inline JUNO_STATUS_T JunoBuff_QueueEnqueue(JUNO_BUFF_QUEUE_T *ptQueue, size_t *ptRetIndex)
63{
64 ASSERT_EXISTS(ptQueue);
65 if(ptQueue->zLength < ptQueue->zCapacity)
66 {
67 ptQueue->zLength += 1;
68 }
69 else
70 {
71 FAIL(JUNO_STATUS_INVALID_SIZE_ERROR, ptQueue->_pfcnFailureHandler, ptQueue->_pvFailurUserData, "Failed to enqueue data");
73 }
74 if(ptRetIndex)
75 {
76 *ptRetIndex = ptQueue->zLength % ptQueue->zCapacity;
77 }
79}
80
81static inline JUNO_STATUS_T JunoBuff_QueueDequeue(JUNO_BUFF_QUEUE_T *ptQueue, size_t *ptRetIndex)
82{
83 ASSERT_EXISTS(ptQueue);
84 if(ptQueue->zLength > 0)
85 {
86 ptQueue->iStartIndex = (ptQueue->iStartIndex + 1) % ptQueue->zCapacity;
87 ptQueue->zLength -= 1;
88 if(ptRetIndex)
89 {
90 *ptRetIndex = ptQueue->iStartIndex;
91 }
93 }
94 FAIL(JUNO_STATUS_ERR, ptQueue->_pfcnFailureHandler, ptQueue->_pvFailurUserData, "Queue is empty");
95 return JUNO_STATUS_ERR;
96}
97
98static inline JUNO_STATUS_T JunoBuff_QueueGetIndex(JUNO_BUFF_QUEUE_T *ptQueue, size_t *ptRetIndex)
99{
100 ASSERT_EXISTS(ptQueue);
101 if(*ptRetIndex)
102 {
103 *ptRetIndex = ptQueue->iStartIndex;
104 }
105 return JUNO_STATUS_SUCCESS;
106}
107
108
109#ifdef __cplusplus
110}
111#endif
112#endif // JUNO_BUFF_QUEUE_API_H
struct JUNO_BUFF_QUEUE_API_TAG JUNO_BUFF_QUEUE_API_T
Definition buff_queue_api.h:38
#define ASSERT_EXISTS(ptr)
Definition macros.h:28
void(* JUNO_FAILURE_HANDLER_T)(JUNO_STATUS_T tStatus, const char *pcCustomMessage, JUNO_USER_DATA_T *pvUserData)
Definition status.h:44
#define FAIL(tStatus, pfcnFailureHandler, pvFailureUserData, pcMessage)
Definition status.h:49
@ JUNO_STATUS_SUCCESS
Definition status.h:24
@ JUNO_STATUS_ERR
Definition status.h:25
@ JUNO_STATUS_INVALID_SIZE_ERROR
Definition status.h:30
enum JUNO_STATUS_TAG JUNO_STATUS_T
void JUNO_USER_DATA_T
Definition status.h:43
Definition buff_queue_api.h:43
size_t zLength
Definition buff_queue_api.h:45
JUNO_USER_DATA_T * JUNO_FAILURE_USER_DATA
Definition buff_queue_api.h:48
size_t zCapacity
Definition buff_queue_api.h:46
JUNO_FAILURE_HANDLER_T JUNO_FAILURE_HANDLER
Definition buff_queue_api.h:47
size_t iStartIndex
Definition buff_queue_api.h:44