gstqueuearray.c 15.3 KB
Newer Older
Edward Hervey's avatar
Edward Hervey committed
1 2
/* GStreamer
 * Copyright (C) 2009 Edward Hervey <bilboed@bilboed.com>
3
 * Copyright (C) 2015 Tim-Philipp Müller <tim@centricular.com>
Edward Hervey's avatar
Edward Hervey committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19 20
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Edward Hervey's avatar
Edward Hervey committed
21 22
 */

23 24
/**
 * SECTION:gstqueuearray
25
 * @title: GstQueueArray
26 27 28 29
 * @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
30
 * caused by memory management by a large factor.
31 32 33
 */


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

38
struct _GstQueueArray
Edward Hervey's avatar
Edward Hervey committed
39
{
40
  /* < private > */
41
  guint8 *array;
42 43 44 45
  guint size;
  guint head;
  guint tail;
  guint length;
46 47
  guint elt_size;
  gboolean struct_array;
48 49 50
};

/**
51 52
 * gst_queue_array_new_for_struct: (skip)
 * @struct_size: Size of each element (e.g. structure) in the array
53 54
 * @initial_size: Initial size of the new queue
 *
55 56
 * Allocates a new #GstQueueArray object for elements (e.g. structures)
 * of size @struct_size, with an initial queue size of @initial_size.
57 58 59
 *
 * Returns: a new #GstQueueArray object
 *
60
 * Since: 1.6
61 62
 */
