Blame glib/garray.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* 
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
Packit ae235b
#include "garray.h"
Packit ae235b
Packit ae235b
#include "gbytes.h"
Packit ae235b
#include "ghash.h"
Packit ae235b
#include "gslice.h"
Packit ae235b
#include "gmem.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
#include "gthread.h"
Packit ae235b
#include "gmessages.h"
Packit ae235b
#include "gqsort.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:arrays
Packit ae235b
 * @title: Arrays
Packit ae235b
 * @short_description: arrays of arbitrary elements which grow
Packit ae235b
 *     automatically as elements are added
Packit ae235b
 *
Packit ae235b
 * Arrays are similar to standard C arrays, except that they grow
Packit ae235b
 * automatically as elements are added.
Packit ae235b
 *
Packit ae235b
 * Array elements can be of any size (though all elements of one array
Packit ae235b
 * are the same size), and the array can be automatically cleared to
Packit ae235b
 * '0's and zero-terminated.
Packit ae235b
 *
Packit ae235b
 * To create a new array use g_array_new().
Packit ae235b
 *
Packit ae235b
 * To add elements to an array, use g_array_append_val(),
Packit ae235b
 * g_array_append_vals(), g_array_prepend_val(), and
Packit ae235b
 * g_array_prepend_vals().
Packit ae235b
 *
Packit ae235b
 * To access an element of an array, use g_array_index().
Packit ae235b
 *
Packit ae235b
 * To set the size of an array, use g_array_set_size().
Packit ae235b
 *
Packit ae235b
 * To free an array, use g_array_free().
Packit ae235b
 *
Packit ae235b
 * Here is an example that stores integers in a #GArray:
Packit ae235b
 * |[
Packit ae235b
 *   GArray *garray;
Packit ae235b
 *   gint i;
Packit ae235b
 *   // We create a new array to store gint values.
Packit ae235b
 *   // We don't want it zero-terminated or cleared to 0's.
Packit ae235b
 *   garray = g_array_new (FALSE, FALSE, sizeof (gint));
Packit ae235b
 *   for (i = 0; i < 10000; i++)
Packit ae235b
 *     g_array_append_val (garray, i);
Packit ae235b
 *   for (i = 0; i < 10000; i++)
Packit ae235b
 *     if (g_array_index (garray, gint, i) != i)
Packit ae235b
 *       g_print ("ERROR: got %d instead of %d\n",
Packit ae235b
 *                g_array_index (garray, gint, i), i);
Packit ae235b
 *   g_array_free (garray, TRUE);
Packit ae235b
 * ]|
Packit ae235b
 */
Packit ae235b
Packit ae235b
#define MIN_ARRAY_SIZE  16
Packit ae235b
Packit ae235b
typedef struct _GRealArray  GRealArray;
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GArray:
Packit ae235b
 * @data: a pointer to the element data. The data may be moved as
Packit ae235b
 *     elements are added to the #GArray.
Packit ae235b
 * @len: the number of elements in the #GArray not including the
Packit ae235b
 *     possible terminating zero element.
Packit ae235b
 *
Packit ae235b
 * Contains the public fields of a GArray.
Packit ae235b
 */
