[webkit-dev] A quick note on Ref and RefPtr changes

Andreas Kling akling at apple.com
Mon Dec 15 10:44:39 PST 2014


Howdy folks,

Thanks to the magic of C++11, we don’t need PassRef and PassRefPtr anymore, as we can use rvalue references and move semantics to achieve churn-free ownership transfers.

I’ve already removed PassRef entirely, and PassRefPtr will be next. Given how widely it’s used in the codebase, I expect it will take some time.

Here’s the endgame I’m shooting for:

Ref<T> is used for all ownership-sharing pointers known to be non-null. It cannot be null.
RefPtr<T> is used for pointers that need a null state.

Return values that were previously PassRef<T> or PassRefPtr<T> will become Ref<T> or RefPtr<T>.

Arguments that were previously PassRef<T> or PassRefPtr<T> will become Ref<T>&& or RefPtr<T>&&.

You will need to WTF::move() when transferring ownership. This is different from the PassRefPtr way, where ownership could be transferred without visual indication at the call site.

To pass a local RefPtr to a function f(RefPtr&&) *without* transferring ownership, use Ref/RefPtr::copyRef() to keep the local reference on the object.

Important note: After you’ve WTF::move()’d out of a Ref, it’s in an invalid state and cannot be used again. I’m working on a solution to turn this into a compile-time error.

Cheers,
Kling

PS. Once the kinks are ironed out, I’ll also be updating the RefPtr documentation on webkit.org.


More information about the webkit-dev mailing list