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 -->
6 <refentry id="sd_device_ref" xmlns:xi="http://www.w3.org/2001/XInclude">
8 <title>sd_device_ref</title>
9 <productname>systemd</productname>
13 <refentrytitle>sd_device_ref</refentrytitle>
14 <manvolnum>3</manvolnum>
18 <refname>sd_device_ref</refname>
19 <refname>sd_device_unref</refname>
20 <refname>sd_device_unrefp</refname>
22 <refpurpose>Create or destroy references to a device object</refpurpose>
27 <funcsynopsisinfo>#include <systemd/sd-device.h></funcsynopsisinfo>
30 <funcdef>sd_device* <function>sd_device_ref</function></funcdef>
31 <paramdef>sd_device *<parameter>device</parameter></paramdef>
35 <funcdef>sd_device* <function>sd_device_unref</function></funcdef>
36 <paramdef>sd_device *<parameter>device</parameter></paramdef>
40 <funcdef>void <function>sd_device_unrefp</function></funcdef>
41 <paramdef>sd_device **<parameter>device</parameter></paramdef>
45 <para><function>sd_device_ref()</function> increases the internal reference counter of
46 <parameter>device</parameter> by one.</para>
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>
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
61 __attribute__((cleanup(sd_device_unrefp))) sd_device *device = NULL;
64 r = sd_device_new_from_syspath(&device, "…");
67 fprintf(stderr, "Failed to allocate device: %m\n");
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>
79 <title>Return Value</title>
81 <para><function>sd_device_ref()</function> always returns the argument, and
82 <function>sd_device_unref()</function> always returns <constant>NULL</constant>.
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>