|
Packit |
bc1512 |
/*
|
|
Packit |
bc1512 |
* Gegl id-pool
|
|
Packit |
bc1512 |
*
|
|
Packit |
bc1512 |
* This library is free software; you can redistribute it and/or
|
|
Packit |
bc1512 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
bc1512 |
* License as published by the Free Software Foundation; either
|
|
Packit |
bc1512 |
* version 2 of the License, or (at your option) any later version.
|
|
Packit |
bc1512 |
*
|
|
Packit |
bc1512 |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
bc1512 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
bc1512 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
bc1512 |
* Lesser General Public License for more details.
|
|
Packit |
bc1512 |
*
|
|
Packit |
bc1512 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
bc1512 |
* License along with this library; if not, write to the
|
|
Packit |
bc1512 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
Packit |
bc1512 |
* Boston, MA 02110-1301, USA.
|
|
Packit |
bc1512 |
*
|
|
Packit |
bc1512 |
* GeglIDPool: pool of reusable integer ids associated with pointers.
|
|
Packit |
bc1512 |
*
|
|
Packit |
bc1512 |
* Copyright (C) 2008 OpenedHand
|
|
Packit |
bc1512 |
* Author: Øyvind Kolås <pippin@o-hand-com>
|
|
Packit |
bc1512 |
*
|
|
Packit |
bc1512 |
*/
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
#define DEBUG_POOL
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
bc1512 |
#include "config.h"
|
|
Packit |
bc1512 |
#endif
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
#include "gegl-id-pool.h"
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
struct _GeglIDPool
|
|
Packit |
bc1512 |
{
|
|
Packit |
bc1512 |
GArray *array; /* Array of pointers */
|
|
Packit |
bc1512 |
GSList *free_ids; /* A stack of freed ids */
|
|
Packit |
bc1512 |
};
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
GeglIDPool *
|
|
Packit |
bc1512 |
gegl_id_pool_new (guint initial_size)
|
|
Packit |
bc1512 |
{
|
|
Packit |
bc1512 |
GeglIDPool *self;
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
self = g_slice_new (GeglIDPool);
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
self->array = g_array_sized_new (FALSE, FALSE,
|
|
Packit |
bc1512 |
sizeof (gpointer), initial_size);
|
|
Packit |
bc1512 |
self->free_ids = NULL;
|
|
Packit |
bc1512 |
return self;
|
|
Packit |
bc1512 |
}
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
void
|
|
Packit |
bc1512 |
gegl_id_pool_free (GeglIDPool *id_pool)
|
|
Packit |
bc1512 |
{
|
|
Packit |
bc1512 |
g_return_if_fail (id_pool != NULL);
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
g_array_free (id_pool->array, TRUE);
|
|
Packit |
bc1512 |
g_slist_free (id_pool->free_ids);
|
|
Packit |
bc1512 |
g_slice_free (GeglIDPool, id_pool);
|
|
Packit |
bc1512 |
}
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
guint32
|
|
Packit |
bc1512 |
gegl_id_pool_add (GeglIDPool *id_pool,
|
|
Packit |
bc1512 |
gpointer ptr)
|
|
Packit |
bc1512 |
{
|
|
Packit |
bc1512 |
gpointer *array;
|
|
Packit |
bc1512 |
guint32 id;
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
g_return_val_if_fail (id_pool != NULL, 0);
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
if (id_pool->free_ids) /* There are items on our freelist, reuse one */
|
|
Packit |
bc1512 |
{
|
|
Packit |
bc1512 |
array = (void*) id_pool->array->data;
|
|
Packit |
bc1512 |
id = GPOINTER_TO_UINT (id_pool->free_ids->data);
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
id_pool->free_ids = g_slist_remove (id_pool->free_ids,
|
|
Packit |
bc1512 |
id_pool->free_ids->data);
|
|
Packit |
bc1512 |
array[id] = ptr;
|
|
Packit |
bc1512 |
return id;
|
|
Packit |
bc1512 |
}
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
/* Allocate new id */
|
|
Packit |
bc1512 |
id = id_pool->array->len;
|
|
Packit |
bc1512 |
g_array_append_val (id_pool->array, ptr);
|
|
Packit |
bc1512 |
return id;
|
|
Packit |
bc1512 |
}
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
void
|
|
Packit |
bc1512 |
gegl_id_pool_remove (GeglIDPool *id_pool,
|
|
Packit |
bc1512 |
guint32 id)
|
|
Packit |
bc1512 |
{
|
|
Packit |
bc1512 |
gpointer *array;
|
|
Packit |
bc1512 |
#ifdef DEBUG_POOL
|
|
Packit |
bc1512 |
return;
|
|
Packit |
bc1512 |
#endif
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
g_return_if_fail (id_pool != NULL);
|
|
Packit |
bc1512 |
array = (void*) id_pool->array->data;
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
array[id] = (void*)0xdecafbad; /* set pointer to a recognizably voided
|
|
Packit |
bc1512 |
value */
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
id_pool->free_ids = g_slist_prepend (id_pool->free_ids,
|
|
Packit |
bc1512 |
GUINT_TO_POINTER (id));
|
|
Packit |
bc1512 |
}
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
gpointer
|
|
Packit |
bc1512 |
gegl_id_pool_lookup (GeglIDPool *id_pool,
|
|
Packit |
bc1512 |
guint32 id)
|
|
Packit |
bc1512 |
{
|
|
Packit |
bc1512 |
gpointer *array;
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
g_return_val_if_fail (id_pool != NULL, NULL);
|
|
Packit |
bc1512 |
g_return_val_if_fail (id_pool->array != NULL, NULL);
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
g_return_val_if_fail (id < id_pool->array->len, NULL);
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
array = (void*) id_pool->array->data;
|
|
Packit |
bc1512 |
|
|
Packit |
bc1512 |
return array[id];
|
|
Packit |
bc1512 |
}
|