| 1 |
<?xml version='1.0'?> |
| 2 |
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" |
| 3 |
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> |
| 4 |
<!-- SPDX-License-Identifier: LGPL-2.1-or-later --> |
| 5 |
|
| 6 |
<refentry id="sd_device_ref" xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 7 |
<refentryinfo> |
| 8 |
<title>sd_device_ref</title> |
| 9 |
<productname>systemd</productname> |
| 10 |
</refentryinfo> |
| 11 |
|
| 12 |
<refmeta> |
| 13 |
<refentrytitle>sd_device_ref</refentrytitle> |
| 14 |
<manvolnum>3</manvolnum> |
| 15 |
</refmeta> |
| 16 |
|
| 17 |
<refnamediv> |
| 18 |
<refname>sd_device_ref</refname> |
| 19 |
<refname>sd_device_unref</refname> |
| 20 |
<refname>sd_device_unrefp</refname> |
| 21 |
|
| 22 |
<refpurpose>Create or destroy references to a device object</refpurpose> |
| 23 |
</refnamediv> |
| 24 |
|
| 25 |
<refsynopsisdiv> |
| 26 |
<funcsynopsis> |
| 27 |
<funcsynopsisinfo>#include <systemd/sd-device.h></funcsynopsisinfo> |
| 28 |
|
| 29 |
<funcprototype> |
| 30 |
<funcdef>sd_device* <function>sd_device_ref</function></funcdef> |
| 31 |
<paramdef>sd_device *<parameter>device</parameter></paramdef> |
| 32 |
</funcprototype> |
| 33 |
|
| 34 |
<funcprototype> |
| 35 |
<funcdef>sd_device* <function>sd_device_unref</function></funcdef> |
| 36 |
<paramdef>sd_device *<parameter>device</parameter></paramdef> |
| 37 |
</funcprototype> |
| 38 |
|
| 39 |
<funcprototype> |
| 40 |
<funcdef>void <function>sd_device_unrefp</function></funcdef> |
| 41 |
<paramdef>sd_device **<parameter>device</parameter></paramdef> |
| 42 |
</funcprototype> |
| 43 |
</funcsynopsis> |
| 44 |
|
| 45 |
<para><function>sd_device_ref()</function> increases the internal reference counter of |
| 46 |
<parameter>device</parameter> by one.</para> |
| 47 |
|
| 48 |
<para><function>sd_device_unref()</function> decreases the internal reference counter of |
| 49 |
<parameter>device</parameter> by one. Once the reference count has dropped to zero, |
| 50 |
<parameter>device</parameter> is destroyed and cannot be used anymore, so further calls to |
| 51 |
<function>sd_device_ref()</function> or <function>sd_device_unref()</function> are illegal.</para> |
| 52 |
|
| 53 |
<para><function>sd_device_unrefp()</function> is similar to <function>sd_device_unref()</function> but |
| 54 |
takes a pointer to a pointer to an <type>sd_device</type> object. This call is useful in conjunction with |
| 55 |
GCC's and LLVM's <ulink url="https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html">Clean-up |
| 56 |
Variable Attribute</ulink>. Note that this function is defined as an inline function. Use a declaration |
| 57 |
like the following, in order to allocate a device object that is freed automatically as the code block is |
| 58 |
left:</para> |
| 59 |
|
| 60 |
<programlisting>{ |
| 61 |
__attribute__((cleanup(sd_device_unrefp))) sd_device *device = NULL; |
| 62 |
int r; |
| 63 |
… |
| 64 |
r = sd_device_new_from_syspath(&device, "…"); |
| 65 |
if (r < 0) { |
| 66 |
errno = -r; |
| 67 |
fprintf(stderr, "Failed to allocate device: %m\n"); |
| 68 |
} |
| 69 |
… |
| 70 |
}</programlisting> |
| 71 |
|
| 72 |
<para><function>sd_device_ref()</function> and <function>sd_device_unref()</function> execute no |
| 73 |
operation if the argument is <constant>NULL</constant>. <function>sd_device_unrefp()</function> will |
| 74 |
first dereference its argument, which must not be <constant>NULL</constant>, and will execute no |
| 75 |
operation if <emphasis>that</emphasis> is <constant>NULL</constant>.</para> |
| 76 |
</refsynopsisdiv> |
| 77 |
|
| 78 |
<refsect1> |
| 79 |
<title>Return Value</title> |
| 80 |
|
| 81 |
<para><function>sd_device_ref()</function> always returns the argument, and |
| 82 |
<function>sd_device_unref()</function> always returns <constant>NULL</constant>. |
| 83 |
</para> |
| 84 |
</refsect1> |
| 85 |
<refsect1> |
| 86 |
<title>History</title> |
| 87 |
<para><function>sd_device_ref()</function>, |
| 88 |
<function>sd_device_unref()</function>, and |
| 89 |
<function>sd_device_unrefp()</function> were added in version 251.</para> |
| 90 |
</refsect1> |
| 91 |
</refentry> |