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/Noncopyable.h>
33#include <wtf/PassRefPtr.h>
34#include <wtf/PrintStream.h>
35#include <wtf/RefPtr.h>
36
37namespace JSC {
38
39class BlockAllocator;
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(BlockAllocator&);
54    ~CodeBlockSet();
55
56    // Add a CodeBlock. This is only called by CodeBlock constructors.
57    void add(PassRefPtr<CodeBlock>);
58
59    // Clear mark bits for certain CodeBlocks depending on the type of collection.
60    void clearMarksForEdenCollection(const Vector<const JSCell*>&);
61
62    // Clear all mark bits for all CodeBlocks.
63    void clearMarksForFullCollection();
64
65    // Mark a pointer that may be a CodeBlock that belongs to the set of DFG
66    // blocks. This is defined in CodeBlock.h.
67    void mark(CodeBlock* candidateCodeBlock);
68    void mark(void* candidateCodeBlock);
69
70    // Delete all code blocks that are only referenced by this set (i.e. owned
71    // by this set), and that have not been marked.
72    void deleteUnmarkedAndUnreferenced(HeapOperation);
73
74    void remove(CodeBlock*);
75
76    // Trace all marked code blocks. The CodeBlock is free to make use of
77    // mayBeExecuting.
78    void traceMarked(SlotVisitor&);
79
80    // Add all currently executing CodeBlocks to the remembered set to be
81    // re-scanned during the next collection.
82    void rememberCurrentlyExecutingCodeBlocks(Heap*);
83
84    // Visits each CodeBlock in the heap until the visitor function returns true
85    // to indicate that it is done iterating, or until every CodeBlock has been
86    // visited.
87    template<typename Functor> void iterate(Functor& functor)
88    {
89        for (auto& codeBlock : m_oldCodeBlocks) {
90            bool done = functor(codeBlock);
91            if (done)
92                return;
93        }
94
95        for (auto& codeBlock : m_newCodeBlocks) {
96            bool done = functor(codeBlock);
97            if (done)
98                return;
99        }
100    }
101
102    void dump(PrintStream&) const;
103
104private:
105    void clearMarksForCodeBlocksInRememberedExecutables(const Vector<const JSCell*>&);
106    void promoteYoungCodeBlocks();
107
108    // This is not a set of RefPtr<CodeBlock> because we need to be able to find
109    // arbitrary bogus pointers. I could have written a thingy that had peek types
110    // and all, but that seemed like overkill.
111    HashSet<CodeBlock*> m_oldCodeBlocks;
112    HashSet<CodeBlock*> m_newCodeBlocks;
113    GCSegmentedArray<CodeBlock*> m_currentlyExecuting;
114};
115
116} // namespace JSC
117
118#endif // CodeBlockSet_h
119
120