1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2011 Red Hat, Inc
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.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Authors: Alexander Larsson <alexl@redhat.com>
25 #include "gresource.h"
26 #include <gvdb/gvdb-reader.h>
27 #include <gi18n-lib.h>
28 #include <gio/gmemoryinputstream.h>
29 #include <gio/gzlibdecompressor.h>
30 #include <gio/gconverterinputstream.h>
39 static void register_lazy_static_resources (void);
41 G_DEFINE_BOXED_TYPE (GResource
, g_resource
, g_resource_ref
, g_resource_unref
)
45 * @short_description: Resource framework
48 * Applications and libraries often contain binary or textual data that is
49 * really part of the application, rather than user data. For instance
50 * #GtkBuilder .ui files, splashscreen images, GMenu markup XML, CSS files,
51 * icons, etc. These are often shipped as files in `$datadir/appname`, or
52 * manually included as literal strings in the code.
54 * The #GResource API and the [glib-compile-resources][glib-compile-resources] program
55 * provide a convenient and efficient alternative to this which has some nice properties. You
56 * maintain the files as normal files, so its easy to edit them, but during the build the files
57 * are combined into a binary bundle that is linked into the executable. This means that loading
58 * the resource files are efficient (as they are already in memory, shared with other instances) and
59 * simple (no need to check for things like I/O errors or locate the files in the filesystem). It
60 * also makes it easier to create relocatable applications.
62 * Resource files can also be marked as compressed. Such files will be included in the resource bundle
63 * in a compressed form, but will be automatically uncompressed when the resource is used. This
64 * is very useful e.g. for larger text files that are parsed once (or rarely) and then thrown away.
66 * Resource files can also be marked to be preprocessed, by setting the value of the
67 * `preprocess` attribute to a comma-separated list of preprocessing options.
68 * The only options currently supported are:
70 * `xml-stripblanks` which will use the xmllint command
71 * to strip ignorable whitespace from the XML file. For this to work,
72 * the `XMLLINT` environment variable must be set to the full path to
73 * the xmllint executable, or xmllint must be in the `PATH`; otherwise
74 * the preprocessing step is skipped.
76 * `to-pixdata` which will use the gdk-pixbuf-pixdata command to convert
77 * images to the GdkPixdata format, which allows you to create pixbufs directly using the data inside
78 * the resource file, rather than an (uncompressed) copy if it. For this, the gdk-pixbuf-pixdata
79 * program must be in the PATH, or the `GDK_PIXBUF_PIXDATA` environment variable must be
80 * set to the full path to the gdk-pixbuf-pixdata executable; otherwise the resource compiler will
83 * Resource bundles are created by the [glib-compile-resources][glib-compile-resources] program
84 * which takes an XML file that describes the bundle, and a set of files that the XML references. These
85 * are combined into a binary resource bundle.
87 * An example resource description:
89 * <?xml version="1.0" encoding="UTF-8"?>
91 * <gresource prefix="/org/gtk/Example">
92 * <file>data/splashscreen.png</file>
93 * <file compressed="true">dialog.ui</file>
94 * <file preprocess="xml-stripblanks">menumarkup.xml</file>
99 * This will create a resource bundle with the following files:
101 * /org/gtk/Example/data/splashscreen.png
102 * /org/gtk/Example/dialog.ui
103 * /org/gtk/Example/menumarkup.xml
106 * Note that all resources in the process share the same namespace, so use Java-style
107 * path prefixes (like in the above example) to avoid conflicts.
109 * You can then use [glib-compile-resources][glib-compile-resources] to compile the XML to a
110 * binary bundle that you can load with g_resource_load(). However, its more common to use the --generate-source and
111 * --generate-header arguments to create a source file and header to link directly into your application.
112 * This will generate `get_resource()`, `register_resource()` and
113 * `unregister_resource()` functions, prefixed by the `--c-name` argument passed
114 * to [glib-compile-resources][glib-compile-resources]. `get_resource()` returns
115 * the generated #GResource object. The register and unregister functions
116 * register the resource so its files can be accessed using
117 * g_resources_lookup_data().
119 * Once a #GResource has been created and registered all the data in it can be accessed globally in the process by
120 * using API calls like g_resources_open_stream() to stream the data or g_resources_lookup_data() to get a direct pointer
121 * to the data. You can also use URIs like "resource:///org/gtk/Example/data/splashscreen.png" with #GFile to access
124 * There are two forms of the generated source, the default version uses the compiler support for constructor
125 * and destructor functions (where available) to automatically create and register the #GResource on startup
126 * or library load time. If you pass --manual-register two functions to register/unregister the resource is instead
127 * created. This requires an explicit initialization call in your application/library, but it works on all platforms,
128 * even on the minor ones where this is not available. (Constructor support is available for at least Win32, Mac OS and Linux.)
130 * Note that resource data can point directly into the data segment of e.g. a library, so if you are unloading libraries
131 * during runtime you need to be very careful with keeping around pointers to data from a resource, as this goes away
132 * when the library is unloaded. However, in practice this is not generally a problem, since most resource accesses
133 * is for your own resources, and resource data is often used once, during parsing, and then released.
141 * #GStaticResource is an opaque data structure and can only be accessed
142 * using the following functions.
146 * g_resource_error_quark:
148 * Gets the #GResource Error Quark.
154 G_DEFINE_QUARK (g
-resource
-error
-quark
, g_resource_error
)
158 * @resource: A #GResource
160 * Atomically increments the reference count of @resource by one. This
161 * function is MT-safe and may be called from any thread.
163 * Returns: The passed in #GResource
168 g_resource_ref (GResource
*resource
)
170 g_atomic_int_inc (&resource
->ref_count
);
176 * @resource: A #GResource
178 * Atomically decrements the reference count of @resource by one. If the
179 * reference count drops to 0, all memory allocated by the resource is
180 * released. This function is MT-safe and may be called from any
186 g_resource_unref (GResource
*resource
)
188 if (g_atomic_int_dec_and_test (&resource
->ref_count
))
190 gvdb_table_unref (resource
->table
);
196 * g_resource_new_from_table:
197 * @table: (transfer full): a GvdbTable
199 * Returns: (transfer full): a new #GResource for @table
202 g_resource_new_from_table (GvdbTable
*table
)
206 resource
= g_new (GResource
, 1);
207 resource
->ref_count
= 1;
208 resource
->table
= table
;
214 * g_resource_new_from_data:
216 * @error: return location for a #GError, or %NULL
218 * Creates a GResource from a reference to the binary resource bundle.
219 * This will keep a reference to @data while the resource lives, so
220 * the data should not be modified or freed.
222 * If you want to use this resource in the global resource namespace you need
223 * to register it with g_resources_register().
225 * Returns: (transfer full): a new #GResource, or %NULL on error
230 g_resource_new_from_data (GBytes
*data
,
235 table
= gvdb_table_new_from_data (g_bytes_get_data (data
, NULL
),
236 g_bytes_get_size (data
),
239 (GvdbRefFunc
)g_bytes_ref
,
240 (GDestroyNotify
)g_bytes_unref
,
246 return g_resource_new_from_table (table
);
251 * @filename: (type filename): the path of a filename to load, in the GLib filename encoding
252 * @error: return location for a #GError, or %NULL
254 * Loads a binary resource bundle and creates a #GResource representation of it, allowing
255 * you to query it for data.
257 * If you want to use this resource in the global resource namespace you need
258 * to register it with g_resources_register().
260 * Returns: (transfer full): a new #GResource, or %NULL on error
265 g_resource_load (const gchar
*filename
,
270 table
= gvdb_table_new (filename
, FALSE
, error
);
274 return g_resource_new_from_table (table
);
278 gboolean
do_lookup (GResource
*resource
,
280 GResourceLookupFlags lookup_flags
,
287 char *free_path
= NULL
;
289 gboolean res
= FALSE
;
292 path_len
= strlen (path
);
293 if (path
[path_len
-1] == '/')
295 path
= free_path
= g_strdup (path
);
296 free_path
[path_len
-1] = 0;
299 value
= gvdb_table_get_raw_value (resource
->table
, path
);
303 g_set_error (error
, G_RESOURCE_ERROR
, G_RESOURCE_ERROR_NOT_FOUND
,
304 _("The resource at '%s' does not exist"),
309 guint32 _size
, _flags
;
312 g_variant_get (value
, "(uu@ay)",
317 _size
= GUINT32_FROM_LE (_size
);
318 _flags
= GUINT32_FROM_LE (_flags
);
325 *data
= g_variant_get_data (array
);
328 /* Don't report trailing newline that non-compressed files has */
329 if (_flags
& G_RESOURCE_FLAGS_COMPRESSED
)
330 *data_size
= g_variant_get_size (array
);
332 *data_size
= g_variant_get_size (array
) - 1;
334 g_variant_unref (array
);
335 g_variant_unref (value
);
345 * g_resource_open_stream:
346 * @resource: A #GResource
347 * @path: A pathname inside the resource
348 * @lookup_flags: A #GResourceLookupFlags
349 * @error: return location for a #GError, or %NULL
351 * Looks for a file at the specified @path in the resource and
352 * returns a #GInputStream that lets you read the data.
354 * @lookup_flags controls the behaviour of the lookup.
356 * Returns: (transfer full): #GInputStream or %NULL on error.
357 * Free the returned object with g_object_unref()
362 g_resource_open_stream (GResource
*resource
,
364 GResourceLookupFlags lookup_flags
,
370 GInputStream
*stream
, *stream2
;
372 if (!do_lookup (resource
, path
, lookup_flags
, NULL
, &flags
, &data
, &data_size
, error
))
375 stream
= g_memory_input_stream_new_from_data (data
, data_size
, NULL
);
376 g_object_set_data_full (G_OBJECT (stream
), "g-resource",
377 g_resource_ref (resource
),
378 (GDestroyNotify
)g_resource_unref
);
380 if (flags
& G_RESOURCE_FLAGS_COMPRESSED
)
382 GZlibDecompressor
*decompressor
=
383 g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB
);
385 stream2
= g_converter_input_stream_new (stream
, G_CONVERTER (decompressor
));
386 g_object_unref (decompressor
);
387 g_object_unref (stream
);
395 * g_resource_lookup_data:
396 * @resource: A #GResource
397 * @path: A pathname inside the resource
398 * @lookup_flags: A #GResourceLookupFlags
399 * @error: return location for a #GError, or %NULL
401 * Looks for a file at the specified @path in the resource and
402 * returns a #GBytes that lets you directly access the data in
405 * The data is always followed by a zero byte, so you
406 * can safely use the data as a C string. However, that byte
407 * is not included in the size of the GBytes.
409 * For uncompressed resource files this is a pointer directly into
410 * the resource bundle, which is typically in some readonly data section
411 * in the program binary. For compressed files we allocate memory on
412 * the heap and automatically uncompress the data.
414 * @lookup_flags controls the behaviour of the lookup.
416 * Returns: (transfer full): #GBytes or %NULL on error.
417 * Free the returned object with g_bytes_unref()
422 g_resource_lookup_data (GResource
*resource
,
424 GResourceLookupFlags lookup_flags
,
432 if (!do_lookup (resource
, path
, lookup_flags
, &size
, &flags
, &data
, &data_size
, error
))
435 if (flags
& G_RESOURCE_FLAGS_COMPRESSED
)
437 char *uncompressed
, *d
;
439 GConverterResult res
;
440 gsize d_size
, s_size
;
441 gsize bytes_read
, bytes_written
;
444 GZlibDecompressor
*decompressor
=
445 g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB
);
447 uncompressed
= g_malloc (size
+ 1);
456 res
= g_converter_convert (G_CONVERTER (decompressor
),
459 G_CONVERTER_INPUT_AT_END
,
463 if (res
== G_CONVERTER_ERROR
)
465 g_free (uncompressed
);
466 g_object_unref (decompressor
);
468 g_set_error (error
, G_RESOURCE_ERROR
, G_RESOURCE_ERROR_INTERNAL
,
469 _("The resource at '%s' failed to decompress"),
475 s_size
-= bytes_read
;
477 d_size
-= bytes_written
;
479 while (res
!= G_CONVERTER_FINISHED
);
481 uncompressed
[size
] = 0; /* Zero terminate */
483 g_object_unref (decompressor
);
485 return g_bytes_new_take (uncompressed
, size
);
488 return g_bytes_new_with_free_func (data
, data_size
, (GDestroyNotify
)g_resource_unref
, g_resource_ref (resource
));
492 * g_resource_get_info:
493 * @resource: A #GResource
494 * @path: A pathname inside the resource
495 * @lookup_flags: A #GResourceLookupFlags
496 * @size: (out) (allow-none): a location to place the length of the contents of the file,
497 * or %NULL if the length is not needed
498 * @flags: (out) (allow-none): a location to place the flags about the file,
499 * or %NULL if the length is not needed
500 * @error: return location for a #GError, or %NULL
502 * Looks for a file at the specified @path in the resource and
503 * if found returns information about it.
505 * @lookup_flags controls the behaviour of the lookup.
507 * Returns: %TRUE if the file was found. %FALSE if there were errors
512 g_resource_get_info (GResource
*resource
,
514 GResourceLookupFlags lookup_flags
,
519 return do_lookup (resource
, path
, lookup_flags
, size
, flags
, NULL
, NULL
, error
);
523 * g_resource_enumerate_children:
524 * @resource: A #GResource
525 * @path: A pathname inside the resource
526 * @lookup_flags: A #GResourceLookupFlags
527 * @error: return location for a #GError, or %NULL
529 * Returns all the names of children at the specified @path in the resource.
530 * The return result is a %NULL terminated list of strings which should
531 * be released with g_strfreev().
533 * If @path is invalid or does not exist in the #GResource,
534 * %G_RESOURCE_ERROR_NOT_FOUND will be returned.
536 * @lookup_flags controls the behaviour of the lookup.
538 * Returns: (array zero-terminated=1) (transfer full): an array of constant strings
543 g_resource_enumerate_children (GResource
*resource
,
545 GResourceLookupFlags lookup_flags
,
550 char *path_with_slash
;
554 g_set_error (error
, G_RESOURCE_ERROR
, G_RESOURCE_ERROR_NOT_FOUND
,
555 _("The resource at '%s' does not exist"),
560 path_len
= strlen (path
);
561 if (path
[path_len
-1] != '/')
562 path_with_slash
= g_strconcat (path
, "/", NULL
);
564 path_with_slash
= g_strdup (path
);
566 children
= gvdb_table_list (resource
->table
, path_with_slash
);
567 g_free (path_with_slash
);
569 if (children
== NULL
)
571 g_set_error (error
, G_RESOURCE_ERROR
, G_RESOURCE_ERROR_NOT_FOUND
,
572 _("The resource at '%s' does not exist"),
580 static GRWLock resources_lock
;
581 static GList
*registered_resources
;
583 /* This is updated atomically, so we can append to it and check for NULL outside the
584 lock, but all other accesses are done under the write lock */
585 static GStaticResource
*lazy_register_resources
;
588 g_resources_register_unlocked (GResource
*resource
)
590 registered_resources
= g_list_prepend (registered_resources
, g_resource_ref (resource
));
594 g_resources_unregister_unlocked (GResource
*resource
)
596 if (g_list_find (registered_resources
, resource
) == NULL
)
598 g_warning ("Tried to remove not registered resource");
602 registered_resources
= g_list_remove (registered_resources
, resource
);
603 g_resource_unref (resource
);
608 * g_resources_register:
609 * @resource: A #GResource
611 * Registers the resource with the process-global set of resources.
612 * Once a resource is registered the files in it can be accessed
613 * with the global resource lookup functions like g_resources_lookup_data().
618 g_resources_register (GResource
*resource
)
620 g_rw_lock_writer_lock (&resources_lock
);
621 g_resources_register_unlocked (resource
);
622 g_rw_lock_writer_unlock (&resources_lock
);
626 * g_resources_unregister:
627 * @resource: A #GResource
629 * Unregisters the resource from the process-global set of resources.
634 g_resources_unregister (GResource
*resource
)
636 g_rw_lock_writer_lock (&resources_lock
);
637 g_resources_unregister_unlocked (resource
);
638 g_rw_lock_writer_unlock (&resources_lock
);
642 * g_resources_open_stream:
643 * @path: A pathname inside the resource
644 * @lookup_flags: A #GResourceLookupFlags
645 * @error: return location for a #GError, or %NULL
647 * Looks for a file at the specified @path in the set of
648 * globally registered resources and returns a #GInputStream
649 * that lets you read the data.
651 * @lookup_flags controls the behaviour of the lookup.
653 * Returns: (transfer full): #GInputStream or %NULL on error.
654 * Free the returned object with g_object_unref()
659 g_resources_open_stream (const gchar
*path
,
660 GResourceLookupFlags lookup_flags
,
663 GInputStream
*res
= NULL
;
665 GInputStream
*stream
;
667 register_lazy_static_resources ();
669 g_rw_lock_reader_lock (&resources_lock
);
671 for (l
= registered_resources
; l
!= NULL
; l
= l
->next
)
673 GResource
*r
= l
->data
;
674 GError
*my_error
= NULL
;
676 stream
= g_resource_open_stream (r
, path
, lookup_flags
, &my_error
);
677 if (stream
== NULL
&&
678 g_error_matches (my_error
, G_RESOURCE_ERROR
, G_RESOURCE_ERROR_NOT_FOUND
))
680 g_clear_error (&my_error
);
685 g_propagate_error (error
, my_error
);
692 g_set_error (error
, G_RESOURCE_ERROR
, G_RESOURCE_ERROR_NOT_FOUND
,
693 _("The resource at '%s' does not exist"),
696 g_rw_lock_reader_unlock (&resources_lock
);
702 * g_resources_lookup_data:
703 * @path: A pathname inside the resource
704 * @lookup_flags: A #GResourceLookupFlags
705 * @error: return location for a #GError, or %NULL
707 * Looks for a file at the specified @path in the set of
708 * globally registered resources and returns a #GBytes that
709 * lets you directly access the data in memory.
711 * The data is always followed by a zero byte, so you
712 * can safely use the data as a C string. However, that byte
713 * is not included in the size of the GBytes.
715 * For uncompressed resource files this is a pointer directly into
716 * the resource bundle, which is typically in some readonly data section
717 * in the program binary. For compressed files we allocate memory on
718 * the heap and automatically uncompress the data.
720 * @lookup_flags controls the behaviour of the lookup.
722 * Returns: (transfer full): #GBytes or %NULL on error.
723 * Free the returned object with g_bytes_unref()
728 g_resources_lookup_data (const gchar
*path
,
729 GResourceLookupFlags lookup_flags
,
736 register_lazy_static_resources ();
738 g_rw_lock_reader_lock (&resources_lock
);
740 for (l
= registered_resources
; l
!= NULL
; l
= l
->next
)
742 GResource
*r
= l
->data
;
743 GError
*my_error
= NULL
;
745 data
= g_resource_lookup_data (r
, path
, lookup_flags
, &my_error
);
747 g_error_matches (my_error
, G_RESOURCE_ERROR
, G_RESOURCE_ERROR_NOT_FOUND
))
749 g_clear_error (&my_error
);
754 g_propagate_error (error
, my_error
);
761 g_set_error (error
, G_RESOURCE_ERROR
, G_RESOURCE_ERROR_NOT_FOUND
,
762 _("The resource at '%s' does not exist"),
765 g_rw_lock_reader_unlock (&resources_lock
);
771 * g_resources_enumerate_children:
772 * @path: A pathname inside the resource
773 * @lookup_flags: A #GResourceLookupFlags
774 * @error: return location for a #GError, or %NULL
776 * Returns all the names of children at the specified @path in the set of
777 * globally registered resources.
778 * The return result is a %NULL terminated list of strings which should
779 * be released with g_strfreev().
781 * @lookup_flags controls the behaviour of the lookup.
783 * Returns: (array zero-terminated=1) (transfer full): an array of constant strings
788 g_resources_enumerate_children (const gchar
*path
,
789 GResourceLookupFlags lookup_flags
,
792 GHashTable
*hash
= NULL
;
797 register_lazy_static_resources ();
799 g_rw_lock_reader_lock (&resources_lock
);
801 for (l
= registered_resources
; l
!= NULL
; l
= l
->next
)
803 GResource
*r
= l
->data
;
805 children
= g_resource_enumerate_children (r
, path
, 0, NULL
);
807 if (children
!= NULL
)
810 hash
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
812 for (i
= 0; children
[i
] != NULL
; i
++)
813 g_hash_table_insert (hash
, children
[i
], children
[i
]);
818 g_rw_lock_reader_unlock (&resources_lock
);
822 g_set_error (error
, G_RESOURCE_ERROR
, G_RESOURCE_ERROR_NOT_FOUND
,
823 _("The resource at '%s' does not exist"),
832 n_children
= g_hash_table_size (hash
);
833 children
= g_new (char *, n_children
+ 1);
836 g_hash_table_iter_init (&iter
, hash
);
837 while (g_hash_table_iter_next (&iter
, (gpointer
*)&key
, NULL
))
838 children
[i
++] = g_strdup (key
);
839 children
[i
++] = NULL
;
841 g_hash_table_destroy (hash
);
848 * g_resources_get_info:
849 * @path: A pathname inside the resource
850 * @lookup_flags: A #GResourceLookupFlags
851 * @size: (out) (allow-none): a location to place the length of the contents of the file,
852 * or %NULL if the length is not needed
853 * @flags: (out) (allow-none): a location to place the flags about the file,
854 * or %NULL if the length is not needed
855 * @error: return location for a #GError, or %NULL
857 * Looks for a file at the specified @path in the set of
858 * globally registered resources and if found returns information about it.
860 * @lookup_flags controls the behaviour of the lookup.
862 * Returns: %TRUE if the file was found. %FALSE if there were errors
867 g_resources_get_info (const gchar
*path
,
868 GResourceLookupFlags lookup_flags
,
873 gboolean res
= FALSE
;
877 register_lazy_static_resources ();
879 g_rw_lock_reader_lock (&resources_lock
);
881 for (l
= registered_resources
; l
!= NULL
; l
= l
->next
)
883 GResource
*r
= l
->data
;
884 GError
*my_error
= NULL
;
886 r_res
= g_resource_get_info (r
, path
, lookup_flags
, size
, flags
, &my_error
);
888 g_error_matches (my_error
, G_RESOURCE_ERROR
, G_RESOURCE_ERROR_NOT_FOUND
))
890 g_clear_error (&my_error
);
895 g_propagate_error (error
, my_error
);
902 g_set_error (error
, G_RESOURCE_ERROR
, G_RESOURCE_ERROR_NOT_FOUND
,
903 _("The resource at '%s' does not exist"),
906 g_rw_lock_reader_unlock (&resources_lock
);
911 /* This code is to handle registration of resources very early, from a constructor.
912 * At that point we'd like to do minimal work, to avoid ordering issues. For instance,
913 * we're not allowed to use g_malloc, as the user need to be able to call g_mem_set_vtable
914 * before the first call to g_malloc.
916 * So, what we do at construction time is that we just register a static structure on
917 * a list of resources that need to be initialized, and then later, when doing any lookups
918 * in the global list of registered resources, or when getting a reference to the
919 * lazily initialized resource we lazily create and register all the GResources on
922 * To avoid having to use locks in the constructor, and having to grab the writer lock
923 * when checking the lazy registering list we update lazy_register_resources in
924 * a lock-less fashion (atomic prepend-only, atomic replace with NULL). However, all
926 * * check if there are any resources to lazily initialize
927 * * Add a static resource to the lazy init list
928 * Do use the full writer lock for protection.
932 register_lazy_static_resources_unlocked (void)
934 GStaticResource
*list
;
937 list
= lazy_register_resources
;
938 while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources
, list
, NULL
));
942 GBytes
*bytes
= g_bytes_new_static (list
->data
, list
->data_len
);
943 GResource
*resource
= g_resource_new_from_data (bytes
, NULL
);
946 g_resources_register_unlocked (resource
);
947 g_atomic_pointer_set (&list
->resource
, resource
);
949 g_bytes_unref (bytes
);
956 register_lazy_static_resources (void)
958 if (g_atomic_pointer_get (&lazy_register_resources
) == NULL
)
961 g_rw_lock_writer_lock (&resources_lock
);
962 register_lazy_static_resources_unlocked ();
963 g_rw_lock_writer_unlock (&resources_lock
);
967 * g_static_resource_init:
968 * @static_resource: pointer to a static #GStaticResource
970 * Initializes a GResource from static data using a
973 * This is normally used by code generated by
974 * [glib-compile-resources][glib-compile-resources]
975 * and is not typically used by other code.
980 g_static_resource_init (GStaticResource
*static_resource
)
986 next
= lazy_register_resources
;
987 static_resource
->next
= next
;
989 while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources
, next
, static_resource
));
993 * g_static_resource_fini:
994 * @static_resource: pointer to a static #GStaticResource
996 * Finalized a GResource initialized by g_static_resource_init().
998 * This is normally used by code generated by
999 * [glib-compile-resources][glib-compile-resources]
1000 * and is not typically used by other code.
1005 g_static_resource_fini (GStaticResource
*static_resource
)
1007 GResource
*resource
;
1009 g_rw_lock_writer_lock (&resources_lock
);
1011 register_lazy_static_resources_unlocked ();
1013 resource
= g_atomic_pointer_get (&static_resource
->resource
);
1016 g_atomic_pointer_set (&static_resource
->resource
, NULL
);
1017 g_resources_unregister_unlocked (resource
);
1018 g_resource_unref (resource
);
1021 g_rw_lock_writer_unlock (&resources_lock
);
1025 * g_static_resource_get_resource:
1026 * @static_resource: pointer to a static #GStaticResource
1028 * Gets the GResource that was registered by a call to g_static_resource_init().
1030 * This is normally used by code generated by
1031 * [glib-compile-resources][glib-compile-resources]
1032 * and is not typically used by other code.
1034 * Returns: (transfer none): a #GResource
1039 g_static_resource_get_resource (GStaticResource
*static_resource
)
1041 register_lazy_static_resources ();
1043 return g_atomic_pointer_get (&static_resource
->resource
);