From: Jaeyun Jung Date: Thu, 2 Nov 2023 04:43:48 +0000 (+0900) Subject: [Common] unref memory when appending tensor buffer X-Git-Tag: accepted/tizen/unified/20231110.172147~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=eb8a8b5f625e122ced9826e13c8db628035df3e0;p=platform%2Fupstream%2Fnnstreamer.git [Common] unref memory when appending tensor buffer Fix util function to take gst-memory if failed to append it. Signed-off-by: Jaeyun Jung --- diff --git a/gst/nnstreamer/elements/gsttensor_demux.c b/gst/nnstreamer/elements/gsttensor_demux.c index f503db2..a07b00c 100644 --- a/gst/nnstreamer/elements/gsttensor_demux.c +++ b/gst/nnstreamer/elements/gsttensor_demux.c @@ -525,7 +525,6 @@ gst_tensor_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf) idx); mem = gst_tensor_buffer_get_nth_memory (buf, idx); if (!gst_tensor_buffer_append_memory (outbuf, mem, _info)) { - gst_memory_unref (mem); gst_buffer_unref (outbuf); res = GST_FLOW_ERROR; goto error; @@ -538,7 +537,6 @@ gst_tensor_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf) gst_tensors_info_get_nth_info (&tensor_demux->tensors_config.info, i); mem = gst_tensor_buffer_get_nth_memory (buf, i); if (!gst_tensor_buffer_append_memory (outbuf, mem, _info)) { - gst_memory_unref (mem); gst_buffer_unref (outbuf); res = GST_FLOW_ERROR; goto error; diff --git a/gst/nnstreamer/include/nnstreamer_plugin_api.h b/gst/nnstreamer/include/nnstreamer_plugin_api.h index e1db435..e75af23 100644 --- a/gst/nnstreamer/include/nnstreamer_plugin_api.h +++ b/gst/nnstreamer/include/nnstreamer_plugin_api.h @@ -130,7 +130,7 @@ gst_tensor_buffer_get_nth_memory (GstBuffer * buffer, const guint index); /** * @brief Append @a memory to given @a buffer. * @param[in/out] buffer GstBuffer to be appended. - * @param[in] memory GstMemory to append. This function will take ownership of this. + * @param[in] memory GstMemory to append. This function takes ownership of this, even if it returns failure. * @param[in] info GstTensorInfo of given @a memory. * @return TRUE if successfully appended, otherwise FALSE. */ diff --git a/gst/nnstreamer/nnstreamer_plugin_api_impl.c b/gst/nnstreamer/nnstreamer_plugin_api_impl.c index 33611af..0601f73 100644 --- a/gst/nnstreamer/nnstreamer_plugin_api_impl.c +++ b/gst/nnstreamer/nnstreamer_plugin_api_impl.c @@ -1635,7 +1635,7 @@ gst_tensor_buffer_get_nth_memory (GstBuffer * buffer, const guint index) /** * @brief Append @a memory to given @a buffer. * @param[in/out] buffer GstBuffer to be appended. - * @param[in] memory GstMemory to append. This function will take ownership of this. + * @param[in] memory GstMemory to append. This function takes ownership of this, even if it returns failure. * @param[in] info GstTensorInfo of given @a memory. * @return TRUE if successfully appended, otherwise FALSE. */ @@ -1644,21 +1644,22 @@ gst_tensor_buffer_append_memory (GstBuffer * buffer, GstMemory * memory, const GstTensorInfo * info) { guint num_mems, offset, new_mem_index; - GstMemory *new_memory, *last_memory; + GstMemory *new_memory = NULL, *last_memory = NULL; gsize new_mem_size, last_mem_size; GstMapInfo new_memory_map, last_memory_map, incoming_memory_map; GstTensorExtraInfo *extra_info; GstTensorMetaInfo meta; gboolean is_extra, is_static; + gboolean appended = FALSE; if (!GST_IS_BUFFER (buffer)) { nns_loge ("Failed to append memory, given buffer is invalid."); - return FALSE; + goto failed; } if (!memory) { nns_loge ("Failed to append memory, given memory is NULL."); - return FALSE; + goto failed; } if (gst_tensor_meta_info_parse_memory (&meta, memory)) { @@ -1670,7 +1671,7 @@ gst_tensor_buffer_append_memory (GstBuffer * buffer, GstMemory * memory, /* Error case if given tensor-info is invalid. */ if (!gst_tensor_info_validate (info)) { nns_loge ("Failed to get tensor info (invalid input info)."); - return FALSE; + goto failed; } } @@ -1686,12 +1687,13 @@ gst_tensor_buffer_append_memory (GstBuffer * buffer, GstMemory * memory, last_memory = gst_buffer_peek_memory (buffer, num_mems - 1); if (!last_memory) { nns_loge ("Failed to get last memory"); - return FALSE; + goto failed; } if (!gst_memory_map (last_memory, &last_memory_map, GST_MAP_READ)) { nns_loge ("Failed to map last memory"); - return FALSE; + last_memory = NULL; + goto failed; } new_mem_size = last_mem_size = gst_memory_get_sizes (last_memory, NULL, NULL); @@ -1707,23 +1709,19 @@ gst_tensor_buffer_append_memory (GstBuffer * buffer, GstMemory * memory, new_memory = gst_allocator_alloc (NULL, new_mem_size, NULL); if (!new_memory) { nns_loge ("Failed to allocate memory for extra tensors."); - gst_memory_unmap (last_memory, &last_memory_map); - return FALSE; + goto failed; } if (!gst_memory_map (new_memory, &new_memory_map, GST_MAP_WRITE)) { nns_loge ("Failed to map extra memory"); - gst_memory_unmap (last_memory, &last_memory_map); gst_memory_unref (new_memory); - return FALSE; + new_memory = NULL; + goto failed; } if (!gst_memory_map (memory, &incoming_memory_map, GST_MAP_READ)) { nns_loge ("Failed to map incoming memory"); - gst_memory_unmap (new_memory, &new_memory_map); - gst_memory_unmap (last_memory, &last_memory_map); - gst_memory_unref (new_memory); - return FALSE; + goto failed; } extra_info = (GstTensorExtraInfo *) new_memory_map.data; @@ -1754,14 +1752,26 @@ gst_tensor_buffer_append_memory (GstBuffer * buffer, GstMemory * memory, memcpy (new_memory_map.data + offset + last_memory_map.size, incoming_memory_map.data, incoming_memory_map.size); - gst_memory_unmap (new_memory, &new_memory_map); gst_memory_unmap (memory, &incoming_memory_map); - gst_memory_unmap (last_memory, &last_memory_map); - gst_memory_unref (memory); gst_buffer_replace_memory (buffer, num_mems - 1, new_memory); + appended = TRUE; - return TRUE; +failed: + if (new_memory) { + gst_memory_unmap (new_memory, &new_memory_map); + if (!appended) + gst_memory_unref (new_memory); + } + + if (last_memory) + gst_memory_unmap (last_memory, &last_memory_map); + + /* Release incoming memory even if failed to append it into buffer. */ + if (memory) + gst_memory_unref (memory); + + return appended; } /**