bzr branch
http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
1 |
#ifndef _H_RING_BUFFER_
|
2 |
#define _H_RING_BUFFER_
|
|
3 |
||
4 |
#include "defs.h" |
|
5 |
#include "Func.h" |
|
6 |
||
|
110
by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub. |
7 |
S_BEGIN_DECLS
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
8 |
|
9 |
||
10 |
/**
|
|
11 |
* @file
|
|
12 |
* @defgroup SRingBuffer SRingBuffer
|
|
13 |
* @addtogroup SRingBuffer
|
|
14 |
* @{
|
|
15 |
* An SRingBuffer is a special data structure that is circular in nature.
|
|
16 |
* It only holds bytes and as such is useful when dealing with strings and
|
|
17 |
* other eight-bit data.
|
|
18 |
*
|
|
19 |
* If you are looking for a circular array, this is not
|
|
20 |
* such a structure.
|
|
21 |
*
|
|
22 |
* @section Theory
|
|
23 |
* The theory behind a circular structure is simple: Never have to deal with
|
|
24 |
* empty slots in an array, only permit indirect, non-indexed access.
|
|
25 |
*
|
|
26 |
<pre>
|
|
|
99
by Gustav Hartvigsson
* Working on RingBuffer and Stream. |
27 |
[b|b|b|b|b|b|b|b|b|b| | | | ]
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
28 |
^ ^
|
29 |
start end
|
|
|
99
by Gustav Hartvigsson
* Working on RingBuffer and Stream. |
30 |
front back
|
31 |
||
32 |
||
33 |
||
34 |
[ | | | | | |b|b|b|b| | | | ]
|
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
35 |
^ ^
|
36 |
start end
|
|
|
99
by Gustav Hartvigsson
* Working on RingBuffer and Stream. |
37 |
front back
|
38 |
||
39 |
||
40 |
||
41 |
[b|b| | | | | | | |b|b|b|b|b]
|
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
42 |
^ ^
|
43 |
end start
|
|
|
99
by Gustav Hartvigsson
* Working on RingBuffer and Stream. |
44 |
back front
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
45 |
</pre>
|
46 |
*/
|
|
47 |
||
48 |
/**
|
|
49 |
* The SRingBuffer is an opaque data structure that represent an array that
|
|
50 |
* is circular in nature.
|
|
51 |
*/
|
|
52 |
typedef struct SRingBuffer SRingBuffer; |
|
53 |
||
54 |
/**
|
|
55 |
* Create a new SRingBuffer objects.
|
|
56 |
*
|
|
57 |
* @param len The number of items to
|
|
58 |
*/
|
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
59 |
S_EXPORTED
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
60 |
SRingBuffer * |
61 |
s_ring_buffer_new (size_t len); |
|
62 |
||
63 |
/**
|
|
64 |
* Free an SRingBuffer.
|
|
65 |
*
|
|
66 |
* @param self The SRingBuffer to free.
|
|
67 |
*/
|
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
68 |
S_EXPORTED
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
69 |
void
|
70 |
s_ring_buffer_free (SRingBuffer * self); |
|
71 |
||
72 |
/**
|
|
|
99
by Gustav Hartvigsson
* Working on RingBuffer and Stream. |
73 |
* determine
|
74 |
*/
|
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
75 |
S_EXPORTED
|
|
99
by Gustav Hartvigsson
* Working on RingBuffer and Stream. |
76 |
sboolean
|
77 |
s_ring_buffer_is_empty (SRingBuffer * self); |
|
78 |
||
79 |
/**
|
|
80 |
* Add data to the front of the ringbuffer.
|
|
81 |
*
|
|
82 |
*/
|
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
83 |
S_EXPORTED
|
|
99
by Gustav Hartvigsson
* Working on RingBuffer and Stream. |
84 |
void
|
85 |
s_ring_buffer_push (SRingBuffer * self, |
|
86 |
sbyte data); |
|
87 |
||
88 |
/**
|
|
89 |
* add an item to the back of the ringbuffer.
|
|
90 |
*/
|
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
91 |
S_EXPORTED
|
|
99
by Gustav Hartvigsson
* Working on RingBuffer and Stream. |
92 |
void
|
93 |
s_ring_buffer_push_back (SRingBuffer * self, |
|
94 |
sbyte data); |
|
95 |
||
96 |
/**
|
|
97 |
* Pop an item from the front of the array.
|
|
98 |
*/
|
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
99 |
S_EXPORTED
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
100 |
sbyte
|
101 |
s_ring_buffer_pop (SRingBuffer * self); |
|
102 |
||
103 |
/**
|
|
|
99
by Gustav Hartvigsson
* Working on RingBuffer and Stream. |
104 |
* Pop the back of the array.
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
105 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
106 |
S_EXPORTED
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
107 |
sbyte
|
|
99
by Gustav Hartvigsson
* Working on RingBuffer and Stream. |
108 |
s_ring_buffer_pop_back (SRingBuffer * self); |
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
109 |
|
110 |
/**
|
|
111 |
* Reallocate an SRingBuffer to a now size for the internal array.
|
|
112 |
*
|
|
113 |
* @param self The SRingBuffer to reallocate.
|
|
114 |
* @param len The new length.
|
|
115 |
*/
|
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
116 |
S_EXPORTED
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
117 |
void
|
|
99
by Gustav Hartvigsson
* Working on RingBuffer and Stream. |
118 |
s_ring_buffer_realloc (SRingBuffer * self, |
119 |
size_t len); |
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
120 |
|
121 |
/**
|
|
122 |
* Get the current length of the ring buffer.
|
|
123 |
*/
|
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
124 |
S_EXPORTED
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
125 |
size_t
|
126 |
s_ring_buffer_len (SRingBuffer * self); |
|
127 |
||
128 |
/**
|
|
129 |
* Get the total size of the internal array.
|
|
130 |
*/
|
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
131 |
S_EXPORTED
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
132 |
size_t
|
133 |
s_ring_buffer_size (SRingBuffer * self); |
|
134 |
||
135 |
/**
|
|
136 |
* Iterate over the bytes in the SRingBuffer.
|
|
137 |
*
|
|
138 |
* This is the only valid way to iterate over the bytes in an SRingBuffer.
|
|
139 |
*
|
|
140 |
* @param self The SRingBuffer to iterate over.
|
|
141 |
* @param func Pointer to a function that is run on each byte.
|
|
142 |
* @param user_data Data that is provided to the function when doing the
|
|
143 |
* iteration.
|
|
144 |
*
|
|
145 |
* @note
|
|
146 |
* This does not use pop or push operations, and is thus the only way to keep
|
|
147 |
* the data and still be able to iterate over it.
|
|
148 |
*
|
|
149 |
* The signature of the function provided should be as follows
|
|
150 |
@code{C}
|
|
151 |
void
|
|
152 |
my_for_each_func (SRingBuffer * buffer,
|
|
153 |
sbyte item,
|
|
154 |
spointer user_data) {
|
|
155 |
// code here
|
|
156 |
}
|
|
157 |
@endcode
|
|
158 |
*
|
|
159 |
* To prevent warnings cast the function using the @c FOREACHFUNC() macro.
|
|
160 |
*/
|
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
161 |
S_EXPORTED
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
162 |
void
|
163 |
s_ring_buffer_for_each (SRingBuffer * self, |
|
164 |
ForEachFunc func, |
|
165 |
spointer user_data); |
|
166 |
||
167 |
/**
|
|
168 |
* @}
|
|
169 |
*/
|
|
170 |
||
|
110
by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub. |
171 |
S_END_DECLS
|
|
98
by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream. |
172 |
|
173 |
#endif /* _H_RING_BUFFER_ */ |