From 770c3b05f38449502ebcd8f1d2687a54b7632ac9 Mon Sep 17 00:00:00 2001 From: wangzhe Date: Tue, 15 Sep 2020 17:19:54 +0800 Subject: [PATCH] add custom-implemented String --- mindspore/lite/internal/CMakeLists.txt | 1 + mindspore/lite/internal/include/lite_utils.h | 5 +- mindspore/lite/internal/include/string.h | 100 +++++ mindspore/lite/internal/src/common/string.cc | 357 ++++++++++++++++++ mindspore/lite/internal/src/lite_log.h | 4 + mindspore/lite/tools/common/protobuf_utils.cc | 4 +- 6 files changed, 466 insertions(+), 5 deletions(-) create mode 100644 mindspore/lite/internal/include/string.h create mode 100644 mindspore/lite/internal/src/common/string.cc diff --git a/mindspore/lite/internal/CMakeLists.txt b/mindspore/lite/internal/CMakeLists.txt index 9a306b203e3..bf1d374309e 100644 --- a/mindspore/lite/internal/CMakeLists.txt +++ b/mindspore/lite/internal/CMakeLists.txt @@ -26,6 +26,7 @@ list(REMOVE_ITEM KERNEL_SRC ${CMAKE_CURRENT_SOURCE_DIR}/../nnacl/opt_op_handler. set(CCSRC ${CMAKE_CURRENT_SOURCE_DIR}/src/lite_session.cc ${CMAKE_CURRENT_SOURCE_DIR}/src/ms_tensor.cc + ${CMAKE_CURRENT_SOURCE_DIR}/src/common/string.cc ${TOP_DIR}/src/common/log_adapter.cc ${CMAKE_CURRENT_SOURCE_DIR}/../../core/gvar/logging_level.cc ${TOP_DIR}/src/runtime/allocator.cc diff --git a/mindspore/lite/internal/include/lite_utils.h b/mindspore/lite/internal/include/lite_utils.h index a9236f04ea2..e2eac49f542 100644 --- a/mindspore/lite/internal/include/lite_utils.h +++ b/mindspore/lite/internal/include/lite_utils.h @@ -17,15 +17,14 @@ #ifndef MINDSPORE_LITE_INTERNAL_INCLUDE_LITE_UTILS_H_ #define MINDSPORE_LITE_INTERNAL_INCLUDE_LITE_UTILS_H_ #include -#include +#include "internal/include/string.h" struct MSTensor; struct Node; using TensorPtr = MSTensor *; using TensorPtrVector = std::vector; using Uint32Vector = std::vector; -using String = std::string; -using StringVector = std::vector; +using StringVector = std::vector; using ShapeVector = std::vector; using NodePtrVector = std::vector; using Int32Vector = std::vector; diff --git a/mindspore/lite/internal/include/string.h b/mindspore/lite/internal/include/string.h new file mode 100644 index 00000000000..3e92a466ea0 --- /dev/null +++ b/mindspore/lite/internal/include/string.h @@ -0,0 +1,100 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef INTERNAL_SRC_STRING_H_ +#define INTERNAL_SRC_STRING_H_ +#include +#include + +typedef struct String { + public: + String(); + String(size_t count, char ch); + String(const char *s, size_t count); + explicit String(const char *s); + String(const String &other); + String(const String &other, size_t pos, size_t count = npos); + + ~String(); + + String &operator=(const String &str); + String &operator=(const char *str); + + char &at(size_t pos); + const char &at(size_t pos) const; + char &operator[](size_t pos); + const char &operator[](size_t pos) const; + char *data() noexcept; + const char *data() const noexcept; + const char *c_str() const noexcept; + + // capacity + bool empty() const noexcept; + size_t size() const noexcept; + size_t length() const noexcept; + + // operations + void clear() noexcept; + String &append(size_t count, const char ch); + String &append(const String &str); + String &append(const char *s); + String &operator+=(const String &str); + String &operator+=(const char *str); + String &operator+=(const char ch); + int compare(const String &str) const; + int compare(const char *str) const; + + String substr(size_t pos = 0, size_t count = npos) const; + + static const size_t npos = -1; + + private: + size_t size_; + char *buffer_; +} String; + +bool operator==(const String &lhs, const String &rhs); +bool operator==(const String &lhs, const char *rhs); +bool operator==(const char *lhs, const String rhs); + +bool operator!=(const String &lhs, const String &rhs); +bool operator!=(const String &lhs, const char *rhs); +bool operator!=(const char *lhs, const String rhs); + +bool operator<(const String &lhs, const String &rhs); +bool operator<(const String &lhs, const char *rhs); +bool operator<(const char *lhs, const String rhs); + +bool operator>(const String &lhs, const String &rhs); +bool operator>(const String &lhs, const char *rhs); +bool operator>(const char *lhs, const String rhs); + +bool operator<=(const String &lhs, const String &rhs); +bool operator<=(const String &lhs, const char *rhs); +bool operator<=(const char *lhs, const String rhs); + +bool operator>=(const String &lhs, const String &rhs); +bool operator>=(const String &lhs, const char *rhs); +bool operator>=(const char *lhs, const String rhs); + +String to_String(int32_t value); +String to_String(int64_t value); +String to_String(uint32_t value); +String to_String(uint64_t value); +String to_String(float value); +String to_String(double value); +String to_String(long double value); + +#endif // INTERNAL_SRC_STRING_H_ diff --git a/mindspore/lite/internal/src/common/string.cc b/mindspore/lite/internal/src/common/string.cc new file mode 100644 index 00000000000..1bc675d027d --- /dev/null +++ b/mindspore/lite/internal/src/common/string.cc @@ -0,0 +1,357 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "internal/include/string.h" +#include +#include +#include +#include +#include +#include "internal/src/lite_log.h" + +String::String() { + buffer_ = reinterpret_cast(malloc(sizeof(char) * 1)); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + buffer_[0] = '\0'; + size_ = 0; +} + +String::String(size_t count, char ch) { + buffer_ = reinterpret_cast(malloc(sizeof(char) * (count + 1))); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + memset(buffer_, ch, count); + buffer_[count] = '\0'; + size_ = count; +} +String::String(const char *s, size_t count) { + if (s == nullptr) { + buffer_ = reinterpret_cast(malloc(sizeof(char) * 1)); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + buffer_[0] = '\0'; + size_ = 0; + return; + } + size_t size_s = strlen(s); + if (size_s <= count) { + size_ = size_s; + } else { + size_ = count; + } + buffer_ = reinterpret_cast(malloc(sizeof(char) * (size_ + 1))); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + strncpy(buffer_, s, size_); + buffer_[size_] = '\0'; +} + +String::String(const char *s) { + if (s == nullptr) { + buffer_ = reinterpret_cast(malloc(sizeof(char) * 1)); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + buffer_[0] = '\0'; + size_ = 0; + return; + } + size_ = strlen(s); + buffer_ = reinterpret_cast(malloc(sizeof(char) * (size_ + 1))); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + memcpy(buffer_, s, size_ + 1); +} + +String::String(const String &other) { + buffer_ = reinterpret_cast(malloc(sizeof(char) * (other.size_ + 1))); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + size_ = other.size_; + memcpy(buffer_, other.buffer_, size_ + 1); +} + +String::String(const String &other, size_t pos, size_t count) { + if (pos >= other.size_) { + buffer_ = reinterpret_cast(malloc(sizeof(char) * 1)); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + buffer_[0] = '\0'; + size_ = 0; + } else { + if (count == npos) { + count = other.size_ - pos; + } + if (pos + count > other.size_) { + size_ = other.size_ - pos; + } else { + size_ = count; + } + buffer_ = reinterpret_cast(malloc(sizeof(char) * (size_ + 1))); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + strncpy(buffer_, other.buffer_ + pos, size_); + buffer_[size_] = '\0'; + } +} + +String::~String() { free(buffer_); } + +String &String::operator=(const String &str) { + if (this == &str) { + return *this; + } + free(buffer_); + buffer_ = reinterpret_cast(malloc(sizeof(char) * (str.size_ + 1))); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + size_ = str.size_; + memcpy(buffer_, str.buffer_, size_ + 1); + return *this; +} + +String &String::operator=(const char *str) { + free(buffer_); + if (str == nullptr) { + buffer_ = reinterpret_cast(malloc(sizeof(char) * 1)); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + buffer_[0] = '\0'; + size_ = 0; + return *this; + } + size_t size_s = strlen(str); + buffer_ = reinterpret_cast(malloc(sizeof(char) * (size_s + 1))); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + size_ = size_s; + memcpy(buffer_, str, size_ + 1); + return *this; +} + +char &String::at(size_t pos) { + if (pos >= size_) { + MS_C_EXCEPTION("pos out of range"); + } + return buffer_[pos]; +} +const char &String::at(size_t pos) const { + if (pos >= size_) { + MS_C_EXCEPTION("pos out of range"); + } + return buffer_[pos]; +} +char &String::operator[](size_t pos) { + if (pos >= size_) { + MS_C_EXCEPTION("pos out of range"); + } + return this->at(pos); +} +const char &String::operator[](size_t pos) const { + if (pos >= size_) { + MS_C_EXCEPTION("pos out of range"); + } + return this->at(pos); +} +char *String::data() noexcept { return buffer_; }; +const char *String::data() const noexcept { return buffer_; } +const char *String::c_str() const noexcept { return buffer_; } + +// capacity +bool String::empty() const noexcept { return size_ == 0; } +size_t String::size() const noexcept { return size_; } +size_t String::length() const noexcept { return size_; } + +// operations +void String::clear() noexcept { + free(buffer_); + buffer_ = reinterpret_cast(malloc(sizeof(char) * 1)); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + buffer_[0] = '\0'; + size_ = 0; +} +String &String::operator+=(const String &str) { + size_t new_size = size_ + str.size_; + char *tmp = reinterpret_cast(malloc(sizeof(char) * (new_size + 1))); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + memcpy(tmp, this->buffer_, size_ + 1); + strncat(tmp, str.buffer_, str.size_); + tmp[new_size] = '\0'; + free(buffer_); + buffer_ = tmp; + size_ = new_size; + return *this; +} + +String &String::operator+=(const char *str) { + if (str == nullptr) { + return *this; + } + size_t str_size = strlen(str); + size_t new_size = size_ + str_size; + char *tmp = reinterpret_cast(malloc(sizeof(char) * (new_size + 1))); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + memcpy(tmp, this->buffer_, size_ + 1); + strncat(tmp, str, str_size); + tmp[new_size] = '\0'; + free(buffer_); + buffer_ = tmp; + size_ = new_size; + return *this; +} + +String &String::operator+=(const char ch) { + char *tmp = reinterpret_cast(malloc(sizeof(char) * (size_ + 2))); + if (buffer_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + memcpy(tmp, this->buffer_, size_ + 1); + tmp[size_] = ch; + tmp[size_ + 1] = '\0'; + free(buffer_); + buffer_ = tmp; + size_ += 1; + return *this; +} + +String &String::append(size_t count, const char ch) { + (*this) += ch; + return *this; +} +String &String::append(const String &str) { + (*this) += str; + return *this; +} +String &String::append(const char *str) { + if (str == nullptr) { + return *this; + } + (*this) += str; + return *this; +} + +int String::compare(const String &str) const { return strcmp(buffer_, str.buffer_); } +int String::compare(const char *str) const { return strcmp(buffer_, str); } + +String String::substr(size_t pos, size_t count) const { return String(*this, pos, count); } + +String operator+(const String &lhs, const String &rhs) { + String str(lhs); + str += rhs; + return str; +} +String operator+(const String &lhs, const char *rhs) { + String str(lhs); + str += rhs; + return str; +} +String operator+(const char *lhs, const String &rhs) { + String str(lhs); + return str + rhs; +} +String operator+(const String &lhs, const char rhs) { + String str(lhs); + str += rhs; + return str; +} +String operator+(const char lhs, const String &rhs) { + String str(1, lhs); + str += rhs; + return str; +} + +bool operator==(const String &lhs, const String &rhs) { return lhs.compare(rhs) == 0; } +bool operator==(const String &lhs, const char *rhs) { return lhs.compare(rhs) == 0; } +bool operator==(const char *lhs, const String rhs) { return rhs.compare(lhs) == 0; } + +bool operator!=(const String &lhs, const String &rhs) { return lhs.compare(rhs) != 0; } +bool operator!=(const String &lhs, const char *rhs) { return lhs.compare(rhs) != 0; } +bool operator!=(const char *lhs, const String rhs) { return rhs.compare(lhs) != 0; } + +bool operator<(const String &lhs, const String &rhs) { return lhs.compare(rhs) < 0; } +bool operator<(const String &lhs, const char *rhs) { return lhs.compare(rhs) < 0; } +bool operator<(const char *lhs, const String rhs) { return rhs.compare(lhs) >= 0; } + +bool operator>(const String &lhs, const String &rhs) { return lhs.compare(rhs) > 0; } +bool operator>(const String &lhs, const char *rhs) { return lhs.compare(rhs) > 0; } +bool operator>(const char *lhs, const String rhs) { return rhs.compare(lhs) <= 0; } + +bool operator<=(const String &lhs, const String &rhs) { return lhs.compare(rhs) <= 0; } +bool operator<=(const String &lhs, const char *rhs) { return lhs.compare(rhs) <= 0; } +bool operator<=(const char *lhs, const String rhs) { return rhs.compare(lhs) > 0; } + +bool operator>=(const String &lhs, const String &rhs) { return lhs.compare(rhs) >= 0; } +bool operator>=(const String &lhs, const char *rhs) { return lhs.compare(rhs) >= 0; } +bool operator>=(const char *lhs, const String rhs) { return rhs.compare(lhs) < 0; } + +String to_String(int32_t value) { + char tmp[sizeof(int32_t) * 4]; + snprintf(tmp, sizeof(int32_t) * 4, "%d", value); + return String(tmp, strlen(tmp)); +} + +String to_String(int64_t value) { + char tmp[sizeof(int64_t) * 4]; + snprintf(tmp, sizeof(int64_t) * 4, "%ld", value); + return String(tmp, strlen(tmp)); +} + +String to_String(u_int32_t value) { + char tmp[sizeof(u_int32_t) * 4]; + snprintf(tmp, sizeof(unsigned) * 4, "%u", value); + return String(tmp, strlen(tmp)); +} + +String to_String(u_int64_t value) { + char tmp[sizeof(u_int64_t) * 4]; + snprintf(tmp, sizeof(u_int64_t) * 4, "%lu", value); + return String(tmp, strlen(tmp)); +} + +String to_String(float value) { + char tmp[FLT_MAX_10_EXP + 20]; + snprintf(tmp, FLT_MAX_10_EXP + 20, "%f", value); + return String(tmp, strlen(tmp)); +} + +String to_String(double value) { + char tmp[DBL_MAX_10_EXP + 20]; + snprintf(tmp, DBL_MAX_10_EXP + 20, "%f", value); + return String(tmp, strlen(tmp)); +} + +String to_String(long double value) { + char tmp[LDBL_MAX_10_EXP + 20]; + snprintf(tmp, DBL_MAX_10_EXP + 20, "%Lf", value); + return String(tmp, strlen(tmp)); +} diff --git a/mindspore/lite/internal/src/lite_log.h b/mindspore/lite/internal/src/lite_log.h index bf5c9db0fdd..0bcf9132cad 100644 --- a/mindspore/lite/internal/src/lite_log.h +++ b/mindspore/lite/internal/src/lite_log.h @@ -32,6 +32,9 @@ #define LITE_LOG_ERROR(...) \ printf("[ERROR] [%s %s] [%s] [%d] %s\n", __DATE__, __TIME__, __FILE__, __LINE__, __VA_ARGS__) #define MS_ASSERT(f) assert(f) +#define MS_C_EXCEPTION(...) \ + printf("[EXCEPTION] [%s %s] [%s] [%d] %s\n", __DATE__, __TIME__, __FILE__, __LINE__, __VA_ARGS__); \ + exit(1) #else #define LITE_DEBUG_LOG(...) #define LITE_INFO_LOG(...) @@ -39,6 +42,7 @@ #define LITE_ERROR_LOG(...) #define LITE_LOG_ERROR(...) #define MS_ASSERT(f) ((void)0) +#define MS_C_EXCEPTION(...) exit(1) #endif #endif // MINDSPORE_LITE_INTERNAL_SRC_LITE_LOG_H_ diff --git a/mindspore/lite/tools/common/protobuf_utils.cc b/mindspore/lite/tools/common/protobuf_utils.cc index 4023845d8f5..206b8122417 100644 --- a/mindspore/lite/tools/common/protobuf_utils.cc +++ b/mindspore/lite/tools/common/protobuf_utils.cc @@ -73,13 +73,13 @@ STATUS ReadProtoFromBinaryFile(const char *file, google::protobuf::Message *mess std::string realPath = RealPath(file); if (realPath.empty()) { - MS_LOG(ERROR) << "Weight file path " << file << " is not valid"; + MS_LOG(ERROR) << "Binary proto file path " << file << " is not valid"; return RET_ERROR; } std::ifstream fs(realPath, std::ifstream::in | std::ifstream::binary); if (!fs.is_open()) { - MS_LOG(ERROR) << "Open weight file " << file << " failed."; + MS_LOG(ERROR) << "Open binary proto file " << file << " failed."; return RET_ERROR; }