gstqueuearray.c 8.43 KB
Newer Older
Edward Hervey's avatar
Edward Hervey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* GStreamer
 * Copyright (C) 2009 Edward Hervey <bilboed@bilboed.com>
 *
 * gstqueuearray.c:
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
18 19
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Edward Hervey's avatar
Edward Hervey committed
20 21
 */

22 23 24 25 26 27 28 29 30 31
/**
 * SECTION:gstqueuearray
 * @short_description: Array based queue object
 *
 * #GstQueueArray is an object that provides standard queue functionality
 * based on an array instead of linked lists. This reduces the overhead
 * caused by memory managment by a large factor.
 */


Edward Hervey's avatar
Edward Hervey committed
32 33 34 35
#include <string.h>
#include <gst/gst.h>
#include "gstqueuearray.h"

36
struct _GstQueueArray
Edward Hervey's avatar
Edward Hervey committed
37
{
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  /* < private > */
  gpointer *array;
  guint size;
  guint head;
  guint tail;
  guint length;
};

/**
 * gst_queue_array_new:
 * @initial_size: Initial size of the new queue
 *
 * Allocates a new #GstQueueArray object with an initial
 * queue size of @initial_size.
 *
 * Returns: a new #GstQueueArray object
 *
55
 * Since: 1.2
56 57 58 59 60 61 62
 */
GstQueueArray *
gst_queue_array_new (guint initial_size)
{
  GstQueueArray *array;

  array = g_slice_new (GstQueueArray);
Edward Hervey's avatar
Edward Hervey committed
63 64 65 66 67
  array->size = initial_size;
  array->array = g_new0 (gpointer, initial_size);
  array->head = 0;
  array->tail = 0;
  array->length = 0;
68
  return array;
69 70 71
}


72 73 74 75 76 77
/**
 * gst_queue_array_free:
 * @array: a #GstQueueArray object
 *
 * Frees queue @array and all memory associated to it.
 *
78
 * Since: 1.2
79 80 81 82 83 84
 */
void
gst_queue_array_free (GstQueueArray * array)
{
  g_free (array->array);
  g_slice_free (GstQueueArray, array);
Edward Hervey's avatar
Edward Hervey committed
85 86
}

87 88 89 90 91 92 93 94 95
/**
 * gst_queue_array_pop_head:
 * @array: a #GstQueueArray object
 *
 * Returns and head of the queue @array and removes
 * it from the queue.
 *
 * Returns: The head of the queue
 *
96
 * Since: 1.2
97
 */
Edward Hervey's avatar
Edward Hervey committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
gpointer
gst_queue_array_pop_head (GstQueueArray * array)
{
  gpointer ret;

  /* empty array */
  if (G_UNLIKELY (array->length == 0))
    return NULL;
  ret = array->array[array->head];
  array->head++;
  array->head %= array->size;
  array->length--;
  return ret;
}

113 114 115 116 117 118 119 120 121
/**
 * gst_queue_array_pop_head:
 * @array: a #GstQueueArray object
 *
 * Returns and head of the queue @array and does not
 * remove it from the queue.
 *
 * Returns: The head of the queue
 *
122
 * Since: 1.2
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
 */
gpointer
gst_queue_array_peek_head (GstQueueArray * array)
{
  /* empty array */
  if (G_UNLIKELY (array->length == 0))
    return NULL;
  return array->array[array->head];
}

/**
 * gst_queue_array_push_tail:
 * @array: a #GstQueueArray object
 * @data: object to push
 *
 * Pushes @data to the tail of the queue @array.
 *
140
 * Since: 1.2
141
 */
Edward Hervey's avatar
Edward Hervey committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
void
gst_queue_array_push_tail (GstQueueArray * array, gpointer data)
{
  /* Check if we need to make room */
  if (G_UNLIKELY (array->length == array->size)) {
    /* newsize is 50% bigger */
    guint newsize = (3 * array->size) / 2;

    /* copy over data */
    if (array->tail != 0) {
      gpointer *array2 = g_new0 (gpointer, newsize);
      guint t1 = array->head;
      guint t2 = array->size - array->head;

      /* [0-----TAIL][HEAD------SIZE]
       *
       * We want to end up with
       * [HEAD------------------TAIL][----FREEDATA------NEWSIZE]
       *
       * 1) move [HEAD-----SIZE] part to beginning of new array
       * 2) move [0-------TAIL] part new array, after previous part
       */

      memcpy (array2, &array->array[array->head], t2 * sizeof (gpointer));
      memcpy (&array2[t2], array->array, t1 * sizeof (gpointer));

      g_free (array->array);
      array->array = array2;
      array->head = 0;
    } else {
      /* Fast path, we just need to grow the array */
      array->array = g_renew (gpointer, array->array, newsize);
    }
    array->tail = array->size;
    array->size = newsize;
  }

  array->array[array->tail] = data;
  array->tail++;
  array->tail %= array->size;
  array->length++;
}

185 186 187 188 189 190 191 192
/**
 * gst_queue_array_is_empty:
 * @array: a #GstQueueArray object
 *
 * Checks if the queue @array is empty.
 *
 * Returns: %TRUE if the queue @array is empty
 *
193
 * Since: 1.2
194
 */
