From 3383d815ec3acbdef545954e873d93bca0cf387e Mon Sep 17 00:00:00 2001 From: hj kim Date: Mon, 13 Apr 2020 16:17:30 +0900 Subject: [PATCH] Remove out of memory related code by using glib APIs glib's memory managed as below. If any call to allocate memory fails, the application is terminated. This also means that there is no need to check if the call succeeded. Change-Id: I6789bf638955d97a28a7a66595c8d41165cf204f --- include/dcm_svc_debug.h | 1 - libdcm-face/dcm-face.c | 36 +++++++-------------- libdcm-face/dcm-face_mediavision.c | 34 +++++-------------- libdcm-face/dcm-face_priv.h | 2 -- libdcm-face/include/dcm-face.h | 2 -- libdcm-util/include/dcm_image_codec_debug.h | 2 -- packaging/dcm-service.spec | 2 +- src/dcm_svc_db.c | 7 +--- src/dcm_svc_detect_face.c | 7 +--- src/dcm_svc_internal.c | 6 ++-- 10 files changed, 26 insertions(+), 73 deletions(-) diff --git a/include/dcm_svc_debug.h b/include/dcm_svc_debug.h index c810eee..6c128c5 100755 --- a/include/dcm_svc_debug.h +++ b/include/dcm_svc_debug.h @@ -101,7 +101,6 @@ #define DCM_CHECK_FALSE(expr) dcm_retvm_if(!(expr), FALSE, "Invalid parameter, return FALSE!") #define DCM_CHECK(expr) dcm_retm_if(!(expr), "Invalid parameter, return!") -#define DCM_SAFE_FREE(ptr) { if (ptr) {free(ptr); ptr = NULL; } } #define DCM_SAFE_STRLCPY(dst, src, n) g_strlcpy(dst, src, n); #endif /* _DCM_DEBUG_UTILS_H_ */ diff --git a/libdcm-face/dcm-face.c b/libdcm-face/dcm-face.c index e02e429..2a48d0a 100755 --- a/libdcm-face/dcm-face.c +++ b/libdcm-face/dcm-face.c @@ -28,8 +28,7 @@ int dcm_face_create(__inout dcm_face_h *handle) dcm_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid handle"); - FaceHandleT *pFaceHandle = (FaceHandleT *)calloc(1, sizeof(FaceHandleT)); - dcm_retvm_if(pFaceHandle == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "malloc fail"); + FaceHandleT *pFaceHandle = g_new0(FaceHandleT, 1); ret = _face_handle_create(&(pFaceHandle->fengine)); if (ret != MS_MEDIA_ERR_NONE) { @@ -63,9 +62,9 @@ int dcm_face_destroy(__in dcm_face_h handle) handle->magic = FACE_MAGIC_INVALID; if (handle->image_info != NULL) - DCM_SAFE_FREE(handle->image_info->data); - DCM_SAFE_FREE(handle->image_info); - DCM_SAFE_FREE(handle); + g_free(handle->image_info->data); + g_free(handle->image_info); + g_free(handle); return ret; } @@ -77,19 +76,14 @@ int dcm_face_set_image_info(dcm_face_h handle, face_image_colorspace_e colorspac dcm_debug_fenter(); - dcm_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid handle"); - dcm_retvm_if(buffer == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid buffer"); + dcm_retvm_if(!handle, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid handle"); + dcm_retvm_if(!buffer, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid buffer"); switch (colorspace) { case FACE_IMAGE_COLORSPACE_YUV420: case FACE_IMAGE_COLORSPACE_RGB888: case FACE_IMAGE_COLORSPACE_RGBA: - data = (unsigned char *)calloc(1, size); - if (data == NULL) { - dcm_error("Not allocate memory"); - return MS_MEDIA_ERR_OUT_OF_MEMORY; - } - memcpy(data, buffer, size); + data = g_memdup(buffer, size); break; default: dcm_error("Invalid colorspace : [%d]", colorspace); @@ -97,16 +91,10 @@ int dcm_face_set_image_info(dcm_face_h handle, face_image_colorspace_e colorspac } if (_handle->image_info != NULL) - DCM_SAFE_FREE(_handle->image_info->data); - DCM_SAFE_FREE(_handle->image_info); - - _handle->image_info = (FaceImage *)calloc(1, sizeof(FaceImage)); + g_free(_handle->image_info->data); + g_free(_handle->image_info); - if (_handle->image_info == NULL) { - dcm_error("Out of memory"); - DCM_SAFE_FREE(data); - return MS_MEDIA_ERR_OUT_OF_MEMORY; - } + _handle->image_info = g_new0(FaceImage, 1); _handle->image_info->data = data; _handle->image_info->width = width; @@ -142,8 +130,8 @@ int dcm_face_destroy_face_info(face_info_s *face_info) { dcm_retvm_if(face_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid face_info"); - DCM_SAFE_FREE(face_info->rects); - DCM_SAFE_FREE(face_info); + g_free(face_info->rects); + g_free(face_info); return MS_MEDIA_ERR_NONE; } diff --git a/libdcm-face/dcm-face_mediavision.c b/libdcm-face/dcm-face_mediavision.c index 36b2917..aa8ed5a 100755 --- a/libdcm-face/dcm-face_mediavision.c +++ b/libdcm-face/dcm-face_mediavision.c @@ -29,7 +29,6 @@ typedef struct media_vision_h { } mv_handle; typedef struct media_vision_result_s { - int error; face_rect_s *face_rect; int count; } mv_faceInfo; @@ -91,29 +90,21 @@ static int __convert_to_mv_colorspace_e(face_image_colorspace_e src, mv_colorspa return ret; } -void __face_detected_cb(mv_source_h source, mv_engine_config_h cfg, mv_rectangle_s *faces_locations, int number_of_faces, void *user_data) +static void __face_detected_cb(mv_source_h source, mv_engine_config_h cfg, mv_rectangle_s *faces_locations, int number_of_faces, void *user_data) { mv_faceInfo* _data = (mv_faceInfo*)user_data; + int i = 0; dcm_error("[No-Error] number of faces: %d", number_of_faces); if (number_of_faces == 0) { _data->face_rect = NULL; _data->count = 0; - _data->error = MS_MEDIA_ERR_NONE; - return; - } - _data->face_rect = (face_rect_s *)calloc(number_of_faces, sizeof(face_rect_s)); - if (_data->face_rect == NULL) { - dcm_error("Cannout allocate face_rect_s"); - _data->face_rect = NULL; - _data->count = 0; - _data->error = MS_MEDIA_ERR_OUT_OF_MEMORY; return; } - int i = 0; + _data->face_rect = g_new0(face_rect_s, number_of_faces); for (i = 0; i < number_of_faces ; i++) { _data->face_rect[i].x = faces_locations[i].point.x; @@ -124,7 +115,6 @@ void __face_detected_cb(mv_source_h source, mv_engine_config_h cfg, mv_rectangle } _data->count = number_of_faces; - _data->error = MS_MEDIA_ERR_NONE; return; } @@ -132,16 +122,14 @@ void __face_detected_cb(mv_source_h source, mv_engine_config_h cfg, mv_rectangle int _face_handle_create(__inout void **handle) { int err = 0; - mv_handle* _handle = (mv_handle*)calloc(1, sizeof(mv_handle)); + mv_handle* _handle = g_new0(mv_handle, 1); dcm_debug_fenter(); - dcm_retvm_if(_handle == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "handle create fail"); - err = mv_create_engine_config(&(_handle->cfg)); if (err != MEDIA_VISION_ERROR_NONE) { dcm_error("fail to mv_create_engine_config"); - DCM_SAFE_FREE(_handle); + g_free(_handle); return __convert_to_mv_error_e(err); } @@ -151,7 +139,7 @@ int _face_handle_create(__inout void **handle) err = mv_destroy_engine_config(_handle->cfg); if (err != MEDIA_VISION_ERROR_NONE) dcm_error("fail to mv_destroy_engine_config"); - DCM_SAFE_FREE(_handle); + g_free(_handle); return __convert_to_mv_error_e(err); } @@ -221,14 +209,8 @@ int _face_detect_faces(__in dcm_face_h handle, __out face_rect_s *face_rect[], _ return __convert_to_mv_error_e(err); } - if (result.error == MS_MEDIA_ERR_NONE) { - *face_rect = result.face_rect; - *count = result.count; - } else { - *face_rect = result.face_rect; - *count = result.count; - return result.error; - } + *face_rect = result.face_rect; + *count = result.count; return MS_MEDIA_ERR_NONE; } diff --git a/libdcm-face/dcm-face_priv.h b/libdcm-face/dcm-face_priv.h index 887a625..df2f824 100755 --- a/libdcm-face/dcm-face_priv.h +++ b/libdcm-face/dcm-face_priv.h @@ -30,8 +30,6 @@ #define FACE_IMAGE_MAGIC (0x1a2b3c4d) #define FACE_INVALID_MAGIC (0xDEADBEAF) -#define DCM_SAFE_FREE(src) { if (src) {free(src); src = NULL; } } - typedef struct face_image_s { unsigned char *data; unsigned int width; diff --git a/libdcm-face/include/dcm-face.h b/libdcm-face/include/dcm-face.h index 8bafeea..7c4b6d5 100644 --- a/libdcm-face/include/dcm-face.h +++ b/libdcm-face/include/dcm-face.h @@ -36,7 +36,6 @@ extern "C" { * @return 0 on success, otherwise a negative error value. * @retval #MS_MEDIA_ERR_NONE Successful * @retval #MS_MEDIA_ERR_INVALID_PARAMETER Invalid parameter - * @retval #MS_MEDIA_ERR_OUT_OF_MEMORY Out of memory * @see dcm_face_destroy() */ int dcm_face_create(dcm_face_h *handle); @@ -64,7 +63,6 @@ int dcm_face_destroy(dcm_face_h handle); * @return 0 on success, otherwise a negative error value. * @retval #MS_MEDIA_ERR_NONE Successful * @retval #MS_MEDIA_ERR_INVALID_PARAMETER Invalid parameter - * @retval #MS_MEDIA_ERR_OUT_OF_MEMORY Out of memory * @see dcm_face_create() */ int dcm_face_set_image_info(dcm_face_h handle, face_image_colorspace_e colorspace, unsigned char *buffer, unsigned int width, unsigned int height, size_t size); diff --git a/libdcm-util/include/dcm_image_codec_debug.h b/libdcm-util/include/dcm_image_codec_debug.h index 3797d46..b0d8def 100755 --- a/libdcm-util/include/dcm_image_codec_debug.h +++ b/libdcm-util/include/dcm_image_codec_debug.h @@ -83,6 +83,4 @@ #define DCM_CHECK_VAL(expr, val) dcm_retvm_if(!(expr), val , "Invalid parameter, return ERROR code!") -#define DCM_SAFE_FREE(ptr) { if (ptr) {free(ptr); ptr = NULL; } } - #endif /* _DCM_DEBUG_UTILS_H_ */ diff --git a/packaging/dcm-service.spec b/packaging/dcm-service.spec index f60609f..63db162 100644 --- a/packaging/dcm-service.spec +++ b/packaging/dcm-service.spec @@ -1,6 +1,6 @@ Name: dcm-service Summary: A media DCM(Digital Contents Management) Service -Version: 0.2.3 +Version: 0.2.4 Release: 0 Group: Multimedia/Service License: Apache-2.0 diff --git a/src/dcm_svc_db.c b/src/dcm_svc_db.c index 0f00fcc..c06a7f0 100755 --- a/src/dcm_svc_db.c +++ b/src/dcm_svc_db.c @@ -86,12 +86,7 @@ int dcm_svc_db_get_scan_image_info_by_path(const char *file_path, uid_t uid, dcm if (ret != MS_MEDIA_ERR_NONE) goto ERROR; - _item = (dcm_svc_item_s *) calloc(1, sizeof(dcm_svc_item_s)); - if (!_item) { - dcm_error("Allocation failed"); - ret = MS_MEDIA_ERR_OUT_OF_MEMORY; - goto ERROR; - } + _item = g_new0(dcm_svc_item_s, 1); _item->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0)); _item->file_path = g_strdup(file_path); diff --git a/src/dcm_svc_detect_face.c b/src/dcm_svc_detect_face.c index ec6345a..d14d921 100755 --- a/src/dcm_svc_detect_face.c +++ b/src/dcm_svc_detect_face.c @@ -103,12 +103,7 @@ int dcm_face_detect_process(dcm_svc_item_s *scan_item, dcm_face_scan_status_e sc goto DCM_SVC_FACE_RECOGNIZE_BUFFER_FAILED; } - face_info = (face_info_s *)calloc(1, sizeof(face_info_s)); - if (face_info == NULL) { - dcm_error("Failed to allocate face info"); - ret = MS_MEDIA_ERR_OUT_OF_MEMORY; - goto DCM_SVC_FACE_RECOGNIZE_BUFFER_FAILED; - } + face_info = (face_info_s *)g_new0(face_info_s, 1); ret = dcm_face_get_face_info(dcm_face_handle, face_info); if (ret != MS_MEDIA_ERR_NONE) { diff --git a/src/dcm_svc_internal.c b/src/dcm_svc_internal.c index a3a9421..730e5a4 100755 --- a/src/dcm_svc_internal.c +++ b/src/dcm_svc_internal.c @@ -33,7 +33,7 @@ static int __dcm_scan_process(dcm_svc_item_s *scan_item, uid_t uid) DCM_CHECK_VAL(scan_item->file_path, MS_MEDIA_ERR_INVALID_PARAMETER); dcm_image_info_s image_info = {0, }; - memset(&image_info, 0, sizeof(dcm_image_info_s)); + image_info.decode_type = DCM_IMAGE_FORMAT_I420; if (g_file_test(scan_item->file_path, G_FILE_TEST_IS_REGULAR)) { @@ -82,7 +82,7 @@ static int __dcm_scan_process(dcm_svc_item_s *scan_item, uid_t uid) dcm_error("Failed to process face detection! err: %d", ret); /* Free image buffer */ - DCM_SAFE_FREE(image_info.pixel); + g_free(image_info.pixel); } else { dcm_warn("The file does not exist"); ret = MS_MEDIA_ERR_INVALID_PARAMETER; @@ -118,7 +118,7 @@ int dcm_scan_single(const char *file_path, uid_t uid, int *face_count) g_free(scan_item->media_uuid); g_free(scan_item->file_path); g_free(scan_item->mime_type); - DCM_SAFE_FREE(scan_item); + g_free(scan_item); dcm_debug_fleave(); -- 2.34.1