OLD | NEW |
1 # Blink GC API reference | 1 # Blink GC API reference |
2 | 2 |
3 This is a through document for Oilpan API usage. | 3 This is a through document for Oilpan API usage. |
4 If you want to learn the API usage quickly, look at | 4 If you want to learn the API usage quickly, look at |
5 [this tutorial](https://docs.google.com/presentation/d/1XPu03ymz8W295mCftEC9KshH
9Icxfq81YwIJQzQrvxo/edit#slide=id.p). | 5 [this tutorial](https://docs.google.com/presentation/d/1XPu03ymz8W295mCftEC9KshH
9Icxfq81YwIJQzQrvxo/edit#slide=id.p). |
6 | 6 |
7 [TOC] | 7 [TOC] |
8 | 8 |
9 ## Header file | 9 ## Header file |
10 | 10 |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 ``` | 328 ``` |
329 | 329 |
330 Specifically, if your class needs a tracing method, you need to: | 330 Specifically, if your class needs a tracing method, you need to: |
331 | 331 |
332 * Declare a tracing method in your class declaration, using the `DECLARE_TRACE
()` macro; and | 332 * Declare a tracing method in your class declaration, using the `DECLARE_TRACE
()` macro; and |
333 * Define a tracing method in your implementation file, using the `DEFINE_TRACE
(ClassName)` macro. | 333 * Define a tracing method in your implementation file, using the `DEFINE_TRACE
(ClassName)` macro. |
334 | 334 |
335 The function implementation must contain: | 335 The function implementation must contain: |
336 | 336 |
337 * For each on-heap object `m_object` in your class, a tracing call: "```visito
r->trace(m_object);```". | 337 * For each on-heap object `m_object` in your class, a tracing call: "```visito
r->trace(m_object);```". |
| 338 * If your class has one or more weak references (`WeakMember<T>`), you have th
e option of |
| 339 registering a *weak callback* for the object. See details below for how. |
338 * For each base class of your class `BaseClass` that is a descendant of `Garba
geCollected<T>` or | 340 * For each base class of your class `BaseClass` that is a descendant of `Garba
geCollected<T>` or |
339 `GarbageCollectedMixin`, delegation call to base class: "```BaseClass::trace
(visitor);```" | 341 `GarbageCollectedMixin`, a delegation call to base class: "```BaseClass::tra
ce(visitor);```" |
340 | 342 |
341 It is recommended that the delegation call, if any, is put at the end of a traci
ng method. | 343 It is recommended that the delegation call, if any, is put at the end of a traci
ng method. |
342 | 344 |
343 If the class does not contain any on-heap object, the tracing method is not need
ed. | 345 If the class does not contain any on-heap object, the tracing method is not need
ed. |
344 | 346 |
345 If you want to define your tracing method inline or need to have your tracing me
thod polymorphic, you can use the | 347 If you want to define your tracing method inline or need to have your tracing me
thod polymorphic, you can use the |
346 following variants of the tracing macros: | 348 following variants of the tracing macros: |
347 | 349 |
348 * "```DECLARE_VIRTUAL_TRACE();```" in a class declaration makes the method ```
virtual```. Use | 350 * "```DECLARE_VIRTUAL_TRACE();```" in a class declaration makes the method ```
virtual```. Use |
349 "```DEFINE_TRACE(ClassName) { ... }```" in the implementation file to define
. | 351 "```DEFINE_TRACE(ClassName) { ... }```" in the implementation file to define
. |
(...skipping 25 matching lines...) Expand all Loading... |
375 | 377 |
376 DEFINE_TRACE(C) | 378 DEFINE_TRACE(C) |
377 { | 379 { |
378 visitor->trace(m_x); | 380 visitor->trace(m_x); |
379 visitor->trace(m_y); // Weak member needs to be traced. | 381 visitor->trace(m_y); // Weak member needs to be traced. |
380 visitor->trace(m_z); // Heap collection does, too. | 382 visitor->trace(m_z); // Heap collection does, too. |
381 B::trace(visitor); // Delegate to the parent. In this case it's empty, but t
his is required. | 383 B::trace(visitor); // Delegate to the parent. In this case it's empty, but t
his is required. |
382 } | 384 } |
383 ``` | 385 ``` |
384 | 386 |
| 387 Given that the class `C` above contained a `WeakMember<Y>` field, you could alte
rnatively |
| 388 register a *weak callback* in the trace method, and have it be invoked after the
marking |
| 389 phase: |
| 390 |
| 391 ```c++ |
| 392 |
| 393 void C::clearWeakMembers(Visitor* visitor) |
| 394 { |
| 395 if (ThreadHeap::isHeapObjectAlive(m_y)) |
| 396 return; |
| 397 |
| 398 // |m_y| is not referred to by anyone else, clear the weak |
| 399 // reference along with updating state / clearing any other |
| 400 // resources at the same time. None of those operations are |
| 401 // allowed to perform heap allocations: |
| 402 m_y->detach(); |
| 403 |
| 404 // Note: if the weak callback merely clears the weak reference, |
| 405 // it is much simpler to just |trace| the field rather than |
| 406 // install a custom weak callback. |
| 407 m_y = nullptr; |
| 408 } |
| 409 |
| 410 DEFINE_TRACE(C) |
| 411 { |
| 412 visitor->template registerWeakMembers<C, &C::clearWeakMembers>(this); |
| 413 visitor->trace(m_x); |
| 414 visitor->trace(m_z); // Heap collection does, too. |
| 415 B::trace(visitor); // Delegate to the parent. In this case it's empty, but t
his is required. |
| 416 } |
| 417 ``` |
| 418 |
| 419 Please notice that if the object (of type `C`) is also not reachable, its `trace
` method |
| 420 will not be invoked and any follow-on weak processing will not be done. Hence, i
f the |
| 421 object must always perform some operation when the weak reference is cleared, th
at |
| 422 needs to (also) happen during finalization. |
| 423 |
| 424 Weak callbacks have so far seen little use in Blink, but a mechanism that's avai
lable. |
| 425 |
385 ## Heap collections | 426 ## Heap collections |
OLD | NEW |