From f3b9a01f2b365e60de5e25a540084f5313072788 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=98=A4=ED=98=95=EC=84=9D/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Tue, 7 Aug 2018 19:43:45 +0900 Subject: [PATCH] [neurun] Use std::nothrow for exception handling (#2184) Use std::nothrow for - Handle exception - Set returned reference by parameter nullptr Signed-off-by: Hyeongseok Oh --- runtimes/neurun/src/frontend/compilation.cc | 7 ++++--- runtimes/neurun/src/frontend/execution.cc | 12 ++++++------ runtimes/neurun/src/frontend/memory.cc | 6 +++--- runtimes/neurun/src/frontend/model.cc | 8 +++----- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/runtimes/neurun/src/frontend/compilation.cc b/runtimes/neurun/src/frontend/compilation.cc index f8b9a57..c0639a3 100644 --- a/runtimes/neurun/src/frontend/compilation.cc +++ b/runtimes/neurun/src/frontend/compilation.cc @@ -1,5 +1,7 @@ #include +#include + #include "model.h" #include "compilation.h" @@ -18,12 +20,11 @@ int ANeuralNetworksCompilation_create(ANeuralNetworksModel *model, model->release(internal); - ANeuralNetworksCompilation *compilation_ptr = new ANeuralNetworksCompilation(internal); - if (compilation_ptr == nullptr) + *compilation = new (std::nothrow) ANeuralNetworksCompilation(internal); + if (*compilation == nullptr) { return ANEURALNETWORKS_OUT_OF_MEMORY; } - *compilation = compilation_ptr; return ANEURALNETWORKS_NO_ERROR; } diff --git a/runtimes/neurun/src/frontend/execution.cc b/runtimes/neurun/src/frontend/execution.cc index 04a7210..0a331c1 100644 --- a/runtimes/neurun/src/frontend/execution.cc +++ b/runtimes/neurun/src/frontend/execution.cc @@ -1,5 +1,7 @@ #include +#include + #include "compilation.h" #include "execution.h" #include "event.h" @@ -21,12 +23,11 @@ int ANeuralNetworksExecution_create(ANeuralNetworksCompilation *compilation, compilation->publish(plan); - ANeuralNetworksExecution *execution_ptr = new ANeuralNetworksExecution{plan}; - if (execution_ptr == nullptr) + *execution = new (std::nothrow) ANeuralNetworksExecution{plan}; + if (*execution == nullptr) { return ANEURALNETWORKS_OUT_OF_MEMORY; } - *execution = execution_ptr; return ANEURALNETWORKS_NO_ERROR; } @@ -127,12 +128,11 @@ int ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution *execution, } // TODO: Handle event - ANeuralNetworksEvent *event_ptr = new ANeuralNetworksEvent{}; - if (event_ptr == nullptr) + *event = new (std::nothrow) ANeuralNetworksEvent{}; + if (*event == nullptr) { return ANEURALNETWORKS_OUT_OF_MEMORY; } - *event = event_ptr; const auto &plan = execution->plan(); const auto &model = plan.model(); diff --git a/runtimes/neurun/src/frontend/memory.cc b/runtimes/neurun/src/frontend/memory.cc index cf2177d..d2f850f 100644 --- a/runtimes/neurun/src/frontend/memory.cc +++ b/runtimes/neurun/src/frontend/memory.cc @@ -1,5 +1,6 @@ #include #include +#include #include "memory.h" @@ -11,12 +12,11 @@ int ANeuralNetworksMemory_createFromFd(size_t size, int protect, int fd, size_t return ANEURALNETWORKS_UNEXPECTED_NULL; } - ANeuralNetworksMemory *memory_ptr = new ANeuralNetworksMemory{size, protect, fd, offset}; - if (memory_ptr == nullptr) + *memory = new (std::nothrow) ANeuralNetworksMemory{size, protect, fd, offset}; + if (*memory == nullptr) { return ANEURALNETWORKS_OUT_OF_MEMORY; } - *memory = memory_ptr; return ANEURALNETWORKS_NO_ERROR; } diff --git a/runtimes/neurun/src/frontend/model.cc b/runtimes/neurun/src/frontend/model.cc index fc6c852..9f0ac0d 100644 --- a/runtimes/neurun/src/frontend/model.cc +++ b/runtimes/neurun/src/frontend/model.cc @@ -3,6 +3,7 @@ #include #include +#include #include "model.h" #include "memory.h" @@ -14,15 +15,12 @@ int ANeuralNetworksModel_create(ANeuralNetworksModel **model) return ANEURALNETWORKS_UNEXPECTED_NULL; } - ANeuralNetworksModel *model_ptr = new ANeuralNetworksModel{}; - - if (model_ptr == nullptr) + *model = new (std::nothrow) ANeuralNetworksModel{}; + if (*model == nullptr) { return ANEURALNETWORKS_OUT_OF_MEMORY; } - *model = model_ptr; - return ANEURALNETWORKS_NO_ERROR; } -- 2.7.4