]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add reference counting to fdisk_partition
authorKarel Zak <kzak@redhat.com>
Fri, 13 Dec 2013 18:26:29 +0000 (19:26 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 11 Mar 2014 10:35:12 +0000 (11:35 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
fdisks/fdisk.c
libfdisk/src/fdiskP.h
libfdisk/src/label.c
libfdisk/src/libfdisk.h
libfdisk/src/partition.c

index db3b298701627572b628e464d1757abf5f145b69..3e0d752058a17a9fe33f7efc3e86954753f4583f 100644 (file)
@@ -193,7 +193,7 @@ void change_partition_type(struct fdisk_context *cxt)
                        _("Type of partition %zu is unchanged: %s."),
                        i + 1, old);
 
-       fdisk_free_partition(pa);
+       fdisk_unref_partition(pa);
 }
 
 void list_disk_geometry(struct fdisk_context *cxt)
index 881e1b2b6a8dcf4a3a240b5070e2e0594b5af28a..2defe336752052171f8a6bb8ff966e2dfe420ad2 100644 (file)
@@ -129,6 +129,7 @@ enum {
 struct fdisk_partition {
        struct fdisk_context    *cxt;
 
+       int             refcount;
        size_t          partno;
 
        uint64_t        start;
index e243dc05f0b7b4d3b774244b723068ec1b443b8b..db9c402045b78f4abf8338961e4523fb1f2237b7 100644 (file)
@@ -412,7 +412,7 @@ done:
        if (org != cols)
                free(cols);
        tt_free_table(tb);
-       fdisk_free_partition(pa);
+       fdisk_unref_partition(pa);
        return rc;
 }
 
index 22741d80fc98c9a15cd334fdd048c439477e8fed..ef9b1a1293103f3ee913606dbb9f7539b5ae6e8d 100644 (file)
@@ -181,7 +181,8 @@ extern int fdisk_partition_toggle_flag(struct fdisk_context *cxt, size_t partnum
 
 extern struct fdisk_partition *fdisk_new_partition(void);
 extern void fdisk_reset_partition(struct fdisk_partition *pa);
-extern void fdisk_free_partition(struct fdisk_partition *pa);
+extern void fdisk_ref_partition(struct fdisk_partition *pa);
+extern void fdisk_unref_partition(struct fdisk_partition *pa);
 extern int fdisk_partition_is_freespace(struct fdisk_partition *pa);
 extern int fdisk_partition_set_start(struct fdisk_partition *pa, uint64_t off);
 extern uint64_t fdisk_partition_get_start(struct fdisk_partition *pa);
index 57c5eb976447f31da07ad4daf16c3f9b73b8aac9..3cef46c20ba1271d1671b41ebe3e7d0d85664e5d 100644 (file)
@@ -8,6 +8,7 @@ struct fdisk_partition *fdisk_new_partition(void)
 {
        struct fdisk_partition *pa = calloc(1, sizeof(*pa));
 
+       pa->refcount = 1;
        pa->partno = FDISK_EMPTY_PARTNO;
        DBG(PART, dbgprint("new %p", pa));
        return pa;
@@ -15,23 +16,38 @@ struct fdisk_partition *fdisk_new_partition(void)
 
 void fdisk_reset_partition(struct fdisk_partition *pa)
 {
+       int ref;
+
        if (!pa)
                return;
+
+       ref = pa->refcount;
        fdisk_free_parttype(pa->type);
        free(pa->name);
        free(pa->uuid);
        free(pa->attrs);
        memset(pa, 0, sizeof(*pa));
        pa->partno = FDISK_EMPTY_PARTNO;
+       pa->refcount = ref;
 }
 
-void fdisk_free_partition(struct fdisk_partition *pa)
+void fdisk_ref_partition(struct fdisk_partition *pa)
+{
+       if (pa)
+               pa->refcount++;
+}
+
+void fdisk_unref_partition(struct fdisk_partition *pa)
 {
        if (!pa)
                return;
-       fdisk_reset_partition(pa);
-       DBG(PART, dbgprint("free %p", pa));
-       free(pa);
+
+       pa->refcount--;
+       if (pa->refcount <= 0) {
+               fdisk_reset_partition(pa);
+               DBG(PART, dbgprint("free %p", pa));
+               free(pa);
+       }
 }
 
 int fdisk_partition_set_start(struct fdisk_partition *pa, uint64_t off)
@@ -212,13 +228,13 @@ int fdisk_partition_next_partno(
  *
  * For exmaple
  *
- *      struct fdisk_parition *pa = fdisk_new_partition();
+ *      struct fdisk_parition *pa;
  *
  *      fdisk_get_partition(cxt, 0, &pa);
  *     fdisk_partition_to_string(pa, FDISK_COL_UUID, &data);
  *     printf("first partition uuid: %s\n", data);
  *     free(data);
- *     fdisk_free_partition(pa);
+ *     fdisk_unref_partition(pa);
  *
  * returns UUID for the first partition.
  *