[gtksourceview] init: drop gconstructor.h, add public functions to init/finalize GSV



commit 276064688813cc33eaa3c4d20c659c4c63837ccc
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Oct 28 11:38:28 2017 +0200

    init: drop gconstructor.h, add public functions to init/finalize GSV
    
    See the comment for gtk_source_finalize().
    
    Tested only on Linux.

 docs/reference/Makefile.am                    |    1 -
 docs/reference/gtksourceview-4.0-sections.txt |    7 +
 docs/reference/gtksourceview-docs.xml.in      |    5 +
 gtksourceview/Makefile.am                     |    4 +-
 gtksourceview/gconstructor.h                  |  120 ------------------
 gtksourceview/gtksource.h                     |    1 +
 gtksourceview/gtksourceinit.c                 |  163 +++++++++++++-----------
 gtksourceview/gtksourceinit.h                 |   40 ++++++
 8 files changed, 143 insertions(+), 198 deletions(-)
---
diff --git a/docs/reference/Makefile.am b/docs/reference/Makefile.am
index 6f2702d..1cc0fe9 100644
--- a/docs/reference/Makefile.am
+++ b/docs/reference/Makefile.am
@@ -34,7 +34,6 @@ EXTRA_HFILES =                                                                \
 # e.g. IGNORE_HFILES=gtkdebug.h gtkintl.h private_code
 IGNORE_HFILES =                                        \
        config.h                                \
-       gconstructor.h                          \
        gtksource.h                             \
        gtksourcebuffer-private.h               \
        gtksourcebufferinputstream.h            \
diff --git a/docs/reference/gtksourceview-4.0-sections.txt b/docs/reference/gtksourceview-4.0-sections.txt
index 3386b52..fa191e9 100644
--- a/docs/reference/gtksourceview-4.0-sections.txt
+++ b/docs/reference/gtksourceview-4.0-sections.txt
@@ -461,6 +461,13 @@ gtk_source_gutter_renderer_text_get_type
 </SECTION>
 
 <SECTION>
+<FILE>init</FILE>
+<TITLE>GtkSourceView Initialization and Finalization</TITLE>
+gtk_source_init
+gtk_source_finalize
+</SECTION>
+
+<SECTION>
 <FILE>language</FILE>
 GtkSourceLanguage
 gtk_source_language_get_id
diff --git a/docs/reference/gtksourceview-docs.xml.in b/docs/reference/gtksourceview-docs.xml.in
index 990dde8..c2f3ec7 100644
--- a/docs/reference/gtksourceview-docs.xml.in
+++ b/docs/reference/gtksourceview-docs.xml.in
@@ -17,6 +17,11 @@
   <part id="api-reference">
     <title>API reference</title>
 
+    <chapter id="general">
+      <title>General</title>
+      <xi:include href="xml/init.xml"/>
+    </chapter>
+
     <chapter id="main-classes">
       <title>Main Classes</title>
       <xi:include href="xml/buffer.xml"/>
diff --git a/gtksourceview/Makefile.am b/gtksourceview/Makefile.am
index 7605c78..02a1aea 100644
--- a/gtksourceview/Makefile.am
+++ b/gtksourceview/Makefile.am
@@ -37,6 +37,7 @@ libgtksourceview_public_headers =             \
        gtksourcegutterrenderer.h               \
        gtksourcegutterrendererpixbuf.h         \
        gtksourcegutterrenderertext.h           \
+       gtksourceinit.h                         \
        gtksourcelanguage.h                     \
        gtksourcelanguagemanager.h              \
        gtksourcemap.h                          \
@@ -75,6 +76,7 @@ libgtksourceview_public_c_files =             \
        gtksourcegutterrenderer.c               \
        gtksourcegutterrendererpixbuf.c         \
        gtksourcegutterrenderertext.c           \
+       gtksourceinit.c                         \
        gtksourcelanguage.c                     \
        gtksourcelanguagemanager.c              \
        gtksourcemap.c                          \
@@ -98,7 +100,6 @@ libgtksourceview_public_c_files =            \
        gtksourceview.c
 
 libgtksourceview_private_headers =             \
-       gconstructor.h                          \
        gtksourcebuffer-private.h               \
        gtksourcebufferinputstream.h            \
        gtksourcebufferinternal.h               \
