1 /** 2 * Copyright 2021 Huawei Technologies Co., Ltd 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 "coder/generator/component/const_blocks/mmodel.h" 18 19 namespace mindspore::lite::micro { 20 const char model_header[] = R"RAW( 21 /** 22 * Copyright 2021 Huawei Technologies Co., Ltd 23 * 24 * Licensed under the Apache License, Version 2.0 (the "License"); 25 * you may not use this file except in compliance with the License. 26 * You may obtain a copy of the License at 27 * 28 * http://www.apache.org/licenses/LICENSE-2.0 29 * 30 * Unless required by applicable law or agreed to in writing, software 31 * distributed under the License is distributed on an "AS IS" BASIS, 32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 33 * See the License for the specific language governing permissions and 34 * limitations under the License. 35 */ 36 #ifndef MINDSPORE_LITE_LIBRARY_SOURCE_MODEL_H_ 37 #define MINDSPORE_LITE_LIBRARY_SOURCE_MODEL_H_ 38 39 #include "include/model.h" 40 #include "session.h" 41 #include <new> 42 #include <string.h> 43 44 namespace mindspore::lite { 45 class MModel : public Model { 46 public: 47 void Free() override { 48 if (this->buf != nullptr) { 49 free(this->buf); 50 this->buf = nullptr; 51 this->buf_size_ = 0; 52 } 53 } 54 55 void Destroy() override { Free(); } 56 57 ~MModel() override { Destroy(); } 58 59 void set_buf_size(size_t size) { buf_size_ = size; } 60 size_t buf_size() const { return buf_size_; } 61 62 private: 63 size_t buf_size_{0}; 64 }; 65 66 Model *Model::Import(const char *model_buf, size_t size) { 67 MS_NULLPTR_IF_NULL(model_buf); 68 if (size == 0) { 69 return nullptr; 70 } 71 MModel *model = new (std::nothrow) MModel(); 72 MS_NULLPTR_IF_NULL(model); 73 model->buf = reinterpret_cast<char *>(malloc(size)); 74 if (model->buf == nullptr) { 75 delete model; 76 return nullptr; 77 } 78 memcpy(model->buf, model_buf, size); 79 model->set_buf_size(size); 80 return model; 81 } 82 } // namespace mindspore::lite 83 #endif // MINDSPORE_LITE_LIBRARY_SOURCE_MODEL_H_ 84 85 )RAW"; 86 } // namespace mindspore::lite::micro 87