From 57fc2441a8c3d6fa56c0687d6a72d679eedf2ace Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Fri, 10 Sep 2010 08:18:04 +0300 Subject: [PATCH] * : make gmime initialization/de-initialization implicit (remove mu_msg_init/mu_msg_uninit) --- src/mu-cmd-extract.c | 4 --- src/mu-cmd-find.c | 4 --- src/mu-cmd-index.c | 5 ---- src/mu-cmd-view.c | 4 --- src/mu-msg.c | 53 ++++++++++++++++++--------------------- src/mu-msg.h | 19 -------------- src/tests/test-mu-msg.c | 6 +---- src/tests/test-mu-query.c | 4 --- 8 files changed, 26 insertions(+), 73 deletions(-) diff --git a/src/mu-cmd-extract.c b/src/mu-cmd-extract.c index 18838c0e..92c0f5ed 100644 --- a/src/mu-cmd-extract.c +++ b/src/mu-cmd-extract.c @@ -211,16 +211,12 @@ mu_cmd_extract (MuConfigOptions *opts) return FALSE; } - mu_msg_init(); - if (!opts->parts && !opts->save_attachments && !opts->save_all) /* show, don't save */ rv = show_parts (opts->params[1], opts); else rv = save_parts (opts->params[1], opts); /* save */ - - mu_msg_uninit(); return rv; } diff --git a/src/mu-cmd-find.c b/src/mu-cmd-find.c index d774240e..f1ed79e4 100644 --- a/src/mu-cmd-find.c +++ b/src/mu-cmd-find.c @@ -343,20 +343,16 @@ mu_cmd_find (MuConfigOptions *opts) /* first param is 'query', search params are after that */ params = (const gchar**)&opts->params[1]; - - mu_msg_init(); xapian = mu_query_new (opts->xpath); if (!xapian) { g_printerr ("Failed to create a Xapian query\n"); - mu_msg_uninit (); return FALSE; } rv = do_output (xapian, opts, params); mu_query_destroy (xapian); - mu_msg_uninit(); return rv; } diff --git a/src/mu-cmd-index.c b/src/mu-cmd-index.c index c227cfcf..bef64a38 100644 --- a/src/mu-cmd-index.c +++ b/src/mu-cmd-index.c @@ -208,14 +208,9 @@ run_index (MuIndex *midx, const char* maildir, MuIndexStats *stats, MuResult rv; mu_index_stats_clear (stats); - mu_msg_init (); - rv = mu_index_run (midx, maildir, reindex, stats, quiet ? index_msg_silent_cb :index_msg_cb, NULL, NULL); - - mu_msg_init (); - return rv; } diff --git a/src/mu-cmd-view.c b/src/mu-cmd-view.c index e78c5d86..47bd00b6 100644 --- a/src/mu-cmd-view.c +++ b/src/mu-cmd-view.c @@ -87,14 +87,10 @@ mu_cmd_view (MuConfigOptions *opts) return FALSE; } - mu_msg_init(); - rv = TRUE; for (i = 1; opts->params[i] && rv; ++i) rv = view_file (opts->params[i], NULL, opts->summary_len); - - mu_msg_uninit(); return rv; } diff --git a/src/mu-msg.c b/src/mu-msg.c index 79583673..8e53f4c6 100644 --- a/src/mu-msg.c +++ b/src/mu-msg.c @@ -32,6 +32,28 @@ #include "mu-msg.h" +static guint _refcount = 0; + +static void +ref_gmime (void) +{ + if (G_UNLIKELY(_refcount == 0)) { + srandom (getpid()*time(NULL)); + g_mime_init(0); + } + ++_refcount; +} + + +static void +unref_gmime (void) +{ + g_return_if_fail (_refcount > 0); + + --_refcount; + if (G_UNLIKELY(_refcount == 0)) + g_mime_shutdown(); +} void mu_msg_destroy (MuMsg *msg) @@ -50,6 +72,7 @@ mu_msg_destroy (MuMsg *msg) g_free (msg->_fields[i]); g_slice_free (MuMsg, msg); + unref_gmime (); } @@ -135,9 +158,9 @@ mu_msg_new (const char* filepath, const gchar* mdir) g_return_val_if_fail (filepath, NULL); + ref_gmime (); + msg = g_slice_new0 (MuMsg); - if (!msg) - return NULL; if (!init_file_metadata(msg, filepath, mdir)) { mu_msg_destroy (msg); @@ -794,29 +817,3 @@ mu_msg_get_field_numeric (MuMsg *msg, const MuMsgField* field) -static gboolean _initialized = FALSE; - -void -mu_msg_init (void) -{ - if (!_initialized) { - - srandom (getpid()*time(NULL)); - - g_mime_init(0); - _initialized = TRUE; - g_debug ("%s", __FUNCTION__); - } -} - - -void -mu_msg_uninit (void) -{ - if (_initialized) { - g_mime_shutdown(); - _initialized = FALSE; - g_debug ("%s", __FUNCTION__); - } -} - diff --git a/src/mu-msg.h b/src/mu-msg.h index 076a56d1..830f039b 100644 --- a/src/mu-msg.h +++ b/src/mu-msg.h @@ -30,25 +30,6 @@ G_BEGIN_DECLS struct _MuMsg; typedef struct _MuMsg MuMsg; -/** - * initialize the message parsing system; this function must be called - * before doing any message parsing (ie., any of the other - * mu_msg functions). when done with the message parsing system, - * call mu_msg_uninit. Note: calling this function on an already - * initialized system has no effect - */ -void mu_msg_init (void); - -/** - * uninitialize the messge parsing system that has previously been - * initialized with mu_msg_init. not calling mu_msg_uninit after - * mu_msg_init has been called will lead to memory leakage. Note: - * calling mu_msg_uninit on an uninitialized system has no - * effect - */ -void mu_msg_uninit (void); - - /** * create a new MuMsg* instance which parses a message and provides * read access to its properties; call mu_msg_destroy when done with it. diff --git a/src/tests/test-mu-msg.c b/src/tests/test-mu-msg.c index 4435a557..87869ec8 100644 --- a/src/tests/test-mu-msg.c +++ b/src/tests/test-mu-msg.c @@ -84,7 +84,6 @@ test_mu_msg_01 (void) MuMsg *msg; gint i; - mu_msg_init (); mfile = get_mailpath (1); msg = mu_msg_new (mfile, NULL); @@ -113,7 +112,6 @@ test_mu_msg_01 (void) mu_msg_destroy (msg); g_free (mfile); - mu_msg_uninit (); } @@ -150,8 +148,7 @@ test_mu_msg_02 (void) char *mfile; MuMsg *msg; int i; - - mu_msg_init (); + mfile = get_mailpath (2); msg = mu_msg_new (mfile, NULL); @@ -179,7 +176,6 @@ test_mu_msg_02 (void) mu_msg_destroy (msg); g_free (mfile); - mu_msg_uninit (); } diff --git a/src/tests/test-mu-query.c b/src/tests/test-mu-query.c index 374aca6a..619fcbf8 100644 --- a/src/tests/test-mu-query.c +++ b/src/tests/test-mu-query.c @@ -159,8 +159,6 @@ test_mu_query_04 (void) query = mu_query_new (xpath); iter = mu_query_run (query, "fünkÿ", NULL, FALSE, 1); - - mu_msg_init (); msg = mu_msg_iter_get_msg (iter); g_assert_cmpstr (mu_msg_get_subject(msg),==, @@ -169,8 +167,6 @@ test_mu_query_04 (void) " Let's write some fünkÿ text using umlauts. "); mu_msg_destroy (msg); - mu_msg_uninit (); - mu_msg_iter_destroy (iter); mu_query_destroy (query); g_free (xpath);