[systeminfo] Prevent possible crash when failure initialization
[platform/core/api/webapi-plugins.git] / src / ml / ml_tensors_data_manager.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include "ml_tensors_data_manager.h"
18 #include "ml_tensors_info_manager.h"
19
20 using common::ErrorCode;
21 using common::PlatformResult;
22
23 namespace extension {
24 namespace ml {
25
26 TensorsData::TensorsData(ml_tensors_data_h handle, int id, TensorsInfo* tensors_info)
27     : handle_(handle), id_(id), tensors_info_(tensors_info) {
28   ScopeLogger();
29 }
30
31 TensorsData::~TensorsData() {
32   ScopeLogger();
33   if (this->NativeDestroy()) {
34     LoggerE("TensorsData NativeDestroy failed");
35   }
36   // TensorsDataManager releases tensors_info_
37 }
38
39 ml_tensors_data_h TensorsData::Handle() {
40   return this->handle_;
41 }
42
43 int TensorsData::Id() {
44   return this->id_;
45 }
46
47 int TensorsData::TensorsInfoId() {
48   return this->tensors_info_->Id();
49 }
50
51 int TensorsData::Count() {
52   return tensors_info_->Count();
53 }
54
55 PlatformResult TensorsData::NativeDestroy() {
56   ScopeLogger("id_: %d", id_);
57   int ret = ml_tensors_data_destroy(handle_);
58   if (ML_ERROR_NONE != ret) {
59     LoggerE("ml_tensors_data_destroy failed: %d (%s)", ret, get_error_message(ret));
60     return util::ToPlatformResult(ret, "Failed to destroy handle");
61   }
62   return PlatformResult(ErrorCode::NO_ERROR);
63 }
64
65 TensorsDataManager::TensorsDataManager() : nextId_(0) {
66   ScopeLogger();
67 }
68
69 TensorsDataManager::~TensorsDataManager() {
70   ScopeLogger();
71   map_.clear();
72 };
73
74 TensorsData* TensorsDataManager::CreateTensorsData(TensorsInfo* tensors_info) {
75   ScopeLogger();
76   if (nullptr == tensors_info) {
77     LoggerE("Could not find tensor");
78     return nullptr;
79   }
80
81   ml_tensors_data_h tensors_data_handle;
82   int ret = ml_tensors_data_create(tensors_info->Handle(), &tensors_data_handle);
83   if (ML_ERROR_NONE != ret) {
84     LoggerE("ml_tensors_data_create failed: %d (%s)", ret, get_error_message(ret));
85     return nullptr;
86   }
87
88   int id = nextId_++;
89   auto t = std::make_unique<TensorsData>(tensors_data_handle, id, tensors_info);
90   map_[id] = std::move(t);
91
92   return map_[id].get();
93 };
94
95 TensorsData* TensorsDataManager::GetTensorsData(int id) {
96   ScopeLogger("id: %d", id);
97
98   if (map_.end() != map_.find(id)) {
99     return map_[id].get();
100   }
101
102   return nullptr;
103 }
104
105 PlatformResult TensorsDataManager::DisposeTensorsData(int id) {
106   ScopeLogger("id: %d", id);
107
108   TensorsData* t = GetTensorsData(id);
109
110   return DisposeTensorsData(t);
111 }
112
113 PlatformResult TensorsDataManager::DisposeTensorsData(TensorsData* t) {
114   ScopeLogger();
115
116   if (nullptr == t) {
117     LoggerE("Could not find tensor");
118     return PlatformResult(ErrorCode::ABORT_ERR);
119   }
120
121   map_.erase(t->Id());
122
123   return PlatformResult(ErrorCode::NO_ERROR);
124 }
125
126 }  // ml
127 }  // extension