1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
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 // -------------------------------------------------------------------------*/
11 * Circular buffer, also known as a round robin.
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"
25 #ifndef SJME_CXX_IS_EXTERNED
26 #define SJME_CXX_IS_EXTERNED
27 #define SJME_CXX_SQUIRRELJME_CIRCLEBUFFER_H
30 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
31 #endif /* #ifdef __cplusplus */
33 /*--------------------------------------------------------------------------*/
36 * Represents the mode of how the buffer acts.
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
;
53 * Used to refer to the front or the back of a circular buffer.
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
;
82 * Represents a circle buffer and its data.
86 typedef struct sjme_circleBuffer
88 /** The buffer mode. */
89 sjme_circleBuffer_mode mode
;
91 /** The size of the buffer. */
94 /** The amount of data that is in the buffer. */
97 /** The current read head. */
100 /** The current write head. */
103 /** The buffer storage. */
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
113 * @return Any resultant error, if any.
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.
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
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.
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.
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.
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.
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.
205 sjme_errorCode
sjme_circleBuffer_stored(
206 sjme_attrInNotNull sjme_circleBuffer
* buffer
,
207 sjme_attrOutNotNull sjme_jint
* outStored
);
209 /*--------------------------------------------------------------------------*/
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 */