1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2008 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "util/u_pointer.h"
30bf215546Sopenharmony_ci#include "util/u_hash_table.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#if DETECT_OS_UNIX
33bf215546Sopenharmony_ci#include <sys/stat.h>
34bf215546Sopenharmony_ci#endif
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistatic uint32_t
38bf215546Sopenharmony_cipointer_hash(const void *key)
39bf215546Sopenharmony_ci{
40bf215546Sopenharmony_ci   return _mesa_hash_pointer(key);
41bf215546Sopenharmony_ci}
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_cistatic bool
45bf215546Sopenharmony_cipointer_equal(const void *a, const void *b)
46bf215546Sopenharmony_ci{
47bf215546Sopenharmony_ci   return a == b;
48bf215546Sopenharmony_ci}
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_cistruct hash_table *
52bf215546Sopenharmony_ciutil_hash_table_create_ptr_keys(void)
53bf215546Sopenharmony_ci{
54bf215546Sopenharmony_ci   return _mesa_hash_table_create(NULL, pointer_hash, pointer_equal);
55bf215546Sopenharmony_ci}
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_cistatic uint32_t hash_fd(const void *key)
59bf215546Sopenharmony_ci{
60bf215546Sopenharmony_ci#if DETECT_OS_UNIX
61bf215546Sopenharmony_ci   int fd = pointer_to_intptr(key);
62bf215546Sopenharmony_ci   struct stat stat;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   fstat(fd, &stat);
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   return stat.st_dev ^ stat.st_ino ^ stat.st_rdev;
67bf215546Sopenharmony_ci#else
68bf215546Sopenharmony_ci   return 0;
69bf215546Sopenharmony_ci#endif
70bf215546Sopenharmony_ci}
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_cistatic bool equal_fd(const void *key1, const void *key2)
74bf215546Sopenharmony_ci{
75bf215546Sopenharmony_ci#if DETECT_OS_UNIX
76bf215546Sopenharmony_ci   int fd1 = pointer_to_intptr(key1);
77bf215546Sopenharmony_ci   int fd2 = pointer_to_intptr(key2);
78bf215546Sopenharmony_ci   struct stat stat1, stat2;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   fstat(fd1, &stat1);
81bf215546Sopenharmony_ci   fstat(fd2, &stat2);
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   return stat1.st_dev == stat2.st_dev &&
84bf215546Sopenharmony_ci          stat1.st_ino == stat2.st_ino &&
85bf215546Sopenharmony_ci          stat1.st_rdev == stat2.st_rdev;
86bf215546Sopenharmony_ci#else
87bf215546Sopenharmony_ci   return 0;
88bf215546Sopenharmony_ci#endif
89bf215546Sopenharmony_ci}
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_cistruct hash_table *
93bf215546Sopenharmony_ciutil_hash_table_create_fd_keys(void)
94bf215546Sopenharmony_ci{
95bf215546Sopenharmony_ci   return _mesa_hash_table_create(NULL, hash_fd, equal_fd);
96bf215546Sopenharmony_ci}
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_civoid *
100bf215546Sopenharmony_ciutil_hash_table_get(struct hash_table *ht,
101bf215546Sopenharmony_ci                    void *key)
102bf215546Sopenharmony_ci{
103bf215546Sopenharmony_ci   struct hash_entry *entry = _mesa_hash_table_search(ht, key);
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   return entry ? entry->data : NULL;
106bf215546Sopenharmony_ci}
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_cienum pipe_error
110bf215546Sopenharmony_ciutil_hash_table_foreach(struct hash_table *ht,
111bf215546Sopenharmony_ci                        enum pipe_error (*callback)
112bf215546Sopenharmony_ci                        (void *key, void *value, void *data),
113bf215546Sopenharmony_ci                        void *data)
114bf215546Sopenharmony_ci{
115bf215546Sopenharmony_ci   hash_table_foreach(ht, entry) {
116bf215546Sopenharmony_ci      enum pipe_error error = callback((void*)entry->key, entry->data, data);
117bf215546Sopenharmony_ci      if (error != PIPE_OK)
118bf215546Sopenharmony_ci         return error;
119bf215546Sopenharmony_ci   }
120bf215546Sopenharmony_ci   return PIPE_OK;
121bf215546Sopenharmony_ci}
122