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 1065 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1076 | 1076 |
1077 // FIXME: The forward declaration is layering violation. | 1077 // FIXME: The forward declaration is layering violation. |
1078 #define DEFINE_TYPED_HEAP_TRAIT(Type) \ | 1078 #define DEFINE_TYPED_HEAP_TRAIT(Type) \ |
1079 class Type; \ | 1079 class Type; \ |
1080 template <> struct HeapIndexTrait<class Type> { \ | 1080 template <> struct HeapIndexTrait<class Type> { \ |
1081 static int index() { return Type##HeapIndex; }; \ | 1081 static int index() { return Type##HeapIndex; }; \ |
1082 }; | 1082 }; |
1083 FOR_EACH_TYPED_HEAP(DEFINE_TYPED_HEAP_TRAIT) | 1083 FOR_EACH_TYPED_HEAP(DEFINE_TYPED_HEAP_TRAIT) |
1084 #undef DEFINE_TYPED_HEAP_TRAIT | 1084 #undef DEFINE_TYPED_HEAP_TRAIT |
1085 | 1085 |
1086 template<typename T, typename Enabled = void> | |
1087 class AllocateObjectTrait { | |
1088 public: | |
1089 static Address allocate(size_t size) | |
1090 { | |
1091 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state(
); | |
1092 return Heap::allocateOnHeapIndex(state, size, HeapIndexTrait<T>::index()
, GCInfoTrait<T>::index()); | |
1093 } | |
1094 | |
1095 static void constructor() | |
1096 { | |
1097 } | |
1098 }; | |
1099 | |
1100 template<typename T> | |
1101 class AllocateObjectTrait<T, typename WTF::EnableIf<blink::IsGarbageCollectedMix
in<T>::value>::Type> { | |
1102 public: | |
1103 // An object which implements GarbageCollectedMixin is marked | |
1104 // and traced during GC by first adjusting object references to | |
1105 // it to refer to the leftmost base for the object (which would | |
1106 // be a GarbageCollected-derived class.) The prefixed object header | |
1107 // can be located after that adjustment and its trace() vtbl slot | |
1108 // will be used to fully trace the object, if not already marked. | |
1109 // | |
1110 // A C++ object's vptr will be initialized to its leftmost base's | |
1111 // vtable after the constructors of all its subclasses have run, | |
1112 // so if a subclass constructor tries to access any of the vtbl | |
1113 // entries of its leftmost base prematurely, it'll find an as-yet | |
1114 // incorrect vptr and fail. Which is exactly what a garbage collector | |
1115 // will try to do if it tries to access the leftmost base while one | |
1116 // of the subclass constructors of a GC mixin object triggers a GC. | |
1117 // It is consequently not safe to allow any GCs while these objects | |
1118 // are under (sub constructor) construction. | |
1119 // | |
1120 // To achieve that, the construction of mixins are handled in a | |
1121 // special manner: | |
1122 // | |
1123 // - The initial allocation of the mixin object will enter a no GC scope. | |
1124 // - The constructor for the leftmost base, which is when the mixin | |
1125 // object is in a state ready for a GC, leaves that GC scope. | |
1126 // - no-GC scope entering/leaving must support nesting. | |
1127 // | |
1128 static Address allocate(size_t size) | |
1129 { | |
1130 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state(
); | |
1131 Address object = Heap::allocateOnHeapIndex(state, size, HeapIndexTrait<T
>::index(), GCInfoTrait<T>::index()); | |
1132 state->enterGCForbiddenScope(); | |
1133 return object; | |
1134 } | |
1135 | |
1136 static void constructor() | |
1137 { | |
1138 // FIXME: if prompt conservative GCs are needed, forced GCs that | |
1139 // were denied while within this scope, could now be performed. | |
1140 // For now, assume the next out-of-line allocation request will | |
1141 // happen soon enough and take care of it. Mixin objects aren't | |
1142 // overly common. | |
1143 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state(
); | |
1144 state->leaveGCForbiddenScope(); | |
1145 } | |
1146 }; | |
1147 | |
1148 // Base class for objects allocated in the Blink garbage-collected heap. | 1086 // Base class for objects allocated in the Blink garbage-collected heap. |
1149 // | 1087 // |
1150 // Defines a 'new' operator that allocates the memory in the heap. 'delete' | 1088 // Defines a 'new' operator that allocates the memory in the heap. 'delete' |
1151 // should not be called on objects that inherit from GarbageCollected. | 1089 // should not be called on objects that inherit from GarbageCollected. |
1152 // | 1090 // |
1153 // Instances of GarbageCollected will *NOT* get finalized. Their destructor | 1091 // Instances of GarbageCollected will *NOT* get finalized. Their destructor |
1154 // will not be called. Therefore, only classes that have trivial destructors | 1092 // will not be called. Therefore, only classes that have trivial destructors |
1155 // with no semantic meaning (including all their subclasses) should inherit from | 1093 // with no semantic meaning (including all their subclasses) should inherit from |
1156 // GarbageCollected. If there are non-trival destructors in a given class or | 1094 // GarbageCollected. If there are non-trival destructors in a given class or |
1157 // any of its subclasses, GarbageCollectedFinalized should be used which | 1095 // any of its subclasses, GarbageCollectedFinalized should be used which |
(...skipping 28 matching lines...) Expand all Loading... |
1186 } | 1124 } |
1187 | 1125 |
1188 void operator delete(void* p) | 1126 void operator delete(void* p) |
1189 { | 1127 { |
1190 ASSERT_NOT_REACHED(); | 1128 ASSERT_NOT_REACHED(); |
1191 } | 1129 } |
1192 | 1130 |
1193 protected: | 1131 protected: |
1194 GarbageCollected() | 1132 GarbageCollected() |
1195 { | 1133 { |
1196 AllocateObjectTrait<T>::constructor(); | |
1197 } | 1134 } |
1198 }; | 1135 }; |
1199 | 1136 |
1200 // Base class for objects allocated in the Blink garbage-collected heap. | 1137 // Base class for objects allocated in the Blink garbage-collected heap. |
1201 // | 1138 // |
1202 // Defines a 'new' operator that allocates the memory in the heap. 'delete' | 1139 // Defines a 'new' operator that allocates the memory in the heap. 'delete' |
1203 // should not be called on objects that inherit from GarbageCollected. | 1140 // should not be called on objects that inherit from GarbageCollected. |
1204 // | 1141 // |
1205 // Instances of GarbageCollectedFinalized will have their destructor called when | 1142 // Instances of GarbageCollectedFinalized will have their destructor called when |
1206 // the garbage collector determines that the object is no longer reachable. | 1143 // the garbage collector determines that the object is no longer reachable. |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1475 { | 1412 { |
1476 ASSERT(state->isAllocationAllowed()); | 1413 ASSERT(state->isAllocationAllowed()); |
1477 ASSERT(heapIndex != LargeObjectHeapIndex); | 1414 ASSERT(heapIndex != LargeObjectHeapIndex); |
1478 NormalPageHeap* heap = static_cast<NormalPageHeap*>(state->heap(heapIndex)); | 1415 NormalPageHeap* heap = static_cast<NormalPageHeap*>(state->heap(heapIndex)); |
1479 return heap->allocateObject(allocationSizeFromSize(size), gcInfoIndex); | 1416 return heap->allocateObject(allocationSizeFromSize(size), gcInfoIndex); |
1480 } | 1417 } |
1481 | 1418 |
1482 template<typename T> | 1419 template<typename T> |
1483 Address Heap::allocate(size_t size) | 1420 Address Heap::allocate(size_t size) |
1484 { | 1421 { |
1485 return AllocateObjectTrait<T>::allocate(size); | 1422 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state(); |
| 1423 return Heap::allocateOnHeapIndex(state, size, HeapIndexTrait<T>::index(), GC
InfoTrait<T>::index()); |
1486 } | 1424 } |
1487 | 1425 |
1488 template<typename T> | 1426 template<typename T> |
1489 Address Heap::reallocate(void* previous, size_t size) | 1427 Address Heap::reallocate(void* previous, size_t size) |
1490 { | 1428 { |
1491 if (!size) { | 1429 if (!size) { |
1492 // If the new size is 0 this is equivalent to either free(previous) or | 1430 // If the new size is 0 this is equivalent to either free(previous) or |
1493 // malloc(0). In both cases we do nothing and return nullptr. | 1431 // malloc(0). In both cases we do nothing and return nullptr. |
1494 return nullptr; | 1432 return nullptr; |
1495 } | 1433 } |
(...skipping 1060 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2556 template<typename T, size_t inlineCapacity> | 2494 template<typename T, size_t inlineCapacity> |
2557 struct GCInfoTrait<HeapVector<T, inlineCapacity>> : public GCInfoTrait<Vector<T,
inlineCapacity, HeapAllocator>> { }; | 2495 struct GCInfoTrait<HeapVector<T, inlineCapacity>> : public GCInfoTrait<Vector<T,
inlineCapacity, HeapAllocator>> { }; |
2558 template<typename T, size_t inlineCapacity> | 2496 template<typename T, size_t inlineCapacity> |
2559 struct GCInfoTrait<HeapDeque<T, inlineCapacity>> : public GCInfoTrait<Deque<T, i
nlineCapacity, HeapAllocator>> { }; | 2497 struct GCInfoTrait<HeapDeque<T, inlineCapacity>> : public GCInfoTrait<Deque<T, i
nlineCapacity, HeapAllocator>> { }; |
2560 template<typename T, typename U, typename V> | 2498 template<typename T, typename U, typename V> |
2561 struct GCInfoTrait<HeapHashCountedSet<T, U, V>> : public GCInfoTrait<HashCounted
Set<T, U, V, HeapAllocator>> { }; | 2499 struct GCInfoTrait<HeapHashCountedSet<T, U, V>> : public GCInfoTrait<HashCounted
Set<T, U, V, HeapAllocator>> { }; |
2562 | 2500 |
2563 } // namespace blink | 2501 } // namespace blink |
2564 | 2502 |
2565 #endif // Heap_h | 2503 #endif // Heap_h |
OLD | NEW |