From 47f24f2a8faf71395a1d054e9823beb000442cce Mon Sep 17 00:00:00 2001
From: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Date: Sun, 4 Dec 2022 01:33:45 +0100
Subject: Implement rudimentary reference counting for sqfs_object_t

Implement grab/drop functions to increase/decrease reference count
and destroy the object if the count drops to 0.

Make sure that all objects that maintain internal references actually
grab that reference, duplicate it in the copy function, drop it in
the destroy handler.

Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
---
 lib/common/writer/cleanup.c | 24 +++++++++++-------------
 lib/common/writer/init.c    | 25 ++++++++++++-------------
 2 files changed, 23 insertions(+), 26 deletions(-)

(limited to 'lib/common/writer')

diff --git a/lib/common/writer/cleanup.c b/lib/common/writer/cleanup.c
index 1af7a99..a3fd039 100644
--- a/lib/common/writer/cleanup.c
+++ b/lib/common/writer/cleanup.c
@@ -10,20 +10,18 @@
 
 void sqfs_writer_cleanup(sqfs_writer_t *sqfs, int status)
 {
-	if (sqfs->xwr != NULL)
-		sqfs_destroy(sqfs->xwr);
-
-	sqfs_destroy(sqfs->dirwr);
-	sqfs_destroy(sqfs->dm);
-	sqfs_destroy(sqfs->im);
-	sqfs_destroy(sqfs->idtbl);
-	sqfs_destroy(sqfs->data);
-	sqfs_destroy(sqfs->blkwr);
-	sqfs_destroy(sqfs->fragtbl);
-	sqfs_destroy(sqfs->cmp);
-	sqfs_destroy(sqfs->uncmp);
+	sqfs_drop(sqfs->xwr);
+	sqfs_drop(sqfs->dirwr);
+	sqfs_drop(sqfs->dm);
+	sqfs_drop(sqfs->im);
+	sqfs_drop(sqfs->idtbl);
+	sqfs_drop(sqfs->data);
+	sqfs_drop(sqfs->blkwr);
+	sqfs_drop(sqfs->fragtbl);
+	sqfs_drop(sqfs->cmp);
+	sqfs_drop(sqfs->uncmp);
 	fstree_cleanup(&sqfs->fs);
-	sqfs_destroy(sqfs->outfile);
+	sqfs_drop(sqfs->outfile);
 
 	if (status != EXIT_SUCCESS) {
 #if defined(_WIN32) || defined(__WINDOWS__)
diff --git a/lib/common/writer/init.c b/lib/common/writer/init.c
index 7940c3f..497fc6e 100644
--- a/lib/common/writer/init.c
+++ b/lib/common/writer/init.c
@@ -70,7 +70,7 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg)
 #ifdef WITH_LZO
 	if (cfg.id == SQFS_COMP_LZO) {
 		if (sqfs->cmp != NULL)
-			sqfs_destroy(sqfs->cmp);
+			sqfs_drop(sqfs->cmp);
 
 		ret = lzo_compressor_create(&cfg, &sqfs->cmp);
 	}
@@ -87,7 +87,7 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg)
 #ifdef WITH_LZO
 	if (cfg.id == SQFS_COMP_LZO) {
 		if (ret == 0 && sqfs->uncmp != NULL)
-			sqfs_destroy(sqfs->uncmp);
+			sqfs_drop(sqfs->uncmp);
 
 		ret = lzo_compressor_create(&cfg, &sqfs->uncmp);
 	}
@@ -193,27 +193,26 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg)
 
 	return 0;
 fail_dm:
-	sqfs_destroy(sqfs->dm);
+	sqfs_drop(sqfs->dm);
 fail_im:
-	sqfs_destroy(sqfs->im);
+	sqfs_drop(sqfs->im);
 fail_xwr:
-	if (sqfs->xwr != NULL)
-		sqfs_destroy(sqfs->xwr);
+	sqfs_drop(sqfs->xwr);
 fail_id:
-	sqfs_destroy(sqfs->idtbl);
+	sqfs_drop(sqfs->idtbl);
 fail_data:
-	sqfs_destroy(sqfs->data);
+	sqfs_drop(sqfs->data);
 fail_fragtbl:
-	sqfs_destroy(sqfs->fragtbl);
+	sqfs_drop(sqfs->fragtbl);
 fail_blkwr:
-	sqfs_destroy(sqfs->blkwr);
+	sqfs_drop(sqfs->blkwr);
 fail_uncmp:
-	sqfs_destroy(sqfs->uncmp);
+	sqfs_drop(sqfs->uncmp);
 fail_cmp:
-	sqfs_destroy(sqfs->cmp);
+	sqfs_drop(sqfs->cmp);
 fail_fs:
 	fstree_cleanup(&sqfs->fs);
 fail_file:
-	sqfs_destroy(sqfs->outfile);
+	sqfs_drop(sqfs->outfile);
 	return -1;
 }
-- 
cgit v1.2.3