libpurple/circularbuffer.c

changeset 43299
9454cc5cd5fb
parent 43298
623929763caf
child 43300
0604c6839974
equal deleted inserted replaced
43298:623929763caf 43299:9454cc5cd5fb
1 /*
2 * Purple - Internet Messaging Library
3 * Copyright (C) Pidgin Developers <devel@pidgin.im>
4 *
5 * Purple is the legal property of its developers, whose names are too numerous
6 * to list here. Please refer to the COPYRIGHT file distributed with this
7 * source distribution.
8 *
9 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this library; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23 #include "circularbuffer.h"
24
25 #define DEFAULT_BUF_SIZE 256
26
27 /******************************************************************************
28 * Structs
29 *****************************************************************************/
30 typedef struct {
31 /* A pointer to the starting address of our chunk of memory. */
32 gchar *buffer;
33
34 /* The incremental amount to increase this buffer by when the buffer is not
35 * big enough to hold incoming data, in bytes. */
36 gsize growsize;
37
38 /* The length of this buffer, in bytes. */
39 gsize buflen;
40
41 /* The number of bytes of this buffer that contain unread data. */
42 gsize bufused;
43
44 /* A pointer to the next byte where new incoming data is buffered to. */
45 gchar *input;
46
47 /* A pointer to the next byte of buffered data that should be read by the
48 * consumer. */
49 gchar *output;
50 } PurpleCircularBufferPrivate;
51
52 /******************************************************************************
53 * Enums
54 *****************************************************************************/
55 enum {
56 PROP_0,
57 PROP_GROW_SIZE,
58 PROP_BUFFER_USED,
59 PROP_INPUT,
60 PROP_OUTPUT,
61 N_PROPERTIES,
62 };
63
64 /******************************************************************************
65 * Globals
66 *****************************************************************************/
67 static GParamSpec *properties[N_PROPERTIES] = {NULL, };
68
69 G_DEFINE_TYPE_WITH_PRIVATE(PurpleCircularBuffer, purple_circular_buffer,
70 G_TYPE_OBJECT);
71
72 /******************************************************************************
73 * Circular Buffer Implementation
74 *****************************************************************************/
75 static void
76 purple_circular_buffer_real_grow(PurpleCircularBuffer *buffer, gsize len) {
77 PurpleCircularBufferPrivate *priv = NULL;
78 gsize in_offset = 0, out_offset = 0;
79 gsize start_buflen;
80 GObject *obj;
81
82 priv = purple_circular_buffer_get_instance_private(buffer);
83
84 start_buflen = priv->buflen;
85
86 while((priv->buflen - priv->bufused) < len)
87 priv->buflen += priv->growsize;
88
89 if(priv->input != NULL) {
90 in_offset = priv->input - priv->buffer;
91 out_offset = priv->output - priv->buffer;
92 }
93
94 priv->buffer = g_realloc(priv->buffer, priv->buflen);
95
96 /* adjust the fill and remove pointer locations */
97 if(priv->input == NULL) {
98 priv->input = priv->output = priv->buffer;
99 } else {
100 priv->input = priv->buffer + in_offset;
101 priv->output = priv->buffer + out_offset;
102 }
103
104 /* If the fill pointer is wrapped to before the remove
105 * pointer, we need to shift the data */
106 if (in_offset < out_offset
107 || (in_offset == out_offset && priv->bufused > 0))
108 {
109 gsize shift_n = MIN(priv->buflen - start_buflen, in_offset);
110 memcpy(priv->buffer + start_buflen, priv->buffer, shift_n);
111
112 /* If we couldn't fit the wrapped read buffer at the end */
113 if (shift_n < in_offset) {
114 memmove(priv->buffer, priv->buffer + shift_n, in_offset - shift_n);
115 priv->input = priv->buffer + (in_offset - shift_n);
116 } else {
117 priv->input = priv->buffer + start_buflen + in_offset;
118 }
119 }
120
121 obj = G_OBJECT(buffer);
122 g_object_freeze_notify(obj);
123 g_object_notify_by_pspec(obj, properties[PROP_INPUT]);
124 g_object_notify_by_pspec(obj, properties[PROP_OUTPUT]);
125 g_object_thaw_notify(obj);
126 }
127
128 static void
129 purple_circular_buffer_real_append(PurpleCircularBuffer *buffer,
130 gconstpointer src, gsize len)
131 {
132 PurpleCircularBufferPrivate *priv = NULL;
133 gsize len_stored;
134 GObject *obj;
135
136 priv = purple_circular_buffer_get_instance_private(buffer);
137
138 /* Grow the buffer, if necessary */
139 if((priv->buflen - priv->bufused) < len)
140 purple_circular_buffer_grow(buffer, len);
141
142 /* If there is not enough room to copy all of src before hitting
143 * the end of the buffer then we will need to do two copies.
144 * One copy from input to the end of the buffer, and the
145 * second copy from the start of the buffer to the end of src. */
146 if(priv->input >= priv->output)
147 len_stored = MIN(len, priv->buflen - (priv->input - priv->buffer));
148 else
149 len_stored = len;
150
151 if(len_stored > 0)
152 memcpy(priv->input, src, len_stored);
153
154 if(len_stored < len) {
155 memcpy(priv->buffer, (char*)src + len_stored, len - len_stored);
156 priv->input = priv->buffer + (len - len_stored);
157 } else {
158 priv->input += len_stored;
159 }
160
161 priv->bufused += len;
162
163 obj = G_OBJECT(buffer);
164 g_object_freeze_notify(obj);
165 g_object_notify_by_pspec(obj, properties[PROP_BUFFER_USED]);
166 g_object_notify_by_pspec(obj, properties[PROP_INPUT]);
167 g_object_thaw_notify(obj);
168 }
169
170 static gsize
171 purple_circular_buffer_real_max_read_size(PurpleCircularBuffer *buffer) {
172 PurpleCircularBufferPrivate *priv = NULL;
173 gsize max_read;
174
175 priv = purple_circular_buffer_get_instance_private(buffer);
176
177 if(priv->bufused == 0)
178 max_read = 0;
179 else if((priv->output - priv->input) >= 0)
180 max_read = priv->buflen - (priv->output - priv->buffer);
181 else
182 max_read = priv->input - priv->output;
183
184 return max_read;
185 }
186
187 static gboolean
188 purple_circular_buffer_real_mark_read(PurpleCircularBuffer *buffer,
189 gsize len)
190 {
191 PurpleCircularBufferPrivate *priv = NULL;
192 GObject *obj;
193
194 g_return_val_if_fail(purple_circular_buffer_get_max_read(buffer) >= len, FALSE);
195
196 priv = purple_circular_buffer_get_instance_private(buffer);
197
198 priv->output += len;
199 priv->bufused -= len;
200
201 /* wrap to the start if we're at the end */
202 if ((gsize)(priv->output - priv->buffer) == priv->buflen)
203 priv->output = priv->buffer;
204
205 obj = G_OBJECT(buffer);
206 g_object_freeze_notify(obj);
207 g_object_notify_by_pspec(obj, properties[PROP_BUFFER_USED]);
208 g_object_notify_by_pspec(obj, properties[PROP_OUTPUT]);
209 g_object_thaw_notify(obj);
210
211 return TRUE;
212 }
213
214 /******************************************************************************
215 * Private API
216 *****************************************************************************/
217 static void
218 purple_circular_buffer_set_grow_size(PurpleCircularBuffer *buffer,
219 gsize grow_size)
220 {
221 PurpleCircularBufferPrivate *priv =
222 purple_circular_buffer_get_instance_private(buffer);
223
224 priv->growsize = (grow_size != 0) ? grow_size : DEFAULT_BUF_SIZE;
225
226 g_object_notify_by_pspec(G_OBJECT(buffer), properties[PROP_GROW_SIZE]);
227 }
228
229 static const gchar *
230 purple_circular_buffer_get_input(PurpleCircularBuffer *buffer) {
231 PurpleCircularBufferPrivate *priv = NULL;
232
233 g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), NULL);
234
235 priv = purple_circular_buffer_get_instance_private(buffer);
236
237 return priv->input;
238 }
239
240 /******************************************************************************
241 * Object Stuff
242 *****************************************************************************/
243 static void
244 purple_circular_buffer_init(G_GNUC_UNUSED PurpleCircularBuffer *buffer)
245 {
246 }
247
248 static void
249 purple_circular_buffer_finalize(GObject *obj) {
250 PurpleCircularBufferPrivate *priv =
251 purple_circular_buffer_get_instance_private(
252 PURPLE_CIRCULAR_BUFFER(obj));
253
254 g_free(priv->buffer);
255
256 G_OBJECT_CLASS(purple_circular_buffer_parent_class)->finalize(obj);
257 }
258
259 static void
260 purple_circular_buffer_get_property(GObject *obj, guint param_id,
261 GValue *value, GParamSpec *pspec)
262 {
263 PurpleCircularBuffer *buffer = PURPLE_CIRCULAR_BUFFER(obj);
264
265 switch(param_id) {
266 case PROP_GROW_SIZE:
267 g_value_set_uint64(value,
268 purple_circular_buffer_get_grow_size(buffer));
269 break;
270 case PROP_BUFFER_USED:
271 g_value_set_uint64(value, purple_circular_buffer_get_used(buffer));
272 break;
273 case PROP_INPUT:
274 g_value_set_pointer(value,
275 (void*) purple_circular_buffer_get_input(buffer));
276 break;
277 case PROP_OUTPUT:
278 g_value_set_pointer(value,
279 (void*) purple_circular_buffer_get_output(buffer));
280 break;
281 default:
282 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
283 break;
284 }
285 }
286
287 static void
288 purple_circular_buffer_set_property(GObject *obj, guint param_id,
289 const GValue *value, GParamSpec *pspec)
290 {
291 PurpleCircularBuffer *buffer = PURPLE_CIRCULAR_BUFFER(obj);
292
293 switch(param_id) {
294 case PROP_GROW_SIZE:
295 purple_circular_buffer_set_grow_size(buffer,
296 g_value_get_uint64(value));
297 break;
298 default:
299 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
300 break;
301 }
302 }
303
304 static void
305 purple_circular_buffer_class_init(PurpleCircularBufferClass *klass) {
306 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
307 PurpleCircularBufferClass *buffer_class = PURPLE_CIRCULAR_BUFFER_CLASS(klass);
308
309 obj_class->finalize = purple_circular_buffer_finalize;
310 obj_class->get_property = purple_circular_buffer_get_property;
311 obj_class->set_property = purple_circular_buffer_set_property;
312
313 buffer_class->grow = purple_circular_buffer_real_grow;
314 buffer_class->append = purple_circular_buffer_real_append;
315 buffer_class->max_read_size = purple_circular_buffer_real_max_read_size;
316 buffer_class->mark_read = purple_circular_buffer_real_mark_read;
317
318 /**
319 * PurpleCircularBuffer:grow-size:
320 *
321 * The grow size of the buffer.
322 *
323 * Since: 3.0
324 */
325 properties[PROP_GROW_SIZE] = g_param_spec_uint64(
326 "grow-size", NULL, NULL,
327 0, G_MAXSIZE, 0,
328 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
329
330 /**
331 * PurpleCircularBuffer:buffer-used:
332 *
333 * How much of the buffer that has been used.
334 *
335 * Since: 3.0
336 */
337 properties[PROP_BUFFER_USED] = g_param_spec_uint64(
338 "buffer-used", NULL, NULL,
339 0, G_MAXSIZE, 0,
340 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
341
342 /**
343 * PurpleCircularBuffer:input:
344 *
345 * The input pointer of the buffer.
346 *
347 * Since: 3.0
348 */
349 properties[PROP_INPUT] = g_param_spec_pointer(
350 "input", NULL, NULL,
351 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
352
353 /**
354 * PurpleCircularBuffer:output:
355 *
356 * The output pointer of the buffer.
357 *
358 * Since: 3.0
359 */
360 properties[PROP_OUTPUT] = g_param_spec_pointer(
361 "output", NULL, NULL,
362 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
363
364 g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
365 }
366
367 /******************************************************************************
368 * API
369 *****************************************************************************/
370 PurpleCircularBuffer *
371 purple_circular_buffer_new(gsize growsize) {
372 return g_object_new(PURPLE_TYPE_CIRCULAR_BUFFER,
373 "grow-size", (guint64)(growsize ? growsize : DEFAULT_BUF_SIZE),
374 NULL);
375 }
376
377 void
378 purple_circular_buffer_grow(PurpleCircularBuffer *buffer, gsize len) {
379 PurpleCircularBufferClass *klass = NULL;
380
381 g_return_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer));
382
383 klass = PURPLE_CIRCULAR_BUFFER_GET_CLASS(buffer);
384 if(klass && klass->grow)
385 klass->grow(buffer, len);
386 }
387
388 void
389 purple_circular_buffer_append(PurpleCircularBuffer *buffer, gconstpointer src,
390 gsize len)
391 {
392 PurpleCircularBufferClass *klass = NULL;
393
394 g_return_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer));
395 g_return_if_fail(src != NULL);
396
397 klass = PURPLE_CIRCULAR_BUFFER_GET_CLASS(buffer);
398 if(klass && klass->append)
399 klass->append(buffer, src, len);
400 }
401
402 gsize
403 purple_circular_buffer_get_max_read(PurpleCircularBuffer *buffer) {
404 PurpleCircularBufferClass *klass = NULL;
405
406 g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), 0);
407
408 klass = PURPLE_CIRCULAR_BUFFER_GET_CLASS(buffer);
409 if(klass && klass->max_read_size)
410 return klass->max_read_size(buffer);
411
412 return 0;
413 }
414
415 gboolean
416 purple_circular_buffer_mark_read(PurpleCircularBuffer *buffer, gsize len) {
417 PurpleCircularBufferClass *klass = NULL;
418
419 g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), FALSE);
420
421 klass = PURPLE_CIRCULAR_BUFFER_GET_CLASS(buffer);
422 if(klass && klass->mark_read)
423 return klass->mark_read(buffer, len);
424
425 return FALSE;
426 }
427
428 gsize
429 purple_circular_buffer_get_grow_size(PurpleCircularBuffer *buffer) {
430
431 PurpleCircularBufferPrivate *priv = NULL;
432
433 g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), 0);
434
435 priv = purple_circular_buffer_get_instance_private(buffer);
436
437 return priv->growsize;
438 }
439
440 gsize
441 purple_circular_buffer_get_used(PurpleCircularBuffer *buffer) {
442 PurpleCircularBufferPrivate *priv = NULL;
443
444 g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), 0);
445
446 priv = purple_circular_buffer_get_instance_private(buffer);
447
448 return priv->bufused;
449 }
450
451 const gchar *
452 purple_circular_buffer_get_output(PurpleCircularBuffer *buffer) {
453 PurpleCircularBufferPrivate *priv = NULL;
454
455 g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), NULL);
456
457 priv = purple_circular_buffer_get_instance_private(buffer);
458
459 return priv->output;
460 }
461
462 void
463 purple_circular_buffer_reset(PurpleCircularBuffer *buffer) {
464 PurpleCircularBufferPrivate *priv = NULL;
465 GObject *obj;
466
467 g_return_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer));
468
469 priv = purple_circular_buffer_get_instance_private(buffer);
470
471 priv->input = priv->buffer;
472 priv->output = priv->buffer;
473
474 obj = G_OBJECT(buffer);
475 g_object_freeze_notify(obj);
476 g_object_notify_by_pspec(obj, properties[PROP_INPUT]);
477 g_object_notify_by_pspec(obj, properties[PROP_OUTPUT]);
478 g_object_thaw_notify(obj);
479 }
480

mercurial