@@ -134,7 +135,6 @@ libgtksourceview_private_c_files =  \
        gtksourceengine.c               \
        gtksourcegutterrendererlines.c  \
        gtksourcegutterrenderermarks.c  \
-       gtksourceinit.c                 \
        gtksourceiter.c                 \
        gtksourcelanguage-parser-2.c    \
        gtksourcemarkssequence.c        \
diff --git a/gtksourceview/gtksource.h b/gtksourceview/gtksource.h
index 7831ab7..e1ae114 100644
--- a/gtksourceview/gtksource.h
+++ b/gtksourceview/gtksource.h
@@ -37,6 +37,7 @@
 #include <gtksourceview/gtksourcegutterrenderer.h>
 #include <gtksourceview/gtksourcegutterrenderertext.h>
 #include <gtksourceview/gtksourcegutterrendererpixbuf.h>
+#include <gtksourceview/gtksourceinit.h>
 #include <gtksourceview/gtksourcelanguage.h>
 #include <gtksourceview/gtksourcelanguagemanager.h>
 #include <gtksourceview/gtksourcemap.h>
diff --git a/gtksourceview/gtksourceinit.c b/gtksourceview/gtksourceinit.c
index b6f0cce..a802e87 100644
--- a/gtksourceview/gtksourceinit.c
+++ b/gtksourceview/gtksourceinit.c
@@ -18,18 +18,12 @@
  * along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
-/* Constructor to init i18n.
- * Destructor to release remaining allocated memory (useful when using
- * memory-debugging tools).
- */
-
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
 
+#include "gtksourceinit.h"
 #include <glib/gi18n-lib.h>
-
-#include "gconstructor.h"
 #include "gtksourcelanguagemanager.h"
 #include "gtksourcestyleschememanager.h"
 
@@ -38,7 +32,30 @@
 #include <windows.h>
 
 static HMODULE gtk_source_dll;
-#endif
+
+BOOL WINAPI DllMain (HINSTANCE hinstDLL,
+                    DWORD     fdwReason,
+                    LPVOID    lpvReserved);
+
+BOOL WINAPI
+DllMain (HINSTANCE hinstDLL,
+        DWORD     fdwReason,
+        LPVOID    lpvReserved)
+{
+       switch (fdwReason)
+       {
+               case DLL_PROCESS_ATTACH:
+                       gtk_source_dll = hinstDLL;
+                       break;
+
+               default:
+                       /* do nothing */
+                       break;
+       }
+
+       return TRUE;
+}
+#endif /* G_OS_WIN32 */
 
 #ifdef OS_OSX
 #include <Cocoa/Cocoa.h>
@@ -115,85 +132,81 @@ get_locale_dir (void)
        return locale_dir;
 }
 
-static void
+/**
+ * gtk_source_init:
+ *
+ * Initializes the GtkSourceView library (e.g. for the internationalization).
+ *
+ * This function can be called several times, but is meant to be called at the
+ * beginning of main(), before any other GtkSourceView function call.
+ *
+ * Since: 4.0
+ */
+void
 gtk_source_init (void)
 {
-       gchar *locale_dir;
+       static gboolean done = FALSE;
 
-       locale_dir = get_locale_dir ();
-       bindtextdomain (GETTEXT_PACKAGE, locale_dir);
-       g_free (locale_dir);
-
-       bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
-}
+       if (!done)
+       {
+               gchar *locale_dir;
 
-static void
-gtk_source_shutdown (void)
-{
-       GtkSourceLanguageManager *language_manager;
-       GtkSourceStyleSchemeManager *style_scheme_manager;
+               locale_dir = get_locale_dir ();
+               bindtextdomain (GETTEXT_PACKAGE, locale_dir);
+               g_free (locale_dir);
 
-       language_manager = _gtk_source_language_manager_peek_default ();
-       g_clear_object (&language_manager);
+               bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
-       style_scheme_manager = _gtk_source_style_scheme_manager_peek_default ();
-       g_clear_object (&style_scheme_manager);
+               done = TRUE;
+       }
 }
 
