|
1 /* |
|
2 * Copyright (C) 2010 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 #ifndef JSDebugWrapperSet_h |
|
27 #define JSDebugWrapperSet_h |
|
28 |
|
29 #include "JSDOMWrapper.h" |
|
30 #include <wtf/HashSet.h> |
|
31 #include <wtf/Noncopyable.h> |
|
32 |
|
33 namespace WebCore { |
|
34 |
|
35 // For debugging, keep a set of wrappers currently cached, and check that |
|
36 // all are uncached before they are destroyed. This helps us catch bugs like: |
|
37 // - wrappers being deleted without being removed from the cache |
|
38 // - wrappers being cached twice |
|
39 |
|
40 class JSDebugWrapperSet : public Noncopyable { |
|
41 friend class WTF::ThreadSpecific<JSDebugWrapperSet>; |
|
42 public: |
|
43 static JSDebugWrapperSet& shared(); |
|
44 |
|
45 void add(DOMObject* object) { m_wrapperSet.add(object); } |
|
46 void remove(DOMObject* object) { m_wrapperSet.remove(object); } |
|
47 bool contains(DOMObject* object) const { return m_wrapperSet.contains(object); } |
|
48 |
|
49 static void willCacheWrapper(DOMObject*); |
|
50 static void didUncacheWrapper(DOMObject*); |
|
51 |
|
52 private: |
|
53 JSDebugWrapperSet(); |
|
54 |
|
55 HashSet<DOMObject*> m_wrapperSet; |
|
56 }; |
|
57 |
|
58 #ifdef NDEBUG |
|
59 |
|
60 inline void JSDebugWrapperSet::willCacheWrapper(DOMObject*) |
|
61 { |
|
62 } |
|
63 |
|
64 inline void JSDebugWrapperSet::didUncacheWrapper(DOMObject*) |
|
65 { |
|
66 } |
|
67 |
|
68 #else |
|
69 |
|
70 inline void JSDebugWrapperSet::willCacheWrapper(DOMObject* wrapper) |
|
71 { |
|
72 ASSERT(!JSDebugWrapperSet::shared().contains(wrapper)); |
|
73 JSDebugWrapperSet::shared().add(wrapper); |
|
74 } |
|
75 |
|
76 inline void JSDebugWrapperSet::didUncacheWrapper(DOMObject* wrapper) |
|
77 { |
|
78 if (!wrapper) |
|
79 return; |
|
80 ASSERT(JSDebugWrapperSet::shared().contains(wrapper)); |
|
81 JSDebugWrapperSet::shared().remove(wrapper); |
|
82 } |
|
83 |
|
84 #endif |
|
85 |
|
86 } // namespace WebCore |
|
87 |
|
88 #endif // JSDebugWrapperSet_h |