Do not allocate backend data 84/314984/3
authorChangyeon Lee <cyeon.lee@samsung.com>
Tue, 23 Jul 2024 10:27:29 +0000 (19:27 +0900)
committerChangyeon Lee <cyeon.lee@samsung.com>
Wed, 24 Jul 2024 06:08:36 +0000 (15:08 +0900)
backend data is allocated in hal-api

Change-Id: I73143a22868bc934d5c758c3c43e7ede34361785

src/tbm_backend_vc4.c

index 7628937a49f6d3c3c3e44c91520e81384892f9b0..f57260c30b88c075f0b7855816a51329c595da73 100644 (file)
@@ -1688,13 +1688,6 @@ hal_backend_tbm_vc4_exit(void *data)
        bufmgr_data = (tbm_vc4_bufmgr *)backend_data->bufmgr;
        TBM_BACKEND_RETURN_VAL_IF_FAIL(bufmgr_data != NULL, -1);
 
-       if (backend_data->bo_funcs)
-               free(backend_data->bo_funcs);
-       if (backend_data->surface_funcs)
-               free(backend_data->surface_funcs);
-       if (backend_data->bufmgr_funcs)
-               free(backend_data->bufmgr_funcs);
-
        if (!LIST_IS_EMPTY(&bufmgr_data->surface_data_list)) {
                LIST_FOR_EACH_ENTRY_SAFE(s, ss, &bufmgr_data->surface_data_list, link) {
                        LIST_DEL(&s->link);
@@ -1713,7 +1706,7 @@ hal_backend_tbm_vc4_exit(void *data)
        close(bufmgr_data->fd);
 
        free(backend_data->bufmgr);
-       free(backend_data);
+       backend_data->bufmgr = NULL;
 
        return HAL_TBM_ERROR_NONE;
 }
@@ -1722,26 +1715,25 @@ static int
 hal_backend_tbm_vc4_init(void **data)
 {
        hal_tbm_backend_data *backend_data = NULL;
-       hal_tbm_bufmgr_funcs *bufmgr_funcs = NULL;
-       hal_tbm_surface_funcs *surface_funcs = NULL;
-       hal_tbm_bo_funcs *bo_funcs = NULL;
        tbm_vc4_bufmgr *bufmgr_data = NULL;
        int drm_fd = -1;
 
-       /* allocate a hal_tbm_backend_data */
-       backend_data = calloc(1, sizeof(struct _hal_tbm_backend_data));
+       if (!data) {
+               TBM_BACKEND_ERR("data is NULL");
+               return -1;
+       }
+
+       backend_data = *(hal_tbm_backend_data **)data;
        if (!backend_data) {
-               TBM_BACKEND_ERR("fail to alloc backend_data!");
-               *data = NULL;
+               TBM_BACKEND_ERR("backend_data is NULL");
                return -1;
        }
-       *data = backend_data;
 
        /* allocate a hal_tbm_bufmgr */
        bufmgr_data = calloc(1, sizeof(struct _tbm_vc4_bufmgr));
        if (!bufmgr_data) {
                TBM_BACKEND_ERR("fail to alloc bufmgr_data!");
-               goto fail_alloc_bufmgr_data;
+               return -1;
        }
        backend_data->bufmgr = (hal_tbm_bufmgr *)bufmgr_data;
 
@@ -1749,7 +1741,8 @@ hal_backend_tbm_vc4_init(void **data)
        drm_fd = _tbm_vc4_open_drm();
        if (drm_fd < 0) {
                TBM_BACKEND_ERR("fail to open drm!");
-               goto fail_open_drm;
+               free(bufmgr_data);
+               return -1;
        }
 
        // set true when backend has a drm_device.
@@ -1781,76 +1774,35 @@ hal_backend_tbm_vc4_init(void **data)
        // initialize the surface_data list
        LIST_INITHEAD(&bufmgr_data->surface_data_list);
 
-       /* alloc and register bufmgr_funcs */
-       bufmgr_funcs = calloc(1, sizeof(struct _hal_tbm_bufmgr_funcs));
-       if (!bufmgr_funcs) {
-               TBM_BACKEND_ERR("fail to alloc bufmgr_funcs!");
-               goto fail_alloc_bufmgr_funcs;
-       }
-       backend_data->bufmgr_funcs = bufmgr_funcs;
-
-       bufmgr_funcs->bufmgr_get_capabilities = tbm_vc4_bufmgr_get_capabilities;
-       bufmgr_funcs->bufmgr_get_supported_formats = tbm_vc4_bufmgr_get_supported_formats;
-       bufmgr_funcs->bufmgr_get_plane_data = tbm_vc4_bufmgr_get_plane_data;
-       bufmgr_funcs->bufmgr_alloc_surface = tbm_vc4_bufmgr_alloc_surface;
-       bufmgr_funcs->bufmgr_import_surface = tbm_vc4_bufmgr_import_surface;
-       bufmgr_funcs->bufmgr_alloc_bo = tbm_vc4_bufmgr_alloc_bo;
-       bufmgr_funcs->bufmgr_alloc_bo_with_format = NULL;
-       bufmgr_funcs->bufmgr_import_fd = tbm_vc4_bufmgr_import_fd;
-       bufmgr_funcs->bufmgr_import_key = tbm_vc4_bufmgr_import_key;
-
-       /* alloc and register surface_funcs */
-       surface_funcs = calloc(1, sizeof(struct _hal_tbm_surface_funcs));
-       if (!surface_funcs) {
-               TBM_BACKEND_ERR("fail to alloc surface_funcs!");
-               goto fail_alloc_surface_funcs;
-       }
-       backend_data->surface_funcs = surface_funcs;
-
-       surface_funcs->surface_free = tbm_vc4_surface_free;
-       surface_funcs->surface_get_bos = tbm_vc4_surface_get_bos;
-       surface_funcs->surface_get_plane_data = tbm_vc4_surface_get_plane_data;
-       surface_funcs->surface_export = tbm_vc4_surface_export;
-
-       /* alloc and register bo_funcs */
-       bo_funcs = calloc(1, sizeof(struct _hal_tbm_bo_funcs));
-       if (!bo_funcs) {
-               TBM_BACKEND_ERR("fail to alloc bo_funcs!");
-               goto fail_alloc_bo_funcs;
-       }
-       backend_data->bo_funcs = bo_funcs;
-
-       bo_funcs->bo_free = tbm_vc4_bo_free;
-       bo_funcs->bo_get_size = tbm_vc4_bo_get_size;
-       bo_funcs->bo_get_memory_types = tbm_vc4_bo_get_memory_type;
-       bo_funcs->bo_get_handle = tbm_vc4_bo_get_handle;
-       bo_funcs->bo_map = tbm_vc4_bo_map;
-       bo_funcs->bo_unmap = tbm_vc4_bo_unmap;
-       bo_funcs->bo_lock = tbm_vc4_bo_lock;
-       bo_funcs->bo_unlock = tbm_vc4_bo_unlock;
-       bo_funcs->bo_export_fd = tbm_vc4_bo_export_fd;
-       bo_funcs->bo_export_key = tbm_vc4_bo_export_key;
+       backend_data->bufmgr_funcs->bufmgr_get_capabilities = tbm_vc4_bufmgr_get_capabilities;
+       backend_data->bufmgr_funcs->bufmgr_get_supported_formats = tbm_vc4_bufmgr_get_supported_formats;
+       backend_data->bufmgr_funcs->bufmgr_get_plane_data = tbm_vc4_bufmgr_get_plane_data;
+       backend_data->bufmgr_funcs->bufmgr_alloc_surface = tbm_vc4_bufmgr_alloc_surface;
+       backend_data->bufmgr_funcs->bufmgr_import_surface = tbm_vc4_bufmgr_import_surface;
+       backend_data->bufmgr_funcs->bufmgr_alloc_bo = tbm_vc4_bufmgr_alloc_bo;
+       backend_data->bufmgr_funcs->bufmgr_alloc_bo_with_format = NULL;
+       backend_data->bufmgr_funcs->bufmgr_import_fd = tbm_vc4_bufmgr_import_fd;
+       backend_data->bufmgr_funcs->bufmgr_import_key = tbm_vc4_bufmgr_import_key;
+
+       backend_data->surface_funcs->surface_free = tbm_vc4_surface_free;
+       backend_data->surface_funcs->surface_get_bos = tbm_vc4_surface_get_bos;
+       backend_data->surface_funcs->surface_get_plane_data = tbm_vc4_surface_get_plane_data;
+       backend_data->surface_funcs->surface_export = tbm_vc4_surface_export;
+
+       backend_data->bo_funcs->bo_free = tbm_vc4_bo_free;
+       backend_data->bo_funcs->bo_get_size = tbm_vc4_bo_get_size;
+       backend_data->bo_funcs->bo_get_memory_types = tbm_vc4_bo_get_memory_type;
+       backend_data->bo_funcs->bo_get_handle = tbm_vc4_bo_get_handle;
+       backend_data->bo_funcs->bo_map = tbm_vc4_bo_map;
+       backend_data->bo_funcs->bo_unmap = tbm_vc4_bo_unmap;
+       backend_data->bo_funcs->bo_lock = tbm_vc4_bo_lock;
+       backend_data->bo_funcs->bo_unlock = tbm_vc4_bo_unlock;
+       backend_data->bo_funcs->bo_export_fd = tbm_vc4_bo_export_fd;
+       backend_data->bo_funcs->bo_export_key = tbm_vc4_bo_export_key;
 
        TBM_BACKEND_DBG("drm_fd:%d", bufmgr_data->fd);
 
        return HAL_TBM_ERROR_NONE;
-
-fail_alloc_bo_funcs:
-       free(surface_funcs);
-fail_alloc_surface_funcs:
-       free(bufmgr_funcs);
-fail_alloc_bufmgr_funcs:
-       if (bufmgr_data->hashBos)
-               drmHashDestroy(bufmgr_data->hashBos);
-       close(bufmgr_data->fd);
-fail_open_drm:
-       free(bufmgr_data);
-fail_alloc_bufmgr_data:
-       free(backend_data);
-
-       *data = NULL;
-
-       return -1;
 }
 
 hal_backend hal_backend_tbm_data = {