-#if defined (G_OS_WIN32)
-
-BOOL WINAPI DllMain (HINSTANCE hinstDLL,
-                    DWORD     fdwReason,
-                    LPVOID    lpvReserved);
+/**
+ * gtk_source_finalize:
+ *
+ * Free the resources allocated by GtkSourceView. For example it unrefs the
+ * singleton objects.
+ *
+ * It is not mandatory to call this function, it's just to be friendlier to
+ * memory debugging tools. This function is meant to be called at the end of
+ * main(). It can be called several times.
+ *
+ * Since: 4.0
+ */
 
-BOOL WINAPI
-DllMain (HINSTANCE hinstDLL,
-        DWORD     fdwReason,
-        LPVOID    lpvReserved)
+/* Another way is to use a DSO destructor, see gconstructor.h in GLib.
+ *
+ * The advantage of calling gtk_source_finalize() at the end of main() is that
+ * gobject-list [1] correctly reports that all GtkSource* objects have been
+ * finalized when quitting the application. On the other hand a DSO destructor
+ * runs after the gobject-list's last output, so it's much less convenient, see:
+ * commit e761de9c2bee90c232875bbc41e6e73e1f63e145
+ *
+ * [1] A tool for debugging the lifetime of GObjects:
+ * https://github.com/danni/gobject-list
+ */
+void
+gtk_source_finalize (void)
 {
-       switch (fdwReason)
+       static gboolean done = FALSE;
+
+       /* Unref the singletons only once, even if this function is called
+        * multiple times, to see if a reference is not released correctly.
+        * Normally the singleton have a ref count of 1. If for some reason the
+        * ref count is increased somewhere, it needs to be decreased
+        * accordingly, at the right place.
+        */
+       if (!done)
        {
-               case DLL_PROCESS_ATTACH:
-                       gtk_source_dll = hinstDLL;
-                       gtk_source_init ();
-                       break;
+               GtkSourceLanguageManager *language_manager;
+               GtkSourceStyleSchemeManager *style_scheme_manager;
 
-               case DLL_THREAD_DETACH:
-                       gtk_source_shutdown ();
-                       break;
-
-               default:
-                       /* do nothing */
-                       break;
-       }
-
-       return TRUE;
-}
-
-#elif defined (G_HAS_CONSTRUCTORS)
+               language_manager = _gtk_source_language_manager_peek_default ();
+               g_clear_object (&language_manager);
 
-#  ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
-#    pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(gtk_source_constructor)
-#  endif
-G_DEFINE_CONSTRUCTOR (gtk_source_constructor)
+               style_scheme_manager = _gtk_source_style_scheme_manager_peek_default ();
+               g_clear_object (&style_scheme_manager);
 
-static void
-gtk_source_constructor (void)
-{
-       gtk_source_init ();
-}
-
-#  ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
-#    pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(gtk_source_destructor)
-#  endif
-G_DEFINE_DESTRUCTOR (gtk_source_destructor)
-
-static void
-gtk_source_destructor (void)
-{
-       gtk_source_shutdown ();
+               done = TRUE;
+       }
 }
-
-#else
-#  error Your platform/compiler is missing constructor support
-#endif
diff --git a/gtksourceview/gtksourceinit.h b/gtksourceview/gtksourceinit.h
new file mode 100644
index 0000000..4047d1b
--- /dev/null
+++ b/gtksourceview/gtksourceinit.h
@@ -0,0 +1,40 @@
+/*
+ * This file is part of GtkSourceView
+ *
+ * Copyright (C) 2017 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * GtkSourceView is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GTK_SOURCE_INIT_H
+#define GTK_SOURCE_INIT_H
+
+#if !defined (GTK_SOURCE_H_INSIDE) && !defined (GTK_SOURCE_COMPILATION)
+#error "Only <gtksourceview/gtksource.h> can be included directly."
+#endif
+
+#include <glib.h>
+#include <gtksourceview/gtksourceversion.h>
+
+G_BEGIN_DECLS
+
+GTK_SOURCE_AVAILABLE_IN_4_0
+void   gtk_source_init         (void);
+
+GTK_SOURCE_AVAILABLE_IN_4_0
+void   gtk_source_finalize     (void);
+
+G_END_DECLS
+
+#endif /* GTK_SOURCE_INIT_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]