Plug a memleak in g_io_modules_scan_all_in_directory
[platform/upstream/glib.git] / gio / giomodule.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include "giomodule.h"
28 #include "giomodule-priv.h"
29 #include "glocalfilemonitor.h"
30 #include "glocaldirectorymonitor.h"
31 #include "gnativevolumemonitor.h"
32 #include "gvfs.h"
33 #ifdef G_OS_UNIX
34 #include "gdesktopappinfo.h"
35 #endif
36 #include "gioalias.h"
37 #include <glib/gstdio.h>
38
39 /**
40  * SECTION:giomodule
41  * @short_description: Loadable GIO Modules
42  * @include: gio/gio.h
43  *
44  * Provides an interface and default functions for loading and unloading 
45  * modules. This is used internally to make GIO extensible, but can also
46  * be used by others to implement module loading.
47  * 
48  **/
49
50 /**
51  * SECTION:extensionpoints
52  * @short_description: Extension Points
53  * @include: gio.h
54  * @see_also: <link linkend="extending-gio">Extending GIO</link>
55  *
56  * #GIOExtensionPoint provides a mechanism for modules to extend the
57  * functionality of the library or application that loaded it in an 
58  * organized fashion.  
59  *
60  * An extension point is identified by a name, and it may optionally
61  * require that any implementation must by of a certain type (or derived
62  * thereof). Use g_io_extension_point_register() to register an
63  * extension point, and g_io_extension_point_set_required_type() to
64  * set a required type.
65  *
66  * A module can implement an extension point by specifying the #GType 
67  * that implements the functionality. Additionally, each implementation
68  * of an extension point has a name, and a priority. Use
69  * g_io_extension_point_implement() to implement an extension point.
70  * 
71  *  |[
72  *  GIOExtensionPoint *ep;
73  *
74  *  /&ast; Register an extension point &ast;/
75  *  ep = g_io_extension_point_register ("my-extension-point");
76  *  g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
77  *  ]|
78  *
79  *  |[
80  *  /&ast; Implement an extension point &ast;/
81  *  G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
82  *  g_io_extension_point_implement ("my-extension-point",
83  *                                  my_example_impl_get_type (),
84  *                                  "my-example",
85  *                                  10);
86  *  ]|
87  *
88  *  It is up to the code that registered the extension point how
89  *  it uses the implementations that have been associated with it.
90  *  Depending on the use case, it may use all implementations, or
91  *  only the one with the highest priority, or pick a specific
92  *  one by name. 
93  */
94 struct _GIOModule {
95   GTypeModule parent_instance;
96
97   gchar       *filename;
98   GModule     *library;
99   gboolean     initialized; /* The module was loaded at least once */
100
101   void (* load)   (GIOModule *module);
102   void (* unload) (GIOModule *module);
103 };
104
105 struct _GIOModuleClass
106 {
107   GTypeModuleClass parent_class;
108
109 };
110
111 static void      g_io_module_finalize      (GObject      *object);
112 static gboolean  g_io_module_load_module   (GTypeModule  *gmodule);
113 static void      g_io_module_unload_module (GTypeModule  *gmodule);
114
115 struct _GIOExtension {
116   char *name;
117   GType type;
118   gint priority;
119 };
120
121 struct _GIOExtensionPoint {
122   GType required_type;
123   char *name;
124   GList *extensions;
125   GList *lazy_load_modules;
126 };
127
128 static GHashTable *extension_points = NULL;
129 G_LOCK_DEFINE_STATIC(extension_points);
130
131 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
132
133 static void
134 g_io_module_class_init (GIOModuleClass *class)
135 {
136   GObjectClass     *object_class      = G_OBJECT_CLASS (class);
137   GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
138
139   object_class->finalize     = g_io_module_finalize;
140
141   type_module_class->load    = g_io_module_load_module;
142   type_module_class->unload  = g_io_module_unload_module;
143 }
144
145 static void
146 g_io_module_init (GIOModule *module)
147 {
148 }
149
150 static void
151 g_io_module_finalize (GObject *object)
152 {
153   GIOModule *module = G_IO_MODULE (object);
154
155   g_free (module->filename);
156
157   G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
158 }
159
160 static gboolean
161 g_io_module_load_module (GTypeModule *gmodule)
162 {
163   GIOModule *module = G_IO_MODULE (gmodule);
164
165   if (!module->filename)
166     {
167       g_warning ("GIOModule path not set");
168       return FALSE;
169     }
170
171   module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
172
173   if (!module->library)
174     {
175       g_printerr ("%s\n", g_module_error ());
176       return FALSE;
177     }
178
179   /* Make sure that the loaded library contains the required methods */
180   if (! g_module_symbol (module->library,
181                          "g_io_module_load",
182                          (gpointer) &module->load) ||
183       ! g_module_symbol (module->library,
184                          "g_io_module_unload",
185                          (gpointer) &module->unload))
186     {
187       g_printerr ("%s\n", g_module_error ());
188       g_module_close (module->library);
189
190       return FALSE;
191     }
192
193   /* Initialize the loaded module */
194   module->load (module);
195   module->initialized = TRUE;
196
197   return TRUE;
198 }
199
200 static void
201 g_io_module_unload_module (GTypeModule *gmodule)
202 {
203   GIOModule *module = G_IO_MODULE (gmodule);
204
205   module->unload (module);
206
207   g_module_close (module->library);
208   module->library = NULL;
209
210   module->load   = NULL;
211   module->unload = NULL;
212 }
213
214 /**
215  * g_io_module_new:
216  * @filename: filename of the shared library module.
217  * 
218  * Creates a new GIOModule that will load the specific
219  * shared library when in use.
220  * 
221  * Returns: a #GIOModule from given @filename, 
222  * or %NULL on error.
223  **/
224 GIOModule *
225 g_io_module_new (const gchar *filename)
226 {
227   GIOModule *module;
228
229   g_return_val_if_fail (filename != NULL, NULL);
230
231   module = g_object_new (G_IO_TYPE_MODULE, NULL);
232   module->filename = g_strdup (filename);
233
234   return module;
235 }
236
237 static gboolean
238 is_valid_module_name (const gchar *basename)
239 {
240 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
241   return
242     g_str_has_prefix (basename, "lib") &&
243     g_str_has_suffix (basename, ".so");
244 #else
245   return g_str_has_suffix (basename, ".dll");
246 #endif
247 }
248
249 /**
250  * g_io_modules_scan_all_in_directory:
251  * @dirname: pathname for a directory containing modules to scan.
252  *
253  * Scans all the modules in the specified directory, ensuring that
254  * any extension point implemented by a module is registered.
255  *
256  * This may not actually load and initialize all the types in each
257  * module, some modules may be lazily loaded and initialized when
258  * an extension point it implementes is used with e.g.
259  * g_io_extension_point_get_extensions() or
260  * g_io_extension_point_get_extension_by_name().
261  *
262  * If you need to guarantee that all types are loaded in all the modules,
263  * use g_io_modules_scan_all_in_directory().
264  *
265  * Since: 2.24
266  **/
267 void
268 g_io_modules_scan_all_in_directory (const char *dirname)
269 {
270   const gchar *name;
271   char *filename;
272   GDir *dir;
273   struct stat statbuf;
274   char *data;
275   time_t cache_mtime;
276   GHashTable *cache;
277
278   if (!g_module_supported ())
279     return;
280
281   dir = g_dir_open (dirname, 0, NULL);
282   if (!dir)
283     return;
284
285   filename = g_build_filename (dirname, "giomodule.cache", NULL);
286
287   cache = g_hash_table_new_full (g_str_hash, g_str_equal,
288                                  g_free, (GDestroyNotify)g_strfreev);
289
290   cache_mtime = 0;
291   if (g_stat (filename, &statbuf) == 0 &&
292       g_file_get_contents (filename, &data, NULL, NULL))
293     {
294       char **lines;
295       int i;
296
297       /* Cache mtime is the time the cache file was created, any file
298        * that has a ctime before this was created then and not modified
299        * since then (userspace can't change ctime). Its possible to change
300        * the ctime forward without changing the file content, by e.g.
301        * chmoding the file, but this is uncommon and will only cause us
302        * to not use the cache so will not cause bugs.
303        */
304       cache_mtime = statbuf.st_mtime;
305
306       lines = g_strsplit (data, "\n", -1);
307       g_free (data);
308
309       for (i = 0;  lines[i] != NULL; i++)
310         {
311           char *line = lines[i];
312           char *file;
313           char *colon;
314           char **extension_points;
315
316           if (line[0] == '#')
317             continue;
318
319           colon = strchr (line, ':');
320           if (colon == NULL || line == colon)
321             continue; /* Invalid line, ignore */
322
323           *colon = 0; /* terminate filename */
324           file = g_strdup (line);
325           colon++; /* after colon */
326
327           while (g_ascii_isspace (*colon))
328             colon++;
329
330           extension_points = g_strsplit (colon, ",", -1);
331           g_hash_table_insert (cache, file, extension_points);
332         }
333       g_strfreev (lines);
334     }
335
336   while ((name = g_dir_read_name (dir)))
337     {
338       if (is_valid_module_name (name))
339         {
340           GIOExtensionPoint *extension_point;
341           GIOModule *module;
342           gchar *path;
343           char **extension_points;
344           int i;
345
346           path = g_build_filename (dirname, name, NULL);
347           module = g_io_module_new (path);
348
349           extension_points = g_hash_table_lookup (cache, name);
350           if (extension_points != NULL &&
351               g_stat (path, &statbuf) == 0 &&
352               statbuf.st_ctime <= cache_mtime)
353             {
354               /* Lazy load/init the library when first required */
355               for (i = 0; extension_points[i] != NULL; i++)
356                 {
357                   extension_point =
358                     g_io_extension_point_register (extension_points[i]);
359                   extension_point->lazy_load_modules =
360                     g_list_prepend (extension_point->lazy_load_modules,
361                                     module);
362                 }
363             }
364           else
365             {
366               /* Try to load and init types */
367               if (g_type_module_use (G_TYPE_MODULE (module)))
368                 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
369               else
370                 { /* Failure to load */
371                   g_printerr ("Failed to load module: %s\n", path);
372                   g_object_unref (module);
373                   g_free (path);
374                   continue;
375                 }
376             }
377
378           g_free (path);
379         }
380     }
381
382   g_dir_close (dir);
383
384   g_hash_table_destroy (cache);
385
386   g_free (filename);
387 }
388
389
390 /**
391  * g_io_modules_load_all_in_directory:
392  * @dirname: pathname for a directory containing modules to load.
393  *
394  * Loads all the modules in the specified directory.
395  *
396  * If don't require all modules to be initialized (and thus registering
397  * all gtypes) then you can use g_io_modules_scan_all_in_directory()
398  * which allows delayed/lazy loading of modules.
399  *
400  * Returns: a list of #GIOModules loaded from the directory,
401  *      All the modules are loaded into memory, if you want to
402  *      unload them (enabling on-demand loading) you must call
403  *      g_type_module_unuse() on all the modules. Free the list
404  *      with g_list_free().
405  **/
406 GList *
407 g_io_modules_load_all_in_directory (const char *dirname)
408 {
409   const gchar *name;
410   GDir        *dir;
411   GList *modules;
412
413   if (!g_module_supported ())
414     return NULL;
415
416   dir = g_dir_open (dirname, 0, NULL);
417   if (!dir)
418     return NULL;
419
420   modules = NULL;
421   while ((name = g_dir_read_name (dir)))
422     {
423       if (is_valid_module_name (name))
424         {
425           GIOModule *module;
426           gchar     *path;
427
428           path = g_build_filename (dirname, name, NULL);
429           module = g_io_module_new (path);
430
431           if (!g_type_module_use (G_TYPE_MODULE (module)))
432             {
433               g_printerr ("Failed to load module: %s\n", path);
434               g_object_unref (module);
435               g_free (path);
436               continue;
437             }
438           
439           g_free (path);
440
441           modules = g_list_prepend (modules, module);
442         }
443     }
444   
445   g_dir_close (dir);
446
447   return modules;
448 }
449
450 G_LOCK_DEFINE_STATIC (registered_extensions);
451 G_LOCK_DEFINE_STATIC (loaded_dirs);
452
453 extern GType _g_fen_directory_monitor_get_type (void);
454 extern GType _g_fen_file_monitor_get_type (void);
455 extern GType _g_inotify_directory_monitor_get_type (void);
456 extern GType _g_inotify_file_monitor_get_type (void);
457 extern GType _g_unix_volume_monitor_get_type (void);
458 extern GType _g_local_vfs_get_type (void);
459
460 extern GType _g_win32_volume_monitor_get_type (void);
461 extern GType g_win32_directory_monitor_get_type (void);
462 extern GType _g_winhttp_vfs_get_type (void);
463
464 #ifdef G_PLATFORM_WIN32
465
466 #include <windows.h>
467
468 static HMODULE gio_dll = NULL;
469
470 #ifdef DLL_EXPORT
471
472 BOOL WINAPI
473 DllMain (HINSTANCE hinstDLL,
474          DWORD     fdwReason,
475          LPVOID    lpvReserved)
476 {
477   if (fdwReason == DLL_PROCESS_ATTACH)
478       gio_dll = hinstDLL;
479
480   return TRUE;
481 }
482
483 #endif
484
485 #undef GIO_MODULE_DIR
486
487 /* GIO_MODULE_DIR is used only in code called just once,
488  * so no problem leaking this
489  */
490 #define GIO_MODULE_DIR \
491   g_build_filename (g_win32_get_package_installation_directory_of_module (gio_dll), \
492                     "lib/gio/modules", \
493                     NULL)
494
495 #endif
496
497 void
498 _g_io_modules_ensure_extension_points_registered (void)
499 {
500   static gboolean registered_extensions = FALSE;
501   GIOExtensionPoint *ep;
502
503   G_LOCK (registered_extensions);
504   
505   if (!registered_extensions)
506     {
507       registered_extensions = TRUE;
508       
509 #ifdef G_OS_UNIX
510       ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
511       g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
512 #endif
513       
514       ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
515       g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
516       
517       ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
518       g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
519       
520       ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
521       g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
522       
523       ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
524       g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
525       
526       ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
527       g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
528
529       ep = g_io_extension_point_register ("gsettings-backend");
530       g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
531     }
532   
533   G_UNLOCK (registered_extensions);
534  }
535
536 void
537 _g_io_modules_ensure_loaded (void)
538 {
539   static gboolean loaded_dirs = FALSE;
540   const char *module_path;
541
542   _g_io_modules_ensure_extension_points_registered ();
543   
544   G_LOCK (loaded_dirs);
545
546   if (!loaded_dirs)
547     {
548       loaded_dirs = TRUE;
549
550       g_io_modules_scan_all_in_directory (GIO_MODULE_DIR);
551
552       module_path = g_getenv ("GIO_EXTRA_MODULES");
553
554       if (module_path)
555         {
556           gchar **paths;
557           int i;
558
559           paths = g_strsplit (module_path, ":", 0);
560
561           for (i = 0; paths[i] != NULL; i++)
562             g_io_modules_scan_all_in_directory (paths[i]);
563
564           g_strfreev (paths);
565         }
566
567       /* Initialize types from built-in "modules" */
568 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
569       _g_inotify_directory_monitor_get_type ();
570       _g_inotify_file_monitor_get_type ();
571 #endif
572 #if defined(HAVE_FEN)
573       _g_fen_directory_monitor_get_type ();
574       _g_fen_file_monitor_get_type ();
575 #endif
576 #ifdef G_OS_WIN32
577       _g_win32_volume_monitor_get_type ();
578       g_win32_directory_monitor_get_type ();
579 #endif
580 #ifdef G_OS_UNIX
581       _g_unix_volume_monitor_get_type ();
582 #endif
583 #ifdef G_OS_WIN32
584       _g_winhttp_vfs_get_type ();
585 #endif
586       _g_local_vfs_get_type ();
587     }
588
589   G_UNLOCK (loaded_dirs);
590 }
591
592 static void
593 g_io_extension_point_free (GIOExtensionPoint *ep)
594 {
595   g_free (ep->name);
596   g_free (ep);
597 }
598
599 /**
600  * g_io_extension_point_register:
601  * @name: The name of the extension point
602  *
603  * Registers an extension point.
604  *
605  * Returns: the new #GIOExtensionPoint. This object is owned by GIO
606  *    and should not be freed
607  */
608 GIOExtensionPoint *
609 g_io_extension_point_register (const char *name)
610 {
611   GIOExtensionPoint *ep;
612   
613   G_LOCK (extension_points);
614   if (extension_points == NULL)
615     extension_points = g_hash_table_new_full (g_str_hash,
616                                               g_str_equal,
617                                               NULL,
618                                               (GDestroyNotify)g_io_extension_point_free);
619
620   ep = g_hash_table_lookup (extension_points, name);
621   if (ep != NULL)
622     {
623       G_UNLOCK (extension_points);
624       return ep;
625     }
626
627   ep = g_new0 (GIOExtensionPoint, 1);
628   ep->name = g_strdup (name);
629   
630   g_hash_table_insert (extension_points, ep->name, ep);
631   
632   G_UNLOCK (extension_points);
633
634   return ep;
635 }
636
637 /**
638  * g_io_extension_point_lookup:
639  * @name: the name of the extension point
640  *
641  * Looks up an existing extension point.
642  *
643  * Returns: the #GIOExtensionPoint, or %NULL if there is no
644  *    registered extension point with the given name
645  */
646 GIOExtensionPoint *
647 g_io_extension_point_lookup (const char *name)
648 {
649   GIOExtensionPoint *ep;
650
651   G_LOCK (extension_points);
652   ep = NULL;
653   if (extension_points != NULL)
654     ep = g_hash_table_lookup (extension_points, name);
655   
656   G_UNLOCK (extension_points);
657
658   return ep;
659   
660 }
661
662 /**
663  * g_io_extension_point_set_required_type:
664  * @extension_point: a #GIOExtensionPoint
665  * @type: the #GType to require
666  *
667  * Sets the required type for @extension_point to @type. 
668  * All implementations must henceforth have this type.
669  */
670 void
671 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
672                                         GType              type)
673 {
674   extension_point->required_type = type;
675 }
676
677 /**
678  * g_io_extension_point_get_required_type:
679  * @extension_point: a #GIOExtensionPoint
680  *
681  * Gets the required type for @extension_point.
682  *
683  * Returns: the #GType that all implementations must have, 
684  *     or #G_TYPE_INVALID if the extension point has no required type
685  */
686 GType
687 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
688 {
689   return extension_point->required_type;
690 }
691
692 void
693 lazy_load_modules (GIOExtensionPoint *extension_point)
694 {
695   GIOModule *module;
696   GList *l;
697
698   for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
699     {
700       module = l->data;
701
702       if (!module->initialized)
703         {
704           if (g_type_module_use (G_TYPE_MODULE (module)))
705             g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
706           else
707             g_printerr ("Failed to load module: %s\n",
708                         module->filename);
709         }
710     }
711 }
712
713 /**
714  * g_io_extension_point_get_extensions:
715  * @extension_point: a #GIOExtensionPoint
716  *
717  * Gets a list of all extensions that implement this extension point.
718  * The list is sorted by priority, beginning with the highest priority.
719  * 
720  * Returns: a #GList of #GIOExtension<!-- -->s. The list is owned by
721  *   GIO and should not be modified
722  */
723 GList *
724 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
725 {
726   lazy_load_modules (extension_point);
727   return extension_point->extensions;
728 }
729
730 /**
731  * g_io_extension_point_get_extension_by_name:
732  * @extension_point: a #GIOExtensionPoint
733  * @name: the name of the extension to get
734  *
735  * Finds a #GIOExtension for an extension point by name.
736  *
737  * Returns: the #GIOExtension for @extension_point that has the
738  *    given name, or %NULL if there is no extension with that name
739  */
740 GIOExtension *
741 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
742                                             const char        *name)
743 {
744   GList *l;
745
746   lazy_load_modules (extension_point);
747   for (l = extension_point->extensions; l != NULL; l = l->next)
748     {
749       GIOExtension *e = l->data;
750
751       if (e->name != NULL &&
752           strcmp (e->name, name) == 0)
753         return e;
754     }
755   
756   return NULL;
757 }
758
759 static gint
760 extension_prio_compare (gconstpointer  a,
761                         gconstpointer  b)
762 {
763   const GIOExtension *extension_a = a, *extension_b = b;
764
765   return extension_b->priority - extension_a->priority;
766 }
767
768 /**
769  * g_io_extension_point_implement:
770  * @extension_point_name: the name of the extension point
771  * @type: the #GType to register as extension 
772  * @extension_name: the name for the extension
773  * @priority: the priority for the extension
774  *
775  * Registers @type as extension for the extension point with name
776  * @extension_point_name. 
777  *
778  * If @type has already been registered as an extension for this 
779  * extension point, the existing #GIOExtension object is returned.
780  *
781  * Returns: a #GIOExtension object for #GType
782  */
783 GIOExtension *
784 g_io_extension_point_implement (const char *extension_point_name,
785                                 GType       type,
786                                 const char *extension_name,
787                                 gint        priority)
788 {
789   GIOExtensionPoint *extension_point;
790   GIOExtension *extension;
791   GList *l;
792
793   g_return_val_if_fail (extension_point_name != NULL, NULL);
794
795   extension_point = g_io_extension_point_lookup (extension_point_name);
796   if (extension_point == NULL)
797     {
798       g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
799       return NULL;
800     }
801   
802   if (extension_point->required_type != 0 &&
803       !g_type_is_a (type, extension_point->required_type))
804     {
805       g_warning ("Tried to register an extension of the type %s to extension point %s. "
806                  "Expected type is %s.",
807                  g_type_name (type),
808                  extension_point_name, 
809                  g_type_name (extension_point->required_type));
810       return NULL;
811     }      
812
813   /* It's safe to register the same type multiple times */
814   for (l = extension_point->extensions; l != NULL; l = l->next)
815     {
816       extension = l->data;
817       if (extension->type == type)
818         return extension;
819     }
820   
821   extension = g_slice_new0 (GIOExtension);
822   extension->type = type;
823   extension->name = g_strdup (extension_name);
824   extension->priority = priority;
825   
826   extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
827                                                       extension, extension_prio_compare);
828   
829   return extension;
830 }
831
832 /**
833  * g_io_extension_ref_class:
834  * @extension: a #GIOExtension
835  *
836  * Gets a reference to the class for the type that is 
837  * associated with @extension.
838  *
839  * Returns: the #GTypeClass for the type of @extension
840  */
841 GTypeClass *
842 g_io_extension_ref_class (GIOExtension *extension)
843 {
844   return g_type_class_ref (extension->type);
845 }
846
847 /**
848  * g_io_extension_get_type:
849  * @extension: a #GIOExtension
850  *
851  * Gets the type associated with @extension.
852  *
853  * Returns: the type of @extension
854  */
855 GType
856 g_io_extension_get_type (GIOExtension *extension)
857 {
858   return extension->type;
859 }
860
861 /**
862  * g_io_extension_get_name:
863  * @extension: a #GIOExtension
864  *
865  * Gets the name under which @extension was registered.
866  *
867  * Note that the same type may be registered as extension
868  * for multiple extension points, under different names.
869  *
870  * Returns: the name of @extension.
871  */
872 const char *
873 g_io_extension_get_name (GIOExtension *extension)
874 {
875   return extension->name;
876 }
877
878 /**
879  * g_io_extension_get_priority:
880  * @extension: a #GIOExtension
881  *
882  * Gets the priority with which @extension was registered.
883  *
884  * Returns: the priority of @extension
885  */
886 gint
887 g_io_extension_get_priority (GIOExtension *extension)
888 {
889   return extension->priority;
890 }
891
892 #define __G_IO_MODULE_C__
893 #include "gioaliasdef.c"