1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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
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 **************************************************************************/
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
48 /** Object array. Empty handles have a null object */
51 /** Number of objects the handle can currently hold */
53 /** Number of consecutive objects allocated at the start of the table */
56 /** Optional object destructor */
57 void (*destroy
)(void *object
);
62 handle_table_create(void)
64 struct handle_table
*ht
;
66 ht
= MALLOC_STRUCT(handle_table
);
70 ht
->objects
= (void **)CALLOC(HANDLE_TABLE_INITIAL_SIZE
, sizeof(void *));
76 ht
->size
= HANDLE_TABLE_INITIAL_SIZE
;
86 handle_table_set_destroy(struct handle_table
*ht
,
87 void (*destroy
)(void *object
))
92 ht
->destroy
= destroy
;
97 * Resize the table if necessary
100 handle_table_resize(struct handle_table
*ht
,
101 unsigned minimum_size
)
106 if(ht
->size
> minimum_size
)
110 while(!(new_size
> minimum_size
))
114 new_objects
= (void **)REALLOC((void *)ht
->objects
,
115 ht
->size
*sizeof(void *),
116 new_size
*sizeof(void *));
120 memset(new_objects
+ ht
->size
, 0, (new_size
- ht
->size
)*sizeof(void *));
123 ht
->objects
= new_objects
;
130 handle_table_clear(struct handle_table
*ht
,
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
141 object
= ht
->objects
[index
];
143 ht
->objects
[index
] = NULL
;
152 handle_table_add(struct handle_table
*ht
,
163 /* linear search for an empty handle */
164 while(ht
->filled
< ht
->size
) {
165 if(!ht
->objects
[ht
->filled
])
173 /* check integer overflow */
177 /* grow the table if necessary */
178 if(!handle_table_resize(ht
, index
))
181 assert(!ht
->objects
[index
]);
182 ht
->objects
[index
] = object
;
190 handle_table_set(struct handle_table
*ht
,
207 /* grow the table if necessary */
208 if(!handle_table_resize(ht
, index
))
211 handle_table_clear(ht
, index
);
213 ht
->objects
[index
] = object
;
220 handle_table_get(struct handle_table
*ht
,
227 if(!handle
|| !ht
|| handle
> ht
->size
)
230 object
= ht
->objects
[handle
- 1];
237 handle_table_remove(struct handle_table
*ht
,
245 if(!handle
|| !ht
|| handle
> ht
->size
)
249 object
= ht
->objects
[index
];
253 handle_table_clear(ht
, index
);
255 if(index
< ht
->filled
)
261 handle_table_get_next_handle(struct handle_table
*ht
,
266 for(index
= handle
; index
< ht
->size
; ++index
) {
267 if(ht
->objects
[index
])
276 handle_table_get_first_handle(struct handle_table
*ht
)
278 return handle_table_get_next_handle(ht
, 0);
283 handle_table_destroy(struct handle_table
*ht
)
292 for(index
= 0; index
< ht
->size
; ++index
)
293 handle_table_clear(ht
, index
);