diff mbox series

[FFmpeg-devel,V2,3/3] dnn/queue: add error check and cleanup

Message ID 20201230153524.25704-1-yejun.guo@intel.com
State Accepted
Commit 6b0cfa8399d34e34ba68d98a837dec82c88d49c6
Headers show
Series [FFmpeg-devel,V2,1/3] dnn: fix redefining typedefs and also refine naming with correct prefix
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Guo, Yejun Dec. 30, 2020, 3:35 p.m. UTC
Signed-off-by: Guo, Yejun <yejun.guo@intel.com>
---
 libavfilter/dnn/dnn_backend_openvino.c | 16 ++++++++++++---
 libavfilter/dnn/queue.c                | 28 ++++++++++++++++++++------
 libavfilter/dnn/queue.h                |  4 ++--
 libavfilter/dnn/safe_queue.c           | 16 ++++++++++-----
 libavfilter/dnn/safe_queue.h           |  4 ++--
 5 files changed, 50 insertions(+), 18 deletions(-)
diff mbox series

Patch

diff --git a/libavfilter/dnn/dnn_backend_openvino.c b/libavfilter/dnn/dnn_backend_openvino.c
index 331f13b263..d27e451eea 100644
--- a/libavfilter/dnn/dnn_backend_openvino.c
+++ b/libavfilter/dnn/dnn_backend_openvino.c
@@ -208,7 +208,10 @@  static void infer_completion_callback(void *args)
 
     if (task->async) {
         request->task = NULL;
-        ff_safe_queue_push_back(task->ov_model->request_queue, request);
+        if (ff_safe_queue_push_back(task->ov_model->request_queue, request) < 0) {
+            av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
+            return;
+        }
     }
 
     task->done = 1;
@@ -436,7 +439,10 @@  DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options,
         item->infer_request = request;
         item->callback.completeCallBackFunc = infer_completion_callback;
         item->callback.args = item;
-        ff_safe_queue_push_back(ov_model->request_queue, item);
+        if (ff_safe_queue_push_back(ov_model->request_queue, item) < 0) {
+            av_freep(&item);
+            goto err;
+        }
     }
 
     ov_model->task_queue = ff_queue_create();
@@ -527,7 +533,11 @@  DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *i
     task->output_name = output_names[0];
     task->out_frame = out_frame;
     task->ov_model = ov_model;
