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 gsize elt_size = array->elt_size;
336 /* newsize is 50% bigger */
337 gsize oldsize = array->size;
340 newsize = MAX ((3 * (guint64) oldsize) / 2, (guint64) oldsize + 1);
341 if (newsize > G_MAXUINT)
342 g_error ("growing the queue array would overflow");
345 if (array->tail != 0) {
346 guint8 *array2 = NULL;
350 array2 = g_malloc0_n (newsize, elt_size);
352 t2 = oldsize - array->head;
354 /* [0-----TAIL][HEAD------SIZE]
356 * We want to end up with
357 * [HEAD------------------TAIL][----FREEDATA------NEWSIZE]
359 * 1) move [HEAD-----SIZE] part to beginning of new array
360 * 2) move [0-------TAIL] part new array, after previous part
363 memcpy (array2, array->array + (elt_size * (gsize) array->head),
365 memcpy (array2 + t2 * elt_size, array->array, t1 * elt_size);
367 g_free (array->array);
368 array->array = array2;
371 /* Fast path, we just need to grow the array */
372 array->array = g_realloc_n (array->array, newsize, elt_size);
373 memset (array->array + elt_size * oldsize, 0,
374 elt_size * (newsize - oldsize));
376 array->tail = oldsize;
377 array->size = newsize;
381 * gst_queue_array_push_element_tail: (skip)
382 * @array: a #GstQueueArray object
383 * @p_struct: address of element or structure to push to the tail of the queue
385 * Pushes the element at address @p_struct to the tail of the queue @array
386 * (Copies the contents of a structure of the struct_size specified when
387 * creating the queue into the array).
392 gst_queue_array_push_tail_struct (GstQueueArray * array, gpointer p_struct)
396 g_return_if_fail (p_struct != NULL);
397 g_return_if_fail (array != NULL);
398 elt_size = array->elt_size;
400 /* Check if we need to make room */
401 if (G_UNLIKELY (array->length == array->size))
402 gst_queue_array_do_expand (array);
404 memcpy (array->array + elt_size * array->tail, p_struct, elt_size);
406 array->tail %= array->size;
411 * gst_queue_array_push_tail: (skip)
412 * @array: a #GstQueueArray object
413 * @data: object to push
415 * Pushes @data to the tail of the queue @array.
420 gst_queue_array_push_tail (GstQueueArray * array, gpointer data)
422 g_return_if_fail (array != NULL);
424 /* Check if we need to make room */
425 if (G_UNLIKELY (array->length == array->size))
426 gst_queue_array_do_expand (array);
428 *(gpointer *) (array->array + sizeof (gpointer) * array->tail) = data;
430 array->tail %= array->size;
435 * gst_queue_array_peek_tail: (skip)
436 * @array: a #GstQueueArray object
438 * Returns the tail of the queue @array, but does not remove it from the queue.
440 * Returns: The tail of the queue
445 gst_queue_array_peek_tail (GstQueueArray * array)
449 g_return_val_if_fail (array != NULL, NULL);
457 idx = (array->head + (len - 1)) % array->size;
459 return *(gpointer *) (array->array + (sizeof (gpointer) * idx));
463 * gst_queue_array_peek_tail_struct: (skip)
464 * @array: a #GstQueueArray object
466 * Returns the tail of the queue @array, but does not remove it from the queue.
468 * Returns: The tail of the queue
473 gst_queue_array_peek_tail_struct (GstQueueArray * array)
477 g_return_val_if_fail (array != NULL, NULL);
485 idx = (array->head + (len - 1)) % array->size;
487 return array->array + (array->elt_size * idx);
491 * gst_queue_array_pop_tail: (skip)
492 * @array: a #GstQueueArray object
494 * Returns the tail of the queue @array and removes
497 * Returns: The tail of the queue
502 gst_queue_array_pop_tail (GstQueueArray * array)
507 g_return_val_if_fail (array != NULL, NULL);
515 idx = (array->head + (len - 1)) % array->size;
517 ret = *(gpointer *) (array->array + (sizeof (gpointer) * idx));
526 * gst_queue_array_pop_tail_struct: (skip)
527 * @array: a #GstQueueArray object
529 * Returns the tail of the queue @array and removes
532 * Returns: The tail of the queue
537 gst_queue_array_pop_tail_struct (GstQueueArray * array)
542 g_return_val_if_fail (array != NULL, NULL);
550 idx = (array->head + (len - 1)) % array->size;
552 ret = array->array + (array->elt_size * idx);
561 * gst_queue_array_is_empty: (skip)
562 * @array: a #GstQueueArray object
564 * Checks if the queue @array is empty.
566 * Returns: %TRUE if the queue @array is empty
571 gst_queue_array_is_empty (GstQueueArray * array)
573 g_return_val_if_fail (array != NULL, FALSE);
574 return (array->length == 0);
579 * gst_queue_array_drop_struct: (skip)
580 * @array: a #GstQueueArray object
581 * @idx: index to drop
582 * @p_struct: address into which to store the data of the dropped structure, or NULL
584 * Drops the queue element at position @idx from queue @array and copies the
585 * data of the element or structure that was removed into @p_struct if
586 * @p_struct is set (not NULL).
588 * Returns: TRUE on success, or FALSE on error
593 gst_queue_array_drop_struct (GstQueueArray * array, guint idx,
596 int first_item_index, last_item_index;
600 g_return_val_if_fail (array != NULL, FALSE);
601 actual_idx = (array->head + idx) % array->size;
603 g_return_val_if_fail (array->length > 0, FALSE);
604 g_return_val_if_fail (actual_idx < array->size, FALSE);
606 elt_size = array->elt_size;
608 first_item_index = array->head;
610 /* tail points to the first free spot */
611 last_item_index = (array->tail - 1 + array->size) % array->size;
613 if (p_struct != NULL)
614 memcpy (p_struct, array->array + elt_size * actual_idx, elt_size);
616 /* simple case actual_idx == first item */
617 if (actual_idx == first_item_index) {
618 /* clear current head position if needed */
619 if (p_struct == NULL)
620 gst_queue_array_clear_idx (array, idx);
622 /* move the head plus one */
624 array->head %= array->size;
629 /* simple case idx == last item */
630 if (actual_idx == last_item_index) {
631 /* clear current tail position if needed */
632 if (p_struct == NULL)
633 gst_queue_array_clear_idx (array, idx);
635 /* move tail minus one, potentially wrapping */
636 array->tail = (array->tail - 1 + array->size) % array->size;
641 /* non-wrapped case */
642 if (first_item_index < last_item_index) {
643 /* clear idx if needed */
644 if (p_struct == NULL)
645 gst_queue_array_clear_idx (array, idx);
647 g_assert (first_item_index < actual_idx && actual_idx < last_item_index);
648 /* move everything beyond actual_idx one step towards zero in array */
649 memmove (array->array + elt_size * actual_idx,
650 array->array + elt_size * (actual_idx + 1),
651 (last_item_index - actual_idx) * elt_size);
652 /* tail might wrap, ie if tail == 0 (and last_item_index == size) */
653 array->tail = (array->tail - 1 + array->size) % array->size;
658 /* only wrapped cases left */
659 g_assert (first_item_index > last_item_index);
661 if (actual_idx < last_item_index) {
662 /* clear idx if needed */
663 if (p_struct == NULL)
664 gst_queue_array_clear_idx (array, idx);
666 /* actual_idx is before last_item_index, move data towards zero */
667 memmove (array->array + elt_size * actual_idx,
668 array->array + elt_size * (actual_idx + 1),
669 (last_item_index - actual_idx) * elt_size);
670 /* tail should not wrap in this case! */
671 g_assert (array->tail > 0);
677 if (actual_idx > first_item_index) {
678 /* clear idx if needed */
679 if (p_struct == NULL)
680 gst_queue_array_clear_idx (array, idx);
682 /* actual_idx is after first_item_index, move data to higher indices */
683 memmove (array->array + elt_size * (first_item_index + 1),
684 array->array + elt_size * first_item_index,
685 (actual_idx - first_item_index) * elt_size);
687 /* head should not wrap in this case! */
688 g_assert (array->head < array->size);
693 g_return_val_if_reached (FALSE);
697 * gst_queue_array_drop_element: (skip)
698 * @array: a #GstQueueArray object
699 * @idx: index to drop
701 * Drops the queue element at position @idx from queue @array.
703 * Returns: the dropped element
708 gst_queue_array_drop_element (GstQueueArray * array, guint idx)
712 if (!gst_queue_array_drop_struct (array, idx, &ptr))
719 * gst_queue_array_find: (skip)
720 * @array: a #GstQueueArray object
721 * @func: (allow-none): comparison function, or %NULL to find @data by value
722 * @data: data for comparison function
724 * Finds an element in the queue @array, either by comparing every element
725 * with @func or by looking up @data if no compare function @func is provided,
726 * and returning the index of the found element.
728 * Returns: Index of the found element or -1 if nothing was found.
733 gst_queue_array_find (GstQueueArray * array, GCompareFunc func, gpointer data)
739 /* For struct arrays we need to implement this differently so that
740 * the user gets a pointer to the element data not the dereferenced
743 g_return_val_if_fail (array != NULL, -1);
744 g_return_val_if_fail (array->struct_array == FALSE, -1);
746 elt_size = array->elt_size;
749 /* Scan from head to tail */
750 for (i = 0; i < array->length; i++) {
751 p_element = array->array + ((i + array->head) % array->size) * elt_size;
752 if (func (*(gpointer *) p_element, data) == 0)
756 for (i = 0; i < array->length; i++) {
757 p_element = array->array + ((i + array->head) % array->size) * elt_size;
758 if (*(gpointer *) p_element == data)
767 * gst_queue_array_get_length: (skip)
768 * @array: a #GstQueueArray object
770 * Returns the length of the queue @array
772 * Returns: the length of the queue @array.
777 gst_queue_array_get_length (GstQueueArray * array)
779 g_return_val_if_fail (array != NULL, 0);
780 return array->length;