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