libmnt_cache
mnt_new_cache
mnt_free_cache
+mnt_ref_cache
+mnt_unref_cache
mnt_cache_device_has_tag
mnt_cache_find_tag_value
mnt_cache_read_tags
struct mnt_cache_entry *ents;
size_t nents;
size_t nallocs;
+ int refcount;
/* blkid_evaluate_tag() works in two ways:
*
if (!cache)
return NULL;
DBG(CACHE, mnt_debug_h(cache, "alloc"));
+ cache->refcount = 1;
return 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];
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)
p = mnt_resolve_path(line, cache);
printf("%s : %s\n", line, p);
}
- mnt_free_cache(cache);
+ mnt_unref_cache(cache);
return 0;
}
p = mnt_resolve_spec(line, cache);
printf("%s : %s\n", line, p);
}
- mnt_free_cache(cache);
+ mnt_unref_cache(cache);
return 0;
}
e->key + strlen(e->key) + 1);
}
- mnt_free_cache(cache);
+ mnt_unref_cache(cache);
return 0;
}
if (!(cxt->flags & MNT_FL_EXTERN_FSTAB))
mnt_free_table(cxt->fstab);
- if (!(cxt->flags & MNT_FL_EXTERN_CACHE))
- mnt_free_cache(cxt->cache);
+
+ mnt_unref_cache(cxt->cache);
mnt_context_clear_loopdev(cxt);
mnt_free_lock(cxt->lock);
*/
int mnt_context_get_fstab(struct libmnt_context *cxt, struct libmnt_table **tb)
{
- struct libmnt_cache *cache;
-
assert(cxt);
if (!cxt)
return -EINVAL;
return rc;
}
- cache = mnt_context_get_cache(cxt);
-
/* never touch an external fstab */
if (!(cxt->flags & MNT_FL_EXTERN_FSTAB))
- mnt_table_set_cache(cxt->fstab, cache);
+ mnt_table_set_cache(cxt->fstab, mnt_context_get_cache(cxt));
if (tb)
*tb = cxt->fstab;
*/
int mnt_context_get_mtab(struct libmnt_context *cxt, struct libmnt_table **tb)
{
- struct libmnt_cache *cache;
-
assert(cxt);
if (!cxt)
return -EINVAL;
return rc;
}
- cache = mnt_context_get_cache(cxt);
- mnt_table_set_cache(cxt->mtab, cache);
+ mnt_table_set_cache(cxt->mtab, mnt_context_get_cache(cxt));
if (tb)
*tb = cxt->mtab;
int mnt_context_get_table(struct libmnt_context *cxt,
const char *filename, struct libmnt_table **tb)
{
- struct libmnt_cache *cache;
int rc;
assert(cxt);
return rc;
}
- cache = mnt_context_get_cache(cxt);
- if (cache)
- mnt_table_set_cache(*tb, cache);
-
+ mnt_table_set_cache(*tb, mnt_context_get_cache(cxt));
return 0;
}
* @cxt: mount context
* @cache: cache instance or nULL
*
- * The mount context maintains a private struct libmnt_cache by default. This function
- * allows to overwrite the private cache with an external instance. Note that
- * the external instance is not deallocated by mnt_free_context().
+ * The mount context maintains a private struct libmnt_cache by default. This
+ * function allows to overwrite the private cache with an external instance.
+ * This function increments cache reference counter.
*
* If the @cache argument is NULL, then the current private cache instance is
* reset.
*
+ * The old cache instance reference counter is de-incremented.
+ *
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_set_cache(struct libmnt_context *cxt, struct libmnt_cache *cache)
{
if (!cxt)
return -EINVAL;
- if (!(cxt->flags & MNT_FL_EXTERN_CACHE))
- mnt_free_cache(cxt->cache);
- set_flag(cxt, MNT_FL_EXTERN_CACHE, cache != NULL);
+ mnt_ref_cache(cache); /* new */
+ mnt_unref_cache(cxt->cache); /* old */
+
cxt->cache = cache;
return 0;
}
cxt->cache = mnt_new_cache();
if (!cxt->cache)
return NULL;
- cxt->flags &= ~MNT_FL_EXTERN_CACHE;
}
return cxt->cache;
}
__ul_attribute__((warn_unused_result));
extern void mnt_free_cache(struct libmnt_cache *cache);
+extern void mnt_ref_cache(struct libmnt_cache *cache);
+extern void mnt_unref_cache(struct libmnt_cache *cache);
+
extern int mnt_cache_read_tags(struct libmnt_cache *cache, const char *devname);
extern int mnt_cache_device_has_tag(struct libmnt_cache *cache,
mnt_fs_append_comment;
mnt_fs_get_comment;
mnt_fs_set_comment;
+ mnt_ref_cache;
mnt_ref_fs;
mnt_table_append_intro_comment;
mnt_table_append_trailing_comment;
mnt_table_set_userdata;
mnt_table_with_comments;
mnt_table_write_file;
+ mnt_unref_cache;
mnt_unref_fs;
} MOUNT_2.23;
return;
mnt_reset_table(tb);
+ mnt_unref_cache(tb->cache);
DBG(TAB, mnt_debug_h(tb, "free"));
free(tb->comm_intro);
* same cache between more threads -- currently the cache does not provide any
* locking method.
*
+ * This function increments cache refrence counter. It's recomented to use
+ * mnt_unref_cache() after mnt_table_set_cache() if you want to keep the cache
+ * referenced by @tb only.
+ *
* See also mnt_new_cache().
*
* Returns: 0 on success or negative number in case of error.
assert(tb);
if (!tb)
return -EINVAL;
+
+ mnt_ref_cache(mpc); /* new */
+ mnt_unref_cache(tb->cache); /* old */
tb->cache = mpc;
return 0;
}
if (!mpc)
goto done;
mnt_table_set_cache(tb, mpc);
+ mnt_unref_cache(mpc);
if (strcasecmp(find, "source") == 0)
fs = mnt_table_find_source(tb, what, dr);
}
done:
mnt_free_table(tb);
- mnt_free_cache(mpc);
return rc;
}
if (!mpc)
goto done;
mnt_table_set_cache(tb, mpc);
+ mnt_unref_cache(mpc);
fs = mnt_table_find_pair(tb, argv[2], argv[3], MNT_ITER_FORWARD);
if (!fs)
rc = 0;
done:
mnt_free_table(tb);
- mnt_free_cache(mpc);
return rc;
}
if (!mpc)
goto done;
mnt_table_set_cache(tb, mpc);
+ mnt_unref_cache(mpc);
fs = mnt_table_find_mountpoint(tb, argv[1], MNT_ITER_BACKWARD);
if (!fs)
rc = 0;
done:
mnt_free_table(tb);
- mnt_free_cache(mpc);
return rc;
}
if (!mpc)
goto done;
mnt_table_set_cache(tb, mpc);
+ mnt_unref_cache(mpc);
while(mnt_table_next_fs(fstab, itr, &fs) == 0) {
if (mnt_table_is_fs_mounted(tb, fs))
done:
mnt_free_table(tb);
mnt_free_table(fstab);
- mnt_free_cache(mpc);
mnt_free_iter(itr);
return rc;
}