1/*
2 * Copyright (C) 2013, 2014 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#ifndef CodeBlockSet_h
27#define CodeBlockSet_h
28
29#include "GCSegmentedArray.h"
30#include "HeapOperation.h"
31#include <wtf/HashSet.h>
32#include <wtf/Lock.h>
33#include <wtf/Noncopyable.h>
34#include <wtf/PassRefPtr.h>
35#include <wtf/PrintStream.h>
36#include <wtf/RefPtr.h>
37
38namespace JSC {
39
40class CodeBlock;
41class Heap;
42class JSCell;
43class SlotVisitor;
44
45// CodeBlockSet tracks all CodeBlocks. Every CodeBlock starts out with one
46// reference coming in from GC. The GC is responsible for freeing CodeBlocks
47// once they hasOneRef() and nobody is running code from that CodeBlock.
48
49class CodeBlockSet {
50 WTF_MAKE_NONCOPYABLE(CodeBlockSet);
51
52public:
53 CodeBlockSet();
54 ~CodeBlockSet();
55
56 void lastChanceToFinalize();
57
58 // Add a CodeBlock. This is only called by CodeBlock constructors.
59 void add(CodeBlock*);
60
61 // Clear all mark bits for all CodeBlocks.
62 void clearMarksForFullCollection();
63
64 // Mark a pointer that may be a CodeBlock that belongs to the set of DFG
65 // blocks. This is defined in CodeBlock.h.
66private:
67 void mark(const LockHolder&, CodeBlock* candidateCodeBlock);
68public:
69 void mark(const LockHolder&, void* candidateCodeBlock);
70
71 // Delete all code blocks that are only referenced by this set (i.e. owned
72 // by this set), and that have not been marked.
73 void deleteUnmarkedAndUnreferenced(HeapOperation);
74
75 // Add all currently executing CodeBlocks to the remembered set to be
76 // re-scanned during the next collection.
77 void writeBarrierCurrentlyExecutingCodeBlocks(Heap*);
78
79 bool contains(const LockHolder&, void* candidateCodeBlock);
80 Lock& getLock() { return m_lock; }
81
82 // Visits each CodeBlock in the heap until the visitor function returns true
83 // to indicate that it is done iterating, or until every CodeBlock has been
84 // visited.
85 template<typename Functor> void iterate(Functor& functor)
86 {
87 LockHolder locker(m_lock);
88 for (auto& codeBlock : m_oldCodeBlocks) {
89 bool done = functor(codeBlock);
90 if (done)
91 return;
92 }
93
94 for (auto& codeBlock : m_newCodeBlocks) {
95 bool done = functor(codeBlock);
96 if (done)
97 return;
98 }
99 }
100
101 void dump(PrintStream&) const;
102
103private:
104 void promoteYoungCodeBlocks(const LockHolder&);
105
106 HashSet<CodeBlock*> m_oldCodeBlocks;
107 HashSet<CodeBlock*> m_newCodeBlocks;
108 HashSet<CodeBlock*> m_currentlyExecuting;
109 Lock m_lock;
110};
111
112} // namespace JSC
113
114#endif // CodeBlockSet_h
115
116