1 /**************************************************************************
2  *
3  * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4  * All Rights Reserved.
5  *
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:
13  *
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.
17  *
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.
25  *
26  **************************************************************************/
27 
28 /**
29  * @file
30  * Generic handle table.
31  *
32  * @author José Fonseca <jrfonseca@tungstengraphics.com>
33  */
34 
35 #ifndef U_HANDLE_TABLE_H_
36 #define U_HANDLE_TABLE_H_
37 
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 
44 /**
45  * Abstract data type to map integer handles to objects.
46  *
47  * Also referred as "pointer array".
48  */
49 struct handle_table;
50 
51 
52 struct handle_table *
53 handle_table_create(void);
54 
55 
56 /**
57  * Set an optional destructor callback.
58  *
59  * If set, it will be called during handle_table_remove and
60  * handle_table_destroy calls.
61  */
62 void
63 handle_table_set_destroy(struct handle_table *ht,
64                          void (*destroy)(void *object));
65 
66 
67 /**
68  * Add a new object.
69  *
70  * Returns a zero handle on failure (out of memory).
71  */
72 unsigned
73 handle_table_add(struct handle_table *ht,
74                  void *object);
75 
76 /**
77  * Returns zero on failure (out of memory).
78  */
79 unsigned
80 handle_table_set(struct handle_table *ht,
81                  unsigned handle,
82                  void *object);
83 
84 /**
85  * Fetch an existing object.
86  *
87  * Returns NULL for an invalid handle.
88  */
89 void *
90 handle_table_get(struct handle_table *ht,
91                  unsigned handle);
92 
93 
94 void
95 handle_table_remove(struct handle_table *ht,
96                     unsigned handle);
97 
98 
99 void
100 handle_table_destroy(struct handle_table *ht);
101 
102 
103 unsigned
104 handle_table_get_first_handle(struct handle_table *ht);
105 
106 
107 unsigned
108 handle_table_get_next_handle(struct handle_table *ht,
109                              unsigned handle);
110 
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #endif /* U_HANDLE_TABLE_H_ */
117