summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/util/u_handle_table.c
blob: 85302f1e1942dc4374ddf43732848e8009d36957 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**************************************************************************
 *
 * Copyright 2008 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

/**
 * @file
 * Generic handle table implementation.
 *  
 * @author José Fonseca <jfonseca@vmware.com>
 */


#include "pipe/p_compiler.h"
#include "util/u_debug.h"

#include "util/u_memory.h"
#include "util/u_handle_table.h"


#define HANDLE_TABLE_INITIAL_SIZE 16  


struct handle_table
{
   /** Object array. Empty handles have a null object */
   void **objects;
   
   /** Number of objects the handle can currently hold */
   unsigned size;
   /** Number of consecutive objects allocated at the start of the table */
   unsigned filled;
   
   /** Optional object destructor */
   void (*destroy)(void *object);
};


struct handle_table *
handle_table_create(void)
{
   struct handle_table *ht;
   
   ht = MALLOC_STRUCT(handle_table);
   if(!ht)
      return NULL;
   
   ht->objects = (void **)CALLOC(HANDLE_TABLE_INITIAL_SIZE, sizeof(void *));
   if(!ht->objects) {
      FREE(ht);
      return NULL;
   }
   
   ht->size = HANDLE_TABLE_INITIAL_SIZE;
   ht->filled = 0;
   
   ht->destroy = NULL;
   
   return ht;
}


void
handle_table_set_destroy(struct handle_table *ht,
                         void (*destroy)(void *object))
{
   assert(ht);
   if (!ht)
      return;
   ht->destroy = destroy;
}


/**
 * Resize the table if necessary 
 */
static INLINE int
handle_table_resize(struct handle_table *ht,
                    unsigned minimum_size)
{
   unsigned new_size;
   void **new_objects;

   if(ht->size > minimum_size)
      return ht->size;

   new_size = ht->size;
   while(!(new_size > minimum_size))
      new_size *= 2;
   assert(new_size);
   
   new_objects = (void **)REALLOC((void *)ht->objects,
				  ht->size*sizeof(void *),
				  new_size*sizeof(void *));
   if(!new_objects)
      return 0;
   
   memset(new_objects + ht->size, 0, (new_size - ht->size)*sizeof(void *));
   
   ht->size = new_size;
   ht->objects = new_objects;
   
   return ht->size;
}


static INLINE void
handle_table_clear(struct handle_table *ht, 
                   unsigned index)
{
   void *object;
   
   /* The order here is important so that the object being destroyed is not
    * present in the table when seen by the destroy callback, because the 
    * destroy callback may directly or indirectly call the other functions in 
    * this module.
    */

   object = ht->objects[index];
   if(object) {
      ht->objects[index] = NULL;
      
      if(ht->destroy)
         ht->destroy(object);
   }   
}


unsigned
handle_table_add(struct handle_table *ht, 
                 void *object)
{
   unsigned index;
   unsigned handle;
   
   assert(ht);
   assert(object);
   if(!object || !ht)
      return 0;

   /* linear search for an empty handle */
   while(ht->filled < ht->size) {
      if(!ht->objects[ht->filled])
	 break;
      ++ht->filled;
   }
  
   index = ht->filled;
   handle = index + 1;
   
   /* check integer overflow */
   if(!handle)
      return 0;
   
   /* grow the table if necessary */
   if(!handle_table_resize(ht, index))
      return 0;

   assert(!ht->objects[index]);
   ht->objects[index] = object;
   ++ht->filled;
   
   return handle;
}


unsigned
handle_table_set(struct handle_table *ht, 
                 unsigned handle,
                 void *object)
{
   unsigned index;
   
   assert(ht);
   assert(handle);
   if(!handle || !ht)
      return 0;

   assert(object);
   if(!object)
      return 0;
   
   index = handle - 1;

   /* grow the table if necessary */
   if(!handle_table_resize(ht, index))
      return 0;

   handle_table_clear(ht, index);

   ht->objects[index] = object;
   
   return handle;
}


void *
handle_table_get(struct handle_table *ht, 
                 unsigned handle)
{
   void *object;
   
   assert(ht);
   assert(handle);
   if(!handle || !ht || handle > ht->size)
      return NULL;

   object = ht->objects[handle - 1];
   
   return object;
}


void
handle_table_remove(struct handle_table *ht, 
                    unsigned handle)
{
   void *object;
   unsigned index;
   
   assert(ht);
   assert(handle);
   if(!handle || !ht || handle > ht->size)
      return;

   index = handle - 1;
   object = ht->objects[index];
   if(!object)
      return;
   
   handle_table_clear(ht, index);

   if(index < ht->filled)
      ht->filled = index;
}


unsigned
handle_table_get_next_handle(struct handle_table *ht, 
                             unsigned handle)
{
   unsigned index;
   
   for(index = handle; index < ht->size; ++index) {
      if(ht->objects[index])
	 return index + 1;
   }

   return 0;
}


unsigned
handle_table_get_first_handle(struct handle_table *ht)
{
   return handle_table_get_next_handle(ht, 0);
}


void
handle_table_destroy(struct handle_table *ht)
{
   unsigned index;
   assert(ht);

   if (!ht)
      return;

   if(ht->destroy)
      for(index = 0; index < ht->size; ++index)
         handle_table_clear(ht, index);
   
   FREE(ht->objects);
   FREE(ht);
}