17ec681f3Smrg/**************************************************************************
27ec681f3Smrg *
37ec681f3Smrg * Copyright 2008 VMware, Inc.
47ec681f3Smrg * All Rights Reserved.
57ec681f3Smrg *
67ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
77ec681f3Smrg * copy of this software and associated documentation files (the
87ec681f3Smrg * "Software"), to deal in the Software without restriction, including
97ec681f3Smrg * without limitation the rights to use, copy, modify, merge, publish,
107ec681f3Smrg * distribute, sub license, and/or sell copies of the Software, and to
117ec681f3Smrg * permit persons to whom the Software is furnished to do so, subject to
127ec681f3Smrg * the following conditions:
137ec681f3Smrg *
147ec681f3Smrg * The above copyright notice and this permission notice (including the
157ec681f3Smrg * next paragraph) shall be included in all copies or substantial portions
167ec681f3Smrg * of the Software.
177ec681f3Smrg *
187ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
197ec681f3Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
207ec681f3Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
217ec681f3Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
227ec681f3Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
237ec681f3Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
247ec681f3Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
257ec681f3Smrg *
267ec681f3Smrg **************************************************************************/
277ec681f3Smrg
287ec681f3Smrg
297ec681f3Smrg#include "util/u_pointer.h"
307ec681f3Smrg#include "util/u_hash_table.h"
317ec681f3Smrg
327ec681f3Smrg#if DETECT_OS_UNIX
337ec681f3Smrg#include <sys/stat.h>
347ec681f3Smrg#endif
357ec681f3Smrg
367ec681f3Smrg
377ec681f3Smrgstatic uint32_t
387ec681f3Smrgpointer_hash(const void *key)
397ec681f3Smrg{
407ec681f3Smrg   return _mesa_hash_pointer(key);
417ec681f3Smrg}
427ec681f3Smrg
437ec681f3Smrg
447ec681f3Smrgstatic bool
457ec681f3Smrgpointer_equal(const void *a, const void *b)
467ec681f3Smrg{
477ec681f3Smrg   return a == b;
487ec681f3Smrg}
497ec681f3Smrg
507ec681f3Smrg
517ec681f3Smrgstruct hash_table *
527ec681f3Smrgutil_hash_table_create_ptr_keys(void)
537ec681f3Smrg{
547ec681f3Smrg   return _mesa_hash_table_create(NULL, pointer_hash, pointer_equal);
557ec681f3Smrg}
567ec681f3Smrg
577ec681f3Smrg
587ec681f3Smrgstatic uint32_t hash_fd(const void *key)
597ec681f3Smrg{
607ec681f3Smrg#if DETECT_OS_UNIX
617ec681f3Smrg   int fd = pointer_to_intptr(key);
627ec681f3Smrg   struct stat stat;
637ec681f3Smrg
647ec681f3Smrg   fstat(fd, &stat);
657ec681f3Smrg
667ec681f3Smrg   return stat.st_dev ^ stat.st_ino ^ stat.st_rdev;
677ec681f3Smrg#else
687ec681f3Smrg   return 0;
697ec681f3Smrg#endif
707ec681f3Smrg}
717ec681f3Smrg
727ec681f3Smrg
737ec681f3Smrgstatic bool equal_fd(const void *key1, const void *key2)
747ec681f3Smrg{
757ec681f3Smrg#if DETECT_OS_UNIX
767ec681f3Smrg   int fd1 = pointer_to_intptr(key1);
777ec681f3Smrg   int fd2 = pointer_to_intptr(key2);
787ec681f3Smrg   struct stat stat1, stat2;
797ec681f3Smrg
807ec681f3Smrg   fstat(fd1, &stat1);
817ec681f3Smrg   fstat(fd2, &stat2);
827ec681f3Smrg
837ec681f3Smrg   return stat1.st_dev == stat2.st_dev &&
847ec681f3Smrg          stat1.st_ino == stat2.st_ino &&
857ec681f3Smrg          stat1.st_rdev == stat2.st_rdev;
867ec681f3Smrg#else
877ec681f3Smrg   return 0;
887ec681f3Smrg#endif
897ec681f3Smrg}
907ec681f3Smrg
917ec681f3Smrg
927ec681f3Smrgstruct hash_table *
937ec681f3Smrgutil_hash_table_create_fd_keys(void)
947ec681f3Smrg{
957ec681f3Smrg   return _mesa_hash_table_create(NULL, hash_fd, equal_fd);
967ec681f3Smrg}
977ec681f3Smrg
987ec681f3Smrg
997ec681f3Smrgvoid *
1007ec681f3Smrgutil_hash_table_get(struct hash_table *ht,
1017ec681f3Smrg                    void *key)
1027ec681f3Smrg{
1037ec681f3Smrg   struct hash_entry *entry = _mesa_hash_table_search(ht, key);
1047ec681f3Smrg
1057ec681f3Smrg   return entry ? entry->data : NULL;
1067ec681f3Smrg}
1077ec681f3Smrg
1087ec681f3Smrg
1097ec681f3Smrgenum pipe_error
1107ec681f3Smrgutil_hash_table_foreach(struct hash_table *ht,
1117ec681f3Smrg                        enum pipe_error (*callback)
1127ec681f3Smrg                        (void *key, void *value, void *data),
1137ec681f3Smrg                        void *data)
1147ec681f3Smrg{
1157ec681f3Smrg   hash_table_foreach(ht, entry) {
1167ec681f3Smrg      enum pipe_error error = callback((void*)entry->key, entry->data, data);
1177ec681f3Smrg      if (error != PIPE_OK)
1187ec681f3Smrg         return error;
1197ec681f3Smrg   }
1207ec681f3Smrg   return PIPE_OK;
1217ec681f3Smrg}
122