summaryrefslogtreecommitdiffstats
path: root/libmount/src/cache.c
diff options
context:
space:
mode:
authorKarel Zak2013-08-21 14:37:55 +0200
committerKarel Zak2013-08-21 14:37:55 +0200
commit0105691d541ea2be766b681b437f3082e78dd664 (patch)
treecb62628fc0d7a82b1a2c5d18d16fb730f51cd48f /libmount/src/cache.c
parentfsck: use libmount fs reference counter (diff)
downloadkernel-qcow2-util-linux-0105691d541ea2be766b681b437f3082e78dd664.tar.gz
kernel-qcow2-util-linux-0105691d541ea2be766b681b437f3082e78dd664.tar.xz
kernel-qcow2-util-linux-0105691d541ea2be766b681b437f3082e78dd664.zip
libmount: add reference counting to libmnt_cache
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libmount/src/cache.c')
-rw-r--r--libmount/src/cache.c41
1 files changed, 38 insertions, 3 deletions
diff --git a/libmount/src/cache.c b/libmount/src/cache.c
index ef1a64107..cf5c6d430 100644
--- a/libmount/src/cache.c
+++ b/libmount/src/cache.c
@@ -48,6 +48,7 @@ struct libmnt_cache {
struct mnt_cache_entry *ents;
size_t nents;
size_t nallocs;
+ int refcount;
/* blkid_evaluate_tag() works in two ways:
*
@@ -72,6 +73,7 @@ struct libmnt_cache *mnt_new_cache(void)
if (!cache)
return NULL;
DBG(CACHE, mnt_debug_h(cache, "alloc"));
+ cache->refcount = 1;
return cache;
}
@@ -89,6 +91,7 @@ void mnt_free_cache(struct libmnt_cache *cache)
return;
DBG(CACHE, mnt_debug_h(cache, "free"));
+ WARN_REFCOUNT(CACHE, cache, cache->refcount);
for (i = 0; i < cache->nents; i++) {
struct mnt_cache_entry *e = &cache->ents[i];
@@ -102,6 +105,38 @@ void mnt_free_cache(struct libmnt_cache *cache)
free(cache);
}
+/**
+ * mnt_ref_cache:
+ * @cache: cache pointer
+ *
+ * Increments reference counter.
+ */
+void mnt_ref_cache(struct libmnt_cache *cache)
+{
+ if (cache) {
+ cache->refcount++;
+ /*DBG(CACHE, mnt_debug_h(cache, "ref=%d", cache->refcount));*/
+ }
+}
+
+/**
+ * mnt_unref_cache:
+ * @cache: cache pointer
+ *
+ * De-increments reference counter, on zero the cache is automatically
+ * deallocated by mnt_free_cache().
+ */
+void mnt_unref_cache(struct libmnt_cache *cache)
+{
+ if (cache) {
+ cache->refcount--;
+ /*DBG(CACHE, mnt_debug_h(cache, "unref=%d", cache->refcount));*/
+ if (cache->refcount <= 0)
+ mnt_free_cache(cache);
+ }
+}
+
+
/* note that the @key could be the same pointer as @value */
static int cache_add_entry(struct libmnt_cache *cache, char *key,
char *value, int flag)
@@ -620,7 +655,7 @@ int test_resolve_path(struct libmnt_test *ts, int argc, char *argv[])
p = mnt_resolve_path(line, cache);
printf("%s : %s\n", line, p);
}
- mnt_free_cache(cache);
+ mnt_unref_cache(cache);
return 0;
}
@@ -643,7 +678,7 @@ int test_resolve_spec(struct libmnt_test *ts, int argc, char *argv[])
p = mnt_resolve_spec(line, cache);
printf("%s : %s\n", line, p);
}
- mnt_free_cache(cache);
+ mnt_unref_cache(cache);
return 0;
}
@@ -695,7 +730,7 @@ int test_read_tags(struct libmnt_test *ts, int argc, char *argv[])
e->key + strlen(e->key) + 1);
}
- mnt_free_cache(cache);
+ mnt_unref_cache(cache);
return 0;
}