LibJuno 0.42.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"
30#include "juno/status.h"
31#include "juno/module.h"
32#include "juno/types.h"
33#ifdef __cplusplus
34extern "C"
35{
36#endif
37
39typedef struct JUNO_BUFF_QUEUE_ROOT_TAG JUNO_BUFF_QUEUE_ROOT_T;
42
44struct JUNO_BUFF_QUEUE_ROOT_TAG JUNO_MODULE_ROOT(void,
46 size_t iStartIndex;
48 size_t zLength;
50 size_t zCapacity;
51);
52
54static inline JUNO_STATUS_T JunoBuff_QueueInit(JUNO_BUFF_QUEUE_T *ptQueue, size_t zCapacity, JUNO_FAILURE_HANDLER_T pfcnFailureHdlr, JUNO_USER_DATA_T *pvFailureUserData)
55{
56 JUNO_ASSERT_EXISTS(ptQueue);
57 JUNO_BUFF_QUEUE_ROOT_T *ptQueueRoot = (JUNO_BUFF_QUEUE_ROOT_T *)(ptQueue);
58 ptQueueRoot->iStartIndex = 0;
59 ptQueueRoot->zLength = 0;
60 ptQueueRoot->zCapacity = zCapacity;
61 ptQueueRoot->_pfcnFailureHandler = pfcnFailureHdlr;
62 ptQueueRoot->_pvFailureUserData = pvFailureUserData;
64}
65
68static inline JUNO_RESULT_SIZE_T JunoBuff_QueueEnqueue(JUNO_BUFF_QUEUE_T *ptQueue)
69{
70 JUNO_RESULT_SIZE_T tResult = {JUNO_STATUS_SUCCESS,0};
71 if(!ptQueue)
72 {
73 tResult.tStatus = JUNO_STATUS_NULLPTR_ERROR;
74 return tResult;
75 }
76 JUNO_BUFF_QUEUE_ROOT_T *ptQueueRoot = (JUNO_BUFF_QUEUE_ROOT_T *)(ptQueue);
77 if(ptQueueRoot->zLength < ptQueueRoot->zCapacity)
78 {
79 tResult.tOk = (ptQueueRoot->iStartIndex + ptQueueRoot->zLength) % ptQueueRoot->zCapacity;
80 ptQueueRoot->zLength += 1;
81 }
82 else
83 {
84 tResult.tStatus = JUNO_STATUS_INVALID_SIZE_ERROR;
85 JUNO_FAIL(tResult.tStatus, ptQueueRoot->_pfcnFailureHandler, ptQueueRoot->_pvFailureUserData, "Failed to enqueue data");
86 return tResult;
87 }
88 return tResult;
89}
90
93static inline JUNO_RESULT_SIZE_T JunoBuff_QueueDequeue(JUNO_BUFF_QUEUE_T *ptQueue)
94{
95 JUNO_RESULT_SIZE_T tResult = {JUNO_STATUS_SUCCESS,0};
96 if(!ptQueue)
97 {
98 tResult.tStatus = JUNO_STATUS_NULLPTR_ERROR;
99 return tResult;
100 }
101 JUNO_BUFF_QUEUE_ROOT_T *ptQueueRoot = (JUNO_BUFF_QUEUE_ROOT_T *)(ptQueue);
102 if(ptQueueRoot->zLength > 0)
103 {
104 tResult.tOk = ptQueueRoot->iStartIndex;
105 ptQueueRoot->iStartIndex = (ptQueueRoot->iStartIndex + 1) % ptQueueRoot->zCapacity;
106 ptQueueRoot->zLength -= 1;
107 return tResult;
108 }
109 tResult.tStatus = JUNO_STATUS_ERR;
110 JUNO_FAIL(tResult.tStatus, ptQueueRoot->_pfcnFailureHandler, ptQueueRoot->_pvFailureUserData, "Queue is empty");
111 return tResult;
112}
113
116static inline JUNO_RESULT_SIZE_T JunoBuff_QueuePeek(JUNO_BUFF_QUEUE_T *ptQueue)
117{
118 JUNO_RESULT_SIZE_T tResult = {JUNO_STATUS_SUCCESS,0};
119 if(!ptQueue)
120 {
121 tResult.tStatus = JUNO_STATUS_NULLPTR_ERROR;
122 return tResult;
123 }
124 JUNO_BUFF_QUEUE_ROOT_T *ptQueueRoot = (JUNO_BUFF_QUEUE_ROOT_T *)(ptQueue);
125 if(ptQueueRoot->zLength == 0)
126 {
127 tResult.tStatus = JUNO_STATUS_INVALID_SIZE_ERROR;
128 JUNO_FAIL(tResult.tStatus, ptQueueRoot->_pfcnFailureHandler, ptQueueRoot->_pvFailureUserData, "Failed to enqueue data");
129 return tResult;
130 }
131 tResult.tOk = ptQueueRoot->iStartIndex;
132 return tResult;
133}
134
135
136#ifdef __cplusplus
137}
138#endif
139#endif // JUNO_BUFF_QUEUE_API_H
static JUNO_RESULT_SIZE_T JunoBuff_QueueDequeue(JUNO_BUFF_QUEUE_T *ptQueue)
Definition buff_queue_api.h:93
static JUNO_RESULT_SIZE_T JunoBuff_QueueEnqueue(JUNO_BUFF_QUEUE_T *ptQueue)
Definition buff_queue_api.h:68
static JUNO_RESULT_SIZE_T JunoBuff_QueuePeek(JUNO_BUFF_QUEUE_T *ptQueue)
Definition buff_queue_api.h:116
union JUNO_BUFF_QUEUE_T JUNO_BUFF_QUEUE_T
The Buffer queue module.
Definition buff_queue_api.h:41
struct JUNO_BUFF_QUEUE_ROOT_TAG JUNO_BUFF_QUEUE_ROOT_T
The Buffer queue root.
Definition buff_queue_api.h:39
static JUNO_STATUS_T JunoBuff_QueueInit(JUNO_BUFF_QUEUE_T *ptQueue, size_t zCapacity, JUNO_FAILURE_HANDLER_T pfcnFailureHdlr, JUNO_USER_DATA_T *pvFailureUserData)
Initialize a buffer queue with a capacity.
Definition buff_queue_api.h:54
#define JUNO_ASSERT_EXISTS(ptr)
Definition macros.h:28
#define JUNO_MODULE_ROOT(API_T,...)
Definition module.h:182
#define JUNO_STATUS_ERR
Definition status.h:25
void(* JUNO_FAILURE_HANDLER_T)(JUNO_STATUS_T tStatus, const char *pcCustomMessage, JUNO_USER_DATA_T *pvUserData)
Definition status.h:43
int32_t JUNO_STATUS_T
Definition status.h:23
#define JUNO_FAIL(tStatus, pfcnFailureHandler, pvFailureUserData, pcMessage)
Definition status.h:48
#define JUNO_STATUS_NULLPTR_ERROR
Definition status.h:26
#define JUNO_STATUS_INVALID_SIZE_ERROR
Definition status.h:30
#define JUNO_STATUS_SUCCESS
Definition status.h:24
void JUNO_USER_DATA_T
Definition status.h:42