Blame gobject/gatomicarray.c

Packit ae235b
/* GObject - GLib Type, Object, Parameter and Signal Library
Packit ae235b
 * Copyright (C) 2009 Benjamin Otte <otte@gnome.org>
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
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "gatomicarray.h"
Packit ae235b
Packit ae235b
/* A GAtomicArray is a growable, mutable array of data
Packit ae235b
 * generally of the form of a header of a specific size and
Packit ae235b
 * then a array of items of a fixed size.
Packit ae235b
 *
Packit ae235b
 * It is possible to do lock-less read transactions from the
Packit ae235b
 * array without any protection against other reads or writes,
Packit ae235b
 * but such read operation must be aware that the data in the
Packit ae235b
 * atomic array can change at any time during the transaction,
Packit ae235b
 * and only at the end can we verify if the transaction succeeded
Packit ae235b
 * or not. Thus the reading transaction cannot for instance
Packit ae235b
 * dereference a pointer in the array inside the transaction.
Packit ae235b
 *
Packit ae235b
 * The size of an array however cannot change during a read
Packit ae235b
 * transaction.
Packit ae235b
 *
Packit ae235b
 * Writes to the array is done in a copy-update style, but there
Packit ae235b
 * is no real protection against multiple writers overwriting each
Packit ae235b
 * others updates, so writes must be protected by an external lock.
Packit ae235b
 */
Packit ae235b
Packit ae235b
G_LOCK_DEFINE_STATIC (array);
Packit ae235b
Packit ae235b
typedef struct _FreeListNode FreeListNode;
Packit ae235b
struct _FreeListNode {
Packit ae235b
  FreeListNode *next;
Packit ae235b
};
Packit ae235b
Packit ae235b
/* This is really a list of array memory blocks, using the
Packit ae235b
 * first item as the next pointer to chain them together.
Packit ae235b
 * Protected by array lock */
Packit ae235b
static FreeListNode *freelist = NULL;
Packit ae235b
Packit ae235b
/* must hold array lock */
Packit ae235b
static gpointer
Packit ae235b
freelist_alloc (gsize size, gboolean reuse)
Packit ae235b
{
Packit ae235b
  gpointer mem;
Packit ae235b
  FreeListNode *free, **prev;
Packit ae235b
  gsize real_size;
Packit ae235b
Packit ae235b
  if (reuse)
Packit ae235b
    {
Packit ae235b
      for (free = freelist, prev = &freelist; free != NULL; prev = &free->next, free = free->next)
Packit ae235b
	{
Packit ae235b
	  if (G_ATOMIC_ARRAY_DATA_SIZE (free) == size)
Packit ae235b
	    {
Packit ae235b
	      *prev = free->next;
Packit ae235b
	      return (gpointer)free;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  real_size = sizeof (gsize) + MAX (size, sizeof (FreeListNode));
Packit ae235b
  mem = g_slice_alloc (real_size);
Packit ae235b
  mem = ((char *) mem) + sizeof (gsize);
Packit ae235b
  G_ATOMIC_ARRAY_DATA_SIZE (mem) = size;
Packit ae235b
  return mem;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* must hold array lock */
Packit ae235b
static void
Packit ae235b
freelist_free (gpointer mem)
Packit ae235b
{
Packit ae235b
  FreeListNode *free;
Packit ae235b
Packit ae235b
  free = mem;
Packit ae235b
  free->next = freelist;
Packit ae235b
  freelist = free;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
_g_atomic_array_init (GAtomicArray *array)
Packit ae235b
{
Packit ae235b
  array->data = NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Get a copy of the data (if non-NULL) that
Packit ae235b
 * can be changed and then re-applied with
Packit ae235b
 * g_atomic_array_update().
Packit ae235b
 *
Packit ae235b
 * If additional_element_size is > 0 then
Packit ae235b
 * then the new memory chunk is that much
Packit ae235b
 * larger, or there were no data we return
Packit ae235b
 * a chunk of header_size + additional_element_size.
Packit ae235b
 * This means you can use this to grow the
Packit ae235b
 * array part and it handles the first element
Packit ae235b
 * being added automatically.
Packit ae235b
 *
Packit ae235b
 * We don't support shrinking arrays, as if
Packit ae235b
 * we then re-grow we may reuse an old pointer
Packit ae235b
 * value and confuse the transaction check.
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
_g_atomic_array_copy (GAtomicArray *array,
Packit ae235b
		      gsize header_size,
Packit ae235b
		      gsize additional_element_size)
Packit ae235b
{
Packit ae235b
  guint8 *new, *old;
Packit ae235b
  gsize old_size, new_size;
Packit ae235b
Packit ae235b
  G_LOCK (array);
Packit ae235b
  old = g_atomic_pointer_get (&array->data);
Packit ae235b
  if (old)
Packit ae235b
    {
Packit ae235b
      old_size = G_ATOMIC_ARRAY_DATA_SIZE (old);
Packit ae235b
      new_size = old_size + additional_element_size;
Packit ae235b
      /* Don't reuse if copying to same size, as this may end
Packit ae235b
	 up reusing the same pointer for the same array thus
Packit ae235b
	 confusing the transaction check */
Packit ae235b
      new = freelist_alloc (new_size, additional_element_size != 0);
Packit ae235b
      memcpy (new, old, old_size);
Packit ae235b
    }
Packit ae235b
  else if (additional_element_size != 0)
Packit ae235b
    {
Packit ae235b
      new_size = header_size + additional_element_size;
Packit ae235b
      new = freelist_alloc (new_size, TRUE);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    new = NULL;
Packit ae235b
  G_UNLOCK (array);
Packit ae235b
  return new;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Replace the data in the array with the new data,
Packit ae235b
 * freeing the old data (for reuse). The new data may
Packit ae235b
 * not be smaller than the current data.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
_g_atomic_array_update (GAtomicArray *array,
Packit ae235b
			gpointer new_data)
Packit ae235b
{
Packit ae235b
  guint8 *old;
Packit ae235b
Packit ae235b
  G_LOCK (array);
Packit ae235b
  old = g_atomic_pointer_get (&array->data);
Packit ae235b
Packit ae235b
  g_assert (old == NULL || G_ATOMIC_ARRAY_DATA_SIZE (old) <= G_ATOMIC_ARRAY_DATA_SIZE (new_data));
Packit ae235b
Packit ae235b
  g_atomic_pointer_set (&array->data, new_data);
Packit ae235b
  if (old)
Packit ae235b
    freelist_free (old);
Packit ae235b
  G_UNLOCK (array);
Packit ae235b
}