Remove Twitter links.
[SquirrelJME.git] / nanocoat / include / sjme / circleBuffer.h
blob1c1e4c902ab8329c0a4360e64a4e80a52681396d
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 /**
11 * Circular buffer, also known as a round robin.
13 * @since 2024/08/25
16 #ifndef SQUIRRELJME_CIRCLEBUFFER_H
17 #define SQUIRRELJME_CIRCLEBUFFER_H
19 #include "sjme/error.h"
20 #include "sjme/stdTypes.h"
21 #include "sjme/alloc.h"
23 /* Anti-C++. */
24 #ifdef __cplusplus
25 #ifndef SJME_CXX_IS_EXTERNED
26 #define SJME_CXX_IS_EXTERNED
27 #define SJME_CXX_SQUIRRELJME_CIRCLEBUFFER_H
28 extern "C"
30 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
31 #endif /* #ifdef __cplusplus */
33 /*--------------------------------------------------------------------------*/
35 /**
36 * Represents the mode of how the buffer acts.
38 * @since 2024/08/25
40 typedef enum sjme_circleBuffer_mode
42 /** The buffer acts as a queue. */
43 SJME_CIRCLE_BUFFER_QUEUE,
45 /** The buffer acts as a circular window. */
46 SJME_CIRCLE_BUFFER_WINDOW,
48 /** The number of overflow modes. */
49 SJME_CIRCLE_BUFFER_NUM_MODES,
50 } sjme_circleBuffer_mode;
52 /**
53 * Used to refer to the front or the back of a circular buffer.
55 * @since 2024/08/25
57 typedef enum sjme_circleBuffer_seekEnd
59 /** The front/head of the buffer. */
60 SJME_CIRCLE_BUFFER_HEAD,
62 /** The back/tail of the buffer. */
63 SJME_CIRCLE_BUFFER_TAIL,
65 /** The number of seek ends. */
66 SJME_CIRCLE_BUFFER_NUM_SEEK_END,
68 /** The front/head of the buffer. */
69 SJME_CIRCLE_BUFFER_FIRST = SJME_CIRCLE_BUFFER_HEAD,
71 /** The back/tail of the buffer. */
72 SJME_CIRCLE_BUFFER_LAST = SJME_CIRCLE_BUFFER_TAIL,
74 /** The front/head of the buffer. */
75 SJME_CIRCLE_BUFFER_FRONT = SJME_CIRCLE_BUFFER_HEAD,
77 /** The back/tail of the buffer. */
78 SJME_CIRCLE_BUFFER_BACK = SJME_CIRCLE_BUFFER_TAIL,
79 } sjme_circleBuffer_seekEnd;
81 /**
82 * Represents a circle buffer and its data.
84 * @since 2024/08/25
86 typedef struct sjme_circleBuffer
88 /** The buffer mode. */
89 sjme_circleBuffer_mode mode;
91 /** The size of the buffer. */
92 sjme_jint size;
94 /** The amount of data that is in the buffer. */
95 sjme_jint ready;
97 /** The current read head. */
98 sjme_jint readHead;
100 /** The current write head. */
101 sjme_jint writeHead;
103 /** The buffer storage. */
104 sjme_jubyte* buffer;
105 } sjme_circleBuffer;
108 * Returns the number of bytes that are free within the buffer.
110 * @param buffer The buffer to get the empty space of.
111 * @param outAvailable The number of bytes not used in the buffer that
112 * are available.
113 * @return Any resultant error, if any.
114 * @since 2024/08/25
116 sjme_errorCode sjme_circleBuffer_available(
117 sjme_attrInNotNull sjme_circleBuffer* buffer,
118 sjme_attrOutNotNull sjme_jint* outAvailable);
121 * Destroys the buffer and frees any associated memory.
123 * @param buffer The buffer to free.
124 * @return Any resultant error, if any.
125 * @since 2024/08/25
127 sjme_errorCode sjme_circleBuffer_destroy(
128 sjme_attrInNotNull sjme_circleBuffer* buffer);
131 * Gets data from the buffer, without removing it, from the specified end
132 * and position.
134 * @param buffer The buffer to read from.
135 * @param outData The destination buffer.
136 * @param length The number of bytes to read.
137 * @param seekType Seek from the head or tail?
138 * @param seekPos The seek position, relative to @c seekType .
139 * @return Any resultant error, if any.
140 * @since 2024/08/25
142 sjme_errorCode sjme_circleBuffer_get(
143 sjme_attrInNotNull sjme_circleBuffer* buffer,
144 sjme_attrOutNotNullBuf(length) sjme_pointer outData,
145 sjme_attrInPositiveNonZero sjme_jint length,
146 sjme_attrInValue sjme_circleBuffer_seekEnd seekType,
147 sjme_attrInPositiveNonZero sjme_jint seekPos);
150 * Allocates a new circular buffer.
152 * @param allocPool The pool to allocate within.
153 * @param outBuffer The resultant newly created buffer.
154 * @param inMode The mode that the buffer should be using.
155 * @param length The maximum length of the buffer to allocate.
156 * @return On any resultant error, if any.
157 * @since 2024/08/25
159 sjme_errorCode sjme_circleBuffer_new(
160 sjme_attrInNotNull sjme_alloc_pool allocPool,
161 sjme_attrOutNotNull sjme_circleBuffer** outBuffer,
162 sjme_attrInValue sjme_circleBuffer_mode inMode,
163 sjme_attrInPositiveNonZero sjme_jint length);
166 * Removes data from the buffer at the given end.
168 * @param buffer The buffer to remove from.
169 * @param outData The resultant data that was in the buffer.
170 * @param length The number of bytes to remove.
171 * @param seekType Remove from the head or the tail?
172 * @return On any resultant error, if any.
173 * @since 2024/08/25
175 sjme_errorCode sjme_circleBuffer_pop(
176 sjme_attrInNotNull sjme_circleBuffer* buffer,
177 sjme_attrOutNotNullBuf(length) sjme_pointer outData,
178 sjme_attrInPositiveNonZero sjme_jint length,
179 sjme_attrInValue sjme_circleBuffer_seekEnd seekType);
182 * Pushes data to the buffer at the given end.
184 * @param buffer The buffer to add to.
185 * @param inData The data to be added.
186 * @param length The number of bytes to add.
187 * @param seekType Add at the head or the tail?
188 * @return On any resultant error, if any.
189 * @since 2024/08/25
191 sjme_errorCode sjme_circleBuffer_push(
192 sjme_attrInNotNull sjme_circleBuffer* buffer,
193 sjme_attrInNotNullBuf(length) sjme_cpointer inData,
194 sjme_attrInPositiveNonZero sjme_jint length,
195 sjme_attrInValue sjme_circleBuffer_seekEnd seekType);
198 * Returns the current amount of data that is stored in the buffer.
200 * @param buffer The buffer to get the current amount of data in.
201 * @param outStored The number of bytes stored in the buffer.
202 * @return Any resultant error, if any.
203 * @since 2024/08/25
205 sjme_errorCode sjme_circleBuffer_stored(
206 sjme_attrInNotNull sjme_circleBuffer* buffer,
207 sjme_attrOutNotNull sjme_jint* outStored);
209 /*--------------------------------------------------------------------------*/
211 /* Anti-C++. */
212 #ifdef __cplusplus
213 #ifdef SJME_CXX_SQUIRRELJME_CIRCLEBUFFER_H
215 #undef SJME_CXX_SQUIRRELJME_CIRCLEBUFFER_H
216 #undef SJME_CXX_IS_EXTERNED
217 #endif /* #ifdef SJME_CXX_SQUIRRELJME_CIRCLEBUFFER_H */
218 #endif /* #ifdef __cplusplus */
220 #endif /* SQUIRRELJME_CIRCLEBUFFER_H */