halapi: power: Replace hal_common_get/put_backend with v2 to support ABI compatibility 23/313723/1 accepted/tizen/unified/20240702.091857 accepted/tizen/unified/dev/20240703.060355 accepted/tizen/unified/x/20240704.023009
authorChanwoo Choi <cw00.choi@samsung.com>
Fri, 28 Jun 2024 06:04:10 +0000 (15:04 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Mon, 1 Jul 2024 07:25:58 +0000 (16:25 +0900)
In order to support HAL ABI compatibility between Platform and HAL,
replace hal_common_get_backend with hal_common_get/put_backend_v2
to allocate/free memory on hal-api side instead of hal-backend.

Change-Id: I0faa4938292dc4003d6c1e7b0356d33ff663d909
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
src/hal-api-power.c
tests/unittest/hal-backend-power.c
tests/unittest/test-hal-api-power.cc

index ccc9b8c1fdb4864c4c5b447064e7adf7592df3f4..f75f08a0cfbb89fe15fa632c99ae2fe23a8b40a0 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <stdio.h>
 #include <stdint.h>
+#include <stdlib.h>
 #include <dlfcn.h>
 #include <dlog.h>
 
@@ -187,6 +188,58 @@ static int get_charging(hal_backend_power_funcs *funcs, int res_type, char *res_
        return 0;
 }
 
+static int init_backend(void **data, void *user_data)
+{
+       hal_backend_power_funcs *power_funcs = NULL;
+       int ret;
+
+       power_funcs = calloc(1, sizeof(hal_backend_power_funcs));
+       if (!power_funcs)
+               return -ENOMEM;
+
+       power_funcs->cpu = calloc(1, sizeof(struct pass_resource_cpu));
+       if (!power_funcs->cpu) {
+               ret = -ENOMEM;
+               goto err_funcs;
+       }
+
+       power_funcs->memory = calloc(1, sizeof(struct pass_resource_memory));
+       if (!power_funcs->memory) {
+               ret = -ENOMEM;
+               goto err_cpu;
+       }
+
+       *data = (void *)power_funcs;
+
+       return 0;
+err_cpu:
+       if (power_funcs->cpu)
+               free(power_funcs->cpu);
+err_funcs:
+       free(power_funcs);
+
+       return ret;
+}
+
+static int exit_backend(void *data, void *user_data)
+{
+       hal_backend_power_funcs *funcs;
+
+       if (!data)
+               return -EINVAL;
+
+       funcs = (hal_backend_power_funcs *)data;
+
+       if (funcs->cpu)
+               free(funcs->cpu);
+       if (funcs->memory)
+               free(funcs->memory);
+
+       free(funcs);
+
+       return 0;
+}
+
 EXPORT int hal_power_get_backend(unsigned int res_type)
 {
        g_power_funcs_count++;
@@ -194,8 +247,8 @@ EXPORT int hal_power_get_backend(unsigned int res_type)
        if (!g_power_funcs) {
                int ret;
 
-               ret = hal_common_get_backend(HAL_MODULE_POWER,
-                                       (void **)&g_power_funcs);
+               ret = hal_common_get_backend_v2(HAL_MODULE_POWER,
+                               (void **)&g_power_funcs, NULL, init_backend);
                if (ret < 0) {
                        _E("Failed to get backend of HAL_MODULE_POWER\n");
                        return ret;
@@ -219,7 +272,8 @@ EXPORT int hal_power_put_backend(void)
        if (--g_power_funcs_count > 0)
                return 0;
 
-       hal_common_put_backend(HAL_MODULE_POWER, (void *)g_power_funcs);
+       hal_common_put_backend_v2(HAL_MODULE_POWER,
+                               (void *)g_power_funcs, NULL, exit_backend);
        g_power_funcs = NULL;
 
        return 0;
index 171dcc2dd53bf72143c4afffd97a9016661f585c..3f0bbb681ddccb9fb4ac3a1a359b9b8210b4a2a2 100644 (file)
@@ -110,93 +110,29 @@ static struct pass_resource_dvfs_ops devfreq_dvfs_ops =  {
 
 static int power_init(void **data)
 {
-       hal_backend_power_funcs *power_funcs = NULL;
-       struct pass_resource_cpu *cpu = NULL;
-       struct pass_resource_bus *bus = NULL;
-       struct pass_resource_gpu *gpu = NULL;
-       struct pass_resource_memory *memory = NULL;
-       int ret;
-
-       /* Allocate memory */
-       power_funcs = calloc(1, sizeof(hal_backend_power_funcs));
+       hal_backend_power_funcs *power_funcs = *(hal_backend_power_funcs **)data;
+
        if (!power_funcs)
-               return -ENOMEM;
-
-       cpu = calloc(1, sizeof(struct pass_resource_cpu));
-       if (!cpu) {
-               ret = -ENOMEM;
-               goto err_cpu;
-       }
-
-       bus = calloc(1, sizeof(struct pass_resource_bus));
-       if (!bus) {
-               ret = -ENOMEM;
-               goto err_bus;
-       }
-
-       gpu = calloc(1, sizeof(struct pass_resource_gpu));
-       if (!gpu) {
-               ret = -ENOMEM;
-               goto err_gpu;
-       }
-
-       memory = calloc(1, sizeof(struct pass_resource_memory));
-       if (!memory) {
-               ret = -ENOMEM;
-               goto err_memory;
-       }
-
-       /* Initialize each h/w resource */
-       cpu->dvfs = cpufreq_dvfs_ops;
-       cpu->hotplug = cpu_hotplus_ops;
-       cpu->tmu = tmu_ops;
-
-       bus->dvfs = devfreq_dvfs_ops;
-       bus->tmu = tmu_ops;
-
-       gpu->dvfs = devfreq_dvfs_ops;
-       gpu->tmu = tmu_ops;
-
-       memory->get_fault_around_bytes = get_value;
-       memory->set_fault_around_bytes = set_value;
-
-       /* Initialize hal_backend_power_funcs  */
-       power_funcs->cpu = cpu;
-       power_funcs->bus = bus;
-       power_funcs->gpu = gpu;
-       power_funcs->memory = memory;
-
-       *data = (void *)power_funcs;
+               return -EINVAL;
 
-       return 0;
+       power_funcs->cpu->dvfs = cpufreq_dvfs_ops;
+       power_funcs->cpu->hotplug = cpu_hotplus_ops;
+       power_funcs->cpu->tmu = tmu_ops;
 
-err_memory:
-       free(gpu);
-err_gpu:
-       free(bus);
-err_bus:
-       free(cpu);
-err_cpu:
-       free(power_funcs);
+       power_funcs->bus->dvfs = devfreq_dvfs_ops;
+       power_funcs->bus->tmu = tmu_ops;
 
-       return ret;
+       power_funcs->gpu->dvfs = devfreq_dvfs_ops;
+       power_funcs->gpu->tmu = tmu_ops;
+
+       power_funcs->memory->get_fault_around_bytes = get_value;
+       power_funcs->memory->set_fault_around_bytes = set_value;
+
+       return 0;
 }
 
 static int power_exit(void *data)
 {
-       hal_backend_power_funcs *funcs;
-
-       if (!data)
-               return -EINVAL;
-
-       funcs = (hal_backend_power_funcs *)data;
-
-       free(funcs->cpu);
-       free(funcs->bus);
-       free(funcs->gpu);
-       free(funcs->memory);
-       free(funcs);
-
        return 0;
 }
 
index f1a14d4790777bbe8c8f4f01166f20374bd31a75..a28af6219dc4063f82b187bd33a2d5c1dc557007 100644 (file)
@@ -40,19 +40,108 @@ class HalApiPowerTest : public testing::Test {
 extern hal_backend hal_backend_power_data;
 
 /*
- * Re-implement hal_common_get_backend/hal_common_put_backend for hooking
+ * Re-implement hal_common_get_backend_v2/hal_common_put_backend_v2 for hooking
  * in order to test hal-api-power functions when building package.
  */
-int hal_common_get_backend(enum hal_module module, void **data) {
+
+static int __init_backend(void **data, void *user_data)
+{
+       hal_backend_power_funcs *power_funcs = NULL;
+       int ret;
+
+       power_funcs = (hal_backend_power_funcs *)calloc(1,
+                                       sizeof(hal_backend_power_funcs));
+       if (!power_funcs)
+               return -ENOMEM;
+
+       power_funcs->cpu = (struct pass_resource_cpu *)calloc(1,
+                                       sizeof(struct pass_resource_cpu));
+       if (!power_funcs->cpu) {
+               ret = -ENOMEM;
+               goto err_cpu;
+       }
+
+       power_funcs->bus = (struct pass_resource_bus *)calloc(1,
+                                       sizeof(struct pass_resource_bus));
+       if (!power_funcs->bus) {
+               ret = -ENOMEM;
+               goto err_bus;
+       }
+
+       power_funcs->gpu = (struct pass_resource_gpu *)calloc(1,
+                                       sizeof(struct pass_resource_gpu));
+       if (!power_funcs->gpu) {
+               ret = -ENOMEM;
+               goto err_gpu;
+       }
+
+       power_funcs->memory = (struct pass_resource_memory *)calloc(1,
+                                       sizeof(struct pass_resource_memory));
+       if (!power_funcs->memory) {
+               ret = -ENOMEM;
+               goto err_memory;
+       }
+
+       *data = (void *)power_funcs;
+
+       return 0;
+
+err_memory:
+       free(power_funcs->gpu);
+err_gpu:
+       free(power_funcs->bus);
+err_bus:
+       free(power_funcs->cpu);
+err_cpu:
+       free(power_funcs);
+
+       return ret;
+}
+
+static int __exit_backend(void *data, void *user_data)
+{
+       hal_backend_power_funcs *funcs;
+
+       if (!data)
+               return -EINVAL;
+
+       funcs = (hal_backend_power_funcs *)data;
+
+       free(funcs->cpu);
+       free(funcs->bus);
+       free(funcs->gpu);
+       free(funcs->memory);
+       free(funcs);
+
+       return 0;
+}
+
+int hal_common_get_backend_v2(enum hal_module module,
+                               void **data, void *user_data,
+                               int (*init_backend)(void **data, void *user_data))
+{
        hal_backend *backend = &hal_backend_power_data;
+       int ret;
+
+       ret = __init_backend(data, user_data);
+       if (ret < 0)
+               return ret;
 
        return backend->init(data);
 }
 
-int hal_common_put_backend(enum hal_module module, void *data) {
+int hal_common_put_backend_v2(enum hal_module module,
+                               void *data, void *user_data,
+                               int (*exit_backend)(void *data, void *user_data))
+{
        hal_backend *backend = &hal_backend_power_data;
+       int ret;
+
+       ret = backend->exit(data);
+       if (ret < 0)
+               return ret;
 
-       return backend->exit(data);
+       return __exit_backend(data, user_data);
 }
 
 static int resources[] = {