revert 213 commits (to 56092) from the last month. 10 still need work to resolve...
[AROS.git] / workbench / libs / mesa / src / gallium / auxiliary / util / u_handle_table.c
blob3703718a621d0a6ae1a90386c11a74c0bafbbe77
1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 /**
29 * @file
30 * Generic handle table implementation.
32 * @author José Fonseca <jrfonseca@tungstengraphics.com>
36 #include "pipe/p_compiler.h"
37 #include "util/u_debug.h"
39 #include "util/u_memory.h"
40 #include "util/u_handle_table.h"
43 #define HANDLE_TABLE_INITIAL_SIZE 16
46 struct handle_table
48 /** Object array. Empty handles have a null object */
49 void **objects;
51 /** Number of objects the handle can currently hold */
52 unsigned size;
53 /** Number of consecutive objects allocated at the start of the table */
54 unsigned filled;
56 /** Optional object destructor */
57 void (*destroy)(void *object);
61 struct handle_table *
62 handle_table_create(void)
64 struct handle_table *ht;
66 ht = MALLOC_STRUCT(handle_table);
67 if(!ht)
68 return NULL;
70 ht->objects = (void **)CALLOC(HANDLE_TABLE_INITIAL_SIZE, sizeof(void *));
71 if(!ht->objects) {
72 FREE(ht);
73 return NULL;
76 ht->size = HANDLE_TABLE_INITIAL_SIZE;
77 ht->filled = 0;
79 ht->destroy = NULL;
81 return ht;
85 void
86 handle_table_set_destroy(struct handle_table *ht,
87 void (*destroy)(void *object))
89 assert(ht);
90 if (!ht)
91 return;
92 ht->destroy = destroy;
96 /**
97 * Resize the table if necessary
99 static INLINE int
100 handle_table_resize(struct handle_table *ht,
101 unsigned minimum_size)
103 unsigned new_size;
104 void **new_objects;
106 if(ht->size > minimum_size)
107 return ht->size;
109 new_size = ht->size;
110 while(!(new_size > minimum_size))
111 new_size *= 2;
112 assert(new_size);
114 new_objects = (void **)REALLOC((void *)ht->objects,
115 ht->size*sizeof(void *),
116 new_size*sizeof(void *));
117 if(!new_objects)
118 return 0;
120 memset(new_objects + ht->size, 0, (new_size - ht->size)*sizeof(void *));
122 ht->size = new_size;
123 ht->objects = new_objects;
125 return ht->size;
129 static INLINE void
130 handle_table_clear(struct handle_table *ht,
131 unsigned index)
133 void *object;
135 /* The order here is important so that the object being destroyed is not
136 * present in the table when seen by the destroy callback, because the
137 * destroy callback may directly or indirectly call the other functions in
138 * this module.
141 object = ht->objects[index];
142 if(object) {
143 ht->objects[index] = NULL;
145 if(ht->destroy)
146 ht->destroy(object);
151 unsigned
152 handle_table_add(struct handle_table *ht,
153 void *object)
155 unsigned index;
156 unsigned handle;
158 assert(ht);
159 assert(object);
160 if(!object || !ht)
161 return 0;
163 /* linear search for an empty handle */
164 while(ht->filled < ht->size) {
165 if(!ht->objects[ht->filled])
166 break;
167 ++ht->filled;
170 index = ht->filled;
171 handle = index + 1;
173 /* check integer overflow */
174 if(!handle)
175 return 0;
177 /* grow the table if necessary */
178 if(!handle_table_resize(ht, index))
179 return 0;
181 assert(!ht->objects[index]);
182 ht->objects[index] = object;
183 ++ht->filled;
185 return handle;
189 unsigned
190 handle_table_set(struct handle_table *ht,
191 unsigned handle,
192 void *object)
194 unsigned index;
196 assert(ht);
197 assert(handle);
198 if(!handle || !ht)
199 return 0;
201 assert(object);
202 if(!object)
203 return 0;
205 index = handle - 1;
207 /* grow the table if necessary */
208 if(!handle_table_resize(ht, index))
209 return 0;
211 handle_table_clear(ht, index);
213 ht->objects[index] = object;
215 return handle;
219 void *
220 handle_table_get(struct handle_table *ht,
221 unsigned handle)
223 void *object;
225 assert(ht);
226 assert(handle);
227 if(!handle || !ht || handle > ht->size)
228 return NULL;
230 object = ht->objects[handle - 1];
232 return object;
236 void
237 handle_table_remove(struct handle_table *ht,
238 unsigned handle)
240 void *object;
241 unsigned index;
243 assert(ht);
244 assert(handle);
245 if(!handle || !ht || handle > ht->size)
246 return;
248 index = handle - 1;
249 object = ht->objects[index];
250 if(!object)
251 return;
253 handle_table_clear(ht, index);
255 if(index < ht->filled)
256 ht->filled = index;
260 unsigned
261 handle_table_get_next_handle(struct handle_table *ht,
262 unsigned handle)
264 unsigned index;
266 for(index = handle; index < ht->size; ++index) {
267 if(ht->objects[index])
268 return index + 1;
271 return 0;
275 unsigned
276 handle_table_get_first_handle(struct handle_table *ht)
278 return handle_table_get_next_handle(ht, 0);
282 void
283 handle_table_destroy(struct handle_table *ht)
285 unsigned index;
286 assert(ht);
288 if (!ht)
289 return;
291 if(ht->destroy)
292 for(index = 0; index < ht->size; ++index)
293 handle_table_clear(ht, index);
295 FREE(ht->objects);
296 FREE(ht);