]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
machine-image: add proper refcounting
authorLennart Poettering <lennart@poettering.net>
Mon, 16 Apr 2018 19:38:24 +0000 (21:38 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 24 May 2018 15:01:57 +0000 (17:01 +0200)
src/shared/machine-image.c
src/shared/machine-image.h

index 274d54013391103a35e7a46f6f3dd68e9b0b8e44..36861d93fcc891bb3dda3a39f6d16abbf923ee86 100644 (file)
@@ -61,6 +61,12 @@ Image *image_unref(Image *i) {
         if (!i)
                 return NULL;
 
+        assert(i->n_ref > 0);
+        i->n_ref--;
+
+        if (i->n_ref > 0)
+                return NULL;
+
         free(i->name);
         free(i->path);
 
@@ -71,6 +77,16 @@ Image *image_unref(Image *i) {
         return mfree(i);
 }
 
+Image *image_ref(Image *i) {
+        if (!i)
+                return NULL;
+
+        assert(i->n_ref > 0);
+        i->n_ref++;
+
+        return i;
+}
+
 static char **image_settings_path(Image *image) {
         _cleanup_strv_free_ char **l = NULL;
         const char *fn, *s;
@@ -131,6 +147,7 @@ static int image_new(
         if (!i)
                 return -ENOMEM;
 
+        i->n_ref = 1;
         i->type = t;
         i->read_only = read_only;
         i->crtime = crtime;
index f92e744f4597f4d1e58da0755ae0a71dedd0d545..899268dbd1d7508d216bb0a3b752a7a27f7a46e2 100644 (file)
@@ -34,6 +34,8 @@ typedef enum ImageType {
 } ImageType;
 
 typedef struct Image {
+        unsigned n_ref;
+
         ImageType type;
         char *name;
         char *path;
@@ -58,6 +60,8 @@ typedef struct Image {
 } Image;
 
 Image *image_unref(Image *i);
+Image *image_ref(Image *i);
+
 static inline Hashmap* image_hashmap_free(Hashmap *map) {
         return hashmap_free_with_destructor(map, image_unref);
 }