4 * An OpenGL based 'interactive canvas' library.
6 * Authored By Matthew Allum <mallum@openedhand.com>
8 * Copyright (C) 2006-2008 OpenedHand
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
25 * ClutterIDPool: pool of reusable integer ids associated with pointers.
27 * Author: Øyvind Kolås <pippin@o-hand-com>
35 #include "clutter-debug.h"
36 #include "clutter-id-pool.h"
40 GArray *array; /* Array of pointers */
41 GSList *free_ids; /* A stack of freed ids */
45 _clutter_id_pool_new (guint initial_size)
49 self = g_slice_new (ClutterIDPool);
51 self->array = g_array_sized_new (FALSE, FALSE,
52 sizeof (gpointer), initial_size);
53 self->free_ids = NULL;
58 _clutter_id_pool_free (ClutterIDPool *id_pool)
60 g_return_if_fail (id_pool != NULL);
62 g_array_free (id_pool->array, TRUE);
63 g_slist_free (id_pool->free_ids);
64 g_slice_free (ClutterIDPool, id_pool);
68 _clutter_id_pool_add (ClutterIDPool *id_pool,
74 g_return_val_if_fail (id_pool != NULL, 0);
76 if (id_pool->free_ids) /* There are items on our freelist, reuse one */
78 array = (void*) id_pool->array->data;
79 retval = GPOINTER_TO_UINT (id_pool->free_ids->data);
81 id_pool->free_ids = g_slist_remove (id_pool->free_ids,
82 id_pool->free_ids->data);
88 retval = id_pool->array->len;
89 g_array_append_val (id_pool->array, ptr);
95 _clutter_id_pool_remove (ClutterIDPool *id_pool,
100 g_return_if_fail (id_pool != NULL);
102 array = (void*) id_pool->array->data;
106 id_pool->free_ids = g_slist_prepend (id_pool->free_ids,
107 GUINT_TO_POINTER (id_));
111 _clutter_id_pool_lookup (ClutterIDPool *id_pool,
116 g_return_val_if_fail (id_pool != NULL, NULL);
117 g_return_val_if_fail (id_pool->array != NULL, NULL);
119 array = (void*) id_pool->array->data;
121 if (id_ >= id_pool->array->len || array[id_] == NULL)
123 g_warning ("The required ID of %u does not refer to an existing actor; "
124 "this usually implies that the pick() of an actor is not "
125 "correctly implemented or that there is an error in the "
126 "glReadPixels() implementation of the GL driver.", id_);