SD\_DEVICE\_REF(3) sd\_device\_ref SD\_DEVICE\_REF(3)

.SH "NAME" sd\_device\_ref, sd\_device\_unref, sd\_device\_unrefp - Create or destroy references to a device object

.SH "SYNOPSIS"

```
#include <systemd/sd-device.h>
```

.HP 600u **sd\_device* sd\_device\_ref(sd\_device \*device);**

.HP 648u **sd\_device* sd\_device\_unref(sd\_device \*device);**

.HP 528u **void sd\_device\_unrefp(sd\_device \*\*device);**

.PP **sd\_device\_ref()** increases the internal reference counter of *device* by one.

.PP **sd\_device\_unref()** decreases the internal reference counter of *device* by one. Once the reference count has dropped to zero, *device* is destroyed and cannot be used anymore, so further calls to **sd\_device\_ref()** or **sd\_device\_unref()** are illegal.

.PP **sd\_device\_unrefp()** is similar to **sd\_device\_unref()** but takes a pointer to a pointer to an **sd\_device** object. This call is useful in conjunction with GCC's and LLVM's **Clean-up Variable Attribute**\[1]. Note that this function is defined as an inline function. Use a declaration like the following, in order to allocate a device object that is freed automatically as the code block is left:

```
{


  __attribute__((cleanup(sd_device_unrefp))) sd_device *device = NULL;


  int r;


  ...


  r = sd_device_new_from_syspath(&device, "...");


  if (r < 0) {


    errno = -r;


    fprintf(stderr, "Failed to allocate device: %m\n");


  }


  ...
}
```

.PP **sd\_device\_ref()** and **sd\_device\_unref()** execute no operation if the argument is **NULL**. **sd\_device\_unrefp()** will first dereference its argument, which must not be **NULL**, and will execute no operation if *that* is **NULL**.

.SH "RETURN VALUE"

.PP **sd\_device\_ref()** always returns the argument, and **sd\_device\_unref()** always returns **NULL**.

# [HISTORY](#HISTORY)

.PP **sd\_device\_ref()**, **sd\_device\_unref()**, and **sd\_device\_unrefp()** were added in version 251.

# [NOTES](#NOTES)

1\.

Clean-up Variable Attribute

https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html

systemd 262.2

---

Document: sd_device_ref(3) from systemd 262.2-r1 (Chainguard OS)
Source: https://github.com/systemd/systemd
