diff --git a/girepository/girepository.c b/girepository/girepository.c index 83ec69e52..9ecf6086b 100644 --- a/girepository/girepository.c +++ b/girepository/girepository.c @@ -1753,7 +1753,12 @@ require_internal (GIRepository *repository, { GError *temp_error = NULL; - typelib = gi_typelib_new_from_mapped_file (mfile, &temp_error); + GBytes *bytes = NULL; + + bytes = g_mapped_file_get_bytes (mfile); + typelib = gi_typelib_new_from_bytes (bytes, &temp_error); + g_bytes_unref (bytes); + if (!typelib) { g_set_error (error, GI_REPOSITORY_ERROR, diff --git a/girepository/girmodule.c b/girepository/girmodule.c index da4519d7f..e595f7a46 100644 --- a/girepository/girmodule.c +++ b/girepository/girmodule.c @@ -324,6 +324,7 @@ GITypelib * gi_ir_module_build_typelib (GIIrModule *module) { GError *error = NULL; + GBytes *bytes = NULL; GITypelib *typelib; size_t length; size_t i; @@ -574,7 +575,11 @@ gi_ir_module_build_typelib (GIIrModule *module) header = (Header *)data; length = header->size = offset2; - typelib = gi_typelib_new_from_memory (data, length, &error); + + bytes = g_bytes_new_take (g_steal_pointer (&data), length); + typelib = gi_typelib_new_from_bytes (bytes, &error); + g_bytes_unref (bytes); + if (!typelib) { g_error ("error building typelib: %s", diff --git a/girepository/gitypelib-internal.h b/girepository/gitypelib-internal.h index 94626055d..271a17967 100644 --- a/girepository/gitypelib-internal.h +++ b/girepository/gitypelib-internal.h @@ -1313,10 +1313,9 @@ typedef struct { struct _GITypelib { /*< private >*/ - uint8_t *data; + const uint8_t *data; /* just a cached pointer to inside @bytes */ size_t len; - gboolean owns_memory; - GMappedFile *mfile; + GBytes *bytes; /* (owned) */ GList *modules; gboolean open_attempted; }; diff --git a/girepository/gitypelib.c b/girepository/gitypelib.c index e2d1abec6..8c6d94a1d 100644 --- a/girepository/gitypelib.c +++ b/girepository/gitypelib.c @@ -2359,95 +2359,34 @@ gi_typelib_ensure_open (GITypelib *typelib) } /** - * gi_typelib_new_from_memory: (skip) - * @memory: (array length=len): address of memory chunk containing the typelib - * @len: length of memory chunk containing the typelib, in bytes + * gi_typelib_new_from_bytes: + * @bytes: memory chunk containing the typelib * @error: a [type@GLib.Error] * - * Creates a new [type@GIRepository.Typelib] from a memory location. + * Creates a new [type@GIRepository.Typelib] from a [type@GLib.Bytes]. * - * The memory block pointed to by @typelib will be automatically freed when the - * repository is destroyed. + * The [type@GLib.Bytes] can point to a memory location or a mapped file, and + * the typelib will hold a reference to it until the repository is destroyed. * * Returns: (transfer full): the new [type@GIRepository.Typelib] * Since: 2.80 */ GITypelib * -gi_typelib_new_from_memory (uint8_t *memory, - size_t len, - GError **error) +gi_typelib_new_from_bytes (GBytes *bytes, + GError **error) { GITypelib *meta; - - if (!validate_header_basic (memory, len, error)) - return NULL; - - meta = g_slice_new0 (GITypelib); - meta->data = memory; - meta->len = len; - meta->owns_memory = TRUE; - meta->modules = NULL; - - return meta; -} - -/** - * gi_typelib_new_from_const_memory: (skip) - * @memory: (array length=len): address of memory chunk containing the typelib - * @len: length of memory chunk containing the typelib - * @error: a [type@GLib.Error] - * - * Creates a new [type@GIRepository.Typelib] from a memory location. - * - * Returns: (transfer full): the new [type@GIRepository.Typelib] - * Since: 2.80 - */ -GITypelib * -gi_typelib_new_from_const_memory (const uint8_t *memory, - size_t len, - GError **error) -{ - GITypelib *meta; - - if (!validate_header_basic (memory, len, error)) - return NULL; - - meta = g_slice_new0 (GITypelib); - meta->data = (uint8_t *) memory; - meta->len = len; - meta->owns_memory = FALSE; - meta->modules = NULL; - - return meta; -} - -/** - * gi_typelib_new_from_mapped_file: (skip) - * @mfile: (transfer full): a [type@GLib.MappedFile], that will be freed when - * the repository is destroyed - * @error: a #GError - * - * Creates a new [type@GIRepository.Typelib] from a [type@GLib.MappedFile]. - * - * Returns: (transfer full): the new [type@GIRepository.Typelib] - * Since: 2.80 - */ -GITypelib * -gi_typelib_new_from_mapped_file (GMappedFile *mfile, - GError **error) -{ - GITypelib *meta; - uint8_t *data = (uint8_t *) g_mapped_file_get_contents (mfile); - size_t len = g_mapped_file_get_length (mfile); + size_t len; + const uint8_t *data = g_bytes_get_data (bytes, &len); if (!validate_header_basic (data, len, error)) return NULL; meta = g_slice_new0 (GITypelib); - meta->mfile = mfile; - meta->owns_memory = FALSE; - meta->data = data; + meta->bytes = g_bytes_ref (bytes); + meta->data = data; meta->len = len; + meta->modules = NULL; return meta; } @@ -2463,11 +2402,8 @@ gi_typelib_new_from_mapped_file (GMappedFile *mfile, void gi_typelib_free (GITypelib *typelib) { - if (typelib->mfile) - g_mapped_file_unref (typelib->mfile); - else - if (typelib->owns_memory) - g_free (typelib->data); + g_clear_pointer (&typelib->bytes, g_bytes_unref); + if (typelib->modules) { g_list_foreach (typelib->modules, (GFunc) (void *) g_module_close, NULL); diff --git a/girepository/gitypelib.h b/girepository/gitypelib.h index ec03759d5..17bbd0020 100644 --- a/girepository/gitypelib.h +++ b/girepository/gitypelib.h @@ -37,18 +37,8 @@ G_BEGIN_DECLS typedef struct _GITypelib GITypelib; GI_AVAILABLE_IN_ALL -GITypelib * gi_typelib_new_from_memory (uint8_t *memory, - size_t len, - GError **error); - -GI_AVAILABLE_IN_ALL -GITypelib * gi_typelib_new_from_const_memory (const uint8_t *memory, - size_t len, - GError **error); - -GI_AVAILABLE_IN_ALL -GITypelib * gi_typelib_new_from_mapped_file (GMappedFile *mfile, - GError **error); +GITypelib * gi_typelib_new_from_bytes (GBytes *bytes, + GError **error); GI_AVAILABLE_IN_ALL void gi_typelib_free (GITypelib *typelib);