| 1 | /* |
| 2 | * Copyright (C) 2016 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include <wtf/HashTable.h> |
| 29 | #include <wtf/PrintStream.h> |
| 30 | |
| 31 | namespace JSC { |
| 32 | |
| 33 | class JSCell; |
| 34 | |
| 35 | class VisitRaceKey { |
| 36 | public: |
| 37 | VisitRaceKey() { } |
| 38 | |
| 39 | VisitRaceKey(JSCell* cell, const char* raceName) |
| 40 | : m_cell(cell) |
| 41 | , m_raceName(raceName) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | VisitRaceKey(WTF::HashTableDeletedValueType) |
| 46 | : m_raceName(m_deletedValueRaceName) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | bool operator==(const VisitRaceKey& other) const |
| 51 | { |
| 52 | return m_cell == other.m_cell |
| 53 | && m_raceName == other.m_raceName; |
| 54 | } |
| 55 | |
| 56 | bool operator!=(const VisitRaceKey& other) const |
| 57 | { |
| 58 | return !(*this == other); |
| 59 | } |
| 60 | |
| 61 | explicit operator bool() const |
| 62 | { |
| 63 | return *this != VisitRaceKey(); |
| 64 | } |
| 65 | |
| 66 | void dump(PrintStream& out) const; |
| 67 | |
| 68 | JSCell* cell() const { return m_cell; } |
| 69 | const char* raceName() const { return m_raceName; } |
| 70 | |
| 71 | bool isHashTableDeletedValue() const |
| 72 | { |
| 73 | return *this == VisitRaceKey(WTF::HashTableDeletedValue); |
| 74 | } |
| 75 | |
| 76 | unsigned hash() const |
| 77 | { |
| 78 | return WTF::PtrHash<JSCell*>::hash(m_cell) ^ WTF::PtrHash<const char*>::hash(m_raceName); |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | static const char* m_deletedValueRaceName; |
| 83 | |
| 84 | JSCell* m_cell { nullptr }; |
| 85 | const char* m_raceName { nullptr }; |
| 86 | }; |
| 87 | |
| 88 | struct VisitRaceKeyHash { |
| 89 | static unsigned hash(const VisitRaceKey& key) { return key.hash(); } |
| 90 | static bool equal(const VisitRaceKey& a, const VisitRaceKey& b) { return a == b; } |
| 91 | static const bool safeToCompareToEmptyOrDeleted = true; |
| 92 | }; |
| 93 | |
| 94 | } // namespace JSC |
| 95 | |
| 96 | namespace WTF { |
| 97 | |
| 98 | template<typename T> struct DefaultHash; |
| 99 | template<> struct DefaultHash<JSC::VisitRaceKey> { |
| 100 | typedef JSC::VisitRaceKeyHash Hash; |
| 101 | }; |
| 102 | |
| 103 | template<typename T> struct HashTraits; |
| 104 | template<> struct HashTraits<JSC::VisitRaceKey> : SimpleClassHashTraits<JSC::VisitRaceKey> { }; |
| 105 | |
| 106 | } // namespace WTF |
| 107 | |
| 108 | |