Merge branch 'typelib-bytes' into 'main'

gitypelib: Replace multiple constructors with gi_typelib_new_from_bytes()

See merge request GNOME/glib!3855
This commit is contained in:
Philip Withnall 2024-01-26 10:15:49 +00:00
commit b4063e1fa8
5 changed files with 30 additions and 95 deletions

View File

@ -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,

View File

@ -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",

View File

@ -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;
};

View File

@ -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->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);

View File

@ -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);