aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/util/u_hash_table.c
blob: 9182da55800268a1ab15f34db1be03d85b068a18 (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
/**************************************************************************
 *
 * 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.
 *
 **************************************************************************/


#include "util/u_pointer.h"
#include "util/u_hash_table.h"

#if DETECT_OS_UNIX
#include <sys/stat.h>
#endif


static uint32_t
pointer_hash(const void *key)
{
   return _mesa_hash_pointer(key);
}


static bool
pointer_equal(const void *a, const void *b)
{
   return a == b;
}


struct hash_table *
util_hash_table_create_ptr_keys(void)
{
   return _mesa_hash_table_create(NULL, pointer_hash, pointer_equal);
}


static uint32_t hash_fd(const void *key)
{
#if DETECT_OS_UNIX
   int fd = pointer_to_intptr(key);
   struct stat stat;

   fstat(fd, &stat);

   return stat.st_dev ^ stat.st_ino ^ stat.st_rdev;
#else
   return 0;
#endif
}


static bool equal_fd(const void *key1, const void *key2)
{
#if DETECT_OS_UNIX
   int fd1 = pointer_to_intptr(key1);
   int fd2 = pointer_to_intptr(key2);
   struct stat stat1, stat2;

   fstat(fd1, &stat1);
   fstat(fd2, &stat2);

   return stat1.st_dev == stat2.st_dev &&
          stat1.st_ino == stat2.st_ino &&
          stat1.st_rdev == stat2.st_rdev;
#else
   return 0;
#endif
}


struct hash_table *
util_hash_table_create_fd_keys(void)
{
   return _mesa_hash_table_create(NULL, hash_fd, equal_fd);
}


void *
util_hash_table_get(struct hash_table *ht,
                    void *key)
{
   struct hash_entry *entry = _mesa_hash_table_search(ht, key);

   return entry ? entry->data : NULL;
}


enum pipe_error
util_hash_table_foreach(struct hash_table *ht,
                        enum pipe_error (*callback)
                        (void *key, void *value, void *data),
                        void *data)
{
   hash_table_foreach(ht, entry) {
      enum pipe_error error = callback((void*)entry->key, entry->data, data);
      if (error != PIPE_OK)
         return error;
   }
   return PIPE_OK;
}