GstQueueArray *
63
gst_queue_array_new_for_struct (gsize struct_size, guint initial_size)
64 65 66
{
  GstQueueArray *array;

67 68
  g_return_val_if_fail (struct_size > 0, NULL);

69
  array = g_slice_new (GstQueueArray);
70
  array->elt_size = struct_size;
Edward Hervey's avatar
Edward Hervey committed
71
  array->size = initial_size;
72
  array->array = g_malloc0 (struct_size * initial_size);
Edward Hervey's avatar
Edward Hervey committed
73 74 75
  array->head = 0;
  array->tail = 0;
  array->length = 0;
76
  array->struct_array = TRUE;
77
  return array;
78 79
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/**
 * gst_queue_array_new: (skip)
 * @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
 *
 * Since: 1.2
 */
GstQueueArray *
gst_queue_array_new (guint initial_size)
{
  GstQueueArray *array;

  array = gst_queue_array_new_for_struct (sizeof (gpointer), initial_size);
  array->struct_array = FALSE;
  return array;
}
100

101
/**
102
 * gst_queue_array_free: (skip)
103 104 105 106
 * @array: a #GstQueueArray object
 *
 * Frees queue @array and all memory associated to it.
 *
107
 * Since: 1.2
108 109 110 111
 */
void
gst_queue_array_free (GstQueueArray * array)
{
112
  g_return_if_fail (array != NULL);
113 114
  g_free (array->array);
  g_slice_free (GstQueueArray, array);
Edward Hervey's avatar
Edward Hervey committed
115 116
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
/**
 * gst_queue_array_pop_head_struct: (skip)
 * @array: a #GstQueueArray object
 *
 * Returns the head of the queue @array and removes it from the queue.
 *
 * Returns: pointer to element or struct, or NULL if @array was empty. The
 *    data pointed to by the returned pointer stays valid only as long as
 *    the queue array is not modified further!
 *
 * Since: 1.6
 */
gpointer
gst_queue_array_pop_head_struct (GstQueueArray * array)
{
  gpointer p_struct;
133
  g_return_val_if_fail (array != NULL, NULL);
134 135 136 137 138 139 140 141 142 143 144 145 146
  /* empty array */
  if (G_UNLIKELY (array->length == 0))
    return NULL;

  p_struct = array->array + (array->elt_size * array->head);

  array->head++;
  array->head %= array->size;
  array->length--;

  return p_struct;
}

147
/**
148
 * gst_queue_array_pop_head: (skip)
149 150 151 152 153 154 155
 * @array: a #GstQueueArray object
 *
 * Returns and head of the queue @array and removes
 * it from the queue.
 *
 * Returns: The head of the queue
 *
156
 * Since: 1.2
157
 */
Edward Hervey's avatar
Edward Hervey committed
158 159 160 161
gpointer
gst_queue_array_pop_head (GstQueueArray * array)
{
  gpointer ret;
162
  g_return_val_if_fail (array != NULL, NULL);
163

Edward Hervey's avatar
Edward Hervey committed
164 165 166
  /* empty array */
  if (G_UNLIKELY (array->length == 0))
    return NULL;
167 168

  ret = *(gpointer *) (array->array + (sizeof (gpointer) * array->head));
Edward Hervey's avatar
Edward Hervey committed
169 170 171 172 173 174
  array->head++;
  array->head %= array->size;
  array->length--;
  return ret;
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/**
 * gst_queue_array_peek_head_struct: (skip)
 * @array: a #GstQueueArray object
 *
 * Returns the head of the queue @array without removing it from the queue.
 *
 * Returns: pointer to element or struct, or NULL if @array was empty. The
 *    data pointed to by the returned pointer stays valid only as long as
 *    the queue array is not modified further!
 *
 * Since: 1.6
 */
gpointer
gst_queue_array_peek_head_struct (GstQueueArray * array)
{
190
  g_return_val_if_fail (array != NULL, NULL);
191 192 193 194 195 196 197
  /* empty array */
  if (G_UNLIKELY (array->length == 0))
    return NULL;

  return array->array + (array->elt_size * array->head);
}

198
/**
199
 * gst_queue_array_peek_head: (skip)
200 201
 * @array: a #GstQueueArray object
 *
202
 * Returns the head of the queue @array and does not
203 204 205 206
 * remove it from the queue.
 *
 * Returns: The head of the queue
 *
207
 * Since: 1.2
208 209 210 211
 */
gpointer
gst_queue_array_peek_head (GstQueueArray * array)
{
212
  g_return_val_if_fail (array != NULL, NULL);
213 214 215
  /* empty array */
  if (G_UNLIKELY (array->length == 0))
    return NULL;
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

  return *(gpointer *) (array->array + (sizeof (gpointer) * array->head));
}

static void
gst_queue_array_do_expand (GstQueueArray * array)
{
  guint elt_size = array->elt_size;
  /* newsize is 50% bigger */
  guint oldsize = array->size;
  guint newsize = MAX ((3 * oldsize) / 2, oldsize + 1);

  /* copy over data */
  if (array->tail != 0) {
    guint8 *array2 = g_malloc0 (elt_size * newsize);
    guint t1 = array->head;
    guint t2 = oldsize - 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 + (elt_size * array->head), t2 * elt_size);
    memcpy (array2 + t2 * elt_size, array->array, t1 * elt_size);

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

/**
 * gst_queue_array_push_element_tail: (skip)
 * @array: a #GstQueueArray object
 * @p_struct: address of element or structure to push to the tail of the queue
 *
 * Pushes the element at address @p_struct to the tail of the queue @array
 * (Copies the contents of a structure of the struct_size specified when
 * creating the queue into the array).
 *
 * Since: 1.6
 */
void
gst_queue_array_push_tail_struct (GstQueueArray * array, gpointer p_struct)
{
  guint elt_size;

  g_return_if_fail (p_struct != NULL);
276
  g_return_if_fail (array != NULL);
277 278 279 280 281 282 283 284 285 286
  elt_size = array->elt_size;

  /* Check if we need to make room */
  if (G_UNLIKELY (array->length == array->size))
    gst_queue_array_do_expand (array);

  memcpy (array->array + elt_size * array->tail, p_struct, elt_size);
  array->tail++;
  array->tail %= array->size;
  array->length++;
287 288 289
}

/**
290
 * gst_queue_array_push_tail: (skip)
291 292 293 294 295
 * @array: a #GstQueueArray object
 * @data: object to push
 *
 * Pushes @data to the tail of the queue @array.
 *
296
 * Since: 1.2
297
 */
Edward Hervey's avatar
Edward Hervey committed
298 299 300
void
gst_queue_array_push_tail (GstQueueArray * array, gpointer data)
{
301
  g_return_if_fail (array != NULL);
302

Edward Hervey's avatar
Edward Hervey committed
303
  /* Check if we need to make room */
304 305
  if (G_UNLIKELY (array->length == array->size))
    gst_queue_array_do_expand (array);
Edward Hervey's avatar
Edward Hervey committed
306

307
  *(gpointer *) (array->array + sizeof (gpointer) * array->tail) = data;
Edward Hervey's avatar
Edward Hervey committed
308 309 310 311 312
  array->tail++;
  array->tail %= array->size;
  array->length++;
}

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
/**
 * gst_queue_array_peek_tail: (skip)
 * @array: a #GstQueueArray object
 *
 * Returns the tail of the queue @array, but does not remove it from the queue.
 *
 * Returns: The tail of the queue
 *
 * Since: 1.14
 */
gpointer
gst_queue_array_peek_tail (GstQueueArray * array)
{
  guint len, idx;

  g_return_val_if_fail (array != NULL, NULL);

  len = array->length;

  /* empty array */
  if (len == 0)
    return NULL;

  idx = (array->head + (len - 1)) % array->size;

  return *(gpointer *) (array->array + (sizeof (gpointer) * idx));
}

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
/**
 * gst_queue_array_peek_tail_struct: (skip)
 * @array: a #GstQueueArray object
 *
 * Returns the tail of the queue @array, but does not remove it from the queue.
 *
 * Returns: The tail of the queue
 *
 * Since: 1.14
 */
gpointer
gst_queue_array_peek_tail_struct (GstQueueArray * array)
{
  guint len, idx;

  g_return_val_if_fail (array != NULL, NULL);

  len = array->length;

  /* empty array */
  if (len == 0)
    return NULL;

  idx = (array->head + (len - 1)) % array->size;

  return array->array + (array->elt_size * idx);
}

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
/**
 * gst_queue_array_pop_tail: (skip)
 * @array: a #GstQueueArray object
 *
 * Returns the tail of the queue @array and removes
 * it from the queue.
 *
 * Returns: The tail of the queue
 *
 * Since: 1.14
 */
gpointer
gst_queue_array_pop_tail (GstQueueArray * array)
{
  gpointer ret;
  guint len, idx;

  g_return_val_if_fail (array != NULL, NULL);

  len = array->length;

  /* empty array */
  if (len == 0)
    return NULL;

  idx = (array->head + (len - 1)) % array->size;

  ret = *(gpointer *) (array->array + (sizeof (gpointer) * idx));

  array->tail = idx;
  array->length--;

  return ret;
}

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
/**
 * gst_queue_array_pop_tail_struct: (skip)
 * @array: a #GstQueueArray object
 *
 * Returns the tail of the queue @array and removes
 * it from the queue.
 *
 * Returns: The tail of the queue
 *
 * Since: 1.14
 */
gpointer
gst_queue_array_pop_tail_struct (GstQueueArray * array)
{
  gpointer ret;
  guint len, idx;

  g_return_val_if_fail (array != NULL, NULL);

  len = array->length;

  /* empty array */
  if (len == 0)
    return NULL;

  idx = (array->head + (len - 1)) % array->size;

  ret = array->array + (array->elt_size * idx);

  array->tail = idx;
  array->length--;

  return ret;
}

439
/**
440
 * gst_queue_array_is_empty: (skip)
441 442 443 444 445 446
 * @array: a #GstQueueArray object
 *
 * Checks if the queue @array is empty.
 *
 * Returns: %TRUE if the queue @array is empty
 *
447
 * Since: 1.2
448
 */
Edward Hervey's avatar
Edward Hervey committed
449 450 451
gboolean
gst_queue_array_is_empty (GstQueueArray * array)
{
452
  g_return_val_if_fail (array != NULL, FALSE);
Edward Hervey's avatar
Edward Hervey committed
453 454 455
  return (array->length == 0);
}

456

457
/**
458
 * gst_queue_array_drop_struct: (skip)
459 460
 * @array: a #GstQueueArray object
 * @idx: index to drop
461
 * @p_struct: address into which to store the data of the dropped structure, or NULL
462
 *
463 464 465
 * Drops the queue element at position @idx from queue @array and copies the
 * data of the element or structure that was removed into @p_struct if
 * @p_struct is set (not NULL).
466
 *
467
 * Returns: TRUE on success, or FALSE on error
468
 *
469
 * Since: 1.6
470
 */
471 472 473
gboolean
gst_queue_array_drop_struct (GstQueueArray * array, guint idx,
    gpointer p_struct)
Edward Hervey's avatar
Edward Hervey committed
474
{
475
  int first_item_index, last_item_index;
476 477
  guint elt_size;

478
  g_return_val_if_fail (array != NULL, FALSE);
479 480
  idx = (array->head + idx) % array->size;

481 482
  g_return_val_if_fail (array->length > 0, FALSE);
  g_return_val_if_fail (idx < array->size, FALSE);
483

484
  elt_size = array->elt_size;
485 486 487 488 489 490

  first_item_index = array->head;

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

491 492
  if (p_struct != NULL)
    memcpy (p_struct, array->array + elt_size * idx, elt_size);
493 494 495 496

  /* simple case idx == first item */
  if (idx == first_item_index) {
    /* move the head plus one */
Edward Hervey's avatar
Edward Hervey committed
497 498
    array->head++;
    array->head %= array->size;
499
    array->length--;
500
    return TRUE;
Edward Hervey's avatar
Edward Hervey committed
501
  }
502 503 504 505

  /* simple case idx == last item */
  if (idx == last_item_index) {
    /* move tail minus one, potentially wrapping */
Edward Hervey's avatar
Edward Hervey committed
506
    array->tail = (array->tail - 1 + array->size) % array->size;
507
    array->length--;
508
    return TRUE;
Edward Hervey's avatar
Edward Hervey committed
509
  }
510 511 512 513 514

  /* 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 */
515 516 517
    memmove (array->array + elt_size * idx,
        array->array + elt_size * (idx + 1),
        (last_item_index - idx) * elt_size);
518 519 520
    /* tail might wrap, ie if tail == 0 (and last_item_index == size) */
    array->tail = (array->tail - 1 + array->size) % array->size;
    array->length--;
521
    return TRUE;
522 523 524 525 526 527 528
  }

  /* 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 */
529 530 531
    memmove (array->array + elt_size * idx,
        array->array + elt_size * (idx + 1),
        (last_item_index - idx) * elt_size);
532 533
    /* tail should not wrap in this case! */
    g_assert (array->tail > 0);
Edward Hervey's avatar
Edward Hervey committed
534
    array->tail--;
535
    array->length--;
536
    return TRUE;
Edward Hervey's avatar
Edward Hervey committed
537
  }
538 539 540

  if (idx > first_item_index) {
    /* idx is after first_item_index, move data to higher indices */
541 542 543
    memmove (array->array + elt_size * (first_item_index + 1),
        array->array + elt_size * first_item_index,
        (idx - first_item_index) * elt_size);
544 545 546 547
    array->head++;
    /* head should not wrap in this case! */
    g_assert (array->head < array->size);
    array->length--;
548
    return TRUE;
549 550
  }

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
  g_return_val_if_reached (FALSE);
}

/**
 * gst_queue_array_drop_element: (skip)
 * @array: a #GstQueueArray object
 * @idx: index to drop
 *
 * Drops the queue element at position @idx from queue @array.
 *
 * Returns: the dropped element
 *
 * Since: 1.2
 */
gpointer
gst_queue_array_drop_element (GstQueueArray * array, guint idx)
{
  gpointer ptr;

  if (!gst_queue_array_drop_struct (array, idx, &ptr))
    return NULL;

  return ptr;
Edward Hervey's avatar
Edward Hervey committed
574 575
}

576
/**
577
 * gst_queue_array_find: (skip)
578
 * @array: a #GstQueueArray object
579
 * @func: (allow-none): comparison function, or %NULL to find @data by value
580 581
 * @data: data for comparison function
 *
582 583 584 585
 * 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.
 *
586 587
 * Returns: Index of the found element or -1 if nothing was found.
 *
588
 * Since: 1.2
589
 */
Edward Hervey's avatar
Edward Hervey committed
590 591 592
guint
gst_queue_array_find (GstQueueArray * array, GCompareFunc func, gpointer data)
{
593 594
  gpointer p_element;
  guint elt_size;
Edward Hervey's avatar
Edward Hervey committed
595 596
  guint i;

597 598 599
  /* For struct arrays we need to implement this differently so that
   * the user gets a pointer to the element data not the dereferenced
   * pointer itself */
600 601

  g_return_val_if_fail (array != NULL, -1);
602 603 604 605
  g_return_val_if_fail (array->struct_array == FALSE, -1);

  elt_size = array->elt_size;

606 607 608
  if (func != NULL) {
    /* Scan from head to tail */
    for (i = 0; i < array->length; i++) {
609 610
      p_element = array->array + ((i + array->head) % array->size) * elt_size;
      if (func (*(gpointer *) p_element, data) == 0)
611
        return i;
612 613 614
    }
  } else {
    for (i = 0; i < array->length; i++) {
615 616
      p_element = array->array + ((i + array->head) % array->size) * elt_size;
      if (*(gpointer *) p_element == data)
617
        return i;
618 619 620
    }
  }

Edward Hervey's avatar
Edward Hervey committed
621 622
  return -1;
}
623 624

/**
625
 * gst_queue_array_get_length: (skip)
626 627 628 629 630 631
 * @array: a #GstQueueArray object
 *
 * Returns the length of the queue @array
 *
 * Returns: the length of the queue @array.
 *
632
 * Since: 1.2
633 634 635 636
 */
guint
gst_queue_array_get_length (GstQueueArray * array)
{
637
  g_return_val_if_fail (array != NULL, 0);
638 639
  return array->length;
}