From 3cd1db09d4fce302d03c0fa04e7dc501026a2bae Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 15 May 2019 14:07:14 +0200 Subject: [PATCH] Documentation: Add a brief description of Serenity's smart pointer types. --- Documentation/SmartPointers.md | 70 ++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Documentation/SmartPointers.md diff --git a/Documentation/SmartPointers.md b/Documentation/SmartPointers.md new file mode 100644 index 00000000000..87cad55ea59 --- /dev/null +++ b/Documentation/SmartPointers.md @@ -0,0 +1,70 @@ +# Serenity smart pointers + +---- +## Introduction + +There are three main C++ smart pointer types used in the Serenity operating system. Each type describes the ownership (or lack thereof) of the pointee. + +The reason for using these pointers is to make it explicit through code who owns which resources, and how ownership is transferred. They also serve as a guard against memory leaks and use-after-free bugs. + + +---- +## OwnPtr + +OwnPtr is used for single-owner objects. An object held by an OwnPtr is owned by that OwnPtr, and not by anybody else. + +This means that the OwnPtr is responsible for deleting the pointee when the OwnPtr goes out of scope. + +There is a make() helper that creates a new object and returns it wrapped in an OwnPtr. + + { + OwnPtr my_object = make(); + my_object->do_stuff(); + // my_object goes out of scope here, and the Foo will be deleted. + } + + +---- +## RetainPtr and Retained + +RetainPtr is used for multiple-owner objects. An object held by a RetainPtr is owned together by every pointer pointing to that object. + +Shared ownership is implemented via retain counting (or "reference counting", as some people call it.) + +Retained is a special variant of RetainPtr with one additional property: it cannot be null. Retained is suitable as a return type from functions that are guaranteed to never return null, and as an argument type where the argument may not be null. In other words, if RetainPtr is "\*", then Retained is "&". + +Objects can only be held by RetainPtr if they meet certain criteria. Specifically, they need to implement the functions `retain()` and `release()`. + +To make a class T retainable, you can simply make it inherit from Retainable. This will add all the necessary pieces to T. + +**Note:** When constructing a Retainable-derived class, the retain count starts out at 1 (since 0 would mean that the object has no owners and should be deleted.) The object must therefore be "adopted" by someone who takes responsibility of that 1. This is done through the global `adopt()` function: + + class Bar : public Retainable { + ... + }; + + RetainPtr our_object = adopt(*new Bar); + RetainPtr another_owner = our_object; + +In the above example, the Bar object will only be deleted once both "our\_object" and "another\_owner" are gone. + +---- +## WeakPtr + +WeakPtr is used for object that somebody else owns. When the pointee of a WeakPtr is deleted, the WeakPtr will magically become null. + +Behind the scenes, this is implemented using the Weakable template. If you want to make it possible for a class to be weakly-pointed-to, have it inherit from Weakable. + +To create a WeakPtr, use `make_weak_ptr()`: + + class Baz : public Weakable { + .... + }; + + WeakPtr a_baz; + { + OwnPtr my_baz = make(); + a_baz = my_baz->make_weak_ptr(); + // a_baz now points to my_baz + } + // a_baz is now null, since my_baz went out of scope.