[neurun] Use std::nothrow for exception handling (#2184)
author오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Tue, 7 Aug 2018 10:43:45 +0000 (19:43 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 7 Aug 2018 10:43:45 +0000 (19:43 +0900)
Use std::nothrow for
- Handle exception
- Set returned reference by parameter nullptr

Signed-off-by: Hyeongseok Oh <hseok82.oh@samsung.com>
runtimes/neurun/src/frontend/compilation.cc
runtimes/neurun/src/frontend/execution.cc
runtimes/neurun/src/frontend/memory.cc
runtimes/neurun/src/frontend/model.cc

index f8b9a57..c0639a3 100644 (file)
@@ -1,5 +1,7 @@
 #include <NeuralNetworks.h>
 
+#include <new>
+
 #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;
 }
index 04a7210..0a331c1 100644 (file)
@@ -1,5 +1,7 @@
 #include <NeuralNetworks.h>
 
+#include <new>
+
 #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();
index cf2177d..d2f850f 100644 (file)
@@ -1,5 +1,6 @@
 #include <NeuralNetworks.h>
 #include <sys/mman.h>
+#include <new>
 
 #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;
 }
index fc6c852..9f0ac0d 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <cassert>
 #include <stdexcept>
+#include <new>
 
 #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;
 }