From: Philippe Proulx Date: Wed, 25 Jan 2017 08:18:14 +0000 (-0500) Subject: API doc: add more details about reference counting X-Git-Tag: v2.0.0-pre1~559 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=223f6c6a53890cd17ee15fd828ef153d2e46a398 API doc: add more details about reference counting Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- diff --git a/doc/api/images/ref-count-api-returns.png b/doc/api/images/ref-count-api-returns.png new file mode 100644 index 00000000..e8f34717 Binary files /dev/null and b/doc/api/images/ref-count-api-returns.png differ diff --git a/doc/api/images/ref-count-callback.png b/doc/api/images/ref-count-callback.png new file mode 100644 index 00000000..4e758627 Binary files /dev/null and b/doc/api/images/ref-count-callback.png differ diff --git a/doc/api/images/ref-count-user-calls.png b/doc/api/images/ref-count-user-calls.png new file mode 100644 index 00000000..0502c8fb Binary files /dev/null and b/doc/api/images/ref-count-user-calls.png differ diff --git a/include/babeltrace/ref.h b/include/babeltrace/ref.h index bb48f91e..9a74a43c 100644 --- a/include/babeltrace/ref.h +++ b/include/babeltrace/ref.h @@ -40,15 +40,46 @@ The macros and functions of this module are everything that is needed to handle the reference counting of Babeltrace objects. -All Babeltrace objects can be shared by multiple owners thanks to +Any Babeltrace object can be shared by multiple owners thanks to reference -counting. A function which returns a Babeltrace object owned -by another one increments its reference count so that the caller -becomes an owner too. +counting. -When a Babeltrace object is created, its reference count is initialized -to 1. It is your responsibility to discard the object when you don't -need it anymore with bt_put(). +The Babeltrace C API complies with the following key principles: + +1. When you call an API function which accepts a Babeltrace object + pointer as a parameter, the API function borrows the + reference for the duration of the function. + + @image html ref-count-user-calls.png + + The API function can also get a new reference if the system needs a + more persistent reference, but the ownership is never + transferred from the caller to the API function. + + In other words, the caller still owns the object after calling any + API function: no function "steals" the user's reference (except + bt_put()). + +2. An API function which \em returns a Babeltrace object pointer to the + user returns a new reference. The caller becomes an + owner of the object. + + @image html ref-count-api-returns.png + + It is your responsibility to discard the object when you don't + need it anymore with bt_put(). + + For example, see bt_value_array_get(). + +3. A Babeltrace object pointer received as a parameter in a user + function called back from an API function is a + borrowed, or weak reference: if you + need a reference which is more persistent that the duration of the + user function, call bt_get() on the pointer. + + @image html ref-count-callback.png + + For example, see bt_value_map_foreach(). The two macros BT_PUT() and BT_MOVE() operate on \em variables rather than pointer values. You should use BT_PUT() instead of bt_put() when