-    ff_queue_push_back(ov_model->task_queue, task);
+    if (ff_queue_push_back(ov_model->task_queue, task) < 0) {
+        av_freep(&task);
+        av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
+        return DNN_ERROR;
+    }
 
     request = ff_safe_queue_pop_front(ov_model->request_queue);
     if (!request) {
diff --git a/libavfilter/dnn/queue.c b/libavfilter/dnn/queue.c
index aa912b6f3a..e64f567906 100644
--- a/libavfilter/dnn/queue.c
+++ b/libavfilter/dnn/queue.c
@@ -40,8 +40,8 @@  struct FFQueue {
 static inline FFQueueEntry *create_entry(void *val)
 {
     FFQueueEntry *entry = av_malloc(sizeof(*entry));
-    av_assert0(entry != NULL);
-    entry->value = val;
+    if (entry)
+        entry->value = val;
     return entry;
 }
 
@@ -53,6 +53,14 @@  FFQueue* ff_queue_create(void)
 
     q->head = create_entry(q);
     q->tail = create_entry(q);
+
+    if (!q->head || !q->tail) {
+        av_freep(&q->head);
+        av_freep(&q->tail);
+        av_freep(&q);
+        return NULL;
+    }
+
     q->head->next = q->tail;
     q->tail->prev = q->head;
     q->head->prev = NULL;
@@ -99,14 +107,16 @@  void *ff_queue_peek_back(FFQueue *q)
     return q->tail->prev->value;
 }
 
-void ff_queue_push_front(FFQueue *q, void *v)
+int ff_queue_push_front(FFQueue *q, void *v)
 {
     FFQueueEntry *new_entry;
     FFQueueEntry *original_next;
     if (!q)
-        return;
+        return 0;
 
     new_entry = create_entry(v);
+    if (!new_entry)
+        return -1;
     original_next = q->head->next;
 
     q->head->next = new_entry;
@@ -114,16 +124,20 @@  void ff_queue_push_front(FFQueue *q, void *v)
     new_entry->prev = q->head;
     new_entry->next = original_next;
     q->length++;
+
+    return q->length;
 }
 
-void ff_queue_push_back(FFQueue *q, void *v)
+int ff_queue_push_back(FFQueue *q, void *v)
 {
     FFQueueEntry *new_entry;
     FFQueueEntry *original_prev;
     if (!q)
-        return;
+        return 0;
 
     new_entry = create_entry(v);
+    if (!new_entry)
+        return -1;
     original_prev = q->tail->prev;
 
     q->tail->prev = new_entry;
@@ -131,6 +145,8 @@  void ff_queue_push_back(FFQueue *q, void *v)
     new_entry->next = q->tail;
     new_entry->prev = original_prev;
     q->length++;
+
+    return q->length;
 }
 
 void *ff_queue_pop_front(FFQueue *q)
diff --git a/libavfilter/dnn/queue.h b/libavfilter/dnn/queue.h
index 5703cef58c..df913d91ac 100644
--- a/libavfilter/dnn/queue.h
+++ b/libavfilter/dnn/queue.h
@@ -32,8 +32,8 @@  size_t ff_queue_size(FFQueue *q);
 void *ff_queue_peek_front(FFQueue *q);
 void *ff_queue_peek_back(FFQueue *q);
 
-void ff_queue_push_front(FFQueue *q, void *v);
-void ff_queue_push_back(FFQueue *q, void *v);
+int ff_queue_push_front(FFQueue *q, void *v);
+int ff_queue_push_back(FFQueue *q, void *v);
 
 void *ff_queue_pop_front(FFQueue *q);
 void *ff_queue_pop_back(FFQueue *q);
diff --git a/libavfilter/dnn/safe_queue.c b/libavfilter/dnn/safe_queue.c
index 559b939ec9..6b55b75246 100644
--- a/libavfilter/dnn/safe_queue.c
+++ b/libavfilter/dnn/safe_queue.c
@@ -56,8 +56,10 @@  FFSafeQueue *ff_safe_queue_create(void)
         return NULL;
 
     sq->q = ff_queue_create();
-    if (!sq->q)
+    if (!sq->q) {
+        av_freep(&sq);
         return NULL;
+    }
 
     ff_mutex_init(&sq->mutex, NULL);
     dnn_cond_init(&sq->cond, NULL);
@@ -80,20 +82,24 @@  size_t ff_safe_queue_size(FFSafeQueue *sq)
     return sq ? ff_queue_size(sq->q) : 0;
 }
 
-void ff_safe_queue_push_front(FFSafeQueue *sq, void *v)
+int ff_safe_queue_push_front(FFSafeQueue *sq, void *v)
 {
+    int ret;
     ff_mutex_lock(&sq->mutex);
-    ff_queue_push_front(sq->q, v);
+    ret = ff_queue_push_front(sq->q, v);
     dnn_cond_signal(&sq->cond);
     ff_mutex_unlock(&sq->mutex);
+    return ret;
 }
 
-void ff_safe_queue_push_back(FFSafeQueue *sq, void *v)
+int ff_safe_queue_push_back(FFSafeQueue *sq, void *v)
 {
+    int ret;
     ff_mutex_lock(&sq->mutex);
-    ff_queue_push_back(sq->q, v);
+    ret = ff_queue_push_back(sq->q, v);
     dnn_cond_signal(&sq->cond);
     ff_mutex_unlock(&sq->mutex);
+    return ret;
 }
 
 void *ff_safe_queue_pop_front(FFSafeQueue *sq)
diff --git a/libavfilter/dnn/safe_queue.h b/libavfilter/dnn/safe_queue.h
index 85488e80ee..548a282f0c 100644
--- a/libavfilter/dnn/safe_queue.h
+++ b/libavfilter/dnn/safe_queue.h
@@ -28,8 +28,8 @@  void ff_safe_queue_destroy(FFSafeQueue *sq);
 
 size_t ff_safe_queue_size(FFSafeQueue *sq);
 
-void ff_safe_queue_push_front(FFSafeQueue *sq, void *v);
-void ff_safe_queue_push_back(FFSafeQueue *sq, void *v);
+int ff_safe_queue_push_front(FFSafeQueue *sq, void *v);
+int ff_safe_queue_push_back(FFSafeQueue *sq, void *v);
 
 void *ff_safe_queue_pop_front(FFSafeQueue *sq);