From 34e431019ef1c9d540c82d35aa44b7780994c1e2 Mon Sep 17 00:00:00 2001 From: hj kim Date: Tue, 14 Apr 2020 15:24:17 +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: If45d428b921af0441437892fec85cb2799f52a75 --- include/image_util_private.h | 1 - src/image_util_decode.c | 51 ++++++++++---------------- src/image_util_encode.c | 87 ++++++++++++++------------------------------ src/image_util_private.c | 11 +++--- test/image_util_testsuite.c | 1 - 5 files changed, 53 insertions(+), 98 deletions(-) diff --git a/include/image_util_private.h b/include/image_util_private.h index 382aabb..1176577 100644 --- a/include/image_util_private.h +++ b/include/image_util_private.h @@ -82,7 +82,6 @@ extern "C" #define IMAGE_UTIL_STRING_VALID(str) \ ((str != NULL && strlen(str) > 0) ? true : false) -#define IMAGE_UTIL_SAFE_FREE(src) { if (src) {free(src); src = NULL; } } #define IMAGE_UTIL_SAFE_G_FREE(src) { if (src) {g_free(src); src = NULL; } } #define _NOT_SUPPORTED_COLORSPACE (-1) diff --git a/src/image_util_decode.c b/src/image_util_decode.c index 933e932..a48c4bc 100644 --- a/src/image_util_decode.c +++ b/src/image_util_decode.c @@ -106,12 +106,11 @@ static int __image_util_decode_check_image_type(const unsigned char *image_buffe int image_util_decode_create(image_util_decode_h * handle) { - image_util_fenter(); - image_util_retvm_if(!handle, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); - decode_s *_handle = (decode_s *) calloc(1, sizeof(decode_s)); - image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY"); + image_util_fenter(); + + decode_s *_handle = g_new0(decode_s, 1); _handle->src_buffer = NULL; _handle->dst_buffer = NULL; @@ -145,7 +144,7 @@ int image_util_decode_set_input_path(image_util_decode_h handle, const char *pat g_free(image_header); image_util_retvm_if(err != IMAGE_UTIL_ERROR_NONE, err, "Fail to __image_util_decode_check_image_type"); - IMAGE_UTIL_SAFE_FREE(_handle->src_buffer); + IMAGE_UTIL_SAFE_G_FREE(_handle->src_buffer); g_free(_handle->path); _handle->path = g_strdup(path); @@ -164,16 +163,12 @@ int image_util_decode_set_input_buffer(image_util_decode_h handle, const unsigne err = __image_util_decode_check_image_type(src_buffer, &_handle->image_type); image_util_retvm_if(err != IMAGE_UTIL_ERROR_NONE, err, "__image_util_decode_check_image_type failed"); - IMAGE_UTIL_SAFE_FREE(_handle->src_buffer); - - _handle->src_buffer = calloc(1, src_size); - image_util_retvm_if(!_handle->src_buffer, IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY"); + IMAGE_UTIL_SAFE_G_FREE(_handle->path); - memcpy(_handle->src_buffer, src_buffer, src_size); + g_free(_handle->src_buffer); + _handle->src_buffer = g_memdup(src_buffer, src_size); _handle->src_size = src_size; - IMAGE_UTIL_SAFE_G_FREE(_handle->path); - return err; } @@ -333,7 +328,7 @@ static int __image_util_decode_run_from_file(image_util_decode_h handle, const c image_util_retvm_if((ret != IMAGE_UTIL_ERROR_NONE), ret, "__image_util_decode_read_header failed"); ret = __image_util_decode_check_image_type(image_header, &image_type); - IMAGE_UTIL_SAFE_FREE(image_header); + g_free(image_header); image_util_retvm_if((ret != IMAGE_UTIL_ERROR_NONE), ret, "__image_util_decode_check_image_type failed"); switch (image_type) { @@ -444,7 +439,7 @@ static gpointer __image_util_decode_thread(gpointer data) } mm_image_destroy_image(image_info); - IMAGE_UTIL_SAFE_FREE(_handle->decode2_cb); + IMAGE_UTIL_SAFE_G_FREE(_handle->decode2_cb); IMAGE_UTIL_SAFE_G_FREE(_handle->path); } else { ret = __image_util_decode_internal(_handle, &image_info); @@ -498,22 +493,16 @@ int image_util_decode_run_async(image_util_decode_h handle, image_util_decode_co image_util_retvm_if(!completed_cb, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid callback"); image_util_retvm_if(_handle->thread, IMAGE_UTIL_ERROR_INVALID_OPERATION, "The thread is already running"); - if (_handle->_decode_cb != NULL) { - IMAGE_UTIL_SAFE_FREE(_handle->_decode_cb); - _handle->_decode_cb = NULL; - } + g_free(_handle->_decode_cb); - _handle->_decode_cb = (decode_cb_s *) calloc(1, sizeof(decode_cb_s)); - image_util_retvm_if((_handle->_decode_cb == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Out of memory"); + _handle->_decode_cb = g_new0(decode_cb_s, 1); _handle->_decode_cb->user_data = user_data; _handle->_decode_cb->image_decode_completed_cb = completed_cb; err = __image_util_decode_create_thread(_handle); - if (err != IMAGE_UTIL_ERROR_NONE) { - IMAGE_UTIL_SAFE_FREE(_handle->_decode_cb); - _handle->_decode_cb = NULL; - } + if (err != IMAGE_UTIL_ERROR_NONE) + IMAGE_UTIL_SAFE_G_FREE(_handle->_decode_cb); image_util_fleave(); @@ -546,15 +535,14 @@ int image_util_decode_run_async2(image_util_decode_h handle, image_util_decode_c _handle->is_decode2 = TRUE; - _handle->decode2_cb = (decode2_cb_s *) calloc(1, sizeof(decode2_cb_s)); - image_util_retvm_if((!_handle->decode2_cb), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Out of memory"); + _handle->decode2_cb = g_new0(decode2_cb_s, 1); _handle->decode2_cb->user_data = user_data; _handle->decode2_cb->image_decode_completed_cb = callback; ret = __image_util_decode_create_thread(_handle); if (ret != IMAGE_UTIL_ERROR_NONE) - IMAGE_UTIL_SAFE_FREE(_handle->decode2_cb); + IMAGE_UTIL_SAFE_G_FREE(_handle->decode2_cb); image_util_fleave(); @@ -572,11 +560,12 @@ int image_util_decode_destroy(image_util_decode_h handle) /* g_thread_exit(handle->thread); */ if (_handle->thread) { g_thread_join(_handle->thread); - IMAGE_UTIL_SAFE_FREE(_handle->_decode_cb); + IMAGE_UTIL_SAFE_G_FREE(_handle->_decode_cb); } - IMAGE_UTIL_SAFE_G_FREE(_handle->path); - IMAGE_UTIL_SAFE_FREE(_handle->src_buffer); - IMAGE_UTIL_SAFE_FREE(_handle); + + g_free(_handle->path); + g_free(_handle->src_buffer); + g_free(_handle); image_util_fleave(); diff --git a/src/image_util_encode.c b/src/image_util_encode.c index 697dd05..0b904e8 100644 --- a/src/image_util_encode.c +++ b/src/image_util_encode.c @@ -37,17 +37,8 @@ static int __allocate_source_buffer(encode_s *handle) image_util_fenter(); image_util_retvm_if(!handle, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid handle"); - if (handle->gif_encode_info.sources == NULL) { - handle->gif_encode_info.sources = calloc(1, sizeof(mm_image_info_s *)); - image_util_retvm_if((handle->gif_encode_info.sources == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY"); - handle->gif_encode_info.sources[0] = calloc(1, sizeof(mm_image_info_s)); - image_util_retvm_if((handle->gif_encode_info.sources[0] == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY"); - } else { - handle->gif_encode_info.sources = realloc(handle->gif_encode_info.sources, sizeof(mm_image_info_s *) * (handle->gif_encode_info.source_count + 1)); - image_util_retvm_if((handle->gif_encode_info.sources == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY"); - handle->gif_encode_info.sources[handle->gif_encode_info.source_count] = calloc(1, sizeof(mm_image_info_s)); - image_util_retvm_if((handle->gif_encode_info.sources[handle->gif_encode_info.source_count] == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY"); - } + handle->gif_encode_info.sources = g_realloc(handle->gif_encode_info.sources, sizeof(mm_image_info_s *) * (handle->gif_encode_info.source_count + 1)); + handle->gif_encode_info.sources[handle->gif_encode_info.source_count] = g_new0(mm_image_info_s, 1); handle->gif_encode_info.sources[handle->gif_encode_info.source_count]->color = IMAGE_UTIL_COLORSPACE_RGBA8888; handle->gif_encode_info.source_count++; @@ -69,10 +60,10 @@ static void __free_source_buffer(encode_s *handle) image_util_fenter(); - for (i = 0; i < handle->gif_encode_info.source_count; i++) { - IMAGE_UTIL_SAFE_FREE(handle->gif_encode_info.sources[i]); - } - IMAGE_UTIL_SAFE_FREE(handle->gif_encode_info.sources); + for (i = 0; i < handle->gif_encode_info.source_count; i++) + IMAGE_UTIL_SAFE_G_FREE(handle->gif_encode_info.sources[i]); + + IMAGE_UTIL_SAFE_G_FREE(handle->gif_encode_info.sources); image_util_fleave(); } @@ -85,8 +76,7 @@ int image_util_encode_create(image_util_type_e image_type, image_util_encode_h * image_util_retvm_if(!handle, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid handle"); IMAGE_UTIL_TYPE_CHECK(image_type); - encode_s *_handle = (encode_s *) calloc(1, sizeof(encode_s)); - image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY"); + encode_s *_handle = g_new0(encode_s, 1); _handle->image_type = image_type; _handle->dst_buffer = NULL; @@ -94,9 +84,6 @@ int image_util_encode_create(image_util_type_e image_type, image_util_encode_h * _handle->quality = 75; _handle->compression = IMAGE_UTIL_PNG_COMPRESSION_6; - memset(&_handle->src, 0, sizeof(mm_image_info_s)); - memset(&_handle->gif_encode_info, 0, sizeof(gif_encode_s)); - _handle->src.color = IMAGE_UTIL_COLORSPACE_RGBA8888; _handle->thread = NULL; @@ -278,7 +265,7 @@ int image_util_encode_set_output_buffer(image_util_encode_h handle, unsigned cha image_util_retvm_if(!_handle, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); image_util_retvm_if(!dst_buffer, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid dst_buffer"); - IMAGE_UTIL_SAFE_FREE(_handle->path); + IMAGE_UTIL_SAFE_G_FREE(_handle->path); _handle->dst_buffer = (void **)dst_buffer; @@ -453,7 +440,7 @@ static gpointer __image_util_encode_thread(gpointer data) } mm_image_destroy_image(_handle->new_src); - IMAGE_UTIL_SAFE_FREE(_handle->encode_to_file_cb); + IMAGE_UTIL_SAFE_G_FREE(_handle->encode_to_file_cb); IMAGE_UTIL_SAFE_G_FREE(_handle->path); } else { @@ -466,8 +453,8 @@ static gpointer __image_util_encode_thread(gpointer data) } mm_image_destroy_image(_handle->new_src); - IMAGE_UTIL_SAFE_FREE(_handle->encode_to_buffer_cb); - IMAGE_UTIL_SAFE_FREE(buffer); + IMAGE_UTIL_SAFE_G_FREE(_handle->encode_to_buffer_cb); + IMAGE_UTIL_SAFE_G_FREE(buffer); } } else { //old encode @@ -482,7 +469,7 @@ static gpointer __image_util_encode_thread(gpointer data) _handle->_encode_cb->image_encode_completed_cb(ret, _handle->_encode_cb->user_data, _handle->dst_size); } - IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb); + IMAGE_UTIL_SAFE_G_FREE(_handle->_encode_cb); } _handle->thread = NULL; @@ -534,21 +521,15 @@ int image_util_encode_run_async(image_util_encode_h handle, image_util_encode_co image_util_retvm_if(!completed_cb, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid callback"); image_util_retvm_if(_handle->thread, IMAGE_UTIL_ERROR_INVALID_OPERATION, "The thread is already running"); - if (_handle->_encode_cb != NULL) { - IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb); - _handle->_encode_cb = NULL; - } - _handle->_encode_cb = (encode_cb_s *) calloc(1, sizeof(encode_cb_s)); - image_util_retvm_if((_handle->_encode_cb == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Out of memory"); + g_free(_handle->_encode_cb); + _handle->_encode_cb = g_new0(encode_cb_s, 1); _handle->_encode_cb->user_data = user_data; _handle->_encode_cb->image_encode_completed_cb = completed_cb; err = __image_util_encode_create_thread(_handle); - if (err != IMAGE_UTIL_ERROR_NONE) { - IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb); - _handle->_encode_cb = NULL; - } + if (err != IMAGE_UTIL_ERROR_NONE) + IMAGE_UTIL_SAFE_G_FREE(_handle->_encode_cb); return err; } @@ -579,13 +560,7 @@ int image_util_encode_run_async_to_file(image_util_encode_h handle, image_util_i ret = mm_image_clone_image(image, &_handle->new_src); image_util_retvm_if(ret != MM_UTIL_ERROR_NONE, IMAGE_UTIL_ERROR_INVALID_OPERATION, "Fail to mm_image_clone_image"); - _handle->encode_to_file_cb = (encode_to_file_cb_s *) calloc(1, sizeof(encode_to_file_cb_s)); - if (!_handle->encode_to_file_cb) { - image_util_error("Out of memory"); - mm_image_destroy_image(_handle->new_src); - - return IMAGE_UTIL_ERROR_OUT_OF_MEMORY; - } + _handle->encode_to_file_cb = g_new0(encode_to_file_cb_s, 1); _handle->path = g_strdup(file_path); @@ -595,7 +570,7 @@ int image_util_encode_run_async_to_file(image_util_encode_h handle, image_util_i ret = __image_util_encode_create_thread(_handle); if (ret != IMAGE_UTIL_ERROR_NONE) { mm_image_destroy_image(_handle->new_src); - IMAGE_UTIL_SAFE_FREE(_handle->encode_to_file_cb); + IMAGE_UTIL_SAFE_G_FREE(_handle->encode_to_file_cb); IMAGE_UTIL_SAFE_G_FREE(_handle->path); } @@ -618,13 +593,7 @@ int image_util_encode_run_async_to_buffer(image_util_encode_h handle, image_util ret = mm_image_clone_image(image, &_handle->new_src); image_util_retvm_if(ret != MM_UTIL_ERROR_NONE, IMAGE_UTIL_ERROR_INVALID_OPERATION, "Fail to mm_image_clone_image"); - _handle->encode_to_buffer_cb = (encode_to_buffer_cb_s *) calloc(1, sizeof(encode_to_buffer_cb_s)); - if (!_handle->encode_to_buffer_cb) { - image_util_error("Out of memory"); - mm_image_destroy_image(_handle->new_src); - - return IMAGE_UTIL_ERROR_OUT_OF_MEMORY; - } + _handle->encode_to_buffer_cb = g_new0(encode_to_buffer_cb_s, 1); _handle->encode_to_buffer_cb->user_data = user_data; _handle->encode_to_buffer_cb->image_encode_to_buffer_cb = completed_cb; @@ -632,7 +601,7 @@ int image_util_encode_run_async_to_buffer(image_util_encode_h handle, image_util ret = __image_util_encode_create_thread(_handle); if (ret != IMAGE_UTIL_ERROR_NONE) { mm_image_destroy_image(_handle->new_src); - IMAGE_UTIL_SAFE_FREE(_handle->encode_to_buffer_cb); + IMAGE_UTIL_SAFE_G_FREE(_handle->encode_to_buffer_cb); } image_util_fleave(); @@ -652,13 +621,13 @@ int image_util_encode_destroy(image_util_encode_h handle) /* g_thread_exit(handle->thread); */ if (_handle->thread) { g_thread_join(_handle->thread); - IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb); + IMAGE_UTIL_SAFE_G_FREE(_handle->_encode_cb); } __free_source_buffer(_handle); - IMAGE_UTIL_SAFE_G_FREE(_handle->path); - IMAGE_UTIL_SAFE_FREE(_handle); + g_free(_handle->path); + g_free(_handle); return err; } @@ -673,7 +642,7 @@ int image_util_agif_encode_create(image_util_agif_encode_h *handle) ret = mm_util_gif_encode_create(&_handle); if (ret != MM_UTIL_ERROR_NONE) { image_util_error("mm_util_gif_encode_create is failed (%d)", ret); - IMAGE_UTIL_SAFE_FREE(_handle); + IMAGE_UTIL_SAFE_G_FREE(_handle); return _image_error_capi(ret); } @@ -687,7 +656,7 @@ int image_util_agif_encode_add_frame(image_util_agif_encode_h handle, image_util int ret = IMAGE_UTIL_ERROR_NONE; mm_gif_file_h _handle = (mm_gif_file_h)handle; - image_util_retvm_if(!handle, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); + image_util_retvm_if(!_handle, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); ret = mm_image_set_delay_time((mm_util_image_h)image, time_delay); if (ret != MM_UTIL_ERROR_NONE) { @@ -709,7 +678,7 @@ int image_util_agif_encode_save_to_file(image_util_agif_encode_h handle, const c int ret = IMAGE_UTIL_ERROR_NONE; mm_gif_file_h _handle = (mm_gif_file_h)handle; - image_util_retvm_if(!handle, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); + image_util_retvm_if(!_handle, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); ret = mm_util_gif_encode_set_file(_handle, file_path); image_util_retvm_if(ret != MM_UTIL_ERROR_NONE, _image_error_capi(ret), "mm_util_gif_encode_set_file is failed (%d)", ret); @@ -725,7 +694,7 @@ int image_util_agif_encode_save_to_buffer(image_util_agif_encode_h handle, unsig int ret = IMAGE_UTIL_ERROR_NONE; mm_gif_file_h _handle = (mm_gif_file_h)handle; - image_util_retvm_if(!handle, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); + image_util_retvm_if(!_handle, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); ret = mm_util_gif_encode_set_mem(_handle, (void **)buffer, buffer_size); image_util_retvm_if(ret != MM_UTIL_ERROR_NONE, _image_error_capi(ret), "mm_util_gif_encode_set_mem is failed (%d)", ret); @@ -742,7 +711,7 @@ int image_util_agif_encode_destroy(image_util_agif_encode_h handle) image_util_fenter(); - image_util_retvm_if(!handle, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); + image_util_retvm_if(!_handle, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); mm_util_gif_encode_destroy(_handle); diff --git a/src/image_util_private.c b/src/image_util_private.c index f66ff19..4992f8b 100755 --- a/src/image_util_private.c +++ b/src/image_util_private.c @@ -438,7 +438,7 @@ int _image_util_image_to_packet(image_util_image_h image, media_packet_h *packet ret = __create_media_format(__image_format_to_mimetype(format), width, height, &fmt); if (ret != IMAGE_UTIL_ERROR_NONE) { image_util_error("__create_media_format failed (%d)", ret); - IMAGE_UTIL_SAFE_FREE(buffer); + g_free(buffer); return IMAGE_UTIL_ERROR_INVALID_OPERATION; } @@ -447,7 +447,7 @@ int _image_util_image_to_packet(image_util_image_h image, media_packet_h *packet image_util_error("__create_media_packet failed (%d)", ret); media_format_unref(fmt); - IMAGE_UTIL_SAFE_FREE(buffer); + g_free(buffer); image_util_fleave(); @@ -516,8 +516,7 @@ int _image_util_packet_to_image(media_packet_h packet, image_util_image_h *image image_util_debug("stride_width: %d, stride_height: %d, plane_ptr: %p, plane_size: %zu", stride_width, stride_height, plane_ptr,plane_size); - ptr = realloc(ptr, size + plane_size); - image_util_retvm_if(!ptr, IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Memory allocation failed"); + ptr = g_realloc(ptr, size + plane_size); memcpy(ptr + size, plane_ptr, plane_size); size += plane_size; @@ -526,7 +525,7 @@ int _image_util_packet_to_image(media_packet_h packet, image_util_image_h *image image_util_debug("[Fotmat: %u] W x H : %d x %d buffer(size) : %p(%zu)", mimetype, width, height, ptr, size); ret = mm_image_create_image(width, height, __mimetype_to_image_format(mimetype), ptr, size, image); - IMAGE_UTIL_SAFE_FREE(ptr); + g_free(ptr); image_util_retvm_if((ret != IMAGE_UTIL_ERROR_NONE), ret, "mm_image_create_image failed (%d)", ret); image_util_fleave(); @@ -534,6 +533,6 @@ int _image_util_packet_to_image(media_packet_h packet, image_util_image_h *image return IMAGE_UTIL_ERROR_NONE; ERROR: - IMAGE_UTIL_SAFE_FREE(ptr); + g_free(ptr); return IMAGE_UTIL_ERROR_INVALID_OPERATION; } diff --git a/test/image_util_testsuite.c b/test/image_util_testsuite.c index d684647..ebc86c5 100644 --- a/test/image_util_testsuite.c +++ b/test/image_util_testsuite.c @@ -629,7 +629,6 @@ static int __write_jpeg(const char *name, image_util_image_h image) char *path = NULL; path = g_strdup_printf("%s_%s.jpg", FILE_FOR_DUMP_RAW_IMAGE, name); - RETVM_IF(!path, IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Memory allocation failed"); ret = image_util_encode_create(IMAGE_UTIL_JPEG, &handle); if (ret != IMAGE_UTIL_ERROR_NONE) { -- 2.7.4