1 | /* |
2 | * Copyright (C) 2017 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 | public: |
36 | StackShot() { } |
37 | |
38 | ALWAYS_INLINE StackShot(size_t size) |
39 | : m_size(size) |
40 | { |
41 | if (size) { |
42 | m_array = makeUniqueArray<void*>(size); |
43 | int intSize = size; |
44 | WTFGetBacktrace(m_array.get(), &intSize); |
45 | RELEASE_ASSERT(static_cast<size_t>(intSize) <= size); |
46 | m_size = intSize; |
47 | if (!m_size) |
48 | m_array = nullptr; |
49 | } |
50 | } |
51 | |
52 | StackShot(WTF::HashTableDeletedValueType) |
53 | : m_array(deletedValueArray()) |
54 | , m_size(0) |
55 | { |
56 | } |
57 | |
58 | StackShot& operator=(const StackShot& other) |
59 | { |
60 | auto newArray = makeUniqueArray<void*>(other.m_size); |
61 | for (size_t i = other.m_size; i--;) |
62 | newArray[i] = other.m_array[i]; |
63 | m_size = other.m_size; |
64 | m_array = WTFMove(newArray); |
65 | return *this; |
66 | } |
67 | |
68 | StackShot(const StackShot& other) |
69 | { |
70 | *this = other; |
71 | } |
72 | |
73 | void** array() const { return m_array.get(); } |
74 | size_t size() const { return m_size; } |
75 | |
76 | explicit operator bool() const { return !!m_array; } |
77 | |
78 | bool operator==(const StackShot& other) const |
79 | { |
80 | if (m_size != other.m_size) |
81 | return false; |
82 | |
83 | for (size_t i = m_size; i--;) { |
84 | if (m_array[i] != other.m_array[i]) |
85 | return false; |
86 | } |
87 | |
88 | return true; |
89 | } |
90 | |
91 | unsigned hash() const |
92 | { |
93 | unsigned result = m_size; |
94 | |
95 | for (size_t i = m_size; i--;) |
96 | result ^= PtrHash<void*>::hash(m_array[i]); |
97 | |
98 | return result; |
99 | } |
100 | |
101 | bool isHashTableDeletedValue() const |
102 | { |
103 | return !m_size && m_array.get() == deletedValueArray(); |
104 | } |
105 | |
106 | // Make Spectrum<> happy. |
107 | bool operator>(const StackShot&) const { return false; } |
108 | |
109 | private: |
110 | static void** deletedValueArray() { return bitwise_cast<void**>(static_cast<uintptr_t>(1)); } |
111 | |
112 | UniqueArray<void*> m_array; |
113 | size_t m_size { 0 }; |
114 | }; |
115 | |
116 | struct StackShotHash { |
117 | static unsigned hash(const StackShot& shot) { return shot.hash(); } |
118 | static bool equal(const StackShot& a, const StackShot& b) { return a == b; } |
119 | static const bool safeToCompareToEmptyOrDeleted = false; |
120 | }; |
121 | |
122 | template<typename T> struct DefaultHash; |
123 | template<> struct DefaultHash<StackShot> { |
124 | typedef StackShotHash Hash; |
125 | }; |
126 | |
127 | template<> struct HashTraits<StackShot> : SimpleClassHashTraits<StackShot> { }; |
128 | |
129 | } // namespace WTF |
130 | |
131 | |