resource: Add g_resource[s]_has_children

These functions avoid allocating the children array when it is
not needed.
This commit is contained in:
Matthias Clasen 2024-09-06 00:25:05 -04:00 committed by Philip Withnall
parent cb5209282b
commit a14241c895
3 changed files with 100 additions and 0 deletions

View File

@ -1023,6 +1023,43 @@ g_resource_enumerate_children (GResource *resource,
return children;
}
/**
* g_resource_has_children:
* @resource: A #GResource
* @path: A pathname inside the resource
*
* Returns whether the specified @path in the resource
* has children.
*
* Returns: %TRUE if @path has children
*
* Since: 2.84
*/
gboolean
g_resource_has_children (GResource *resource,
const char *path)
{
/* Size of 256 is arbitrarily chosen based on being large enough
* for pretty much everything we come across, but not cumbersome
* on the stack. It also matches common cacheline sizes.
*/
char local_str[256];
const char *path_with_slash;
guint n;
char *freeme = NULL;
if (*path == 0)
return FALSE;
path_with_slash = ensure_slash_suffix (path, local_str, sizeof (local_str), &freeme);
n = gvdb_table_n_children (resource->table, path_with_slash);
g_free (freeme);
return n > 0;
}
static GRWLock resources_lock;
static GList *registered_resources;
@ -1301,6 +1338,39 @@ g_resources_enumerate_children (const gchar *path,
}
}
/**
* g_resources_has_children:
* @path: A pathname
*
* Returns whether the specified @path in the set of
* globally registered resources has children.
*
* Returns: %TRUE if @patch has children
*
* Since: 2.84
*/
gboolean
g_resources_has_children (const char *path)
{
register_lazy_static_resources ();
g_rw_lock_reader_lock (&resources_lock);
for (GList *l = registered_resources; l != NULL; l = l->next)
{
GResource *r = l->data;
if (g_resource_has_children (r, path))
{
g_rw_lock_reader_unlock (&resources_lock);
return TRUE;
}
}
g_rw_lock_reader_unlock (&resources_lock);
return FALSE;
}
/**
* g_resources_get_info:
* @path: A pathname inside the resource

View File

@ -119,6 +119,12 @@ gboolean g_resources_get_info (const char *path,
guint32 *flags,
GError **error);
GIO_AVAILABLE_IN_2_84
gboolean g_resource_has_children (GResource *resource,
const char *path);
GIO_AVAILABLE_IN_2_84
gboolean g_resources_has_children (const char *path);
GIO_AVAILABLE_IN_2_32
void g_static_resource_init (GStaticResource *static_resource);

View File

@ -1046,6 +1046,29 @@ test_overlay (void)
g_test_trap_assert_passed ();
}
static void
test_resource_has_children (void)
{
GResource *resource;
GError *error = NULL;
g_assert_true (g_resources_has_children ("/auto_loaded"));
g_assert_true (g_resources_has_children ("/auto_loaded/"));
g_assert_false (g_resources_has_children ("/auto_loaded/test1.txt"));
g_assert_false (g_resources_has_children ("/no/such/prefix"));
resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
g_assert_nonnull (resource);
g_assert_no_error (error);
g_assert_true (g_resource_has_children (resource, "/a_prefix"));
g_assert_true (g_resource_has_children (resource, "/a_prefix/"));
g_assert_false (g_resource_has_children (resource, "/a_prefix/test2.txt"));
g_assert_false (g_resource_has_children (resource, "/no/such/prefix"));
g_resource_unref (resource);
}
int
main (int argc,
char *argv[])
@ -1075,6 +1098,7 @@ main (int argc,
g_test_add_func ("/resource/64k", test_resource_64k);
g_test_add_func ("/resource/overlay", test_overlay);
g_test_add_func ("/resource/digits", test_resource_digits);
g_test_add_func ("/resource/has-children", test_resource_has_children);
return g_test_run();
}