]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-storage: Added refcounting support for namespaces
authorTimo Sirainen <tss@iki.fi>
Mon, 26 Jul 2010 18:10:52 +0000 (19:10 +0100)
committerTimo Sirainen <tss@iki.fi>
Mon, 26 Jul 2010 18:10:52 +0000 (19:10 +0100)
src/lib-storage/mail-namespace.c
src/lib-storage/mail-namespace.h

index f587f19929e367a88c21611e0ece14f856b9fe6f..769847c2f1851f8c5cb6ee8c9b0437850c471723 100644 (file)
@@ -68,6 +68,7 @@ namespace_add(struct mail_user *user,
        const char *driver, *error;
 
        ns = i_new(struct mail_namespace, 1);
+       ns->refcount = 1;
        ns->user = user;
        if (strncmp(ns_set->type, "private", 7) == 0) {
                ns->owner = user;
@@ -303,6 +304,7 @@ int mail_namespaces_init(struct mail_user *user, const char **error_r)
 
        /* no namespaces defined, create a default one */
        ns = i_new(struct mail_namespace, 1);
+       ns->refcount = 1;
        ns->type = NAMESPACE_PRIVATE;
        ns->flags = NAMESPACE_FLAG_INBOX_USER | NAMESPACE_FLAG_INBOX_ANY |
                NAMESPACE_FLAG_LIST_PREFIX | NAMESPACE_FLAG_SUBSCRIPTIONS;
@@ -376,6 +378,7 @@ struct mail_namespace *mail_namespaces_init_empty(struct mail_user *user)
        struct mail_namespace *ns;
 
        ns = i_new(struct mail_namespace, 1);
+       ns->refcount = 1;
        ns->user = user;
        ns->owner = user;
        ns->prefix = i_strdup("");
@@ -412,6 +415,28 @@ void mail_namespaces_set_storage_callbacks(struct mail_namespace *namespaces,
                mail_storage_set_callbacks(ns->storage, callbacks, context);
 }
 
+void mail_namespace_ref(struct mail_namespace *ns)
+{
+       i_assert(ns->refcount > 0);
+
+       ns->refcount++;
+}
+
+void mail_namespace_unref(struct mail_namespace **_ns)
+{
+       struct mail_namespace *ns = *_ns;
+
+       i_assert(ns->refcount > 0);
+
+       *_ns = NULL;
+
+       if (--ns->refcount > 0)
+               return;
+
+       i_assert(ns->destroyed);
+       mail_namespace_free(ns);
+}
+
 void mail_namespace_destroy(struct mail_namespace *ns)
 {
        struct mail_namespace **nsp;
@@ -423,8 +448,9 @@ void mail_namespace_destroy(struct mail_namespace *ns)
                        break;
                }
        }
+       ns->destroyed = TRUE;
 
-       mail_namespace_free(ns);
+       mail_namespace_unref(&ns);
 }
 
 const char *mail_namespace_fix_sep(struct mail_namespace *ns, const char *name)
index e206c391c7d04b53b6333d957ed4dee70990d9a8..5c87f131c5f69496ffd591f16f7f93c39429c1d8 100644 (file)
@@ -43,6 +43,7 @@ enum namespace_flags {
 struct mail_namespace {
        /* Namespaces are sorted by their prefix length, "" comes first */
        struct mail_namespace *next;
+       int refcount;
 
         enum namespace_type type;
        char sep, real_sep, sep_str[3];
@@ -68,6 +69,8 @@ struct mail_namespace {
 
        const struct mail_namespace_settings *set, *unexpanded_set;
        const struct mail_storage_settings *mail_set;
+
+       unsigned int destroyed:1;
 };
 
 int mail_namespaces_init(struct mail_user *user, const char **error_r);
@@ -76,6 +79,9 @@ struct mail_namespace *mail_namespaces_init_empty(struct mail_user *user);
    for user's namespaces. */
 void mail_namespaces_deinit(struct mail_namespace **namespaces);
 
+void mail_namespace_ref(struct mail_namespace *ns);
+void mail_namespace_unref(struct mail_namespace **ns);
+
 /* Set storage callback functions to use in all namespaces. */
 void mail_namespaces_set_storage_callbacks(struct mail_namespace *namespaces,
                                           struct mail_storage_callbacks *callbacks,