1/*
2 * Copyright (C) 2011 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 CopiedSpace_h
27#define CopiedSpace_h
28
29#include "CopiedAllocator.h"
30#include "HeapOperation.h"
31#include "TinyBloomFilter.h"
32#include <wtf/Assertions.h>
33#include <wtf/CheckedBoolean.h>
34#include <wtf/DoublyLinkedList.h>
35#include <wtf/HashSet.h>
36#include <wtf/Lock.h>
37
38namespace JSC {
39
40class Heap;
41class CopiedBlock;
42
43class CopiedSpace {
44 friend class CopyVisitor;
45 friend class Heap;
46 friend class SlotVisitor;
47 friend class JIT;
48public:
49 CopiedSpace(Heap*);
50 ~CopiedSpace();
51 void init();
52
53 CheckedBoolean tryAllocate(size_t, void**);
54 CheckedBoolean tryReallocate(void**, size_t, size_t);
55
56 CopiedAllocator& allocator() { return m_allocator; }
57
58 void didStartFullCollection();
59
60 template <HeapOperation collectionType>
61 void startedCopying();
62 void doneCopying();
63 bool isInCopyPhase() const { return m_inCopyingPhase; }
64
65 void pin(CopiedBlock*);
66 bool isPinned(void*);
67
68 bool contains(CopiedBlock*);
69 bool contains(void*, CopiedBlock*&);
70
71 void pinIfNecessary(void* pointer);
72
73 size_t size();
74 size_t capacity();
75
76 bool isPagedOut(double deadline);
77 bool shouldDoCopyPhase() const { return m_shouldDoCopyPhase; }
78
79 static CopiedBlock* blockFor(void*);
80
81 Heap* heap() const { return m_heap; }
82
83 size_t takeBytesRemovedFromOldSpaceDueToReallocation()
84 {
85 size_t result = 0;
86 std::swap(m_bytesRemovedFromOldSpaceDueToReallocation, result);
87 return result;
88 }
89
90private:
91 static bool isOversize(size_t);
92
93 JS_EXPORT_PRIVATE CheckedBoolean tryAllocateSlowCase(size_t, void**);
94 CheckedBoolean tryAllocateOversize(size_t, void**);
95 CheckedBoolean tryReallocateOversize(void**, size_t, size_t);
96
97 void allocateBlock();
98 CopiedBlock* allocateBlockForCopyingPhase();
99
100 void doneFillingBlock(CopiedBlock*, CopiedBlock**);
101 void recycleEvacuatedBlock(CopiedBlock*, HeapOperation collectionType);
102 void recycleBorrowedBlock(CopiedBlock*);
103
104 Heap* m_heap;
105
106 CopiedAllocator m_allocator;
107
108 HashSet<CopiedBlock*> m_blockSet;
109
110 Lock m_toSpaceLock;
111
112 struct CopiedGeneration {
113 CopiedGeneration()
114 : toSpace(0)
115 , fromSpace(0)
116 {
117 }
118
119 DoublyLinkedList<CopiedBlock>* toSpace;
120 DoublyLinkedList<CopiedBlock>* fromSpace;
121
122 DoublyLinkedList<CopiedBlock> blocks1;
123 DoublyLinkedList<CopiedBlock> blocks2;
124 DoublyLinkedList<CopiedBlock> oversizeBlocks;
125
126 TinyBloomFilter blockFilter;
127 };
128
129 CopiedGeneration m_oldGen;
130 CopiedGeneration m_newGen;
131
132 bool m_inCopyingPhase;
133 bool m_shouldDoCopyPhase;
134
135 Lock m_loanedBlocksLock;
136 size_t m_numberOfLoanedBlocks;
137
138 size_t m_bytesRemovedFromOldSpaceDueToReallocation;
139
140 static const size_t s_maxAllocationSize = CopiedBlock::blockSize / 2;
141 static const size_t s_blockMask = ~(CopiedBlock::blockSize - 1);
142};
143
144} // namespace JSC
145
146#endif
147