| OLD | NEW |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 ASSERT(state_ != DESTROYED); | 144 ASSERT(state_ != DESTROYED); |
| 145 return parameter_or_next_free_.parameter; | 145 return parameter_or_next_free_.parameter; |
| 146 } | 146 } |
| 147 | 147 |
| 148 // Returns the callback for this weak handle. | 148 // Returns the callback for this weak handle. |
| 149 WeakReferenceCallback callback() { return callback_; } | 149 WeakReferenceCallback callback() { return callback_; } |
| 150 | 150 |
| 151 bool PostGarbageCollectionProcessing() { | 151 bool PostGarbageCollectionProcessing() { |
| 152 if (state_ != Node::PENDING) return false; | 152 if (state_ != Node::PENDING) return false; |
| 153 LOG(HandleEvent("GlobalHandle::Processing", handle().location())); | 153 LOG(HandleEvent("GlobalHandle::Processing", handle().location())); |
| 154 WeakReferenceCallback func = callback(); |
| 155 if (func == NULL) { |
| 156 Destroy(); |
| 157 return false; |
| 158 } |
| 154 void* par = parameter(); | 159 void* par = parameter(); |
| 155 state_ = NEAR_DEATH; | 160 state_ = NEAR_DEATH; |
| 156 set_parameter(NULL); | 161 set_parameter(NULL); |
| 157 // The callback function is resolved as late as possible to preserve old | |
| 158 // behavior. | |
| 159 WeakReferenceCallback func = callback(); | |
| 160 if (func == NULL) return false; | |
| 161 | 162 |
| 162 v8::Persistent<v8::Object> object = ToApi<v8::Object>(handle()); | 163 v8::Persistent<v8::Object> object = ToApi<v8::Object>(handle()); |
| 163 { | 164 { |
| 164 // Forbid reuse of destroyed nodes as they might be already deallocated. | 165 // Forbid reuse of destroyed nodes as they might be already deallocated. |
| 165 // It's fine though to reuse nodes that were destroyed in weak callback | 166 // It's fine though to reuse nodes that were destroyed in weak callback |
| 166 // as those cannot be deallocated until we are back from the callback. | 167 // as those cannot be deallocated until we are back from the callback. |
| 167 set_first_free(NULL); | 168 set_first_free(NULL); |
| 168 if (first_deallocated()) { | 169 if (first_deallocated()) { |
| 169 first_deallocated()->set_next(head()); | 170 first_deallocated()->set_next(head()); |
| 170 } | 171 } |
| 171 // Check that we are not passing a finalized external string to | 172 // Check that we are not passing a finalized external string to |
| 172 // the callback. | 173 // the callback. |
| 173 ASSERT(!object_->IsExternalAsciiString() || | 174 ASSERT(!object_->IsExternalAsciiString() || |
| 174 ExternalAsciiString::cast(object_)->resource() != NULL); | 175 ExternalAsciiString::cast(object_)->resource() != NULL); |
| 175 ASSERT(!object_->IsExternalTwoByteString() || | 176 ASSERT(!object_->IsExternalTwoByteString() || |
| 176 ExternalTwoByteString::cast(object_)->resource() != NULL); | 177 ExternalTwoByteString::cast(object_)->resource() != NULL); |
| 177 // Leaving V8. | 178 // Leaving V8. |
| 178 VMState state(EXTERNAL); | 179 VMState state(EXTERNAL); |
| 179 func(object, par); | 180 func(object, par); |
| 180 } | 181 } |
| 182 // Absense of explicit cleanup or revival of weak handle |
| 183 // in most of the cases would lead to memory leak. |
| 184 ASSERT(state_ != NEAR_DEATH); |
| 181 return true; | 185 return true; |
| 182 } | 186 } |
| 183 | 187 |
| 184 // Place the handle address first to avoid offset computation. | 188 // Place the handle address first to avoid offset computation. |
| 185 Object* object_; // Storage for object pointer. | 189 Object* object_; // Storage for object pointer. |
| 186 | 190 |
| 187 // Transition diagram: | 191 // Transition diagram: |
| 188 // NORMAL <-> WEAK -> PENDING -> NEAR_DEATH -> { NORMAL, WEAK, DESTROYED } | 192 // NORMAL <-> WEAK -> PENDING -> NEAR_DEATH -> { NORMAL, WEAK, DESTROYED } |
| 189 enum State { | 193 enum State { |
| 190 NORMAL, // Normal global handle. | 194 NORMAL, // Normal global handle. |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 | 511 |
| 508 void GlobalHandles::RemoveObjectGroups() { | 512 void GlobalHandles::RemoveObjectGroups() { |
| 509 List<ObjectGroup*>* object_groups = ObjectGroups(); | 513 List<ObjectGroup*>* object_groups = ObjectGroups(); |
| 510 for (int i = 0; i< object_groups->length(); i++) { | 514 for (int i = 0; i< object_groups->length(); i++) { |
| 511 delete object_groups->at(i); | 515 delete object_groups->at(i); |
| 512 } | 516 } |
| 513 object_groups->Clear(); | 517 object_groups->Clear(); |
| 514 } | 518 } |
| 515 | 519 |
| 516 } } // namespace v8::internal | 520 } } // namespace v8::internal |
| OLD | NEW |