cleanup: Use arrays to hold list of strings

Using GList is sub-optimal and eventually needs just more allocations
and iterations, so use arrays instead which provide a nicer API.
This commit is contained in:
Marco Trevisan (Treviño) 2024-04-03 23:39:05 +02:00
parent 73ab21910f
commit f7a90c751e
2 changed files with 31 additions and 21 deletions

View file

@ -37,7 +37,7 @@ struct _PpdDriverAmdPstate
PpdDriverCpu parent_instance;
PpdProfile activated_profile;
GList *epp_devices; /* GList of paths */
GPtrArray *epp_devices; /* Array of paths */
gboolean on_battery;
};
@ -127,7 +127,10 @@ probe_epp (PpdDriverAmdPstate *pstate)
if (!g_file_test (path, G_FILE_TEST_EXISTS))
continue;
pstate->epp_devices = g_list_prepend (pstate->epp_devices, g_steal_pointer (&base));
if (!pstate->epp_devices)
pstate->epp_devices = g_ptr_array_new_with_free_func (g_free);
g_ptr_array_add (pstate->epp_devices, g_steal_pointer (&base));
ret = PPD_PROBE_RESULT_SUCCESS;
}
@ -184,19 +187,18 @@ profile_to_epp_pref (PpdProfile profile, gboolean battery)
}
static gboolean
apply_pref_to_devices (GList *devices,
apply_pref_to_devices (GPtrArray *devices,
PpdProfile profile,
gboolean battery,
GError **error)
{
gboolean ret = TRUE;
GList *l;
if (profile == PPD_PROFILE_UNSET)
return TRUE;
for (l = devices; l != NULL; l = l->next) {
const char *base = l->data;
for (guint i = 0; i < devices->len; ++i) {
const char *base = g_ptr_array_index (devices, i);
g_autofree char *epp = NULL;
g_autofree char *gov = NULL;
@ -230,6 +232,7 @@ ppd_driver_amd_pstate_activate_profile (PpdDriver *driver,
gboolean ret = FALSE;
g_return_val_if_fail (pstate->epp_devices != NULL, FALSE);
g_return_val_if_fail (pstate->epp_devices->len != 0, FALSE);
ret = apply_pref_to_devices (pstate->epp_devices, profile, pstate->on_battery, error);
if (!ret && pstate->activated_profile != PPD_PROFILE_UNSET) {
@ -280,7 +283,7 @@ ppd_driver_amd_pstate_finalize (GObject *object)
PpdDriverAmdPstate *driver;
driver = PPD_DRIVER_AMD_PSTATE (object);
g_clear_list (&driver->epp_devices, g_free);
g_clear_pointer (&driver->epp_devices, g_ptr_array_unref);
G_OBJECT_CLASS (ppd_driver_amd_pstate_parent_class)->finalize (object);
}

View file

@ -30,8 +30,8 @@ struct _PpdDriverIntelPstate
PpdDriverCpu parent_instance;
PpdProfile activated_profile;
GList *epp_devices; /* GList of paths */
GList *epb_devices; /* GList of paths */
GPtrArray *epp_devices; /* Array of paths */
GPtrArray *epb_devices; /* Array of paths */
GFileMonitor *no_turbo_mon;
char *no_turbo_path;
gboolean on_battery;
@ -180,7 +180,10 @@ probe_epb (PpdDriverIntelPstate *pstate)
if (!g_file_test (path, G_FILE_TEST_EXISTS))
continue;
pstate->epb_devices = g_list_prepend (pstate->epb_devices, g_steal_pointer (&path));
if (!pstate->epb_devices)
pstate->epb_devices = g_ptr_array_new_with_free_func (g_free);
g_ptr_array_add (pstate->epb_devices, g_steal_pointer (&path));
ret = PPD_PROBE_RESULT_SUCCESS;
}
@ -236,7 +239,10 @@ probe_epp (PpdDriverIntelPstate *pstate)
continue;
}
pstate->epp_devices = g_list_prepend (pstate->epp_devices, g_steal_pointer (&path));
if (!pstate->epp_devices)
pstate->epp_devices = g_ptr_array_new_with_free_func (g_free);
g_ptr_array_add (pstate->epp_devices, g_steal_pointer (&path));
ret = PPD_PROBE_RESULT_SUCCESS;
}
@ -323,23 +329,24 @@ apply_pref_to_devices (PpdDriver *driver,
GError **error)
{
PpdDriverIntelPstate *pstate = PPD_DRIVER_INTEL_PSTATE (driver);
GList *l;
g_return_val_if_fail (pstate->epp_devices != NULL ||
pstate->epb_devices, FALSE);
if (profile == PPD_PROFILE_UNSET)
return TRUE;
for (l = pstate->epp_devices; l != NULL; l = l->next) {
const char *path = l->data;
g_return_val_if_fail (pstate->epp_devices != NULL ||
pstate->epb_devices != NULL, FALSE);
g_return_val_if_fail ((pstate->epp_devices && pstate->epp_devices->len != 0) ||
(pstate->epb_devices && pstate->epb_devices->len != 0), FALSE);
for (guint i = 0; pstate->epp_devices && i < pstate->epp_devices->len; i++) {
const char *path = g_ptr_array_index (pstate->epp_devices, i);
if (!ppd_utils_write (path, profile_to_epp_pref (profile, pstate->on_battery), error))
return FALSE;
}
for (l = pstate->epb_devices; l != NULL; l = l->next) {
const char *path = l->data;
for (guint i = 0; pstate->epb_devices && i < pstate->epb_devices->len; i++) {
const char *path = g_ptr_array_index (pstate->epb_devices, i);
if (!ppd_utils_write (path, profile_to_epb_pref (profile, pstate->on_battery), error))
return FALSE;
@ -390,8 +397,8 @@ ppd_driver_intel_pstate_finalize (GObject *object)
driver = PPD_DRIVER_INTEL_PSTATE (object);
g_clear_list (&driver->epp_devices, g_free);
g_clear_list (&driver->epb_devices, g_free);
g_clear_pointer (&driver->epp_devices, g_ptr_array_unref);
g_clear_pointer (&driver->epb_devices, g_ptr_array_unref);
g_clear_pointer (&driver->no_turbo_path, g_free);
g_clear_object (&driver->no_turbo_mon);
G_OBJECT_CLASS (ppd_driver_intel_pstate_parent_class)->finalize (object);