[PATCH 06/23] lib/open: allocate notmuch_t struct early

Subject: [PATCH 06/23] lib/open: allocate notmuch_t struct early

Date: Sun, 7 Feb 2021 20:40:52 -0400

To: notmuch@notmuchmail.org

Cc: David Bremner

From: David Bremner


This gives more flexibility in restructuring the database opening
code.
---
 lib/open.cc | 46 +++++++++++++++++++++++++++++-----------------
 1 file changed, 29 insertions(+), 17 deletions(-)

diff --git a/lib/open.cc b/lib/open.cc
index c200bed6..09fec6e4 100644
--- a/lib/open.cc
+++ b/lib/open.cc
@@ -186,6 +186,20 @@ _choose_database_path (const char *config_path,
     return NOTMUCH_STATUS_SUCCESS;
 }
 
+notmuch_database_t * _alloc_notmuch() {
+    notmuch_database_t * notmuch;
+    notmuch = talloc_zero (NULL, notmuch_database_t);
+    if (!notmuch)
+	return NULL;
+
+    notmuch->exception_reported = false;
+    notmuch->status_string = NULL;
+    notmuch->writable_xapian_db = NULL;
+    notmuch->atomic_nesting = 0;
+    notmuch->view = 1;
+    return notmuch;
+}
+
 notmuch_status_t
 notmuch_database_open_with_config (const char *database_path,
 				   notmuch_database_mode_t mode,
@@ -205,9 +219,18 @@ notmuch_database_open_with_config (const char *database_path,
     GKeyFile *key_file = NULL;
     static int initialized = 0;
 
+    notmuch = _alloc_notmuch ();
+    if (!notmuch) {
+	status = NOTMUCH_STATUS_OUT_OF_MEMORY;
+	goto DONE;
+    }
+
     if ((status = _choose_database_path (config_path, profile, &key_file, &database_path, &message)))
 	goto DONE;
 
+    notmuch->path = talloc_strdup (notmuch, database_path);
+    strip_trailing (notmuch->path, '/');
+
     if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) {
 	message = strdup ("Out of memory\n");
 	status = NOTMUCH_STATUS_OUT_OF_MEMORY;
@@ -222,6 +245,12 @@ notmuch_database_open_with_config (const char *database_path,
 	goto DONE;
     }
 
+    if (! (notmuch->xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
+	message = strdup ("Out of memory\n");
+	status = NOTMUCH_STATUS_OUT_OF_MEMORY;
+	goto DONE;
+    }
+
     /* Initialize the GLib type system and threads */
 #if ! GLIB_CHECK_VERSION (2, 35, 1)
     g_type_init ();
@@ -233,23 +262,6 @@ notmuch_database_open_with_config (const char *database_path,
 	initialized = 1;
     }
 
-    notmuch = talloc_zero (NULL, notmuch_database_t);
-    notmuch->exception_reported = false;
-    notmuch->status_string = NULL;
-    notmuch->path = talloc_strdup (notmuch, database_path);
-
-    strip_trailing (notmuch->path, '/');
-
-    notmuch->writable_xapian_db = NULL;
-    notmuch->atomic_nesting = 0;
-    notmuch->view = 1;
-
-    if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
-	message = strdup ("Out of memory\n");
-	status = NOTMUCH_STATUS_OUT_OF_MEMORY;
-	goto DONE;
-    }
-
     try {
 	std::string last_thread_id;
 	std::string last_mod;
-- 
2.30.0
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: