2 * Copyright (C) 2009 Edward Hervey <bilboed@bilboed.com>
3 * Copyright (C) 2015 Tim-Philipp Müller <tim@centricular.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 * SECTION:gstqueuearray
25 * @title: GstQueueArray
26 * @short_description: Array based queue object
28 * #GstQueueArray is an object that provides standard queue functionality
29 * based on an array instead of linked lists. This reduces the overhead
30 * caused by memory management by a large factor.
38 #include "gstqueuearray.h"
49 gboolean struct_array;
50 GDestroyNotify clear_func;
54 * gst_queue_array_new_for_struct: (skip)
55 * @struct_size: Size of each element (e.g. structure) in the array
56 * @initial_size: Initial size of the new queue
58 * Allocates a new #GstQueueArray object for elements (e.g. structures)
59 * of size @struct_size, with an initial queue size of @initial_size.
61 * Returns: a new #GstQueueArray object
66 gst_queue_array_new_for_struct (gsize struct_size, guint initial_size)
70 g_return_val_if_fail (struct_size > 0, NULL);
72 array = g_slice_new (GstQueueArray);
73 array->elt_size = struct_size;
74 array->size = initial_size;
75 array->array = g_malloc0 (struct_size * initial_size);
79 array->struct_array = TRUE;
80 array->clear_func = NULL;
85 * gst_queue_array_new: (skip)
86 * @initial_size: Initial size of the new queue
88 * Allocates a new #GstQueueArray object with an initial
89 * queue size of @initial_size.
91 * Returns: a new #GstQueueArray object
96 gst_queue_array_new (guint initial_size)
100 array = gst_queue_array_new_for_struct (sizeof (gpointer), initial_size);
101 array->struct_array = FALSE;
106 * gst_queue_array_free: (skip)
107 * @array: a #GstQueueArray object
109 * Frees queue @array and all memory associated to it.
114 gst_queue_array_free (GstQueueArray * array)
116 g_return_if_fail (array != NULL);
117 gst_queue_array_clear (array);
118 g_free (array->array);
119 g_slice_free (GstQueueArray, array);
123 * gst_queue_array_set_clear_func: (skip)
124 * @array: a #GstQueueArray object
125 * @clear_func: a function to clear an element of @array
127 * Sets a function to clear an element of @array.
129 * The @clear_func will be called when an element in the array
130 * data segment is removed and when the array is freed and data
131 * segment is deallocated as well. @clear_func will be passed a
132 * pointer to the element to clear, rather than the element itself.
134 * Note that in contrast with other uses of #GDestroyNotify
135 * functions, @clear_func is expected to clear the contents of
136 * the array element it is given, but not free the element itself.
141 gst_queue_array_set_clear_func (GstQueueArray * array,
142 GDestroyNotify clear_func)
144 g_return_if_fail (array != NULL);
145 array->clear_func = clear_func;
149 gst_queue_array_clear_idx (GstQueueArray * array, guint idx)
153 if (!array->clear_func)
156 pos = (idx + array->head) % array->size;
157 if (array->struct_array)
158 array->clear_func (array->array + pos * array->elt_size);
160 array->clear_func (*(gpointer *) (array->array + pos * array->elt_size));
164 * gst_queue_array_clear: (skip)
165 * @array: a #GstQueueArray object
167 * Clears queue @array and frees all memory associated to it.
172 gst_queue_array_clear (GstQueueArray * array)
174 g_return_if_fail (array != NULL);
176 if (array->clear_func != NULL) {
179 for (i = 0; i < array->length; i++) {
180 gst_queue_array_clear_idx (array, i);
190 * gst_queue_array_pop_head_struct: (skip)
191 * @array: a #GstQueueArray object
193 * Returns the head of the queue @array and removes it from the queue.
195 * Returns: pointer to element or struct, or NULL if @array was empty. The
196 * data pointed to by the returned pointer stays valid only as long as
197 * the queue array is not modified further!
202 gst_queue_array_pop_head_struct (GstQueueArray * array)
205 g_return_val_if_fail (array != NULL, NULL);
207 if (G_UNLIKELY (array->length == 0))
210 p_struct = array->array + (array->elt_size * array->head);
213 array->head %= array->size;
220 * gst_queue_array_pop_head: (skip)
221 * @array: a #GstQueueArray object
223 * Returns and head of the queue @array and removes
226 * Returns: The head of the queue
231 gst_queue_array_pop_head (GstQueueArray * array)
234 g_return_val_if_fail (array != NULL, NULL);
237 if (G_UNLIKELY (array->length == 0))
240 ret = *(gpointer *) (array->array + (sizeof (gpointer) * array->head));
242 array->head %= array->size;
248 * gst_queue_array_peek_head_struct: (skip)
249 * @array: a #GstQueueArray object
251 * Returns the head of the queue @array without removing it from the queue.
253 * Returns: pointer to element or struct, or NULL if @array was empty. The
254 * data pointed to by the returned pointer stays valid only as long as
255 * the queue array is not modified further!
260 gst_queue_array_peek_head_struct (GstQueueArray * array)
262 g_return_val_if_fail (array != NULL, NULL);
264 if (G_UNLIKELY (array->length == 0))
267 return array->array + (array->elt_size * array->head);
271 * gst_queue_array_peek_head: (skip)
272 * @array: a #GstQueueArray object
274 * Returns the head of the queue @array and does not
275 * remove it from the queue.
277 * Returns: The head of the queue
282 gst_queue_array_peek_head (GstQueueArray * array)
284 g_return_val_if_fail (array != NULL, NULL);
286 if (G_UNLIKELY (array->length == 0))
289 return *(gpointer *) (array->array + (sizeof (gpointer) * array->head));
293 * gst_queue_array_peek_nth: (skip)
295 * Returns the item at @idx in @array, but does not remove it from the queue.
297 * Returns: The item, or %NULL if @idx was out of bounds
302 gst_queue_array_peek_nth (GstQueueArray * array, guint idx)
304 g_return_val_if_fail (array != NULL, NULL);
305 g_return_val_if_fail (idx < array->length, NULL);
307 idx = (array->head + idx) % array->size;
309 return *(gpointer *) (array->array + (sizeof (gpointer) * idx));
313 * gst_queue_array_peek_nth_struct: (skip)
315 * Returns the item at @idx in @array, but does not remove it from the queue.
317 * Returns: The item, or %NULL if @idx was out of bounds
322 gst_queue_array_peek_nth_struct (GstQueueArray * array, guint idx)
324 g_return_val_if_fail (array != NULL, NULL);
325 g_return_val_if_fail (idx < array->length, NULL);
327 idx = (array->head + idx) % array->size;
329 return array->array + (array->elt_size * idx);
333 gst_queue_array_do_expand (GstQueueArray * array)
335 guint elt_size = array->elt_size;
336 /* newsize is 50% bigger */
337 guint oldsize = array->size;
338 guint newsize = MAX ((3 * oldsize) / 2, oldsize + 1);
341 if (array->tail != 0) {
342 guint8 *array2 = g_malloc0 (elt_size * newsize);
343 guint t1 = array->head;
344 guint t2 = oldsize - array->head;
346 /* [0-----TAIL][HEAD------SIZE]
348 * We want to end up with
349 * [HEAD------------------TAIL][----FREEDATA------NEWSIZE]
351 * 1) move [HEAD-----SIZE] part to beginning of new array
352 * 2) move [0-------TAIL] part new array, after previous part
355 memcpy (array2, array->array + (elt_size * array->head), t2 * elt_size);
356 memcpy (array2 + t2 * elt_size, array->array, t1 * elt_size);
358 g_free (array->array);
359 array->array = array2;
362 /* Fast path, we just need to grow the array */
363 array->array = g_realloc (array->array, elt_size * newsize);
364 memset (array->array + elt_size * oldsize, 0,
365 elt_size * (newsize - oldsize));
367 array->tail = oldsize;
368 array->size = newsize;
372 * gst_queue_array_push_element_tail: (skip)
373 * @array: a #GstQueueArray object
374 * @p_struct: address of element or structure to push to the tail of the queue
376 * Pushes the element at address @p_struct to the tail of the queue @array
377 * (Copies the contents of a structure of the struct_size specified when
378 * creating the queue into the array).
383 gst_queue_array_push_tail_struct (GstQueueArray * array, gpointer p_struct)
387 g_return_if_fail (p_struct != NULL);
388 g_return_if_fail (array != NULL);
389 elt_size = array->elt_size;
391 /* Check if we need to make room */
392 if (G_UNLIKELY (array->length == array->size))
393 gst_queue_array_do_expand (array);
395 memcpy (array->array + elt_size * array->tail, p_struct, elt_size);
397 array->tail %= array->size;
402 * gst_queue_array_push_tail: (skip)
403 * @array: a #GstQueueArray object
404 * @data: object to push
406 * Pushes @data to the tail of the queue @array.
411 gst_queue_array_push_tail (GstQueueArray * array, gpointer data)
413 g_return_if_fail (array != NULL);
415 /* Check if we need to make room */
416 if (G_UNLIKELY (array->length == array->size))
417 gst_queue_array_do_expand (array);
419 *(gpointer *) (array->array + sizeof (gpointer) * array->tail) = data;
421 array->tail %= array->size;
426 * gst_queue_array_peek_tail: (skip)
427 * @array: a #GstQueueArray object
429 * Returns the tail of the queue @array, but does not remove it from the queue.
431 * Returns: The tail of the queue
436 gst_queue_array_peek_tail (GstQueueArray * array)
440 g_return_val_if_fail (array != NULL, NULL);
448 idx = (array->head + (len - 1)) % array->size;
450 return *(gpointer *) (array->array + (sizeof (gpointer) * idx));
454 * gst_queue_array_peek_tail_struct: (skip)
455 * @array: a #GstQueueArray object
457 * Returns the tail of the queue @array, but does not remove it from the queue.
459 * Returns: The tail of the queue
464 gst_queue_array_peek_tail_struct (GstQueueArray * array)
468 g_return_val_if_fail (array != NULL, NULL);
476 idx = (array->head + (len - 1)) % array->size;
478 return array->array + (array->elt_size * idx);
482 * gst_queue_array_pop_tail: (skip)
483 * @array: a #GstQueueArray object
485 * Returns the tail of the queue @array and removes
488 * Returns: The tail of the queue
493 gst_queue_array_pop_tail (GstQueueArray * array)
498 g_return_val_if_fail (array != NULL, NULL);
506 idx = (array->head + (len - 1)) % array->size;
508 ret = *(gpointer *) (array->array + (sizeof (gpointer) * idx));
517 * gst_queue_array_pop_tail_struct: (skip)
518 * @array: a #GstQueueArray object
520 * Returns the tail of the queue @array and removes
523 * Returns: The tail of the queue
528 gst_queue_array_pop_tail_struct (GstQueueArray * array)
533 g_return_val_if_fail (array != NULL, NULL);
541 idx = (array->head + (len - 1)) % array->size;
543 ret = array->array + (array->elt_size * idx);
552 * gst_queue_array_is_empty: (skip)
553 * @array: a #GstQueueArray object
555 * Checks if the queue @array is empty.
557 * Returns: %TRUE if the queue @array is empty
562 gst_queue_array_is_empty (GstQueueArray * array)
564 g_return_val_if_fail (array != NULL, FALSE);
565 return (array->length == 0);
570 * gst_queue_array_drop_struct: (skip)
571 * @array: a #GstQueueArray object
572 * @idx: index to drop
573 * @p_struct: address into which to store the data of the dropped structure, or NULL
575 * Drops the queue element at position @idx from queue @array and copies the
576 * data of the element or structure that was removed into @p_struct if
577 * @p_struct is set (not NULL).
579 * Returns: TRUE on success, or FALSE on error
584 gst_queue_array_drop_struct (GstQueueArray * array, guint idx,
587 int first_item_index, last_item_index;
591 g_return_val_if_fail (array != NULL, FALSE);
592 actual_idx = (array->head + idx) % array->size;
594 g_return_val_if_fail (array->length > 0, FALSE);
595 g_return_val_if_fail (actual_idx < array->size, FALSE);
597 elt_size = array->elt_size;
599 first_item_index = array->head;
601 /* tail points to the first free spot */
602 last_item_index = (array->tail - 1 + array->size) % array->size;
604 if (p_struct != NULL)
605 memcpy (p_struct, array->array + elt_size * actual_idx, elt_size);
607 /* simple case actual_idx == first item */
608 if (actual_idx == first_item_index) {
609 /* clear current head position if needed */
610 gst_queue_array_clear_idx (array, idx);
612 /* move the head plus one */
614 array->head %= array->size;
619 /* simple case idx == last item */
620 if (actual_idx == last_item_index) {
621 /* clear current tail position if needed */
622 gst_queue_array_clear_idx (array, idx);
624 /* move tail minus one, potentially wrapping */
625 array->tail = (array->tail - 1 + array->size) % array->size;
630 /* non-wrapped case */
631 if (first_item_index < last_item_index) {
632 /* clear idx if needed */
633 gst_queue_array_clear_idx (array, idx);
635 g_assert (first_item_index < actual_idx && actual_idx < last_item_index);
636 /* move everything beyond actual_idx one step towards zero in array */
637 memmove (array->array + elt_size * actual_idx,
638 array->array + elt_size * (actual_idx + 1),
639 (last_item_index - actual_idx) * elt_size);
640 /* tail might wrap, ie if tail == 0 (and last_item_index == size) */
641 array->tail = (array->tail - 1 + array->size) % array->size;
646 /* only wrapped cases left */
647 g_assert (first_item_index > last_item_index);
649 if (actual_idx < last_item_index) {
650 /* clear idx if needed */
651 gst_queue_array_clear_idx (array, idx);
653 /* actual_idx is before last_item_index, move data towards zero */
654 memmove (array->array + elt_size * actual_idx,
655 array->array + elt_size * (actual_idx + 1),
656 (last_item_index - actual_idx) * elt_size);
657 /* tail should not wrap in this case! */
658 g_assert (array->tail > 0);
664 if (actual_idx > first_item_index) {
665 /* clear idx if needed */
666 gst_queue_array_clear_idx (array, idx);
668 /* actual_idx is after first_item_index, move data to higher indices */
669 memmove (array->array + elt_size * (first_item_index + 1),
670 array->array + elt_size * first_item_index,
671 (actual_idx - first_item_index) * elt_size);
673 /* head should not wrap in this case! */
674 g_assert (array->head < array->size);
679 g_return_val_if_reached (FALSE);
683 * gst_queue_array_drop_element: (skip)
684 * @array: a #GstQueueArray object
685 * @idx: index to drop
687 * Drops the queue element at position @idx from queue @array.
689 * Returns: the dropped element
694 gst_queue_array_drop_element (GstQueueArray * array, guint idx)
698 if (!gst_queue_array_drop_struct (array, idx, &ptr))
705 * gst_queue_array_find: (skip)
706 * @array: a #GstQueueArray object
707 * @func: (allow-none): comparison function, or %NULL to find @data by value
708 * @data: data for comparison function
710 * Finds an element in the queue @array, either by comparing every element
711 * with @func or by looking up @data if no compare function @func is provided,
712 * and returning the index of the found element.
714 * Returns: Index of the found element or -1 if nothing was found.
719 gst_queue_array_find (GstQueueArray * array, GCompareFunc func, gpointer data)
725 /* For struct arrays we need to implement this differently so that
726 * the user gets a pointer to the element data not the dereferenced
729 g_return_val_if_fail (array != NULL, -1);
730 g_return_val_if_fail (array->struct_array == FALSE, -1);
732 elt_size = array->elt_size;
735 /* Scan from head to tail */
736 for (i = 0; i < array->length; i++) {
737 p_element = array->array + ((i + array->head) % array->size) * elt_size;
738 if (func (*(gpointer *) p_element, data) == 0)
742 for (i = 0; i < array->length; i++) {
743 p_element = array->array + ((i + array->head) % array->size) * elt_size;
744 if (*(gpointer *) p_element == data)
753 * gst_queue_array_get_length: (skip)
754 * @array: a #GstQueueArray object
756 * Returns the length of the queue @array
758 * Returns: the length of the queue @array.
763 gst_queue_array_get_length (GstQueueArray * array)
765 g_return_val_if_fail (array != NULL, 0);
766 return array->length;