mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-02 17:26:17 +01:00
gitypelib: Replace multiple constructors with gi_typelib_new_from_bytes()
`GBytes` provides a way of handling const memory blobs, stolen memory blobs, and mapped files. Rather than having `GITypelib` implement all of those itself, just take a `GBytes` as input. This is an API break, but libgirepository hasn’t been in a stable release yet. Signed-off-by: Philip Withnall <pwithnall@gnome.org> Helps: #3155
This commit is contained in:
parent
7262f5ab14
commit
1eec66c898
@ -1752,7 +1752,12 @@ require_internal (GIRepository *repository,
|
|||||||
|
|
||||||
{
|
{
|
||||||
GError *temp_error = NULL;
|
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)
|
if (!typelib)
|
||||||
{
|
{
|
||||||
g_set_error (error, GI_REPOSITORY_ERROR,
|
g_set_error (error, GI_REPOSITORY_ERROR,
|
||||||
|
@ -318,6 +318,7 @@ GITypelib *
|
|||||||
gi_ir_module_build_typelib (GIIrModule *module)
|
gi_ir_module_build_typelib (GIIrModule *module)
|
||||||
{
|
{
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
|
GBytes *bytes = NULL;
|
||||||
GITypelib *typelib;
|
GITypelib *typelib;
|
||||||
size_t length;
|
size_t length;
|
||||||
size_t i;
|
size_t i;
|
||||||
@ -568,7 +569,11 @@ gi_ir_module_build_typelib (GIIrModule *module)
|
|||||||
header = (Header *)data;
|
header = (Header *)data;
|
||||||
|
|
||||||
length = header->size = offset2;
|
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)
|
if (!typelib)
|
||||||
{
|
{
|
||||||
g_error ("error building typelib: %s",
|
g_error ("error building typelib: %s",
|
||||||
|
@ -1313,10 +1313,9 @@ typedef struct {
|
|||||||
|
|
||||||
struct _GITypelib {
|
struct _GITypelib {
|
||||||
/*< private >*/
|
/*< private >*/
|
||||||
uint8_t *data;
|
const uint8_t *data; /* just a cached pointer to inside @bytes */
|
||||||
size_t len;
|
size_t len;
|
||||||
gboolean owns_memory;
|
GBytes *bytes; /* (owned) */
|
||||||
GMappedFile *mfile;
|
|
||||||
GList *modules;
|
GList *modules;
|
||||||
gboolean open_attempted;
|
gboolean open_attempted;
|
||||||
};
|
};
|
||||||
|
@ -2359,95 +2359,34 @@ gi_typelib_ensure_open (GITypelib *typelib)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gi_typelib_new_from_memory: (skip)
|
* gi_typelib_new_from_bytes:
|
||||||
* @memory: (array length=len): address of memory chunk containing the typelib
|
* @bytes: memory chunk containing the typelib
|
||||||
* @len: length of memory chunk containing the typelib, in bytes
|
|
||||||
* @error: a [type@GLib.Error]
|
* @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
|
* The [type@GLib.Bytes] can point to a memory location or a mapped file, and
|
||||||
* repository is destroyed.
|
* the typelib will hold a reference to it until the repository is destroyed.
|
||||||
*
|
*
|
||||||
* Returns: (transfer full): the new [type@GIRepository.Typelib]
|
* Returns: (transfer full): the new [type@GIRepository.Typelib]
|
||||||
* Since: 2.80
|
* Since: 2.80
|
||||||
*/
|
*/
|
||||||
GITypelib *
|
GITypelib *
|
||||||
gi_typelib_new_from_memory (uint8_t *memory,
|
gi_typelib_new_from_bytes (GBytes *bytes,
|
||||||
size_t len,
|
GError **error)
|
||||||
GError **error)
|
|
||||||
{
|
{
|
||||||
GITypelib *meta;
|
GITypelib *meta;
|
||||||
|
size_t len;
|
||||||
if (!validate_header_basic (memory, len, error))
|
const uint8_t *data = g_bytes_get_data (bytes, &len);
|
||||||
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);
|
|
||||||
|
|
||||||
if (!validate_header_basic (data, len, error))
|
if (!validate_header_basic (data, len, error))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
meta = g_slice_new0 (GITypelib);
|
meta = g_slice_new0 (GITypelib);
|
||||||
meta->mfile = mfile;
|
meta->bytes = g_bytes_ref (bytes);
|
||||||
meta->owns_memory = FALSE;
|
meta->data = data;
|
||||||
meta->data = data;
|
|
||||||
meta->len = len;
|
meta->len = len;
|
||||||
|
meta->modules = NULL;
|
||||||
|
|
||||||
return meta;
|
return meta;
|
||||||
}
|
}
|
||||||
@ -2463,11 +2402,8 @@ gi_typelib_new_from_mapped_file (GMappedFile *mfile,
|
|||||||
void
|
void
|
||||||
gi_typelib_free (GITypelib *typelib)
|
gi_typelib_free (GITypelib *typelib)
|
||||||
{
|
{
|
||||||
if (typelib->mfile)
|
g_clear_pointer (&typelib->bytes, g_bytes_unref);
|
||||||
g_mapped_file_unref (typelib->mfile);
|
|
||||||
else
|
|
||||||
if (typelib->owns_memory)
|
|
||||||
g_free (typelib->data);
|
|
||||||
if (typelib->modules)
|
if (typelib->modules)
|
||||||
{
|
{
|
||||||
g_list_foreach (typelib->modules, (GFunc) (void *) g_module_close, NULL);
|
g_list_foreach (typelib->modules, (GFunc) (void *) g_module_close, NULL);
|
||||||
|
@ -37,18 +37,8 @@ G_BEGIN_DECLS
|
|||||||
typedef struct _GITypelib GITypelib;
|
typedef struct _GITypelib GITypelib;
|
||||||
|
|
||||||
GI_AVAILABLE_IN_ALL
|
GI_AVAILABLE_IN_ALL
|
||||||
GITypelib * gi_typelib_new_from_memory (uint8_t *memory,
|
GITypelib * gi_typelib_new_from_bytes (GBytes *bytes,
|
||||||
size_t len,
|
GError **error);
|
||||||
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);
|
|
||||||
|
|
||||||
GI_AVAILABLE_IN_ALL
|
GI_AVAILABLE_IN_ALL
|
||||||
void gi_typelib_free (GITypelib *typelib);
|
void gi_typelib_free (GITypelib *typelib);
|
||||||
|
Loading…
Reference in New Issue
Block a user