tbm_module: handle the memory leak of the bo_data_array
[platform/core/uifw/libtbm.git] / src / tbm_module.c
index 652713d..22dc061 100644 (file)
@@ -650,18 +650,16 @@ tbm_module_support_surface_data(tbm_module *module)
        tbm_error_e error = TBM_ERROR_NONE;
        tbm_surface_data *surface_data = NULL;
 
-       static int tbm_module_check_support_surface_data = 0;
-
        TBM_RETURN_VAL_IF_FAIL(module, 0);
 
        // check once support_surface_data or not.
-       if (tbm_module_check_support_surface_data) {
+       if (module->check_surface_data) {
                // return the value which already set.
                return module->support_surface_data;
        }
 
        // check this only once
-       tbm_module_check_support_surface_data = 1;
+       module->check_surface_data = 1;
 
        if (module->type != TBM_MODULE_TYPE_HAL_TBM)
                goto done;
@@ -839,7 +837,7 @@ tbm_module_alloc_bo_data_with_format(tbm_module *module, int format, int bo_idx,
                break;
        case TBM_MODULE_TYPE_BUFMGR_BACKEND:
                TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
-               TBM_ERR("error: not supported tbm_bufmgr_internal_alloc_bo_with_format.");
+               TBM_ERR("error: not supported tbm_module_alloc_bo_data_with_format.");
 
                *error = TBM_ERROR_NOT_SUPPORTED;
                goto failed;
@@ -1037,7 +1035,7 @@ tbm_surface_data_get_plane_data(tbm_surface_data *surface_data, int plane_idx, u
 }
 
 tbm_bo_data **
-tbm_surface_data_get_bo_data_array(tbm_surface_data *surface_data, int *num_bos, tbm_error_e *error)
+tbm_surface_data_get_bo_data_array(tbm_surface_data *surface_data, int *num_bos, int *memory_types, tbm_error_e *error)
 {
        tbm_bo_data **bo_data_array = NULL;
        hal_tbm_bo **hal_bos = NULL;
@@ -1049,11 +1047,12 @@ tbm_surface_data_get_bo_data_array(tbm_surface_data *surface_data, int *num_bos,
        hal_bos = hal_tbm_surface_get_bos(surface_data->hal_surface, num_bos, (hal_tbm_error *)error);
        TBM_RETURN_VAL_IF_FAIL(hal_bos, NULL);
 
-       bo_data_array = calloc(*num_bos, sizeof(struct _tbm_bo_data));
-       if (bo_data_array) {
-               TBM_ERR("memory allocation failed.");
-               *error = TBM_ERROR_OUT_OF_MEMORY;
-               return NULL;
+       bo_data_array = calloc(*num_bos, sizeof(tbm_bo_data *));
+       TBM_RETURN_VAL_IF_FAIL(bo_data_array, NULL);
+
+       for (i = 0; i < *num_bos; i++) {
+               bo_data_array[i] = calloc(*num_bos, sizeof(struct _tbm_bo_data));
+               TBM_GOTO_VAL_SET_ERR_IF_FAIL(bo_data_array[i], failed, *error, TBM_ERROR_OUT_OF_MEMORY);
        }
 
        for (i = 0; i < *num_bos; i++) {
@@ -1061,7 +1060,22 @@ tbm_surface_data_get_bo_data_array(tbm_surface_data *surface_data, int *num_bos,
                bo_data_array[i]->module = surface_data->module;
        }
 
+       // get memory_types(bo flags)
+       *memory_types = tbm_bo_data_get_memory_types(bo_data_array[0], error);
+       TBM_GOTO_VAL_IF_FAIL(*error == TBM_ERROR_NONE, failed);
+
        return bo_data_array;
+
+/* LCOV_EXCL_START */
+failed:
+       for (i = 0; i < *num_bos; i++) {
+               if (bo_data_array[i])
+                       free(bo_data_array[i]);
+       }
+       free(bo_data_array);
+
+       return NULL;
+/* LCOV_EXCL_STOP */
 }
 
 tbm_surface_buffer_data *