SD_DEVICE_REF(3)
πΈ sd_device_ref(3), sd_device_unref(3), sd_device_unrefp(3) β the device ref-counting trio πΈ
okay bestie, this is the SD_DEVICE_REF(3) manual page, and we're talking about creating and destroying references to a device object. It's giving βI hold a tiny handle to a device and I refuse to leak itβ energy. π
β¨
β¨ NAME β¨
sd_device_ref, sd_device_unref, sd_device_unrefp β create or destroy references to a device object. That's the whole vibe: ref it, unref it, or unrefp it with the cleanup-attribute magic. π«Ά
π¦ SYNOPSIS π¦
#include <systemd/sd-device.h>
sd_device* sd_device_ref(sd_device *device);
sd_device* sd_device_unref(sd_device *device);
void sd_device_unrefp(sd_device **device);
π DESCRIPTION π
sd_device_ref() increases the internal reference counter of device by one. One more little grip on the device, bestie. π
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. Not βoopsieβ illegal β like, actually illegal, so do not. π
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");
}
...
}
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. So yes, the outer pointer has to actually exist; the inner one can be NULL and then it just chills. π§
π₯ RETURN VALUE π₯
sd_device_ref() always returns the argument, and sd_device_unref() always returns NULL. No surprises, bestie. β¨
π HISTORY π
sd_device_ref(), sd_device_unref(), and sd_device_unrefp() were added in version 251. So they're not ancient relics; they're version 251 babies. π
π NOTES π
- Clean-up Variable Attribute β
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
okay that's the whole page, bestie. The footer says source/version systemd 261.3 and the manual name SD_DEVICE_REF(3); there's no date printed on this footer, so I'm not inventing one. bye bestie, go unref responsibly ππ«Άβ¨
π¦ where this page comes from
- ubuntu stonking: libsystemd-dev 261.3-0ubuntu3 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms