59c8c6cf98dc07c1935e434ac53fa877e9c82bd7
1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
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 VMWARE 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 **************************************************************************/
29 #include "pipe/p_compiler.h"
30 #include "util/u_debug.h"
32 #include "util/u_memory.h"
33 #include "util/u_hash_table.h"
34 #include "util/hash_table.h"
42 util_hash_table_create(uint32_t (*hash
)(const void *key
),
43 bool (*equal
)(const void *key1
, const void *key2
))
45 return _mesa_hash_table_create(NULL
, hash
, equal
);
50 pointer_hash(const void *key
)
52 return _mesa_hash_pointer(key
);
57 pointer_equal(const void *a
, const void *b
)
64 util_hash_table_create_ptr_keys(void)
66 return _mesa_hash_table_create(NULL
, pointer_hash
, pointer_equal
);
70 static uint32_t hash_fd(const void *key
)
73 int fd
= pointer_to_intptr(key
);
78 return stat
.st_dev
^ stat
.st_ino
^ stat
.st_rdev
;
85 static bool equal_fd(const void *key1
, const void *key2
)
88 int fd1
= pointer_to_intptr(key1
);
89 int fd2
= pointer_to_intptr(key2
);
90 struct stat stat1
, stat2
;
95 return stat1
.st_dev
== stat2
.st_dev
&&
96 stat1
.st_ino
== stat2
.st_ino
&&
97 stat1
.st_rdev
== stat2
.st_rdev
;
105 util_hash_table_create_fd_keys(void)
107 return _mesa_hash_table_create(NULL
, hash_fd
, equal_fd
);
112 util_hash_table_set(struct hash_table
*ht
,
116 _mesa_hash_table_insert(ht
, key
, value
);
122 util_hash_table_get(struct hash_table
*ht
,
125 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, key
);
127 return entry
? entry
->data
: NULL
;
132 util_hash_table_remove(struct hash_table
*ht
,
135 _mesa_hash_table_remove_key(ht
, key
);
140 util_hash_table_clear(struct hash_table
*ht
)
142 _mesa_hash_table_clear(ht
, NULL
);
147 util_hash_table_foreach(struct hash_table
*ht
,
148 enum pipe_error (*callback
)
149 (void *key
, void *value
, void *data
),
152 hash_table_foreach(ht
, entry
) {
153 enum pipe_error error
= callback((void*)entry
->key
, entry
->data
, data
);
154 if (error
!= PIPE_OK
)
162 util_hash_table_count(struct hash_table
*ht
)
164 return _mesa_hash_table_num_entries(ht
);
169 util_hash_table_destroy(struct hash_table
*ht
)
171 _mesa_hash_table_destroy(ht
, NULL
);