OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
435 // Forward declaration. | 435 // Forward declaration. |
436 template<typename T> class Persistent; | 436 template<typename T> class Persistent; |
437 | 437 |
438 // Base class for the transition period. Provides new and delete | 438 // Base class for the transition period. Provides new and delete |
439 // operators for allocation on the heap as well as finalization | 439 // operators for allocation on the heap as well as finalization |
440 // behavior. | 440 // behavior. |
441 template<typename T> | 441 template<typename T> |
442 class HeapAllocatedFinalized : public HeapAllocated<T> { | 442 class HeapAllocatedFinalized : public HeapAllocated<T> { |
443 WTF_MAKE_NONCOPYABLE(HeapAllocatedFinalized); | 443 WTF_MAKE_NONCOPYABLE(HeapAllocatedFinalized); |
444 | 444 |
445 // If this assert fails then you are inheriting this method instead of | |
Mads Ager (chromium)
2013/06/11 06:42:43
This comment seems to belong on the inherited new
| |
446 // overriding it. This may be an issue because the inherited class | |
447 // specialization (T) is used to find the finalizer. This may mean that the | |
448 // wrong method is used in the allocateFinalized specialization below and | |
449 // the wrong accept method may be used to visit the method if there is a GC | |
450 // before the new object has been adopted. | |
451 #define OVERRIDE_NEW(subClass) \ | |
452 void* operator new(size_t size) \ | |
453 { \ | |
454 ASSERT(size == sizeof(class subClass)); \ | |
455 return Heap::allocateFinalized<subClass>(size); \ | |
456 } | |
457 | |
458 // Only use this for classes where we know that accept(Visitor*) is dynamic, | |
459 // ie. it is virtual or uses a type field that is immediately initialized | |
460 // in the constuctors. Similarly the finalizer must be the same for all | |
461 // subclasses. | |
462 #define OVERRIDE_NEW_ALL_SUBCLASSES_HAVE_DYNAMIC_ACCEPT(subClass) \ | |
463 void* operator new(size_t size) \ | |
464 { \ | |
465 return Heap::allocateFinalized<subClass>(size); \ | |
466 } | |
467 | |
445 public: | 468 public: |
446 // Override new to pass the finalizer. | 469 // Override new to pass the finalizer. |
447 void* operator new(size_t size) | 470 void* operator new(size_t size) |
448 { | 471 { |
472 // If this assert fails you probably need to use OVERRIDE_NEW | |
473 // (see above). | |
474 ASSERT(size == sizeof(T)); | |
449 return Heap::allocateFinalized<T>(size); | 475 return Heap::allocateFinalized<T>(size); |
450 } | 476 } |
451 | 477 |
452 // Finalize is called when the object is freed from the heap. Finalize | 478 // Finalize is called when the object is freed from the heap. Finalize |
453 // can be overridden to support calling the destructor of a subclass. | 479 // can be overridden to support calling the destructor of a subclass. |
454 // This is similar to the way deref is overridden to call delete on | 480 // This is similar to the way deref is overridden to call delete on |
455 // the correct subtype. | 481 // the correct subtype. |
456 void finalize() | 482 void finalize() |
457 { | 483 { |
458 static_cast<T*>(this)->~T(); | 484 static_cast<T*>(this)->~T(); |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
611 // You can't mark an object that isn't in the heap. | 637 // You can't mark an object that isn't in the heap. |
612 ASSERT(contains(header)); | 638 ASSERT(contains(header)); |
613 // End of object should also be in the heap. | 639 // End of object should also be in the heap. |
614 ASSERT(contains(header->payloadEnd() - 1)); | 640 ASSERT(contains(header->payloadEnd() - 1)); |
615 header->mark(); | 641 header->mark(); |
616 } | 642 } |
617 | 643 |
618 } // namespace WebCore | 644 } // namespace WebCore |
619 | 645 |
620 #endif | 646 #endif |
OLD | NEW |