1 | /* |
2 | * Copyright (C) 2017-2019 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. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include <wtf/Assertions.h> |
29 | #include <wtf/HashTraits.h> |
30 | #include <wtf/UniqueArray.h> |
31 | |
32 | namespace WTF { |
33 | |
34 | class StackShot { |
35 | WTF_MAKE_FAST_ALLOCATED; |
36 | public: |
37 | StackShot() { } |
38 | |
39 | ALWAYS_INLINE StackShot(size_t size) |
40 | : m_size(size) |
41 | { |
42 | if (size) { |
43 | m_array = makeUniqueArray<void*>(size); |
44 | int intSize = size; |
45 | WTFGetBacktrace(m_array.get(), &intSize); |
46 | RELEASE_ASSERT(static_cast<size_t>(intSize) <= size); |
47 | m_size = intSize; |
48 | if (!m_size) |
49 | m_array = nullptr; |
50 | } |
51 | } |
52 | |
53 | StackShot(WTF::HashTableDeletedValueType) |
54 | : m_array(deletedValueArray()) |
55 | , m_size(0) |
56 | { |
57 | } |
58 | |
59 | StackShot& operator=(const StackShot& other) |
60 | { |
61 | auto newArray = makeUniqueArray<void*>(other.m_size); |
62 | for (size_t i = other.m_size; i--;) |
63 | newArray[i] = other.m_array[i]; |
64 | m_size = other.m_size; |
65 | m_array = WTFMove(newArray); |
66 | return *this; |
67 | } |
68 | |
69 | StackShot(const StackShot& other) |
70 | { |
71 | *this = other; |
72 | } |
73 | |
74 | void** array() const { return m_array.get(); } |
75 | size_t size() const { return m_size; } |
76 | |
77 | explicit operator bool() const { return !!m_array; } |
78 | |
79 | bool operator==(const StackShot& other) const |
80 | { |
81 | if (m_size != other.m_size) |
82 | return false; |
83 | |
84 | for (size_t i = m_size; i--;) { |
85 | if (m_array[i] != other.m_array[i]) |
86 | return false; |
87 | } |
88 | |
89 | return true; |
90 | } |
91 | |
92 | unsigned hash() const |
93 | { |
94 | unsigned result = m_size; |
95 | |
96 | for (size_t i = m_size; i--;) |
97 | result ^= PtrHash<void*>::hash(m_array[i]); |
98 | |
99 | return result; |
100 | } |
101 | |
102 | bool isHashTableDeletedValue() const |
103 | { |
104 | return !m_size && m_array.get() == deletedValueArray(); |
105 | } |
106 | |
107 | // Make Spectrum<> happy. |
108 | bool operator>(const StackShot&) const { return false; } |
109 | |
110 | private: |
111 | static void** deletedValueArray() { return bitwise_cast<void**>(static_cast<uintptr_t>(1)); } |
112 | |
113 | UniqueArray<void*> m_array; |
114 | size_t m_size { 0 }; |
115 | }; |
116 | |
117 | struct StackShotHash { |
118 | static unsigned hash(const StackShot& shot) { return shot.hash(); } |
119 | static bool equal(const StackShot& a, const StackShot& b) { return a == b; } |
120 | static constexpr bool safeToCompareToEmptyOrDeleted = false; |
121 | }; |
122 | |
123 | template<typename T> struct DefaultHash; |
124 | template<> struct DefaultHash<StackShot> { |
125 | typedef StackShotHash Hash; |
126 | }; |
127 | |
128 | template<> struct HashTraits<StackShot> : SimpleClassHashTraits<StackShot> { }; |
129 | |
130 | } // namespace WTF |
131 | |
132 | |