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