]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
loop-util: introduce reference counter for LoopDevice
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 11 Sep 2022 13:41:03 +0000 (22:41 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 17 Sep 2022 22:56:32 +0000 (07:56 +0900)
src/shared/loop-util.c
src/shared/loop-util.h

index 49f4b2bf6efaf704d18d1251d02ec4f9776aaec9..54251f0fb72e63929e66c71c58cf674e8232c4c1 100644 (file)
@@ -369,6 +369,7 @@ static int loop_configure(
                 return -ENOMEM;
 
         *d = (LoopDevice) {
+                .n_ref = 1,
                 .fd = TAKE_FD(loop_with_fd),
                 .lock_fd = TAKE_FD(lock_fd),
                 .node = TAKE_PTR(node),
@@ -624,7 +625,7 @@ int loop_device_make_by_path(
         return loop_device_make_internal(path, fd, open_flags, 0, 0, loop_flags, lock_op, ret);
 }
 
-LoopDevice* loop_device_unref(LoopDevice *d) {
+static LoopDevice* loop_device_free(LoopDevice *d) {
         _cleanup_close_ int control = -1;
         int r;
 
@@ -696,6 +697,8 @@ LoopDevice* loop_device_unref(LoopDevice *d) {
         return mfree(d);
 }
 
+DEFINE_TRIVIAL_REF_UNREF_FUNC(LoopDevice, loop_device, loop_device_free);
+
 void loop_device_relinquish(LoopDevice *d) {
         assert(d);
 
@@ -796,6 +799,7 @@ int loop_device_open_full(
                 return -ENOMEM;
 
         *d = (LoopDevice) {
+                .n_ref = 1,
                 .fd = TAKE_FD(fd),
                 .lock_fd = TAKE_FD(lock_fd),
                 .nr = nr,
index cb0183ba07ae7c5ae7f212ea3cc47450eddbd50a..756041adaab9b9ff88a6ec6f2e8874c8684a8d6f 100644 (file)
@@ -11,6 +11,7 @@ typedef struct LoopDevice LoopDevice;
 /* Some helpers for setting up loopback block devices */
 
 struct LoopDevice {
+        unsigned n_ref;
         int fd;
         int lock_fd;
         int nr;   /* The loopback device index (i.e. 4 for /dev/loop4); if this object encapsulates a non-loopback block device, set to -1 */
@@ -34,6 +35,7 @@ static inline int loop_device_open(const char *loop_path, int open_flags, int lo
         return loop_device_open_full(loop_path, -1, open_flags, lock_op, ret);
 }
 
+LoopDevice* loop_device_ref(LoopDevice *d);
 LoopDevice* loop_device_unref(LoopDevice *d);
 DEFINE_TRIVIAL_CLEANUP_FUNC(LoopDevice*, loop_device_unref);