Packit ae235b
struct _GRealArray
Packit ae235b
{
Packit ae235b
  guint8 *data;
Packit ae235b
  guint   len;
Packit ae235b
  guint   alloc;
Packit ae235b
  guint   elt_size;
Packit ae235b
  guint   zero_terminated : 1;
Packit ae235b
  guint   clear : 1;
Packit ae235b
  gint    ref_count;
Packit ae235b
  GDestroyNotify clear_func;
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_index:
Packit ae235b
 * @a: a #GArray
Packit ae235b
 * @t: the type of the elements
Packit ae235b
 * @i: the index of the element to return
Packit ae235b
 *
Packit ae235b
 * Returns the element of a #GArray at the given index. The return
Packit ae235b
 * value is cast to the given type.
Packit ae235b
 *
Packit ae235b
 * This example gets a pointer to an element in a #GArray:
Packit ae235b
 * |[
Packit ae235b
 *   EDayViewEvent *event;
Packit ae235b
 *   // This gets a pointer to the 4th element in the array of
Packit ae235b
 *   // EDayViewEvent structs.
Packit ae235b
 *   event = &g_array_index (events, EDayViewEvent, 3);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Returns: the element of the #GArray at the index given by @i
Packit ae235b
 */
Packit ae235b
Packit ae235b
#define g_array_elt_len(array,i) ((array)->elt_size * (i))
Packit ae235b
#define g_array_elt_pos(array,i) ((array)->data + g_array_elt_len((array),(i)))
Packit ae235b
#define g_array_elt_zero(array, pos, len)                               \
Packit ae235b
  (memset (g_array_elt_pos ((array), pos), 0,  g_array_elt_len ((array), len)))
Packit ae235b
#define g_array_zero_terminate(array) G_STMT_START{                     \
Packit ae235b
  if ((array)->zero_terminated)                                         \
Packit ae235b
    g_array_elt_zero ((array), (array)->len, 1);                        \
Packit ae235b
}G_STMT_END
Packit ae235b
Packit ae235b
static guint g_nearest_pow        (gint        num) G_GNUC_CONST;
Packit ae235b
static void  g_array_maybe_expand (GRealArray *array,
Packit ae235b
                                   gint        len);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_new:
Packit ae235b
 * @zero_terminated: %TRUE if the array should have an extra element at
Packit ae235b
 *     the end which is set to 0
Packit ae235b
 * @clear_: %TRUE if #GArray elements should be automatically cleared
Packit ae235b
 *     to 0 when they are allocated
Packit ae235b
 * @element_size: the size of each element in bytes
Packit ae235b
 *
Packit ae235b
 * Creates a new #GArray with a reference count of 1.
Packit ae235b
 *
Packit ae235b
 * Returns: the new #GArray
Packit ae235b
 */
Packit ae235b
GArray*
Packit ae235b
g_array_new (gboolean zero_terminated,
Packit ae235b
             gboolean clear,
Packit ae235b
             guint    elt_size)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (elt_size > 0, NULL);
Packit ae235b
Packit ae235b
  return g_array_sized_new (zero_terminated, clear, elt_size, 0);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_sized_new:
Packit ae235b
 * @zero_terminated: %TRUE if the array should have an extra element at
Packit ae235b
 *     the end with all bits cleared
Packit ae235b
 * @clear_: %TRUE if all bits in the array should be cleared to 0 on
Packit ae235b
 *     allocation
Packit ae235b
 * @element_size: size of each element in the array
Packit ae235b
 * @reserved_size: number of elements preallocated
Packit ae235b
 *
Packit ae235b
 * Creates a new #GArray with @reserved_size elements preallocated and
Packit ae235b
 * a reference count of 1. This avoids frequent reallocation, if you
Packit ae235b
 * are going to add many elements to the array. Note however that the
Packit ae235b
 * size of the array is still 0.
Packit ae235b
 *
Packit ae235b
 * Returns: the new #GArray
Packit ae235b
 */
Packit ae235b
GArray*
Packit ae235b
g_array_sized_new (gboolean zero_terminated,
Packit ae235b
                   gboolean clear,
Packit ae235b
                   guint    elt_size,
Packit ae235b
                   guint    reserved_size)
Packit ae235b
{
Packit ae235b
  GRealArray *array;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (elt_size > 0, NULL);
Packit ae235b
Packit ae235b
  array = g_slice_new (GRealArray);
Packit ae235b
Packit ae235b
  array->data            = NULL;
Packit ae235b
  array->len             = 0;
Packit ae235b
  array->alloc           = 0;
Packit ae235b
  array->zero_terminated = (zero_terminated ? 1 : 0);
Packit ae235b
  array->clear           = (clear ? 1 : 0);
Packit ae235b
  array->elt_size        = elt_size;
Packit ae235b
  array->ref_count       = 1;
Packit ae235b
  array->clear_func      = NULL;
Packit ae235b
Packit ae235b
  if (array->zero_terminated || reserved_size != 0)
Packit ae235b
    {
Packit ae235b
      g_array_maybe_expand (array, reserved_size);
Packit ae235b
      g_array_zero_terminate(array);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return (GArray*) array;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_set_clear_func:
Packit ae235b
 * @array: A #GArray
Packit ae235b
 * @clear_func: a function to clear an element of @array
Packit ae235b
 *
Packit ae235b
 * Sets a function to clear an element of @array.
Packit ae235b
 *
Packit ae235b
 * The @clear_func will be called when an element in the array
Packit ae235b
 * data segment is removed and when the array is freed and data
Packit ae235b
 * segment is deallocated as well. @clear_func will be passed a
Packit ae235b
 * pointer to the element to clear, rather than the element itself.
Packit ae235b
 *
Packit ae235b
 * Note that in contrast with other uses of #GDestroyNotify
Packit ae235b
 * functions, @clear_func is expected to clear the contents of
Packit ae235b
 * the array element it is given, but not free the element itself.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_array_set_clear_func (GArray         *array,
Packit ae235b
                        GDestroyNotify  clear_func)
Packit ae235b
{
Packit ae235b
  GRealArray *rarray = (GRealArray *) array;
Packit ae235b
Packit ae235b
  g_return_if_fail (array != NULL);
Packit ae235b
Packit ae235b
  rarray->clear_func = clear_func;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_ref:
Packit ae235b
 * @array: A #GArray
Packit ae235b
 *
Packit ae235b
 * Atomically increments the reference count of @array by one.
Packit ae235b
 * This function is thread-safe and may be called from any thread.
Packit ae235b
 *
Packit ae235b
 * Returns: The passed in #GArray
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GArray *
Packit ae235b
g_array_ref (GArray *array)
Packit ae235b
{
Packit ae235b
  GRealArray *rarray = (GRealArray*) array;
Packit ae235b
  g_return_val_if_fail (array, NULL);
Packit ae235b
Packit ae235b
  g_atomic_int_inc (&rarray->ref_count);
Packit ae235b
Packit ae235b
  return array;
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef enum
Packit ae235b
{
Packit ae235b
  FREE_SEGMENT = 1 << 0,
Packit ae235b
  PRESERVE_WRAPPER = 1 << 1
Packit ae235b
} ArrayFreeFlags;
Packit ae235b
Packit ae235b
static gchar *array_free (GRealArray *, ArrayFreeFlags);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_unref:
Packit ae235b
 * @array: A #GArray
Packit ae235b
 *
Packit ae235b
 * Atomically decrements the reference count of @array by one. If the
Packit ae235b
 * reference count drops to 0, all memory allocated by the array is
Packit ae235b
 * released. This function is thread-safe and may be called from any
Packit ae235b
 * thread.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_array_unref (GArray *array)
Packit ae235b
{
Packit ae235b
  GRealArray *rarray = (GRealArray*) array;
Packit ae235b
  g_return_if_fail (array);
Packit ae235b
Packit ae235b
  if (g_atomic_int_dec_and_test (&rarray->ref_count))
Packit ae235b
    array_free (rarray, FREE_SEGMENT);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_get_element_size:
Packit ae235b
 * @array: A #GArray
Packit ae235b
 *
Packit ae235b
 * Gets the size of the elements in @array.
Packit ae235b
 *
Packit ae235b
 * Returns: Size of each element, in bytes
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_array_get_element_size (GArray *array)
Packit ae235b
{
Packit ae235b
  GRealArray *rarray = (GRealArray*) array;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (array, 0);
Packit ae235b
Packit ae235b
  return rarray->elt_size;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_free:
Packit ae235b
 * @array: a #GArray
Packit ae235b
 * @free_segment: if %TRUE the actual element data is freed as well
Packit ae235b
 *
Packit ae235b
 * Frees the memory allocated for the #GArray. If @free_segment is
Packit ae235b
 * %TRUE it frees the memory block holding the elements as well and
Packit ae235b
 * also each element if @array has a @element_free_func set. Pass
Packit ae235b
 * %FALSE if you want to free the #GArray wrapper but preserve the
Packit ae235b
 * underlying array for use elsewhere. If the reference count of @array
Packit ae235b
 * is greater than one, the #GArray wrapper is preserved but the size
Packit ae235b
 * of @array will be set to zero.
Packit ae235b
 *
Packit ae235b
 * If array elements contain dynamically-allocated memory, they should
Packit ae235b
 * be freed separately.
Packit ae235b
 *
Packit ae235b
 * This function is not thread-safe. If using a #GArray from multiple
Packit ae235b
 * threads, use only the atomic g_array_ref() and g_array_unref()
Packit ae235b
 * functions.
Packit ae235b
 *
Packit ae235b
 * Returns: the element data if @free_segment is %FALSE, otherwise
Packit ae235b
 *     %NULL. The element data should be freed using g_free().
Packit ae235b
 */
Packit ae235b
gchar*
Packit ae235b
g_array_free (GArray   *farray,
Packit ae235b
              gboolean  free_segment)
Packit ae235b
{
Packit ae235b
  GRealArray *array = (GRealArray*) farray;
Packit ae235b
  ArrayFreeFlags flags;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (array, NULL);
Packit ae235b
Packit ae235b
  flags = (free_segment ? FREE_SEGMENT : 0);
Packit ae235b
Packit ae235b
  /* if others are holding a reference, preserve the wrapper but do free/return the data */
Packit ae235b
  if (!g_atomic_int_dec_and_test (&array->ref_count))
Packit ae235b
    flags |= PRESERVE_WRAPPER;
Packit ae235b
Packit ae235b
  return array_free (array, flags);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
array_free (GRealArray     *array,
Packit ae235b
            ArrayFreeFlags  flags)
Packit ae235b
{
Packit ae235b
  gchar *segment;
Packit ae235b
Packit ae235b
  if (flags & FREE_SEGMENT)
Packit ae235b
    {
Packit ae235b
      if (array->clear_func != NULL)
Packit ae235b
        {
Packit ae235b
          guint i;
Packit ae235b
Packit ae235b
          for (i = 0; i < array->len; i++)
Packit ae235b
            array->clear_func (g_array_elt_pos (array, i));
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_free (array->data);
Packit ae235b
      segment = NULL;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    segment = (gchar*) array->data;
Packit ae235b
Packit ae235b
  if (flags & PRESERVE_WRAPPER)
Packit ae235b
    {
Packit ae235b
      array->data            = NULL;
Packit ae235b
      array->len             = 0;
Packit ae235b
      array->alloc           = 0;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_slice_free1 (sizeof (GRealArray), array);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return segment;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_append_vals:
Packit ae235b
 * @array: a #GArray
Packit ae235b
 * @data: (not nullable): a pointer to the elements to append to the end of the array
Packit ae235b
 * @len: the number of elements to append
Packit ae235b
 *
Packit ae235b
 * Adds @len elements onto the end of the array.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GArray
Packit ae235b
 */
Packit ae235b
/**
Packit ae235b
 * g_array_append_val:
Packit ae235b
 * @a: a #GArray
Packit ae235b
 * @v: the value to append to the #GArray
Packit ae235b
 *
Packit ae235b
 * Adds the value on to the end of the array. The array will grow in
Packit ae235b
 * size automatically if necessary.
Packit ae235b
 *
Packit ae235b
 * g_array_append_val() is a macro which uses a reference to the value
Packit ae235b
 * parameter @v. This means that you cannot use it with literal values
Packit ae235b
 * such as "27". You must use variables.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GArray
Packit ae235b
 */
Packit ae235b
GArray*
Packit ae235b
g_array_append_vals (GArray       *farray,
Packit ae235b
                     gconstpointer data,
Packit ae235b
                     guint         len)
Packit ae235b
{
Packit ae235b
  GRealArray *array = (GRealArray*) farray;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (array, NULL);
Packit ae235b
Packit ae235b
  if (len == 0)
Packit ae235b
    return farray;
Packit ae235b
Packit ae235b
  g_array_maybe_expand (array, len);
Packit ae235b
Packit ae235b
  memcpy (g_array_elt_pos (array, array->len), data, 
Packit ae235b
          g_array_elt_len (array, len));
Packit ae235b
Packit ae235b
  array->len += len;
Packit ae235b
Packit ae235b
  g_array_zero_terminate (array);
Packit ae235b
Packit ae235b
  return farray;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_prepend_vals:
Packit ae235b
 * @array: a #GArray
Packit ae235b
 * @data: (not nullable): a pointer to the elements to prepend to the start of the array
Packit ae235b
 * @len: the number of elements to prepend
Packit ae235b
 *
Packit ae235b
 * Adds @len elements onto the start of the array.
Packit ae235b
 *
Packit ae235b
 * This operation is slower than g_array_append_vals() since the
Packit ae235b
 * existing elements in the array have to be moved to make space for
Packit ae235b
 * the new elements.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GArray
Packit ae235b
 */
Packit ae235b
/**
Packit ae235b
 * g_array_prepend_val:
Packit ae235b
 * @a: a #GArray
Packit ae235b
 * @v: the value to prepend to the #GArray
Packit ae235b
 *
Packit ae235b
 * Adds the value on to the start of the array. The array will grow in
Packit ae235b
 * size automatically if necessary.
Packit ae235b
 *
Packit ae235b
 * This operation is slower than g_array_append_val() since the
Packit ae235b
 * existing elements in the array have to be moved to make space for
Packit ae235b
 * the new element.
Packit ae235b
 *
Packit ae235b
 * g_array_prepend_val() is a macro which uses a reference to the value
Packit ae235b
 * parameter @v. This means that you cannot use it with literal values
Packit ae235b
 * such as "27". You must use variables.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GArray
Packit ae235b
 */
Packit ae235b
GArray*
Packit ae235b
g_array_prepend_vals (GArray        *farray,
Packit ae235b
                      gconstpointer  data,
Packit ae235b
                      guint          len)
Packit ae235b
{
Packit ae235b
  GRealArray *array = (GRealArray*) farray;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (array, NULL);
Packit ae235b
Packit ae235b
  if (len == 0)
Packit ae235b
    return farray;
Packit ae235b
Packit ae235b
  g_array_maybe_expand (array, len);
Packit ae235b
Packit ae235b
  memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
Packit ae235b
           g_array_elt_len (array, array->len));
Packit ae235b
Packit ae235b
  memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
Packit ae235b
Packit ae235b
  array->len += len;
Packit ae235b
Packit ae235b
  g_array_zero_terminate (array);
Packit ae235b
Packit ae235b
  return farray;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_insert_vals:
Packit ae235b
 * @array: a #GArray
Packit ae235b
 * @index_: the index to place the elements at
Packit ae235b
 * @data: (not nullable): a pointer to the elements to insert
Packit ae235b
 * @len: the number of elements to insert
Packit ae235b
 *
Packit ae235b
 * Inserts @len elements into a #GArray at the given index.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GArray
Packit ae235b
 */
Packit ae235b
/**
Packit ae235b
 * g_array_insert_val:
Packit ae235b
 * @a: a #GArray
Packit ae235b
 * @i: the index to place the element at
Packit ae235b
 * @v: the value to insert into the array
Packit ae235b
 *
Packit ae235b
 * Inserts an element into an array at the given index.
Packit ae235b
 *
Packit ae235b
 * g_array_insert_val() is a macro which uses a reference to the value
Packit ae235b
 * parameter @v. This means that you cannot use it with literal values
Packit ae235b
 * such as "27". You must use variables.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GArray
Packit ae235b
 */
Packit ae235b
GArray*
Packit ae235b
g_array_insert_vals (GArray        *farray,
Packit ae235b
                     guint          index_,
Packit ae235b
                     gconstpointer  data,
Packit ae235b
                     guint          len)
Packit ae235b
{
Packit ae235b
  GRealArray *array = (GRealArray*) farray;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (array, NULL);
Packit ae235b
Packit ae235b
  if (len == 0)
Packit ae235b
    return farray;
Packit ae235b
Packit ae235b
  g_array_maybe_expand (array, len);
Packit ae235b
Packit ae235b
  memmove (g_array_elt_pos (array, len + index_),
Packit ae235b
           g_array_elt_pos (array, index_),
Packit ae235b
           g_array_elt_len (array, array->len - index_));
Packit ae235b
Packit ae235b
  memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
Packit ae235b
Packit ae235b
  array->len += len;
Packit ae235b
Packit ae235b
  g_array_zero_terminate (array);
Packit ae235b
Packit ae235b
  return farray;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_set_size:
Packit ae235b
 * @array: a #GArray
Packit ae235b
 * @length: the new size of the #GArray
Packit ae235b
 *
Packit ae235b
 * Sets the size of the array, expanding it if necessary. If the array
Packit ae235b
 * was created with @clear_ set to %TRUE, the new elements are set to 0.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GArray
Packit ae235b
 */
Packit ae235b
GArray*
Packit ae235b
g_array_set_size (GArray *farray,
Packit ae235b
                  guint   length)
Packit ae235b
{
Packit ae235b
  GRealArray *array = (GRealArray*) farray;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (array, NULL);
Packit ae235b
Packit ae235b
  if (length > array->len)
Packit ae235b
    {
Packit ae235b
      g_array_maybe_expand (array, length - array->len);
Packit ae235b
      
Packit ae235b
      if (array->clear)
Packit ae235b
        g_array_elt_zero (array, array->len, length - array->len);
Packit ae235b
    }
Packit ae235b
  else if (length < array->len)
Packit ae235b
    g_array_remove_range (farray, length, array->len - length);
Packit ae235b
  
Packit ae235b
  array->len = length;
Packit ae235b
  
Packit ae235b
  g_array_zero_terminate (array);
Packit ae235b
  
Packit ae235b
  return farray;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_remove_index:
Packit ae235b
 * @array: a #GArray
Packit ae235b
 * @index_: the index of the element to remove
Packit ae235b
 *
Packit ae235b
 * Removes the element at the given index from a #GArray. The following
Packit ae235b
 * elements are moved down one place.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GArray
Packit ae235b
 */
Packit ae235b
GArray*
Packit ae235b
g_array_remove_index (GArray *farray,
Packit ae235b
                      guint   index_)
Packit ae235b
{
Packit ae235b
  GRealArray* array = (GRealArray*) farray;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (array, NULL);
Packit ae235b
Packit ae235b
  g_return_val_if_fail (index_ < array->len, NULL);
Packit ae235b
Packit ae235b
  if (array->clear_func != NULL)
Packit ae235b
    array->clear_func (g_array_elt_pos (array, index_));
Packit ae235b
Packit ae235b
  if (index_ != array->len - 1)
Packit ae235b
    memmove (g_array_elt_pos (array, index_),
Packit ae235b
             g_array_elt_pos (array, index_ + 1),
Packit ae235b
             g_array_elt_len (array, array->len - index_ - 1));
Packit ae235b
Packit ae235b
  array->len -= 1;
Packit ae235b
Packit ae235b
  if (G_UNLIKELY (g_mem_gc_friendly))
Packit ae235b
    g_array_elt_zero (array, array->len, 1);
Packit ae235b
  else
Packit ae235b
    g_array_zero_terminate (array);
Packit ae235b
Packit ae235b
  return farray;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_remove_index_fast:
Packit ae235b
 * @array: a @GArray
Packit ae235b
 * @index_: the index of the element to remove
Packit ae235b
 *
Packit ae235b
 * Removes the element at the given index from a #GArray. The last
Packit ae235b
 * element in the array is used to fill in the space, so this function
Packit ae235b
 * does not preserve the order of the #GArray. But it is faster than
Packit ae235b
 * g_array_remove_index().
Packit ae235b
 *
Packit ae235b
 * Returns: the #GArray
Packit ae235b
 */
Packit ae235b
GArray*
Packit ae235b
g_array_remove_index_fast (GArray *farray,
Packit ae235b
                           guint   index_)
Packit ae235b
{
Packit ae235b
  GRealArray* array = (GRealArray*) farray;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (array, NULL);
Packit ae235b
Packit ae235b
  g_return_val_if_fail (index_ < array->len, NULL);
Packit ae235b
Packit ae235b
  if (array->clear_func != NULL)
Packit ae235b
    array->clear_func (g_array_elt_pos (array, index_));
Packit ae235b
Packit ae235b
  if (index_ != array->len - 1)
Packit ae235b
    memcpy (g_array_elt_pos (array, index_),
Packit ae235b
            g_array_elt_pos (array, array->len - 1),
Packit ae235b
            g_array_elt_len (array, 1));
Packit ae235b
  
Packit ae235b
  array->len -= 1;
Packit ae235b
Packit ae235b
  if (G_UNLIKELY (g_mem_gc_friendly))
Packit ae235b
    g_array_elt_zero (array, array->len, 1);
Packit ae235b
  else
Packit ae235b
    g_array_zero_terminate (array);
Packit ae235b
Packit ae235b
  return farray;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_remove_range:
Packit ae235b
 * @array: a @GArray
Packit ae235b
 * @index_: the index of the first element to remove
Packit ae235b
 * @length: the number of elements to remove
Packit ae235b
 *
Packit ae235b
 * Removes the given number of elements starting at the given index
Packit ae235b
 * from a #GArray.  The following elements are moved to close the gap.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GArray
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
GArray*
Packit ae235b
g_array_remove_range (GArray *farray,
Packit ae235b
                      guint   index_,
Packit ae235b
                      guint   length)
Packit ae235b
{
Packit ae235b
  GRealArray *array = (GRealArray*) farray;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (array, NULL);
Packit ae235b
  g_return_val_if_fail (index_ <= array->len, NULL);
Packit ae235b
  g_return_val_if_fail (index_ + length <= array->len, NULL);
Packit ae235b
Packit ae235b
  if (array->clear_func != NULL)
Packit ae235b
    {
Packit ae235b
      guint i;
Packit ae235b
Packit ae235b
      for (i = 0; i < length; i++)
Packit ae235b
        array->clear_func (g_array_elt_pos (array, index_ + i));
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (index_ + length != array->len)
Packit ae235b
    memmove (g_array_elt_pos (array, index_),
Packit ae235b
             g_array_elt_pos (array, index_ + length),
Packit ae235b
             (array->len - (index_ + length)) * array->elt_size);
Packit ae235b
Packit ae235b
  array->len -= length;
Packit ae235b
  if (G_UNLIKELY (g_mem_gc_friendly))
Packit ae235b
    g_array_elt_zero (array, array->len, length);
Packit ae235b
  else
Packit ae235b
    g_array_zero_terminate (array);
Packit ae235b
Packit ae235b
  return farray;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_sort:
Packit ae235b
 * @array: a #GArray
Packit ae235b
 * @compare_func: comparison function
Packit ae235b
 *
Packit ae235b
 * Sorts a #GArray using @compare_func which should be a qsort()-style
Packit ae235b
 * comparison function (returns less than zero for first arg is less
Packit ae235b
 * than second arg, zero for equal, greater zero if first arg is
Packit ae235b
 * greater than second arg).
Packit ae235b
 *
Packit ae235b
 * This is guaranteed to be a stable sort since version 2.32.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_array_sort (GArray       *farray,
Packit ae235b
              GCompareFunc  compare_func)
Packit ae235b
{
Packit ae235b
  GRealArray *array = (GRealArray*) farray;
Packit ae235b
Packit ae235b
  g_return_if_fail (array != NULL);
Packit ae235b
Packit ae235b
  /* Don't use qsort as we want a guaranteed stable sort */
Packit ae235b
  g_qsort_with_data (array->data,
Packit ae235b
                     array->len,
Packit ae235b
                     array->elt_size,
Packit ae235b
                     (GCompareDataFunc)compare_func,
Packit ae235b
                     NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_array_sort_with_data:
Packit ae235b
 * @array: a #GArray
Packit ae235b
 * @compare_func: comparison function
Packit ae235b
 * @user_data: data to pass to @compare_func
Packit ae235b
 *
Packit ae235b
 * Like g_array_sort(), but the comparison function receives an extra
Packit ae235b
 * user data argument.
Packit ae235b
 *
Packit ae235b
 * This is guaranteed to be a stable sort since version 2.32.
Packit ae235b
 *
Packit ae235b
 * There used to be a comment here about making the sort stable by
Packit ae235b
 * using the addresses of the elements in the comparison function.
Packit ae235b
 * This did not actually work, so any such code should be removed.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_array_sort_with_data (GArray           *farray,
Packit ae235b
                        GCompareDataFunc  compare_func,
Packit ae235b
                        gpointer          user_data)
Packit ae235b
{
Packit ae235b
  GRealArray *array = (GRealArray*) farray;
Packit ae235b
Packit ae235b
  g_return_if_fail (array != NULL);
Packit ae235b
Packit ae235b
  g_qsort_with_data (array->data,
Packit ae235b
                     array->len,
Packit ae235b
                     array->elt_size,
Packit ae235b
                     compare_func,
Packit ae235b
                     user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Returns the smallest power of 2 greater than n, or n if
Packit ae235b
 * such power does not fit in a guint
Packit ae235b
 */
Packit ae235b
static guint
Packit ae235b
g_nearest_pow (gint num)
Packit ae235b
{
Packit ae235b
  guint n = 1;
Packit ae235b
Packit ae235b
  while (n < num && n > 0)
Packit ae235b
    n <<= 1;
Packit ae235b
Packit ae235b
  return n ? n : num;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_array_maybe_expand (GRealArray *array,
Packit ae235b
                      gint        len)
Packit ae235b
{
Packit ae235b
  guint want_alloc = g_array_elt_len (array, array->len + len + 
Packit ae235b
                                      array->zero_terminated);
Packit ae235b
Packit ae235b
  if (want_alloc > array->alloc)
Packit ae235b
    {
Packit ae235b
      want_alloc = g_nearest_pow (want_alloc);
Packit ae235b
      want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
Packit ae235b
Packit ae235b
      array->data = g_realloc (array->data, want_alloc);
Packit ae235b
Packit ae235b
      if (G_UNLIKELY (g_mem_gc_friendly))
Packit ae235b
        memset (array->data + array->alloc, 0, want_alloc - array->alloc);
Packit ae235b
Packit ae235b
      array->alloc = want_alloc;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:arrays_pointer
Packit ae235b
 * @title: Pointer Arrays
Packit ae235b
 * @short_description: arrays of pointers to any type of data, which
Packit ae235b
 *     grow automatically as new elements are added
Packit ae235b
 *
Packit ae235b
 * Pointer Arrays are similar to Arrays but are used only for storing
Packit ae235b
 * pointers.
Packit ae235b
 *
Packit ae235b
 * If you remove elements from the array, elements at the end of the
Packit ae235b
 * array are moved into the space previously occupied by the removed
Packit ae235b
 * element. This means that you should not rely on the index of particular
Packit ae235b
 * elements remaining the same. You should also be careful when deleting
Packit ae235b
 * elements while iterating over the array.
Packit ae235b
 *
Packit ae235b
 * To create a pointer array, use g_ptr_array_new().
Packit ae235b
 *
Packit ae235b
 * To add elements to a pointer array, use g_ptr_array_add().
Packit ae235b
 *
Packit ae235b
 * To remove elements from a pointer array, use g_ptr_array_remove(),
Packit ae235b
 * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
Packit ae235b
 *
Packit ae235b
 * To access an element of a pointer array, use g_ptr_array_index().
Packit ae235b
 *
Packit ae235b
 * To set the size of a pointer array, use g_ptr_array_set_size().
Packit ae235b
 *
Packit ae235b
 * To free a pointer array, use g_ptr_array_free().
Packit ae235b
 *
Packit ae235b
 * An example using a #GPtrArray:
Packit ae235b
 * |[
Packit ae235b
 *   GPtrArray *array;
Packit ae235b
 *   gchar *string1 = "one";
Packit ae235b
 *   gchar *string2 = "two";
Packit ae235b
 *   gchar *string3 = "three";
Packit ae235b
 *
Packit ae235b
 *   array = g_ptr_array_new ();
Packit ae235b
 *   g_ptr_array_add (array, (gpointer) string1);
Packit ae235b
 *   g_ptr_array_add (array, (gpointer) string2);
Packit ae235b
 *   g_ptr_array_add (array, (gpointer) string3);
Packit ae235b
 *
Packit ae235b
 *   if (g_ptr_array_index (array, 0) != (gpointer) string1)
Packit ae235b
 *     g_print ("ERROR: got %p instead of %p\n",
Packit ae235b
 *              g_ptr_array_index (array, 0), string1);
Packit ae235b
 *
Packit ae235b
 *   g_ptr_array_free (array, TRUE);
Packit ae235b
 * ]|
Packit ae235b
 */
Packit ae235b
Packit ae235b
typedef struct _GRealPtrArray  GRealPtrArray;
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GPtrArray:
Packit ae235b
 * @pdata: points to the array of pointers, which may be moved when the
Packit ae235b
 *     array grows
Packit ae235b
 * @len: number of pointers in the array
Packit ae235b
 *
Packit ae235b
 * Contains the public fields of a pointer array.
Packit ae235b
 */
Packit ae235b
struct _GRealPtrArray
Packit ae235b
{
Packit ae235b
  gpointer       *pdata;
Packit ae235b
  guint           len;
Packit ae235b
  guint           alloc;
Packit ae235b
  gint            ref_count;
Packit ae235b
  GDestroyNotify  element_free_func;
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_index:
Packit ae235b
 * @array: a #GPtrArray
Packit ae235b
 * @index_: the index of the pointer to return
Packit ae235b
 *
Packit ae235b
 * Returns the pointer at the given index of the pointer array.
Packit ae235b
 *
Packit ae235b
 * This does not perform bounds checking on the given @index_,
Packit ae235b
 * so you are responsible for checking it against the array length.
Packit ae235b
 *
Packit ae235b
 * Returns: the pointer at the given index
Packit ae235b
 */
Packit ae235b
Packit ae235b
static void g_ptr_array_maybe_expand (GRealPtrArray *array,
Packit ae235b
                                      gint           len);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_new:
Packit ae235b
 *
Packit ae235b
 * Creates a new #GPtrArray with a reference count of 1.
Packit ae235b
 *
Packit ae235b
 * Returns: the new #GPtrArray
Packit ae235b
 */
Packit ae235b
GPtrArray*
Packit ae235b
g_ptr_array_new (void)
Packit ae235b
{
Packit ae235b
  return g_ptr_array_sized_new (0);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_sized_new:
Packit ae235b
 * @reserved_size: number of pointers preallocated
Packit ae235b
 *
Packit ae235b
 * Creates a new #GPtrArray with @reserved_size pointers preallocated
Packit ae235b
 * and a reference count of 1. This avoids frequent reallocation, if
Packit ae235b
 * you are going to add many pointers to the array. Note however that
Packit ae235b
 * the size of the array is still 0.
Packit ae235b
 *
Packit ae235b
 * Returns: the new #GPtrArray
Packit ae235b
 */
Packit ae235b
GPtrArray*  
Packit ae235b
g_ptr_array_sized_new (guint reserved_size)
Packit ae235b
{
Packit ae235b
  GRealPtrArray *array;
Packit ae235b
Packit ae235b
  array = g_slice_new (GRealPtrArray);
Packit ae235b
Packit ae235b
  array->pdata = NULL;
Packit ae235b
  array->len = 0;
Packit ae235b
  array->alloc = 0;
Packit ae235b
  array->ref_count = 1;
Packit ae235b
  array->element_free_func = NULL;
Packit ae235b
Packit ae235b
  if (reserved_size != 0)
Packit ae235b
    g_ptr_array_maybe_expand (array, reserved_size);
Packit ae235b
Packit ae235b
  return (GPtrArray*) array;  
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_new_with_free_func:
Packit ae235b
 * @element_free_func: (nullable): A function to free elements with
Packit ae235b
 *     destroy @array or %NULL
Packit ae235b
 *
Packit ae235b
 * Creates a new #GPtrArray with a reference count of 1 and use
Packit ae235b
 * @element_free_func for freeing each element when the array is destroyed
Packit ae235b
 * either via g_ptr_array_unref(), when g_ptr_array_free() is called with
Packit ae235b
 * @free_segment set to %TRUE or when removing elements.
Packit ae235b
 *
Packit ae235b
 * Returns: A new #GPtrArray
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GPtrArray*
Packit ae235b
g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
Packit ae235b
{
Packit ae235b
  GPtrArray *array;
Packit ae235b
Packit ae235b
  array = g_ptr_array_new ();
Packit ae235b
  g_ptr_array_set_free_func (array, element_free_func);
Packit ae235b
Packit ae235b
  return array;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_new_full:
Packit ae235b
 * @reserved_size: number of pointers preallocated
Packit ae235b
 * @element_free_func: (nullable): A function to free elements with
Packit ae235b
 *     destroy @array or %NULL
Packit ae235b
 *
Packit ae235b
 * Creates a new #GPtrArray with @reserved_size pointers preallocated
Packit ae235b
 * and a reference count of 1. This avoids frequent reallocation, if
Packit ae235b
 * you are going to add many pointers to the array. Note however that
Packit ae235b
 * the size of the array is still 0. It also set @element_free_func
Packit ae235b
 * for freeing each element when the array is destroyed either via
Packit ae235b
 * g_ptr_array_unref(), when g_ptr_array_free() is called with
Packit ae235b
 * @free_segment set to %TRUE or when removing elements.
Packit ae235b
 *
Packit ae235b
 * Returns: A new #GPtrArray
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
GPtrArray*
Packit ae235b
g_ptr_array_new_full (guint          reserved_size,
Packit ae235b
                      GDestroyNotify element_free_func)
Packit ae235b
{
Packit ae235b
  GPtrArray *array;
Packit ae235b
Packit ae235b
  array = g_ptr_array_sized_new (reserved_size);
Packit ae235b
  g_ptr_array_set_free_func (array, element_free_func);
Packit ae235b
Packit ae235b
  return array;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_set_free_func:
Packit ae235b
 * @array: A #GPtrArray
Packit ae235b
 * @element_free_func: (nullable): A function to free elements with
Packit ae235b
 *     destroy @array or %NULL
Packit ae235b
 *
Packit ae235b
 * Sets a function for freeing each element when @array is destroyed
Packit ae235b
 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
Packit ae235b
 * with @free_segment set to %TRUE or when removing elements.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_ptr_array_set_free_func (GPtrArray      *array,
Packit ae235b
                           GDestroyNotify  element_free_func)
Packit ae235b
{
Packit ae235b
  GRealPtrArray *rarray = (GRealPtrArray *)array;
Packit ae235b
Packit ae235b
  g_return_if_fail (array);
Packit ae235b
Packit ae235b
  rarray->element_free_func = element_free_func;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_ref:
Packit ae235b
 * @array: a #GPtrArray
Packit ae235b
 *
Packit ae235b
 * Atomically increments the reference count of @array by one.
Packit ae235b
 * This function is thread-safe and may be called from any thread.
Packit ae235b
 *
Packit ae235b
 * Returns: The passed in #GPtrArray
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GPtrArray*
Packit ae235b
g_ptr_array_ref (GPtrArray *array)
Packit ae235b
{
Packit ae235b
  GRealPtrArray *rarray = (GRealPtrArray *)array;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (array, NULL);
Packit ae235b
Packit ae235b
  g_atomic_int_inc (&rarray->ref_count);
Packit ae235b
Packit ae235b
  return array;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer *ptr_array_free (GPtrArray *, ArrayFreeFlags);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_unref:
Packit ae235b
 * @array: A #GPtrArray
Packit ae235b
 *
Packit ae235b
 * Atomically decrements the reference count of @array by one. If the
Packit ae235b
 * reference count drops to 0, the effect is the same as calling
Packit ae235b
 * g_ptr_array_free() with @free_segment set to %TRUE. This function
Packit ae235b
 * is thread-safe and may be called from any thread.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_ptr_array_unref (GPtrArray *array)
Packit ae235b
{
Packit ae235b
  GRealPtrArray *rarray = (GRealPtrArray *)array;
Packit ae235b
Packit ae235b
  g_return_if_fail (array);
Packit ae235b
Packit ae235b
  if (g_atomic_int_dec_and_test (&rarray->ref_count))
Packit ae235b
    ptr_array_free (array, FREE_SEGMENT);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_free:
Packit ae235b
 * @array: a #GPtrArray
Packit ae235b
 * @free_seg: if %TRUE the actual pointer array is freed as well
Packit ae235b
 *
Packit ae235b
 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
Packit ae235b
 * it frees the memory block holding the elements as well. Pass %FALSE
Packit ae235b
 * if you want to free the #GPtrArray wrapper but preserve the
Packit ae235b
 * underlying array for use elsewhere. If the reference count of @array
Packit ae235b
 * is greater than one, the #GPtrArray wrapper is preserved but the
Packit ae235b
 * size of @array will be set to zero.
Packit ae235b
 *
Packit ae235b
 * If array contents point to dynamically-allocated memory, they should
Packit ae235b
 * be freed separately if @free_seg is %TRUE and no #GDestroyNotify
Packit ae235b
 * function has been set for @array.
Packit ae235b
 *
Packit ae235b
 * This function is not thread-safe. If using a #GPtrArray from multiple
Packit ae235b
 * threads, use only the atomic g_ptr_array_ref() and g_ptr_array_unref()
Packit ae235b
 * functions.
Packit ae235b
 *
Packit ae235b
 * Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
Packit ae235b
 *     The pointer array should be freed using g_free().
Packit ae235b
 */
Packit ae235b
gpointer*
Packit ae235b
g_ptr_array_free (GPtrArray *array,
Packit ae235b
                  gboolean   free_segment)
Packit ae235b
{
Packit ae235b
  GRealPtrArray *rarray = (GRealPtrArray *)array;
Packit ae235b
  ArrayFreeFlags flags;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (rarray, NULL);
Packit ae235b
Packit ae235b
  flags = (free_segment ? FREE_SEGMENT : 0);
Packit ae235b
Packit ae235b
  /* if others are holding a reference, preserve the wrapper but
Packit ae235b
   * do free/return the data
Packit ae235b
   */
Packit ae235b
  if (!g_atomic_int_dec_and_test (&rarray->ref_count))
Packit ae235b
    flags |= PRESERVE_WRAPPER;
Packit ae235b
Packit ae235b
  return ptr_array_free (array, flags);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer *
Packit ae235b
ptr_array_free (GPtrArray      *array,
Packit ae235b
                ArrayFreeFlags  flags)
Packit ae235b
{
Packit ae235b
  GRealPtrArray *rarray = (GRealPtrArray *)array;
Packit ae235b
  gpointer *segment;
Packit ae235b
Packit ae235b
  if (flags & FREE_SEGMENT)
Packit ae235b
    {
Packit ae235b
      if (rarray->element_free_func != NULL)
Packit ae235b
        g_ptr_array_foreach (array, (GFunc) rarray->element_free_func, NULL);
Packit ae235b
      g_free (rarray->pdata);
Packit ae235b
      segment = NULL;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    segment = rarray->pdata;
Packit ae235b
Packit ae235b
  if (flags & PRESERVE_WRAPPER)
Packit ae235b
    {
Packit ae235b
      rarray->pdata = NULL;
Packit ae235b
      rarray->len = 0;
Packit ae235b
      rarray->alloc = 0;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_slice_free1 (sizeof (GRealPtrArray), rarray);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return segment;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_ptr_array_maybe_expand (GRealPtrArray *array,
Packit ae235b
                          gint           len)
Packit ae235b
{
Packit ae235b
  if ((array->len + len) > array->alloc)
Packit ae235b
    {
Packit ae235b
      guint old_alloc = array->alloc;
Packit ae235b
      array->alloc = g_nearest_pow (array->len + len);
Packit ae235b
      array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
Packit ae235b
      array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
Packit ae235b
      if (G_UNLIKELY (g_mem_gc_friendly))
Packit ae235b
        for ( ; old_alloc < array->alloc; old_alloc++)
Packit ae235b
          array->pdata [old_alloc] = NULL;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_set_size:
Packit ae235b
 * @array: a #GPtrArray
Packit ae235b
 * @length: the new length of the pointer array
Packit ae235b
 *
Packit ae235b
 * Sets the size of the array. When making the array larger,
Packit ae235b
 * newly-added elements will be set to %NULL. When making it smaller,
Packit ae235b
 * if @array has a non-%NULL #GDestroyNotify function then it will be
Packit ae235b
 * called for the removed elements.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_ptr_array_set_size  (GPtrArray *array,
Packit ae235b
                       gint       length)
Packit ae235b
{
Packit ae235b
  GRealPtrArray *rarray = (GRealPtrArray *)array;
Packit ae235b
Packit ae235b
  g_return_if_fail (rarray);
Packit ae235b
Packit ae235b
  if (length > rarray->len)
Packit ae235b
    {
Packit ae235b
      int i;
Packit ae235b
      g_ptr_array_maybe_expand (rarray, (length - rarray->len));
Packit ae235b
      /* This is not 
Packit ae235b
       *     memset (array->pdata + array->len, 0,
Packit ae235b
       *            sizeof (gpointer) * (length - array->len));
Packit ae235b
       * to make it really portable. Remember (void*)NULL needn't be
Packit ae235b
       * bitwise zero. It of course is silly not to use memset (..,0,..).
Packit ae235b
       */
Packit ae235b
      for (i = rarray->len; i < length; i++)
Packit ae235b
        rarray->pdata[i] = NULL;
Packit ae235b
    }
Packit ae235b
  else if (length < rarray->len)
Packit ae235b
    g_ptr_array_remove_range (array, length, rarray->len - length);
Packit ae235b
Packit ae235b
  rarray->len = length;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_remove_index:
Packit ae235b
 * @array: a #GPtrArray
Packit ae235b
 * @index_: the index of the pointer to remove
Packit ae235b
 *
Packit ae235b
 * Removes the pointer at the given index from the pointer array.
Packit ae235b
 * The following elements are moved down one place. If @array has
Packit ae235b
 * a non-%NULL #GDestroyNotify function it is called for the removed
Packit ae235b
 * element.
Packit ae235b
 *
Packit ae235b
 * Returns: the pointer which was removed
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_ptr_array_remove_index (GPtrArray *array,
Packit ae235b
                          guint      index_)
Packit ae235b
{
Packit ae235b
  GRealPtrArray *rarray = (GRealPtrArray *)array;
Packit ae235b
  gpointer result;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (rarray, NULL);
Packit ae235b
Packit ae235b
  g_return_val_if_fail (index_ < rarray->len, NULL);
Packit ae235b
Packit ae235b
  result = rarray->pdata[index_];
Packit ae235b
  
Packit ae235b
  if (rarray->element_free_func != NULL)
Packit ae235b
    rarray->element_free_func (rarray->pdata[index_]);
Packit ae235b
Packit ae235b
  if (index_ != rarray->len - 1)
Packit ae235b
    memmove (rarray->pdata + index_, rarray->pdata + index_ + 1,
Packit ae235b
             sizeof (gpointer) * (rarray->len - index_ - 1));
Packit ae235b
  
Packit ae235b
  rarray->len -= 1;
Packit ae235b
Packit ae235b
  if (G_UNLIKELY (g_mem_gc_friendly))
Packit ae235b
    rarray->pdata[rarray->len] = NULL;
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_remove_index_fast:
Packit ae235b
 * @array: a #GPtrArray
Packit ae235b
 * @index_: the index of the pointer to remove
Packit ae235b
 *
Packit ae235b
 * Removes the pointer at the given index from the pointer array.
Packit ae235b
 * The last element in the array is used to fill in the space, so
Packit ae235b
 * this function does not preserve the order of the array. But it
Packit ae235b
 * is faster than g_ptr_array_remove_index(). If @array has a non-%NULL
Packit ae235b
 * #GDestroyNotify function it is called for the removed element.
Packit ae235b
 *
Packit ae235b
 * Returns: the pointer which was removed
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_ptr_array_remove_index_fast (GPtrArray *array,
Packit ae235b
                               guint      index_)
Packit ae235b
{
Packit ae235b
  GRealPtrArray *rarray = (GRealPtrArray *)array;
Packit ae235b
  gpointer result;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (rarray, NULL);
Packit ae235b
Packit ae235b
  g_return_val_if_fail (index_ < rarray->len, NULL);
Packit ae235b
Packit ae235b
  result = rarray->pdata[index_];
Packit ae235b
Packit ae235b
  if (rarray->element_free_func != NULL)
Packit ae235b
    rarray->element_free_func (rarray->pdata[index_]);
Packit ae235b
Packit ae235b
  if (index_ != rarray->len - 1)
Packit ae235b
    rarray->pdata[index_] = rarray->pdata[rarray->len - 1];
Packit ae235b
Packit ae235b
  rarray->len -= 1;
Packit ae235b
Packit ae235b
  if (G_UNLIKELY (g_mem_gc_friendly))
Packit ae235b
    rarray->pdata[rarray->len] = NULL;
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_remove_range:
Packit ae235b
 * @array: a @GPtrArray
Packit ae235b
 * @index_: the index of the first pointer to remove
Packit ae235b
 * @length: the number of pointers to remove
Packit ae235b
 *
Packit ae235b
 * Removes the given number of pointers starting at the given index
Packit ae235b
 * from a #GPtrArray. The following elements are moved to close the
Packit ae235b
 * gap. If @array has a non-%NULL #GDestroyNotify function it is
Packit ae235b
 * called for the removed elements.
Packit ae235b
 *
Packit ae235b
 * Returns: the @array
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
GPtrArray*
Packit ae235b
g_ptr_array_remove_range (GPtrArray *array,
Packit ae235b
                          guint      index_,
Packit ae235b
                          guint      length)
Packit ae235b
{
Packit ae235b
  GRealPtrArray *rarray = (GRealPtrArray *)array;
Packit ae235b
  guint n;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (rarray != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (index_ <= rarray->len, NULL);
Packit ae235b
  g_return_val_if_fail (index_ + length <= rarray->len, NULL);
Packit ae235b
Packit ae235b
  if (rarray->element_free_func != NULL)
Packit ae235b
    {
Packit ae235b
      for (n = index_; n < index_ + length; n++)
Packit ae235b
        rarray->element_free_func (rarray->pdata[n]);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (index_ + length != rarray->len)
Packit ae235b
    {
Packit ae235b
      memmove (&rarray->pdata[index_],
Packit ae235b
               &rarray->pdata[index_ + length],
Packit ae235b
               (rarray->len - (index_ + length)) * sizeof (gpointer));
Packit ae235b
    }
Packit ae235b
Packit ae235b
  rarray->len -= length;
Packit ae235b
  if (G_UNLIKELY (g_mem_gc_friendly))
Packit ae235b
    {
Packit ae235b
      guint i;
Packit ae235b
      for (i = 0; i < length; i++)
Packit ae235b
        rarray->pdata[rarray->len + i] = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return array;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_remove:
Packit ae235b
 * @array: a #GPtrArray
Packit ae235b
 * @data: the pointer to remove
Packit ae235b
 *
Packit ae235b
 * Removes the first occurrence of the given pointer from the pointer
Packit ae235b
 * array. The following elements are moved down one place. If @array
Packit ae235b
 * has a non-%NULL #GDestroyNotify function it is called for the
Packit ae235b
 * removed element.
Packit ae235b
 *
Packit ae235b
 * It returns %TRUE if the pointer was removed, or %FALSE if the
Packit ae235b
 * pointer was not found.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the pointer is removed, %FALSE if the pointer
Packit ae235b
 *     is not found in the array
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_ptr_array_remove (GPtrArray *array,
Packit ae235b
                    gpointer   data)
Packit ae235b
{
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (array, FALSE);
Packit ae235b
Packit ae235b
  for (i = 0; i < array->len; i += 1)
Packit ae235b
    {
Packit ae235b
      if (array->pdata[i] == data)
Packit ae235b
        {
Packit ae235b
          g_ptr_array_remove_index (array, i);
Packit ae235b
          return TRUE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_remove_fast:
Packit ae235b
 * @array: a #GPtrArray
Packit ae235b
 * @data: the pointer to remove
Packit ae235b
 *
Packit ae235b
 * Removes the first occurrence of the given pointer from the pointer
Packit ae235b
 * array. The last element in the array is used to fill in the space,
Packit ae235b
 * so this function does not preserve the order of the array. But it
Packit ae235b
 * is faster than g_ptr_array_remove(). If @array has a non-%NULL
Packit ae235b
 * #GDestroyNotify function it is called for the removed element.
Packit ae235b
 *
Packit ae235b
 * It returns %TRUE if the pointer was removed, or %FALSE if the
Packit ae235b
 * pointer was not found.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the pointer was found in the array
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_ptr_array_remove_fast (GPtrArray *array,
Packit ae235b
                         gpointer   data)
Packit ae235b
{
Packit ae235b
  GRealPtrArray *rarray = (GRealPtrArray *)array;
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (rarray, FALSE);
Packit ae235b
Packit ae235b
  for (i = 0; i < rarray->len; i += 1)
Packit ae235b
    {
Packit ae235b
      if (rarray->pdata[i] == data)
Packit ae235b
        {
Packit ae235b
          g_ptr_array_remove_index_fast (array, i);
Packit ae235b
          return TRUE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_add:
Packit ae235b
 * @array: a #GPtrArray
Packit ae235b
 * @data: the pointer to add
Packit ae235b
 *
Packit ae235b
 * Adds a pointer to the end of the pointer array. The array will grow
Packit ae235b
 * in size automatically if necessary.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_ptr_array_add (GPtrArray *array,
Packit ae235b
                 gpointer   data)
Packit ae235b
{
Packit ae235b
  GRealPtrArray *rarray = (GRealPtrArray *)array;
Packit ae235b
Packit ae235b
  g_return_if_fail (rarray);
Packit ae235b
Packit ae235b
  g_ptr_array_maybe_expand (rarray, 1);
Packit ae235b
Packit ae235b
  rarray->pdata[rarray->len++] = data;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_insert:
Packit ae235b
 * @array: a #GPtrArray
Packit ae235b
 * @index_: the index to place the new element at, or -1 to append
Packit ae235b
 * @data: the pointer to add.
Packit ae235b
 *
Packit ae235b
 * Inserts an element into the pointer array at the given index. The 
Packit ae235b
 * array will grow in size automatically if necessary.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_ptr_array_insert (GPtrArray *array,
Packit ae235b
                    gint       index_,
Packit ae235b
                    gpointer   data)
Packit ae235b
{
Packit ae235b
  GRealPtrArray *rarray = (GRealPtrArray *)array;
Packit ae235b
Packit ae235b
  g_return_if_fail (rarray);
Packit ae235b
  g_return_if_fail (index_ >= -1);
Packit ae235b
  g_return_if_fail (index_ <= (gint)rarray->len);
Packit ae235b
Packit ae235b
  g_ptr_array_maybe_expand (rarray, 1);
Packit ae235b
Packit ae235b
  if (index_ < 0)
Packit ae235b
    index_ = rarray->len;
Packit ae235b
Packit ae235b
  if (index_ < rarray->len)
Packit ae235b
    memmove (&(rarray->pdata[index_ + 1]),
Packit ae235b
             &(rarray->pdata[index_]),
Packit ae235b
             (rarray->len - index_) * sizeof (gpointer));
Packit ae235b
Packit ae235b
  rarray->len++;
Packit ae235b
  rarray->pdata[index_] = data;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_sort:
Packit ae235b
 * @array: a #GPtrArray
Packit ae235b
 * @compare_func: comparison function
Packit ae235b
 *
Packit ae235b
 * Sorts the array, using @compare_func which should be a qsort()-style
Packit ae235b
 * comparison function (returns less than zero for first arg is less
Packit ae235b
 * than second arg, zero for equal, greater than zero if irst arg is
Packit ae235b
 * greater than second arg).
Packit ae235b
 *
Packit ae235b
 * Note that the comparison function for g_ptr_array_sort() doesn't
Packit ae235b
 * take the pointers from the array as arguments, it takes pointers to
Packit ae235b
 * the pointers in the array.
Packit ae235b
 *
Packit ae235b
 * This is guaranteed to be a stable sort since version 2.32.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_ptr_array_sort (GPtrArray    *array,
Packit ae235b
                  GCompareFunc  compare_func)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (array != NULL);
Packit ae235b
Packit ae235b
  /* Don't use qsort as we want a guaranteed stable sort */
Packit ae235b
  g_qsort_with_data (array->pdata,
Packit ae235b
                     array->len,
Packit ae235b
                     sizeof (gpointer),
Packit ae235b
                     (GCompareDataFunc)compare_func,
Packit ae235b
                     NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_sort_with_data:
Packit ae235b
 * @array: a #GPtrArray
Packit ae235b
 * @compare_func: comparison function
Packit ae235b
 * @user_data: data to pass to @compare_func
Packit ae235b
 *
Packit ae235b
 * Like g_ptr_array_sort(), but the comparison function has an extra
Packit ae235b
 * user data argument.
Packit ae235b
 *
Packit ae235b
 * Note that the comparison function for g_ptr_array_sort_with_data()
Packit ae235b
 * doesn't take the pointers from the array as arguments, it takes
Packit ae235b
 * pointers to the pointers in the array.
Packit ae235b
 *
Packit ae235b
 * This is guaranteed to be a stable sort since version 2.32.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_ptr_array_sort_with_data (GPtrArray        *array,
Packit ae235b
                            GCompareDataFunc  compare_func,
Packit ae235b
                            gpointer          user_data)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (array != NULL);
Packit ae235b
Packit ae235b
  g_qsort_with_data (array->pdata,
Packit ae235b
                     array->len,
Packit ae235b
                     sizeof (gpointer),
Packit ae235b
                     compare_func,
Packit ae235b
                     user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_foreach:
Packit ae235b
 * @array: a #GPtrArray
Packit ae235b
 * @func: the function to call for each array element
Packit ae235b
 * @user_data: user data to pass to the function
Packit ae235b
 * 
Packit ae235b
 * Calls a function for each element of a #GPtrArray. @func must not
Packit ae235b
 * add elements to or remove elements from the array.
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_ptr_array_foreach (GPtrArray *array,
Packit ae235b
                     GFunc      func,
Packit ae235b
                     gpointer   user_data)
Packit ae235b
{
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  g_return_if_fail (array);
Packit ae235b
Packit ae235b
  for (i = 0; i < array->len; i++)
Packit ae235b
    (*func) (array->pdata[i], user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_find: (skip)
Packit ae235b
 * @haystack: pointer array to be searched
Packit ae235b
 * @needle: pointer to look for
Packit ae235b
 * @index_: (optional) (out caller-allocates): return location for the index of
Packit ae235b
 *    the element, if found
Packit ae235b
 *
Packit ae235b
 * Checks whether @needle exists in @haystack. If the element is found, %TRUE is
Packit ae235b
 * returned and the element’s index is returned in @index_ (if non-%NULL).
Packit ae235b
 * Otherwise, %FALSE is returned and @index_ is undefined. If @needle exists
Packit ae235b
 * multiple times in @haystack, the index of the first instance is returned.
Packit ae235b
 *
Packit ae235b
 * This does pointer comparisons only. If you want to use more complex equality
Packit ae235b
 * checks, such as string comparisons, use g_ptr_array_find_with_equal_func().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @needle is one of the elements of @haystack
Packit ae235b
 * Since: 2.54
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_ptr_array_find (GPtrArray     *haystack,
Packit ae235b
                  gconstpointer  needle,
Packit ae235b
                  guint         *index_)
Packit ae235b
{
Packit ae235b
  return g_ptr_array_find_with_equal_func (haystack, needle, NULL, index_);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_ptr_array_find_with_equal_func: (skip)
Packit ae235b
 * @haystack: pointer array to be searched
Packit ae235b
 * @needle: pointer to look for
Packit ae235b
 * @equal_func: (nullable): the function to call for each element, which should
Packit ae235b
 *    return %TRUE when the desired element is found; or %NULL to use pointer
Packit ae235b
 *    equality
Packit ae235b
 * @index_: (optional) (out caller-allocates): return location for the index of
Packit ae235b
 *    the element, if found
Packit ae235b
 *
Packit ae235b
 * Checks whether @needle exists in @haystack, using the given @equal_func.
Packit ae235b
 * If the element is found, %TRUE is returned and the element’s index is
Packit ae235b
 * returned in @index_ (if non-%NULL). Otherwise, %FALSE is returned and @index_
Packit ae235b
 * is undefined. If @needle exists multiple times in @haystack, the index of
Packit ae235b
 * the first instance is returned.
Packit ae235b
 *
Packit ae235b
 * @equal_func is called with the element from the array as its first parameter,
Packit ae235b
 * and @needle as its second parameter. If @equal_func is %NULL, pointer
Packit ae235b
 * equality is used.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @needle is one of the elements of @haystack
Packit ae235b
 * Since: 2.54
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_ptr_array_find_with_equal_func (GPtrArray     *haystack,
Packit ae235b
                                  gconstpointer  needle,
Packit ae235b
                                  GEqualFunc     equal_func,
Packit ae235b
                                  guint         *index_)
Packit ae235b
{
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (haystack != NULL, FALSE);
Packit ae235b
Packit ae235b
  if (equal_func == NULL)
Packit ae235b
    equal_func = g_direct_equal;
Packit ae235b
Packit ae235b
  for (i = 0; i < haystack->len; i++)
Packit ae235b
    {
Packit ae235b
      if (equal_func (g_ptr_array_index (haystack, i), needle))
Packit ae235b
        {
Packit ae235b
          if (index_ != NULL)
Packit ae235b
            *index_ = i;
Packit ae235b
          return TRUE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:arrays_byte
Packit ae235b
 * @title: Byte Arrays
Packit ae235b
 * @short_description: arrays of bytes
Packit ae235b
 *
Packit ae235b
 * #GByteArray is a mutable array of bytes based on #GArray, to provide arrays
Packit ae235b
 * of bytes which grow automatically as elements are added.
Packit ae235b
 *
Packit ae235b
 * To create a new #GByteArray use g_byte_array_new(). To add elements to a
Packit ae235b
 * #GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
Packit ae235b
 *
Packit ae235b
 * To set the size of a #GByteArray, use g_byte_array_set_size().
Packit ae235b
 *
Packit ae235b
 * To free a #GByteArray, use g_byte_array_free().
Packit ae235b
 *
Packit ae235b
 * An example for using a #GByteArray:
Packit ae235b
 * |[
Packit ae235b
 *   GByteArray *gbarray;
Packit ae235b
 *   gint i;
Packit ae235b
 *
Packit ae235b
 *   gbarray = g_byte_array_new ();
Packit ae235b
 *   for (i = 0; i < 10000; i++)
Packit ae235b
 *     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
Packit ae235b
 *
Packit ae235b
 *   for (i = 0; i < 10000; i++)
Packit ae235b
 *     {
Packit ae235b
 *       g_assert (gbarray->data[4*i] == 'a');
Packit ae235b
 *       g_assert (gbarray->data[4*i+1] == 'b');
Packit ae235b
 *       g_assert (gbarray->data[4*i+2] == 'c');
Packit ae235b
 *       g_assert (gbarray->data[4*i+3] == 'd');
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *   g_byte_array_free (gbarray, TRUE);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * See #GBytes if you are interested in an immutable object representing a
Packit ae235b
 * sequence of bytes.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GByteArray:
Packit ae235b
 * @data: a pointer to the element data. The data may be moved as
Packit ae235b
 *     elements are added to the #GByteArray
Packit ae235b
 * @len: the number of elements in the #GByteArray
Packit ae235b
 *
Packit ae235b
 * Contains the public fields of a GByteArray.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_new:
Packit ae235b
 *
Packit ae235b
 * Creates a new #GByteArray with a reference count of 1.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): the new #GByteArray
Packit ae235b
 */
Packit ae235b
GByteArray*
Packit ae235b
g_byte_array_new (void)
Packit ae235b
{
Packit ae235b
  return (GByteArray *)g_array_sized_new (FALSE, FALSE, 1, 0);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_new_take:
Packit ae235b
 * @data: (transfer full) (array length=len): byte data for the array
Packit ae235b
 * @len: length of @data
Packit ae235b
 *
Packit ae235b
 * Create byte array containing the data. The data will be owned by the array
Packit ae235b
 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a new #GByteArray
Packit ae235b
 */
Packit ae235b
GByteArray*
Packit ae235b
g_byte_array_new_take (guint8 *data,
Packit ae235b
                       gsize   len)
Packit ae235b
{
Packit ae235b
  GByteArray *array;
Packit ae235b
  GRealArray *real;
Packit ae235b
Packit ae235b
  array = g_byte_array_new ();
Packit ae235b
  real = (GRealArray *)array;
Packit ae235b
  g_assert (real->data == NULL);
Packit ae235b
  g_assert (real->len == 0);
Packit ae235b
Packit ae235b
  real->data = data;
Packit ae235b
  real->len = len;
Packit ae235b
  real->alloc = len;
Packit ae235b
Packit ae235b
  return array;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_sized_new:
Packit ae235b
 * @reserved_size: number of bytes preallocated
Packit ae235b
 *
Packit ae235b
 * Creates a new #GByteArray with @reserved_size bytes preallocated.
Packit ae235b
 * This avoids frequent reallocation, if you are going to add many
Packit ae235b
 * bytes to the array. Note however that the size of the array is still
Packit ae235b
 * 0.
Packit ae235b
 *
Packit ae235b
 * Returns: the new #GByteArray
Packit ae235b
 */
Packit ae235b
GByteArray*
Packit ae235b
g_byte_array_sized_new (guint reserved_size)
Packit ae235b
{
Packit ae235b
  return (GByteArray *)g_array_sized_new (FALSE, FALSE, 1, reserved_size);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_free:
Packit ae235b
 * @array: a #GByteArray
Packit ae235b
 * @free_segment: if %TRUE the actual byte data is freed as well
Packit ae235b
 *
Packit ae235b
 * Frees the memory allocated by the #GByteArray. If @free_segment is
Packit ae235b
 * %TRUE it frees the actual byte data. If the reference count of
Packit ae235b
 * @array is greater than one, the #GByteArray wrapper is preserved but
Packit ae235b
 * the size of @array will be set to zero.
Packit ae235b
 *
Packit ae235b
 * Returns: the element data if @free_segment is %FALSE, otherwise
Packit ae235b
 *          %NULL.  The element data should be freed using g_free().
Packit ae235b
 */
Packit ae235b
guint8*
Packit ae235b
g_byte_array_free (GByteArray *array,
Packit ae235b
                   gboolean    free_segment)
Packit ae235b
{
Packit ae235b
  return (guint8 *)g_array_free ((GArray *)array, free_segment);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_free_to_bytes:
Packit ae235b
 * @array: (transfer full): a #GByteArray
Packit ae235b
 *
Packit ae235b
 * Transfers the data from the #GByteArray into a new immutable #GBytes.
Packit ae235b
 *
Packit ae235b
 * The #GByteArray is freed unless the reference count of @array is greater
Packit ae235b
 * than one, the #GByteArray wrapper is preserved but the size of @array
Packit ae235b
 * will be set to zero.
Packit ae235b
 *
Packit ae235b
 * This is identical to using g_bytes_new_take() and g_byte_array_free()
Packit ae235b
 * together.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a new immutable #GBytes representing same
Packit ae235b
 *     byte data that was in the array
Packit ae235b
 */
Packit ae235b
GBytes*
Packit ae235b
g_byte_array_free_to_bytes (GByteArray *array)
Packit ae235b
{
Packit ae235b
  gsize length;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (array != NULL, NULL);
Packit ae235b
Packit ae235b
  length = array->len;
Packit ae235b
  return g_bytes_new_take (g_byte_array_free (array, FALSE), length);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_ref:
Packit ae235b
 * @array: A #GByteArray
Packit ae235b
 *
Packit ae235b
 * Atomically increments the reference count of @array by one.
Packit ae235b
 * This function is thread-safe and may be called from any thread.
Packit ae235b
 *
Packit ae235b
 * Returns: The passed in #GByteArray
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GByteArray*
Packit ae235b
g_byte_array_ref (GByteArray *array)
Packit ae235b
{
Packit ae235b
  return (GByteArray *)g_array_ref ((GArray *)array);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_unref:
Packit ae235b
 * @array: A #GByteArray
Packit ae235b
 *
Packit ae235b
 * Atomically decrements the reference count of @array by one. If the
Packit ae235b
 * reference count drops to 0, all memory allocated by the array is
Packit ae235b
 * released. This function is thread-safe and may be called from any
Packit ae235b
 * thread.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_byte_array_unref (GByteArray *array)
Packit ae235b
{
Packit ae235b
  g_array_unref ((GArray *)array);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_append:
Packit ae235b
 * @array: a #GByteArray
Packit ae235b
 * @data: the byte data to be added
Packit ae235b
 * @len: the number of bytes to add
Packit ae235b
 *
Packit ae235b
 * Adds the given bytes to the end of the #GByteArray.
Packit ae235b
 * The array will grow in size automatically if necessary.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GByteArray
Packit ae235b
 */
Packit ae235b
GByteArray*
Packit ae235b
g_byte_array_append (GByteArray   *array,
Packit ae235b
                     const guint8 *data,
Packit ae235b
                     guint         len)
Packit ae235b
{
Packit ae235b
  g_array_append_vals ((GArray *)array, (guint8 *)data, len);
Packit ae235b
Packit ae235b
  return array;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_prepend:
Packit ae235b
 * @array: a #GByteArray
Packit ae235b
 * @data: the byte data to be added
Packit ae235b
 * @len: the number of bytes to add
Packit ae235b
 *
Packit ae235b
 * Adds the given data to the start of the #GByteArray.
Packit ae235b
 * The array will grow in size automatically if necessary.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GByteArray
Packit ae235b
 */
Packit ae235b
GByteArray*
Packit ae235b
g_byte_array_prepend (GByteArray   *array,
Packit ae235b
                      const guint8 *data,
Packit ae235b
                      guint         len)
Packit ae235b
{
Packit ae235b
  g_array_prepend_vals ((GArray *)array, (guint8 *)data, len);
Packit ae235b
Packit ae235b
  return array;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_set_size:
Packit ae235b
 * @array: a #GByteArray
Packit ae235b
 * @length: the new size of the #GByteArray
Packit ae235b
 *
Packit ae235b
 * Sets the size of the #GByteArray, expanding it if necessary.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GByteArray
Packit ae235b
 */
Packit ae235b
GByteArray*
Packit ae235b
g_byte_array_set_size (GByteArray *array,
Packit ae235b
                       guint       length)
Packit ae235b
{
Packit ae235b
  g_array_set_size ((GArray *)array, length);
Packit ae235b
Packit ae235b
  return array;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_remove_index:
Packit ae235b
 * @array: a #GByteArray
Packit ae235b
 * @index_: the index of the byte to remove
Packit ae235b
 *
Packit ae235b
 * Removes the byte at the given index from a #GByteArray.
Packit ae235b
 * The following bytes are moved down one place.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GByteArray
Packit ae235b
 **/
Packit ae235b
GByteArray*
Packit ae235b
g_byte_array_remove_index (GByteArray *array,
Packit ae235b
                           guint       index_)
Packit ae235b
{
Packit ae235b
  g_array_remove_index ((GArray *)array, index_);
Packit ae235b
Packit ae235b
  return array;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_remove_index_fast:
Packit ae235b
 * @array: a #GByteArray
Packit ae235b
 * @index_: the index of the byte to remove
Packit ae235b
 *
Packit ae235b
 * Removes the byte at the given index from a #GByteArray. The last
Packit ae235b
 * element in the array is used to fill in the space, so this function
Packit ae235b
 * does not preserve the order of the #GByteArray. But it is faster
Packit ae235b
 * than g_byte_array_remove_index().
Packit ae235b
 *
Packit ae235b
 * Returns: the #GByteArray
Packit ae235b
 */
Packit ae235b
GByteArray*
Packit ae235b
g_byte_array_remove_index_fast (GByteArray *array,
Packit ae235b
                                guint       index_)
Packit ae235b
{
Packit ae235b
  g_array_remove_index_fast ((GArray *)array, index_);
Packit ae235b
Packit ae235b
  return array;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_remove_range:
Packit ae235b
 * @array: a @GByteArray
Packit ae235b
 * @index_: the index of the first byte to remove
Packit ae235b
 * @length: the number of bytes to remove
Packit ae235b
 *
Packit ae235b
 * Removes the given number of bytes starting at the given index from a
Packit ae235b
 * #GByteArray.  The following elements are moved to close the gap.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GByteArray
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
GByteArray*
Packit ae235b
g_byte_array_remove_range (GByteArray *array,
Packit ae235b
                           guint       index_,
Packit ae235b
                           guint       length)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (array, NULL);
Packit ae235b
  g_return_val_if_fail (index_ <= array->len, NULL);
Packit ae235b
  g_return_val_if_fail (index_ + length <= array->len, NULL);
Packit ae235b
Packit ae235b
  return (GByteArray *)g_array_remove_range ((GArray *)array, index_, length);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_sort:
Packit ae235b
 * @array: a #GByteArray
Packit ae235b
 * @compare_func: comparison function
Packit ae235b
 *
Packit ae235b
 * Sorts a byte array, using @compare_func which should be a
Packit ae235b
 * qsort()-style comparison function (returns less than zero for first
Packit ae235b
 * arg is less than second arg, zero for equal, greater than zero if
Packit ae235b
 * first arg is greater than second arg).
Packit ae235b
 *
Packit ae235b
 * If two array elements compare equal, their order in the sorted array
Packit ae235b
 * is undefined. If you want equal elements to keep their order (i.e.
Packit ae235b
 * you want a stable sort) you can write a comparison function that,
Packit ae235b
 * if two elements would otherwise compare equal, compares them by
Packit ae235b
 * their addresses.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_byte_array_sort (GByteArray   *array,
Packit ae235b
                   GCompareFunc  compare_func)
Packit ae235b
{
Packit ae235b
  g_array_sort ((GArray *)array, compare_func);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_byte_array_sort_with_data:
Packit ae235b
 * @array: a #GByteArray
Packit ae235b
 * @compare_func: comparison function
Packit ae235b
 * @user_data: data to pass to @compare_func
Packit ae235b
 *
Packit ae235b
 * Like g_byte_array_sort(), but the comparison function takes an extra
Packit ae235b
 * user data argument.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_byte_array_sort_with_data (GByteArray       *array,
Packit ae235b
                             GCompareDataFunc  compare_func,
Packit ae235b
                             gpointer          user_data)
Packit ae235b
{
Packit ae235b
  g_array_sort_with_data ((GArray *)array, compare_func, user_data);
Packit ae235b
}