2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "ml_tensors_data_manager.h"
18 #include "ml_tensors_info_manager.h"
20 using common::ErrorCode;
21 using common::PlatformResult;
26 TensorsData::TensorsData(ml_tensors_data_h handle, int id, TensorsInfo* tensors_info)
27 : handle_(handle), id_(id), tensors_info_(tensors_info) {
31 TensorsData::~TensorsData() {
33 if (this->NativeDestroy()) {
34 LoggerE("TensorsData NativeDestroy failed");
36 // TensorsDataManager releases tensors_info_
39 ml_tensors_data_h TensorsData::Handle() {
43 int TensorsData::Id() {
47 int TensorsData::TensorsInfoId() {
48 return this->tensors_info_->Id();
51 int TensorsData::Count() {
52 return tensors_info_->Count();
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");
62 return PlatformResult(ErrorCode::NO_ERROR);
65 TensorsDataManager::TensorsDataManager() : nextId_(0) {
69 TensorsDataManager::~TensorsDataManager() {
74 TensorsData* TensorsDataManager::CreateTensorsData(TensorsInfo* tensors_info) {
76 if (nullptr == tensors_info) {
77 LoggerE("Could not find tensor");
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));
89 auto t = std::make_unique<TensorsData>(tensors_data_handle, id, tensors_info);
90 map_[id] = std::move(t);
92 return map_[id].get();
95 TensorsData* TensorsDataManager::GetTensorsData(int id) {
96 ScopeLogger("id: %d", id);
98 if (map_.end() != map_.find(id)) {
99 return map_[id].get();
105 PlatformResult TensorsDataManager::DisposeTensorsData(int id) {
106 ScopeLogger("id: %d", id);
108 TensorsData* t = GetTensorsData(id);
110 return DisposeTensorsData(t);
113 PlatformResult TensorsDataManager::DisposeTensorsData(TensorsData* t) {
117 LoggerE("Could not find tensor");
118 return PlatformResult(ErrorCode::ABORT_ERR);
123 return PlatformResult(ErrorCode::NO_ERROR);