You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.h 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * Copyright 2019 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. #ifndef MINDSPORE_CCSRC_UTILS_SYSTEM_BASE_H_
  17. #define MINDSPORE_CCSRC_UTILS_SYSTEM_BASE_H_
  18. #include <string>
  19. #include <memory>
  20. #include "securec/include/securec.h"
  21. #include "utils/log_adapter.h"
  22. namespace mindspore {
  23. namespace system {
  24. using string = std::string;
  25. using int8 = int8_t;
  26. using int16 = int16_t;
  27. using int32 = int32_t;
  28. using int64 = int64_t;
  29. using uint8 = uint8_t;
  30. using uint16 = uint16_t;
  31. using uint32 = uint32_t;
  32. using uint64 = uint64_t;
  33. #define MS_MALLOC(size) malloc(size)
  34. #define MS_FREE(ptr) free(ptr)
  35. // Use the macro to confirm the system env
  36. #if defined(ANDROID) || defined(__ANDROID__)
  37. #define SYSTEM_ENV_POSIX_ANDROID
  38. #elif defined(__APPLE__)
  39. #define SYSTEM_ENV_POSIX
  40. #elif defined(_WIN32)
  41. #define SYSTEM_ENV_WINDOWS
  42. #elif defined(__arm__)
  43. #define SYSTEM_ENV_POSIX
  44. #else // default set the POSIX
  45. #define SYSTEM_ENV_POSIX
  46. #endif
  47. // define the platform
  48. enum PlatformDefine {
  49. kPlatformPosix = 0, // Posix platform
  50. kPlatformPosixAndroid, // Android of posix
  51. kPlatformWindows, // Windows system
  52. kPlatformUnknow = 0xFF // Error
  53. };
  54. class Platform {
  55. public:
  56. Platform() {
  57. platform_ = kPlatformUnknow;
  58. #if defined(SYSTEM_ENV_POSIX)
  59. platform_ = kPlatformPosix;
  60. #elif defined(SYSTEM_ENV_POSIX_ANDROID)
  61. platform_ = kPlatformPosixAndroid
  62. #elif defined(SYSTEM_ENV_WINDOWS)
  63. platform_ = kPlatformWindows;
  64. #endif
  65. }
  66. ~Platform() = default;
  67. static PlatformDefine get_platform() {
  68. static const auto sys_env = std::make_shared<Platform>();
  69. return sys_env->platform_;
  70. }
  71. private:
  72. PlatformDefine platform_;
  73. };
  74. // check the null point
  75. #define MS_EXCEPT_CHECK_NULL(value) \
  76. do { \
  77. if (value == nullptr) { \
  78. MS_LOG(EXCEPTION) << "The value is null."; \
  79. } \
  80. } while (0)
  81. // define the big or little endian type
  82. constexpr bool kLittleEndian = true;
  83. // implement common define function
  84. // Get the 32 bits align value
  85. inline uint32 DecodeFixed32(const char *ptr) {
  86. uint32 result;
  87. if (EOK != memcpy_s(&result, sizeof(result), ptr, sizeof(result))) {
  88. MS_LOG(EXCEPTION) << "Call DecodeFixed32 memcpy value failure.";
  89. }
  90. return result;
  91. }
  92. // Used to fetch a naturally-aligned 32-bit word in little endian byte-order
  93. inline uint32 LE_LOAD32(const uint8_t *p) { return DecodeFixed32(reinterpret_cast<const char *>(p)); }
  94. // Encode the data to buffer
  95. inline void EncodeFixed32(char *buf, uint32 value) {
  96. if (EOK != memcpy_s(buf, sizeof(value), &value, sizeof(value))) {
  97. MS_LOG(EXCEPTION) << "Call EncodeFixed32 memcpy value failure.";
  98. }
  99. }
  100. inline void EncodeFixed64(char *buf, const unsigned int array_len, int64 value) {
  101. if (sizeof(value) > array_len) {
  102. MS_LOG(EXCEPTION) << "Buffer overflow, real size is " << array_len << ", but required " << sizeof(value) << ".";
  103. }
  104. if (EOK != memcpy_s(buf, sizeof(value), &value, sizeof(value))) {
  105. MS_LOG(EXCEPTION) << "Call EncodeFixed64 memcpy value failure.";
  106. }
  107. }
  108. } // namespace system
  109. } // namespace mindspore
  110. #endif // MINDSPORE_CCSRC_UTILS_SYSTEM_BASE_H_