ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "api.h" |
| 31 | #include "global-handles.h" |
| 32 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 33 | #include "vm-state-inl.h" |
| 34 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 35 | namespace v8 { |
| 36 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 38 | |
| 39 | ObjectGroup::~ObjectGroup() { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 40 | if (info != NULL) info->Dispose(); |
| 41 | delete[] objects; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | ImplicitRefGroup::~ImplicitRefGroup() { |
| 46 | delete[] children; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 50 | class GlobalHandles::Node { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | public: |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 52 | // State transition diagram: |
| 53 | // FREE -> NORMAL <-> WEAK -> PENDING -> NEAR_DEATH -> { NORMAL, WEAK, FREE } |
| 54 | enum State { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 55 | FREE = 0, |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 56 | NORMAL, // Normal global handle. |
| 57 | WEAK, // Flagged as weak but not yet finalized. |
| 58 | PENDING, // Has been recognized as only reachable by weak handles. |
| 59 | NEAR_DEATH // Callback has informed the handle is near death. |
| 60 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 61 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 62 | // Maps handle location (slot) to the containing node. |
| 63 | static Node* FromLocation(Object** location) { |
| 64 | ASSERT(OFFSET_OF(Node, object_) == 0); |
| 65 | return reinterpret_cast<Node*>(location); |
| 66 | } |
| 67 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 68 | Node() { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 69 | ASSERT(OFFSET_OF(Node, class_id_) == Internals::kNodeClassIdOffset); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 70 | ASSERT(OFFSET_OF(Node, flags_) == Internals::kNodeFlagsOffset); |
| 71 | STATIC_ASSERT(static_cast<int>(NodeState::kMask) == |
| 72 | Internals::kNodeStateMask); |
| 73 | STATIC_ASSERT(WEAK == Internals::kNodeStateIsWeakValue); |
| 74 | STATIC_ASSERT(NEAR_DEATH == Internals::kNodeStateIsNearDeathValue); |
| 75 | STATIC_ASSERT(static_cast<int>(IsIndependent::kShift) == |
| 76 | Internals::kNodeIsIndependentShift); |
| 77 | STATIC_ASSERT(static_cast<int>(IsPartiallyDependent::kShift) == |
| 78 | Internals::kNodeIsPartiallyDependentShift); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 79 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 80 | |
danno@chromium.org | c99cd48 | 2013-03-21 15:26:42 +0000 | [diff] [blame] | 81 | #ifdef ENABLE_EXTRA_CHECKS |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 82 | ~Node() { |
| 83 | // TODO(1428): if it's a weak handle we should have invoked its callback. |
| 84 | // Zap the values for eager trapping. |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 85 | object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 86 | class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId; |
| 87 | index_ = 0; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 88 | set_independent(false); |
| 89 | set_partially_dependent(false); |
| 90 | set_in_new_space_list(false); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 91 | parameter_or_next_free_.next_free = NULL; |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 92 | weak_reference_callback_ = NULL; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 93 | } |
| 94 | #endif |
| 95 | |
| 96 | void Initialize(int index, Node** first_free) { |
| 97 | index_ = static_cast<uint8_t>(index); |
| 98 | ASSERT(static_cast<int>(index_) == index); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 99 | set_state(FREE); |
| 100 | set_in_new_space_list(false); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 101 | parameter_or_next_free_.next_free = *first_free; |
| 102 | *first_free = this; |
| 103 | } |
| 104 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 105 | void Acquire(Object* object) { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 106 | ASSERT(state() == FREE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | object_ = object; |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 108 | class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 109 | set_independent(false); |
| 110 | set_partially_dependent(false); |
| 111 | set_state(NORMAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 112 | parameter_or_next_free_.parameter = NULL; |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 113 | weak_reference_callback_ = NULL; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 114 | IncreaseBlockUses(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | } |
| 116 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 117 | void Release() { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 118 | ASSERT(state() != FREE); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 119 | set_state(FREE); |
danno@chromium.org | c99cd48 | 2013-03-21 15:26:42 +0000 | [diff] [blame] | 120 | #ifdef ENABLE_EXTRA_CHECKS |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 121 | // Zap the values for eager trapping. |
danno@chromium.org | c99cd48 | 2013-03-21 15:26:42 +0000 | [diff] [blame] | 122 | object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 123 | class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId; |
| 124 | set_independent(false); |
| 125 | set_partially_dependent(false); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 126 | weak_reference_callback_ = NULL; |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 127 | #endif |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 128 | DecreaseBlockUses(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 129 | } |
| 130 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 131 | // Object slot accessors. |
| 132 | Object* object() const { return object_; } |
| 133 | Object** location() { return &object_; } |
| 134 | Handle<Object> handle() { return Handle<Object>(location()); } |
| 135 | |
| 136 | // Wrapper class ID accessors. |
| 137 | bool has_wrapper_class_id() const { |
| 138 | return class_id_ != v8::HeapProfiler::kPersistentHandleNoClassId; |
| 139 | } |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 140 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 141 | uint16_t wrapper_class_id() const { return class_id_; } |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 142 | |
| 143 | // State and flag accessors. |
| 144 | |
| 145 | State state() const { |
| 146 | return NodeState::decode(flags_); |
| 147 | } |
| 148 | void set_state(State state) { |
| 149 | flags_ = NodeState::update(flags_, state); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 150 | } |
| 151 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 152 | bool is_independent() { |
| 153 | return IsIndependent::decode(flags_); |
| 154 | } |
| 155 | void set_independent(bool v) { |
| 156 | flags_ = IsIndependent::update(flags_, v); |
| 157 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 158 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 159 | bool is_partially_dependent() { |
| 160 | return IsPartiallyDependent::decode(flags_); |
| 161 | } |
| 162 | void set_partially_dependent(bool v) { |
| 163 | flags_ = IsPartiallyDependent::update(flags_, v); |
| 164 | } |
| 165 | |
| 166 | bool is_in_new_space_list() { |
| 167 | return IsInNewSpaceList::decode(flags_); |
| 168 | } |
| 169 | void set_in_new_space_list(bool v) { |
| 170 | flags_ = IsInNewSpaceList::update(flags_, v); |
| 171 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 172 | |
| 173 | bool IsNearDeath() const { |
| 174 | // Check for PENDING to ensure correct answer when processing callbacks. |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 175 | return state() == PENDING || state() == NEAR_DEATH; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 176 | } |
| 177 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 178 | bool IsWeak() const { return state() == WEAK; } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 179 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 180 | bool IsRetainer() const { return state() != FREE; } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 181 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 182 | bool IsStrongRetainer() const { return state() == NORMAL; } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 183 | |
| 184 | bool IsWeakRetainer() const { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 185 | return state() == WEAK || state() == PENDING || state() == NEAR_DEATH; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void MarkPending() { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 189 | ASSERT(state() == WEAK); |
| 190 | set_state(PENDING); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // Independent flag accessors. |
| 194 | void MarkIndependent() { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 195 | ASSERT(state() != FREE); |
| 196 | set_independent(true); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 197 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 198 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 199 | void MarkPartiallyDependent() { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 200 | ASSERT(state() != FREE); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 201 | if (GetGlobalHandles()->isolate()->heap()->InNewSpace(object_)) { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 202 | set_partially_dependent(true); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 205 | void clear_partially_dependent() { set_partially_dependent(false); } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 206 | |
| 207 | // Callback accessor. |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 208 | // TODO(svenpanne) Re-enable or nuke later. |
| 209 | // WeakReferenceCallback callback() { return callback_; } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 210 | |
| 211 | // Callback parameter accessors. |
| 212 | void set_parameter(void* parameter) { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 213 | ASSERT(state() != FREE); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 214 | parameter_or_next_free_.parameter = parameter; |
| 215 | } |
| 216 | void* parameter() const { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 217 | ASSERT(state() != FREE); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 218 | return parameter_or_next_free_.parameter; |
| 219 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 220 | |
| 221 | // Accessors for next free node in the free list. |
| 222 | Node* next_free() { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 223 | ASSERT(state() == FREE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 224 | return parameter_or_next_free_.next_free; |
| 225 | } |
| 226 | void set_next_free(Node* value) { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 227 | ASSERT(state() == FREE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 228 | parameter_or_next_free_.next_free = value; |
| 229 | } |
| 230 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 231 | void MakeWeak(void* parameter, |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 232 | RevivableCallback weak_reference_callback) { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 233 | ASSERT(state() != FREE); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 234 | set_state(WEAK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 235 | set_parameter(parameter); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 236 | weak_reference_callback_ = weak_reference_callback; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 237 | } |
| 238 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 239 | void ClearWeakness() { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 240 | ASSERT(state() != FREE); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 241 | set_state(NORMAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 242 | set_parameter(NULL); |
| 243 | } |
| 244 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 245 | bool PostGarbageCollectionProcessing(Isolate* isolate) { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 246 | if (state() != Node::PENDING) return false; |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 247 | if (weak_reference_callback_ == NULL) { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 248 | Release(); |
fschneider@chromium.org | ed78ffd | 2010-07-21 11:05:19 +0000 | [diff] [blame] | 249 | return false; |
| 250 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 251 | void* par = parameter(); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 252 | set_state(NEAR_DEATH); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | set_parameter(NULL); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 254 | |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 255 | Object** object = location(); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 256 | { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 257 | // Check that we are not passing a finalized external string to |
| 258 | // the callback. |
| 259 | ASSERT(!object_->IsExternalAsciiString() || |
| 260 | ExternalAsciiString::cast(object_)->resource() != NULL); |
| 261 | ASSERT(!object_->IsExternalTwoByteString() || |
| 262 | ExternalTwoByteString::cast(object_)->resource() != NULL); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 263 | // Leaving V8. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 264 | VMState<EXTERNAL> state(isolate); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 265 | weak_reference_callback_(reinterpret_cast<v8::Isolate*>(isolate), |
| 266 | reinterpret_cast<Persistent<Value>*>(&object), |
jkummerow@chromium.org | 4c54a2a | 2013-03-19 17:51:30 +0000 | [diff] [blame] | 267 | par); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 268 | } |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 269 | // Absence of explicit cleanup or revival of weak handle |
fschneider@chromium.org | ed78ffd | 2010-07-21 11:05:19 +0000 | [diff] [blame] | 270 | // in most of the cases would lead to memory leak. |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 271 | ASSERT(state() != NEAR_DEATH); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 272 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 273 | } |
| 274 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 275 | private: |
| 276 | inline NodeBlock* FindBlock(); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 277 | inline GlobalHandles* GetGlobalHandles(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 278 | inline void IncreaseBlockUses(); |
| 279 | inline void DecreaseBlockUses(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 280 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 281 | // Storage for object pointer. |
| 282 | // Placed first to avoid offset computation. |
| 283 | Object* object_; |
| 284 | |
| 285 | // Next word stores class_id, index, state, and independent. |
| 286 | // Note: the most aligned fields should go first. |
| 287 | |
| 288 | // Wrapper class ID. |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 289 | uint16_t class_id_; |
| 290 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 291 | // Index in the containing handle block. |
| 292 | uint8_t index_; |
| 293 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 294 | // This stores three flags (independent, partially_dependent and |
| 295 | // in_new_space_list) and a State. |
| 296 | class NodeState: public BitField<State, 0, 4> {}; |
| 297 | class IsIndependent: public BitField<bool, 4, 1> {}; |
| 298 | class IsPartiallyDependent: public BitField<bool, 5, 1> {}; |
| 299 | class IsInNewSpaceList: public BitField<bool, 6, 1> {}; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 300 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 301 | uint8_t flags_; |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 302 | |
jkummerow@chromium.org | 4c54a2a | 2013-03-19 17:51:30 +0000 | [diff] [blame] | 303 | // Handle specific callback - might be a weak reference in disguise. |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 304 | RevivableCallback weak_reference_callback_; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 305 | |
| 306 | // Provided data for callback. In FREE state, this is used for |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 307 | // the free list link. |
| 308 | union { |
| 309 | void* parameter; |
| 310 | Node* next_free; |
| 311 | } parameter_or_next_free_; |
| 312 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 313 | DISALLOW_COPY_AND_ASSIGN(Node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 317 | class GlobalHandles::NodeBlock { |
| 318 | public: |
| 319 | static const int kSize = 256; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 320 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 321 | explicit NodeBlock(GlobalHandles* global_handles, NodeBlock* next) |
| 322 | : next_(next), |
| 323 | used_nodes_(0), |
| 324 | next_used_(NULL), |
| 325 | prev_used_(NULL), |
| 326 | global_handles_(global_handles) {} |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 327 | |
| 328 | void PutNodesOnFreeList(Node** first_free) { |
| 329 | for (int i = kSize - 1; i >= 0; --i) { |
| 330 | nodes_[i].Initialize(i, first_free); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | Node* node_at(int index) { |
| 335 | ASSERT(0 <= index && index < kSize); |
| 336 | return &nodes_[index]; |
| 337 | } |
| 338 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 339 | void IncreaseUses() { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 340 | ASSERT(used_nodes_ < kSize); |
| 341 | if (used_nodes_++ == 0) { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 342 | NodeBlock* old_first = global_handles_->first_used_block_; |
| 343 | global_handles_->first_used_block_ = this; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 344 | next_used_ = old_first; |
| 345 | prev_used_ = NULL; |
| 346 | if (old_first == NULL) return; |
| 347 | old_first->prev_used_ = this; |
| 348 | } |
| 349 | } |
| 350 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 351 | void DecreaseUses() { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 352 | ASSERT(used_nodes_ > 0); |
| 353 | if (--used_nodes_ == 0) { |
| 354 | if (next_used_ != NULL) next_used_->prev_used_ = prev_used_; |
| 355 | if (prev_used_ != NULL) prev_used_->next_used_ = next_used_; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 356 | if (this == global_handles_->first_used_block_) { |
| 357 | global_handles_->first_used_block_ = next_used_; |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 360 | } |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 361 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 362 | GlobalHandles* global_handles() { return global_handles_; } |
| 363 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 364 | // Next block in the list of all blocks. |
| 365 | NodeBlock* next() const { return next_; } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 366 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 367 | // Next/previous block in the list of blocks with used nodes. |
| 368 | NodeBlock* next_used() const { return next_used_; } |
| 369 | NodeBlock* prev_used() const { return prev_used_; } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 370 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 371 | private: |
| 372 | Node nodes_[kSize]; |
| 373 | NodeBlock* const next_; |
| 374 | int used_nodes_; |
| 375 | NodeBlock* next_used_; |
| 376 | NodeBlock* prev_used_; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 377 | GlobalHandles* global_handles_; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 378 | }; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 379 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 380 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 381 | GlobalHandles* GlobalHandles::Node::GetGlobalHandles() { |
| 382 | return FindBlock()->global_handles(); |
| 383 | } |
| 384 | |
| 385 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 386 | GlobalHandles::NodeBlock* GlobalHandles::Node::FindBlock() { |
| 387 | intptr_t ptr = reinterpret_cast<intptr_t>(this); |
| 388 | ptr = ptr - index_ * sizeof(Node); |
| 389 | NodeBlock* block = reinterpret_cast<NodeBlock*>(ptr); |
| 390 | ASSERT(block->node_at(index_) == this); |
| 391 | return block; |
| 392 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 393 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 394 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 395 | void GlobalHandles::Node::IncreaseBlockUses() { |
| 396 | NodeBlock* node_block = FindBlock(); |
| 397 | node_block->IncreaseUses(); |
| 398 | GlobalHandles* global_handles = node_block->global_handles(); |
| 399 | global_handles->isolate()->counters()->global_handles()->Increment(); |
| 400 | global_handles->number_of_global_handles_++; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 404 | void GlobalHandles::Node::DecreaseBlockUses() { |
| 405 | NodeBlock* node_block = FindBlock(); |
| 406 | GlobalHandles* global_handles = node_block->global_handles(); |
| 407 | parameter_or_next_free_.next_free = global_handles->first_free_; |
| 408 | global_handles->first_free_ = this; |
| 409 | node_block->DecreaseUses(); |
| 410 | global_handles->isolate()->counters()->global_handles()->Decrement(); |
| 411 | global_handles->number_of_global_handles_--; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | |
| 415 | class GlobalHandles::NodeIterator { |
| 416 | public: |
| 417 | explicit NodeIterator(GlobalHandles* global_handles) |
| 418 | : block_(global_handles->first_used_block_), |
| 419 | index_(0) {} |
| 420 | |
| 421 | bool done() const { return block_ == NULL; } |
| 422 | |
| 423 | Node* node() const { |
| 424 | ASSERT(!done()); |
| 425 | return block_->node_at(index_); |
| 426 | } |
| 427 | |
| 428 | void Advance() { |
| 429 | ASSERT(!done()); |
| 430 | if (++index_ < NodeBlock::kSize) return; |
| 431 | index_ = 0; |
| 432 | block_ = block_->next_used(); |
| 433 | } |
| 434 | |
| 435 | private: |
| 436 | NodeBlock* block_; |
| 437 | int index_; |
| 438 | |
| 439 | DISALLOW_COPY_AND_ASSIGN(NodeIterator); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 440 | }; |
| 441 | |
| 442 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 443 | GlobalHandles::GlobalHandles(Isolate* isolate) |
| 444 | : isolate_(isolate), |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 445 | number_of_global_handles_(0), |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 446 | first_block_(NULL), |
| 447 | first_used_block_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 448 | first_free_(NULL), |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 449 | post_gc_processing_count_(0), |
| 450 | object_group_connections_(kObjectGroupConnectionsCapacity) {} |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 451 | |
| 452 | |
| 453 | GlobalHandles::~GlobalHandles() { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 454 | NodeBlock* block = first_block_; |
| 455 | while (block != NULL) { |
| 456 | NodeBlock* tmp = block->next(); |
| 457 | delete block; |
| 458 | block = tmp; |
| 459 | } |
| 460 | first_block_ = NULL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 461 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 462 | |
| 463 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 464 | Handle<Object> GlobalHandles::Create(Object* value) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 465 | if (first_free_ == NULL) { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 466 | first_block_ = new NodeBlock(this, first_block_); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 467 | first_block_->PutNodesOnFreeList(&first_free_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 468 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 469 | ASSERT(first_free_ != NULL); |
| 470 | // Take the first node in the free list. |
| 471 | Node* result = first_free_; |
| 472 | first_free_ = result->next_free(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 473 | result->Acquire(value); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 474 | if (isolate_->heap()->InNewSpace(value) && |
| 475 | !result->is_in_new_space_list()) { |
| 476 | new_space_nodes_.Add(result); |
| 477 | result->set_in_new_space_list(true); |
| 478 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 479 | return result->handle(); |
| 480 | } |
| 481 | |
| 482 | |
| 483 | void GlobalHandles::Destroy(Object** location) { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 484 | if (location != NULL) Node::FromLocation(location)->Release(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 488 | void GlobalHandles::MakeWeak(Object** location, |
| 489 | void* parameter, |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 490 | RevivableCallback weak_reference_callback) { |
| 491 | ASSERT(weak_reference_callback != NULL); |
| 492 | Node::FromLocation(location)->MakeWeak(parameter, weak_reference_callback); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | |
| 496 | void GlobalHandles::ClearWeakness(Object** location) { |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 497 | Node::FromLocation(location)->ClearWeakness(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 501 | void GlobalHandles::MarkIndependent(Object** location) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 502 | Node::FromLocation(location)->MarkIndependent(); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 506 | void GlobalHandles::MarkPartiallyDependent(Object** location) { |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 507 | Node::FromLocation(location)->MarkPartiallyDependent(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 511 | bool GlobalHandles::IsIndependent(Object** location) { |
| 512 | return Node::FromLocation(location)->is_independent(); |
| 513 | } |
| 514 | |
| 515 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 516 | bool GlobalHandles::IsNearDeath(Object** location) { |
| 517 | return Node::FromLocation(location)->IsNearDeath(); |
| 518 | } |
| 519 | |
| 520 | |
| 521 | bool GlobalHandles::IsWeak(Object** location) { |
| 522 | return Node::FromLocation(location)->IsWeak(); |
| 523 | } |
| 524 | |
| 525 | |
| 526 | void GlobalHandles::IterateWeakRoots(ObjectVisitor* v) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 527 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 528 | if (it.node()->IsWeakRetainer()) v->VisitPointer(it.node()->location()); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
| 532 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 533 | void GlobalHandles::IdentifyWeakHandles(WeakSlotCallback f) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 534 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 535 | if (it.node()->IsWeak() && f(it.node()->location())) { |
| 536 | it.node()->MarkPending(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 542 | void GlobalHandles::IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v) { |
| 543 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 544 | Node* node = new_space_nodes_[i]; |
| 545 | if (node->IsStrongRetainer() || |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 546 | (node->IsWeakRetainer() && !node->is_independent() && |
| 547 | !node->is_partially_dependent())) { |
| 548 | v->VisitPointer(node->location()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | |
| 554 | void GlobalHandles::IdentifyNewSpaceWeakIndependentHandles( |
| 555 | WeakSlotCallbackWithHeap f) { |
| 556 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 557 | Node* node = new_space_nodes_[i]; |
| 558 | ASSERT(node->is_in_new_space_list()); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 559 | if ((node->is_independent() || node->is_partially_dependent()) && |
| 560 | node->IsWeak() && f(isolate_->heap(), node->location())) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 561 | node->MarkPending(); |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | |
| 567 | void GlobalHandles::IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v) { |
| 568 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 569 | Node* node = new_space_nodes_[i]; |
| 570 | ASSERT(node->is_in_new_space_list()); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 571 | if ((node->is_independent() || node->is_partially_dependent()) && |
| 572 | node->IsWeakRetainer()) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 573 | v->VisitPointer(node->location()); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 579 | bool GlobalHandles::IterateObjectGroups(ObjectVisitor* v, |
| 580 | WeakSlotCallbackWithHeap can_skip) { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 581 | ComputeObjectGroupsAndImplicitReferences(); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 582 | int last = 0; |
| 583 | bool any_group_was_visited = false; |
| 584 | for (int i = 0; i < object_groups_.length(); i++) { |
| 585 | ObjectGroup* entry = object_groups_.at(i); |
| 586 | ASSERT(entry != NULL); |
| 587 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 588 | Object*** objects = entry->objects; |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 589 | bool group_should_be_visited = false; |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 590 | for (size_t j = 0; j < entry->length; j++) { |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 591 | Object* object = *objects[j]; |
| 592 | if (object->IsHeapObject()) { |
| 593 | if (!can_skip(isolate_->heap(), &object)) { |
| 594 | group_should_be_visited = true; |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | if (!group_should_be_visited) { |
| 601 | object_groups_[last++] = entry; |
| 602 | continue; |
| 603 | } |
| 604 | |
| 605 | // An object in the group requires visiting, so iterate over all |
| 606 | // objects in the group. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 607 | for (size_t j = 0; j < entry->length; ++j) { |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 608 | Object* object = *objects[j]; |
| 609 | if (object->IsHeapObject()) { |
| 610 | v->VisitPointer(&object); |
| 611 | any_group_was_visited = true; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // Once the entire group has been iterated over, set the object |
| 616 | // group to NULL so it won't be processed again. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 617 | delete entry; |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 618 | object_groups_.at(i) = NULL; |
| 619 | } |
| 620 | object_groups_.Rewind(last); |
| 621 | return any_group_was_visited; |
| 622 | } |
| 623 | |
| 624 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 625 | bool GlobalHandles::PostGarbageCollectionProcessing( |
rossberg@chromium.org | cddc71f | 2012-12-07 12:40:13 +0000 | [diff] [blame] | 626 | GarbageCollector collector, GCTracer* tracer) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 627 | // Process weak global handle callbacks. This must be done after the |
| 628 | // GC is completely done, because the callbacks may invoke arbitrary |
| 629 | // API functions. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 630 | ASSERT(isolate_->heap()->gc_state() == Heap::NOT_IN_GC); |
| 631 | const int initial_post_gc_processing_count = ++post_gc_processing_count_; |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 632 | bool next_gc_likely_to_collect_more = false; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 633 | if (collector == SCAVENGER) { |
| 634 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 635 | Node* node = new_space_nodes_[i]; |
| 636 | ASSERT(node->is_in_new_space_list()); |
| 637 | // Skip dependent handles. Their weak callbacks might expect to be |
| 638 | // called between two global garbage collection callbacks which |
| 639 | // are not called for minor collections. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 640 | if (!node->is_independent() && !node->is_partially_dependent()) { |
| 641 | continue; |
| 642 | } |
| 643 | node->clear_partially_dependent(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 644 | if (node->PostGarbageCollectionProcessing(isolate_)) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 645 | if (initial_post_gc_processing_count != post_gc_processing_count_) { |
| 646 | // Weak callback triggered another GC and another round of |
| 647 | // PostGarbageCollection processing. The current node might |
| 648 | // have been deleted in that round, so we need to bail out (or |
| 649 | // restart the processing). |
| 650 | return next_gc_likely_to_collect_more; |
| 651 | } |
| 652 | } |
| 653 | if (!node->IsRetainer()) { |
| 654 | next_gc_likely_to_collect_more = true; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 657 | } else { |
| 658 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 659 | it.node()->clear_partially_dependent(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 660 | if (it.node()->PostGarbageCollectionProcessing(isolate_)) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 661 | if (initial_post_gc_processing_count != post_gc_processing_count_) { |
| 662 | // See the comment above. |
| 663 | return next_gc_likely_to_collect_more; |
| 664 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 665 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 666 | if (!it.node()->IsRetainer()) { |
| 667 | next_gc_likely_to_collect_more = true; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | // Update the list of new space nodes. |
| 672 | int last = 0; |
| 673 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 674 | Node* node = new_space_nodes_[i]; |
| 675 | ASSERT(node->is_in_new_space_list()); |
rossberg@chromium.org | cddc71f | 2012-12-07 12:40:13 +0000 | [diff] [blame] | 676 | if (node->IsRetainer()) { |
| 677 | if (isolate_->heap()->InNewSpace(node->object())) { |
| 678 | new_space_nodes_[last++] = node; |
| 679 | tracer->increment_nodes_copied_in_new_space(); |
| 680 | } else { |
| 681 | node->set_in_new_space_list(false); |
| 682 | tracer->increment_nodes_promoted(); |
| 683 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 684 | } else { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 685 | node->set_in_new_space_list(false); |
rossberg@chromium.org | cddc71f | 2012-12-07 12:40:13 +0000 | [diff] [blame] | 686 | tracer->increment_nodes_died_in_new_space(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 687 | } |
| 688 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 689 | new_space_nodes_.Rewind(last); |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 690 | return next_gc_likely_to_collect_more; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 694 | void GlobalHandles::IterateStrongRoots(ObjectVisitor* v) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 695 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 696 | if (it.node()->IsStrongRetainer()) { |
| 697 | v->VisitPointer(it.node()->location()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 702 | |
| 703 | void GlobalHandles::IterateAllRoots(ObjectVisitor* v) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 704 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 705 | if (it.node()->IsRetainer()) { |
| 706 | v->VisitPointer(it.node()->location()); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 712 | void GlobalHandles::IterateAllRootsWithClassIds(ObjectVisitor* v) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 713 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 714 | if (it.node()->IsRetainer() && it.node()->has_wrapper_class_id()) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 715 | v->VisitEmbedderReference(it.node()->location(), |
| 716 | it.node()->wrapper_class_id()); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 722 | void GlobalHandles::IterateAllRootsInNewSpaceWithClassIds(ObjectVisitor* v) { |
| 723 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 724 | Node* node = new_space_nodes_[i]; |
| 725 | if (node->IsRetainer() && node->has_wrapper_class_id()) { |
| 726 | v->VisitEmbedderReference(node->location(), |
| 727 | node->wrapper_class_id()); |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | |
yangguo@chromium.org | 28381b4 | 2013-01-21 14:39:38 +0000 | [diff] [blame] | 733 | int GlobalHandles::NumberOfWeakHandles() { |
| 734 | int count = 0; |
| 735 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 736 | if (it.node()->IsWeakRetainer()) { |
| 737 | count++; |
| 738 | } |
| 739 | } |
| 740 | return count; |
| 741 | } |
| 742 | |
| 743 | |
| 744 | int GlobalHandles::NumberOfGlobalObjectWeakHandles() { |
| 745 | int count = 0; |
| 746 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 747 | if (it.node()->IsWeakRetainer() && |
| 748 | it.node()->object()->IsJSGlobalObject()) { |
| 749 | count++; |
| 750 | } |
| 751 | } |
| 752 | return count; |
| 753 | } |
| 754 | |
| 755 | |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 756 | void GlobalHandles::RecordStats(HeapStats* stats) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 757 | *stats->global_handle_count = 0; |
| 758 | *stats->weak_global_handle_count = 0; |
| 759 | *stats->pending_global_handle_count = 0; |
| 760 | *stats->near_death_global_handle_count = 0; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 761 | *stats->free_global_handle_count = 0; |
| 762 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 763 | *stats->global_handle_count += 1; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 764 | if (it.node()->state() == Node::WEAK) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 765 | *stats->weak_global_handle_count += 1; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 766 | } else if (it.node()->state() == Node::PENDING) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 767 | *stats->pending_global_handle_count += 1; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 768 | } else if (it.node()->state() == Node::NEAR_DEATH) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 769 | *stats->near_death_global_handle_count += 1; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 770 | } else if (it.node()->state() == Node::FREE) { |
| 771 | *stats->free_global_handle_count += 1; |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 776 | #ifdef DEBUG |
| 777 | |
| 778 | void GlobalHandles::PrintStats() { |
| 779 | int total = 0; |
| 780 | int weak = 0; |
| 781 | int pending = 0; |
| 782 | int near_death = 0; |
| 783 | int destroyed = 0; |
| 784 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 785 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 786 | total++; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 787 | if (it.node()->state() == Node::WEAK) weak++; |
| 788 | if (it.node()->state() == Node::PENDING) pending++; |
| 789 | if (it.node()->state() == Node::NEAR_DEATH) near_death++; |
| 790 | if (it.node()->state() == Node::FREE) destroyed++; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | PrintF("Global Handle Statistics:\n"); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 794 | PrintF(" allocated memory = %" V8_PTR_PREFIX "dB\n", sizeof(Node) * total); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 795 | PrintF(" # weak = %d\n", weak); |
| 796 | PrintF(" # pending = %d\n", pending); |
| 797 | PrintF(" # near_death = %d\n", near_death); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 798 | PrintF(" # free = %d\n", destroyed); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 799 | PrintF(" # total = %d\n", total); |
| 800 | } |
| 801 | |
| 802 | void GlobalHandles::Print() { |
| 803 | PrintF("Global handles:\n"); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 804 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 805 | PrintF(" handle %p to %p%s\n", |
| 806 | reinterpret_cast<void*>(it.node()->location()), |
| 807 | reinterpret_cast<void*>(it.node()->object()), |
| 808 | it.node()->IsWeak() ? " (weak)" : ""); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
| 812 | #endif |
| 813 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 814 | |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 815 | |
| 816 | void GlobalHandles::AddObjectGroup(Object*** handles, |
| 817 | size_t length, |
| 818 | v8::RetainedObjectInfo* info) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 819 | #ifdef DEBUG |
| 820 | for (size_t i = 0; i < length; ++i) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 821 | ASSERT(!Node::FromLocation(handles[i])->is_independent()); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 822 | } |
| 823 | #endif |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 824 | if (length == 0) { |
| 825 | if (info != NULL) info->Dispose(); |
| 826 | return; |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 827 | } |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 828 | ObjectGroup* group = new ObjectGroup(length); |
| 829 | for (size_t i = 0; i < length; ++i) |
| 830 | group->objects[i] = handles[i]; |
| 831 | group->info = info; |
| 832 | object_groups_.Add(group); |
| 833 | } |
| 834 | |
| 835 | |
| 836 | void GlobalHandles::SetObjectGroupId(Object** handle, |
| 837 | UniqueId id) { |
| 838 | object_group_connections_.Add(ObjectGroupConnection(id, handle)); |
| 839 | } |
| 840 | |
| 841 | |
| 842 | void GlobalHandles::SetRetainedObjectInfo(UniqueId id, |
| 843 | RetainedObjectInfo* info) { |
| 844 | retainer_infos_.Add(ObjectGroupRetainerInfo(id, info)); |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 848 | void GlobalHandles::AddImplicitReferences(HeapObject** parent, |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 849 | Object*** children, |
| 850 | size_t length) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 851 | #ifdef DEBUG |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 852 | ASSERT(!Node::FromLocation(BitCast<Object**>(parent))->is_independent()); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 853 | for (size_t i = 0; i < length; ++i) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 854 | ASSERT(!Node::FromLocation(children[i])->is_independent()); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 855 | } |
| 856 | #endif |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 857 | if (length == 0) return; |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 858 | ImplicitRefGroup* group = new ImplicitRefGroup(parent, length); |
| 859 | for (size_t i = 0; i < length; ++i) |
| 860 | group->children[i] = children[i]; |
| 861 | implicit_ref_groups_.Add(group); |
| 862 | } |
| 863 | |
| 864 | |
| 865 | void GlobalHandles::SetReferenceFromGroup(UniqueId id, Object** child) { |
| 866 | ASSERT(!Node::FromLocation(child)->is_independent()); |
| 867 | implicit_ref_connections_.Add(ObjectGroupConnection(id, child)); |
| 868 | } |
| 869 | |
| 870 | |
| 871 | void GlobalHandles::SetReference(HeapObject** parent, Object** child) { |
| 872 | ASSERT(!Node::FromLocation(child)->is_independent()); |
| 873 | ImplicitRefGroup* group = new ImplicitRefGroup(parent, 1); |
| 874 | group->children[0] = child; |
| 875 | implicit_ref_groups_.Add(group); |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 879 | void GlobalHandles::RemoveObjectGroups() { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 880 | for (int i = 0; i < object_groups_.length(); i++) |
| 881 | delete object_groups_.at(i); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 882 | object_groups_.Clear(); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 883 | for (int i = 0; i < retainer_infos_.length(); ++i) |
| 884 | retainer_infos_[i].info->Dispose(); |
| 885 | retainer_infos_.Clear(); |
| 886 | object_group_connections_.Clear(); |
| 887 | object_group_connections_.Initialize(kObjectGroupConnectionsCapacity); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 888 | } |
| 889 | |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 890 | |
| 891 | void GlobalHandles::RemoveImplicitRefGroups() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 892 | for (int i = 0; i < implicit_ref_groups_.length(); i++) { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 893 | delete implicit_ref_groups_.at(i); |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 894 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 895 | implicit_ref_groups_.Clear(); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 896 | implicit_ref_connections_.Clear(); |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 900 | void GlobalHandles::TearDown() { |
| 901 | // TODO(1428): invoke weak callbacks. |
| 902 | } |
| 903 | |
| 904 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 905 | void GlobalHandles::ComputeObjectGroupsAndImplicitReferences() { |
| 906 | if (object_group_connections_.length() == 0) { |
| 907 | for (int i = 0; i < retainer_infos_.length(); ++i) |
| 908 | retainer_infos_[i].info->Dispose(); |
| 909 | retainer_infos_.Clear(); |
| 910 | implicit_ref_connections_.Clear(); |
| 911 | return; |
| 912 | } |
| 913 | |
| 914 | object_group_connections_.Sort(); |
| 915 | retainer_infos_.Sort(); |
| 916 | implicit_ref_connections_.Sort(); |
| 917 | |
| 918 | int info_index = 0; // For iterating retainer_infos_. |
| 919 | UniqueId current_group_id(0); |
| 920 | int current_group_start = 0; |
| 921 | |
| 922 | int current_implicit_refs_start = 0; |
| 923 | int current_implicit_refs_end = 0; |
| 924 | for (int i = 0; i <= object_group_connections_.length(); ++i) { |
| 925 | if (i == 0) |
| 926 | current_group_id = object_group_connections_[i].id; |
| 927 | if (i == object_group_connections_.length() || |
| 928 | current_group_id != object_group_connections_[i].id) { |
| 929 | // Group detected: objects in indices [current_group_start, i[. |
| 930 | |
| 931 | // Find out which implicit references are related to this group. (We want |
| 932 | // to ignore object groups which only have 1 object, but that object is |
| 933 | // needed as a representative object for the implicit refrerence group.) |
| 934 | while (current_implicit_refs_start < implicit_ref_connections_.length() && |
| 935 | implicit_ref_connections_[current_implicit_refs_start].id < |
| 936 | current_group_id) |
| 937 | ++current_implicit_refs_start; |
| 938 | current_implicit_refs_end = current_implicit_refs_start; |
| 939 | while (current_implicit_refs_end < implicit_ref_connections_.length() && |
| 940 | implicit_ref_connections_[current_implicit_refs_end].id == |
| 941 | current_group_id) |
| 942 | ++current_implicit_refs_end; |
| 943 | |
| 944 | if (current_implicit_refs_end > current_implicit_refs_start) { |
| 945 | // Find a representative object for the implicit references. |
| 946 | HeapObject** representative = NULL; |
| 947 | for (int j = current_group_start; j < i; ++j) { |
| 948 | Object** object = object_group_connections_[j].object; |
| 949 | if ((*object)->IsHeapObject()) { |
| 950 | representative = reinterpret_cast<HeapObject**>(object); |
| 951 | break; |
| 952 | } |
| 953 | } |
| 954 | if (representative) { |
| 955 | ImplicitRefGroup* group = new ImplicitRefGroup( |
| 956 | representative, |
| 957 | current_implicit_refs_end - current_implicit_refs_start); |
| 958 | for (int j = current_implicit_refs_start; |
| 959 | j < current_implicit_refs_end; |
| 960 | ++j) { |
| 961 | group->children[j - current_implicit_refs_start] = |
| 962 | implicit_ref_connections_[j].object; |
| 963 | } |
| 964 | implicit_ref_groups_.Add(group); |
| 965 | } |
| 966 | current_implicit_refs_start = current_implicit_refs_end; |
| 967 | } |
| 968 | |
| 969 | // Find a RetainedObjectInfo for the group. |
| 970 | RetainedObjectInfo* info = NULL; |
| 971 | while (info_index < retainer_infos_.length() && |
| 972 | retainer_infos_[info_index].id < current_group_id) { |
| 973 | retainer_infos_[info_index].info->Dispose(); |
| 974 | ++info_index; |
| 975 | } |
| 976 | if (info_index < retainer_infos_.length() && |
| 977 | retainer_infos_[info_index].id == current_group_id) { |
| 978 | // This object group has an associated ObjectGroupRetainerInfo. |
| 979 | info = retainer_infos_[info_index].info; |
| 980 | ++info_index; |
| 981 | } |
| 982 | |
| 983 | // Ignore groups which only contain one object. |
| 984 | if (i > current_group_start + 1) { |
| 985 | ObjectGroup* group = new ObjectGroup(i - current_group_start); |
| 986 | for (int j = current_group_start; j < i; ++j) { |
| 987 | group->objects[j - current_group_start] = |
| 988 | object_group_connections_[j].object; |
| 989 | } |
| 990 | group->info = info; |
| 991 | object_groups_.Add(group); |
| 992 | } else if (info) { |
| 993 | info->Dispose(); |
| 994 | } |
| 995 | |
| 996 | if (i < object_group_connections_.length()) { |
| 997 | current_group_id = object_group_connections_[i].id; |
| 998 | current_group_start = i; |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | object_group_connections_.Clear(); |
| 1003 | object_group_connections_.Initialize(kObjectGroupConnectionsCapacity); |
| 1004 | retainer_infos_.Clear(); |
| 1005 | implicit_ref_connections_.Clear(); |
| 1006 | } |
| 1007 | |
| 1008 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1009 | } } // namespace v8::internal |