/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk

« back to all changes in this revision

Viewing changes to libssts/RingBuffer.h

  • Committer: Gustav Hartvigsson
  • Date: 2016-08-31 18:56:59 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20160831185659-vj3kpiv6sadtfjbw
* Started work on the ring buffer, still untested and may contain
logical errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 * @addtogroup SRingBuffer
13
13
 * @{
14
14
 * An SRingBuffer is a special data structure that is circular in nature.
15
 
 * It only holds bytes and as such is useful when dealing with strings and
16
 
 * other eight-bit data.
17
15
 *
18
 
 * If you are looking for a circular array, this is not
19
 
 * such a structure.
 
16
 * This is not a represent for a queue, this is only for byte sized data
 
17
 * elements, like text and such.
20
18
 *
21
19
 * @section Theory
22
20
 * The theory behind a circular structure is simple: Never have to deal with
42
40
     end             start
43
41
     back            front
44
42
 </pre>
 
43
 *
45
44
 */
46
45
 
47
46
/**
53
52
/**
54
53
 * Create a new SRingBuffer objects.
55
54
 *
56
 
 * @param len The number of items to 
 
55
 * @param size The number of items to allocate space for.
 
56
 * @param free_func The function that is used to free the data.
 
57
 *
 
58
 * @note It is recomended to have a big size of the buffer to make room for
 
59
 *       each item that should be stored plus a bit extra. (if you expect to
 
60
 *       have \f$n\f$ items allocate \f$n + \frac{n}{2}\f$ slots.)
57
61
 */
58
62
S_EXPORTED
59
63
SRingBuffer *
60
 
s_ring_buffer_new (size_t len);
 
64
s_ring_buffer_new (size_t size);
61
65
 
62
66
/**
63
67
 * Free an SRingBuffer.
64
68
 *
65
69
 * @param self The SRingBuffer to free.
 
70
 * @param free_data Set to @c TRUE to free the data.
66
71
 */
67
72
S_EXPORTED
68
73
void
69
74
s_ring_buffer_free (SRingBuffer * self);
70
75
 
71
76
/**
72
 
 * determine 
 
77
 * determine if buffer is empty.
73
78
 */
74
79
S_EXPORTED
75
80
sboolean
77
82
 
78
83
/**
79
84
 * Add data to the front of the ringbuffer.
80
 
 * 
 
85
 *
 
86
 * @param err Set to TRUE on error, FALSE otherwise.
81
87
 */
82
88
S_EXPORTED
83
89
void
84
90
s_ring_buffer_push (SRingBuffer * self,
85
 
                    sbyte data);
 
91
                    sbyte data,
 
92
                    sboolean * err);
86
93
 
87
94
/**
88
 
 * add an item to the back of the ringbuffer.
 
95
 * add an item to the front of the ringbuffer.
 
96
 
 
97
 * @param err Set to TRUE on error, FALSE otherwise.
89
98
 */
90
99
S_EXPORTED
91
100
void
92
 
s_ring_buffer_push_back (SRingBuffer * self,
93
 
                         sbyte data);
 
101
s_ring_buffer_push_front (SRingBuffer * self,
 
102
                          subyte data,
 
103
                          sboolean * err);
94
104
 
95
105
/**
96
106
 * Pop an item from the front of the array.
97
 
 */
98
 
S_EXPORTED
99
 
sbyte
100
 
s_ring_buffer_pop (SRingBuffer * self);
101
 
 
102
 
/**
103
 
 * Pop the back of the array.
104
 
 */
105
 
S_EXPORTED
106
 
sbyte
107
 
s_ring_buffer_pop_back (SRingBuffer * self);
108
 
 
109
 
/**
110
 
 * Reallocate an SRingBuffer to a now size for the internal array.
 
107
 
 
108
 * @param err Set to TRUE on error, FALSE otherwise.
 
109
 */
 
110
S_EXPORTED
 
111
spointer
 
112
s_ring_buffer_pop (SRingBuffer * self,
 
113
                   sboolean * err);
 
114
 
 
115
/**
 
116
 * Pop the front of the array.
 
117
 
 
118
 * @param err Set to TRUE on error, FALSE otherwise.
 
119
 */
 
120
S_EXPORTED
 
121
spointer
 
122
s_ring_buffer_pop_front (SRingBuffer * self,
 
123
                         sboolean * err);
 
124
 
 
125
/**
 
126
 * Peek at the byte at the end of the buffer.
 
127
 
 
128
 * @param err Set to TRUE on error, FALSE otherwise.
 
129
 */
 
130
S_EXPORTED
 
131
sbyte
 
132
s_ring_buffer_peek (SRingBuffer * self,
 
133
                    sboolean * err);
 
134
 
 
135
/**
 
136
 * Peek at the byte at the front of the buffer.
 
137
 *
 
138
 * @param err Set to TRUE on error, FALSE otherwise.
 
139
 */
 
140
S_EXPORTED
 
141
sbyte
 
142
s_ring_buffer_peek_front (SRingBuffer * self,
 
143
                          sboolean * err);
 
144
 
 
145
/**
 
146
 * Reallocate an SRingBuffer to a new size for the internal array.
111
147
 *
112
148
 * @param self The SRingBuffer to reallocate.
113
149
 * @param len The new length.
 
150
 * @param err Set to TRUE on error, FALSE otherwise.
114
151
 */
115
152
S_EXPORTED
116
153
void
117
154
s_ring_buffer_realloc (SRingBuffer * self,
118
 
                       size_t len);
 
155
                       size_t len
 
156
                       sboolean * err);
119
157
 
120
158
/**
121
159
 * Get the current length of the ring buffer.
140
178
 * @param func Pointer to a function that is run on each byte.
141
179
 * @param user_data Data that is provided to the function when doing the
142
180
 *                  iteration.
 
181
 * @param err Set to TRUE on error, FALSE otherwise.
 
182
 *
143
183
 *
144
184
 * @note
145
185
 * This does not use pop or push operations, and is thus the only way to keep
161
201
void
162
202
s_ring_buffer_for_each (SRingBuffer * self,
163
203
                        ForEachFunc func,
164
 
                        spointer user_data);
 
204
                        spointer user_data,
 
205
                        sboolean * err);
165
206
 
166
207
/**
167
208
 * @}