Edward Hervey's avatar
Edward Hervey committed
195 196 197 198 199 200
gboolean
gst_queue_array_is_empty (GstQueueArray * array)
{
  return (array->length == 0);
}

201 202 203 204 205 206 207 208 209
/**
 * gst_queue_array_drop_element:
 * @array: a #GstQueueArray object
 * @idx: index to drop
 *
 * Drops the queue element at position @idx from queue @array.
 *
 * Returns: the dropped element
 *
210
 * Since: 1.2
211 212
 */
gpointer
Edward Hervey's avatar
Edward Hervey committed
213 214
gst_queue_array_drop_element (GstQueueArray * array, guint idx)
{
215
  int first_item_index, last_item_index;
216 217
  gpointer element;

218 219 220 221 222 223 224 225 226 227 228 229 230
  g_return_val_if_fail (array->length > 0, NULL);
  g_return_val_if_fail (idx < array->size, NULL);

  first_item_index = array->head;

  /* tail points to the first free spot */
  last_item_index = (array->tail - 1 + array->size) % array->size;

  element = array->array[idx];

  /* simple case idx == first item */
  if (idx == first_item_index) {
    /* move the head plus one */
Edward Hervey's avatar
Edward Hervey committed
231 232
    array->head++;
    array->head %= array->size;
233
    array->length--;
234
    return element;
Edward Hervey's avatar
Edward Hervey committed
235
  }
236 237 238 239

  /* simple case idx == last item */
  if (idx == last_item_index) {
    /* move tail minus one, potentially wrapping */
Edward Hervey's avatar
Edward Hervey committed
240
    array->tail = (array->tail - 1 + array->size) % array->size;
241
    array->length--;
242
    return element;
Edward Hervey's avatar
Edward Hervey committed
243
  }
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

  /* non-wrapped case */
  if (first_item_index < last_item_index) {
    g_assert (first_item_index < idx && idx < last_item_index);
    /* move everything beyond idx one step towards zero in array */
    memmove (&array->array[idx],
        &array->array[idx + 1], (last_item_index - idx) * sizeof (gpointer));
    /* tail might wrap, ie if tail == 0 (and last_item_index == size) */
    array->tail = (array->tail - 1 + array->size) % array->size;
    array->length--;
    return element;
  }

  /* only wrapped cases left */
  g_assert (first_item_index > last_item_index);

  if (idx < last_item_index) {
    /* idx is before last_item_index, move data towards zero */
    memmove (&array->array[idx],
        &array->array[idx + 1], (last_item_index - idx) * sizeof (gpointer));
    /* tail should not wrap in this case! */
    g_assert (array->tail > 0);
Edward Hervey's avatar
Edward Hervey committed
266
    array->tail--;
267 268
    array->length--;
    return element;
Edward Hervey's avatar
Edward Hervey committed
269
  }
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

  if (idx > first_item_index) {
    element = array->array[idx];
    /* idx is after first_item_index, move data to higher indices */
    memmove (&array->array[first_item_index + 1],
        &array->array[first_item_index],
        (idx - first_item_index) * sizeof (gpointer));
    array->head++;
    /* head should not wrap in this case! */
    g_assert (array->head < array->size);
    array->length--;
    return element;
  }

  g_return_val_if_reached (NULL);
Edward Hervey's avatar
Edward Hervey committed
285 286
}

287 288 289
/**
 * gst_queue_array_find:
 * @array: a #GstQueueArray object
290
 * @func: (allow-none): comparison function, or %NULL to find @data by value
291 292
 * @data: data for comparison function
 *
293 294 295 296 297 298 299
 * Finds an element in the queue @array, either by comparing every element
 * with @func or by looking up @data if no compare function @func is provided,
 * and returning the index of the found element.
 *
 * Note that the index is not 0-based, but an internal index number with a
 * random offset. The index can be used in connection with
 * gst_queue_array_drop_element(). FIXME: return index 0-based and make
300
 * gst_queue_array_drop_element() take a 0-based index.
301 302 303
 *
 * Returns: Index of the found element or -1 if nothing was found.
 *
304
 * Since: 1.2
305
 */
Edward Hervey's avatar
Edward Hervey committed
306 307 308 309 310
guint
gst_queue_array_find (GstQueueArray * array, GCompareFunc func, gpointer data)
{
  guint i;

311 312 313 314 315 316 317 318 319 320 321 322 323
  if (func != NULL) {
    /* Scan from head to tail */
    for (i = 0; i < array->length; i++) {
      if (func (array->array[(i + array->head) % array->size], data) == 0)
        return (i + array->head) % array->size;
    }
  } else {
    for (i = 0; i < array->length; i++) {
      if (array->array[(i + array->head) % array->size] == data)
        return (i + array->head) % array->size;
    }
  }

Edward Hervey's avatar
Edward Hervey committed
324 325
  return -1;
}
326 327 328 329 330 331 332 333 334

/**
 * gst_queue_array_get_length:
 * @array: a #GstQueueArray object
 *
 * Returns the length of the queue @array
 *
 * Returns: the length of the queue @array.
 *
335
 * Since: 1.2
336 337 338 339 340 341
 */
guint
gst_queue_array_get_length (GstQueueArray * array)
{
  return array->length;
}