• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MINDDATA_DATASET_UTIL_STATUS_H_
17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_STATUS_H_
18 
19 #if defined(__GNUC__) || defined(__clang__)
20 #define DEPRECATED __attribute__((deprecated))
21 #elif defined(_MSC_VER)
22 #define DEPRECATED __declspec(deprecated)
23 #else
24 #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
25 #define DEPRECATED
26 #endif
27 
28 #include <iostream>
29 #include <string>
30 #include <utility>
31 
32 #include "include/api/status.h"
33 #include "minddata/dataset/util/log_adapter.h"
34 
35 namespace mindspore {
36 namespace dataset {
37 #define RETURN_IF_NOT_OK(_s)              \
38   do {                                    \
39     const mindspore::Status &__rc = (_s); \
40     if (__rc.IsError()) {                 \
41       return __rc;                        \
42     }                                     \
43   } while (false)
44 
45 #define STATUS_ERROR(_error_code, _e) mindspore::Status(_error_code, __LINE__, DATASET_SRC_FILE_NAME, _e)
46 
47 #define RETURN_STATUS_ERROR(_error_code, _e) \
48   do {                                       \
49     return STATUS_ERROR(_error_code, _e);    \
50   } while (false)
51 
52 #define RETURN_STATUS_UNEXPECTED(_e)                                    \
53   do {                                                                  \
54     RETURN_STATUS_ERROR(mindspore::StatusCode::kMDUnexpectedError, _e); \
55   } while (false)
56 
57 #define CHECK_FAIL_RETURN_UNEXPECTED(_condition, _e) \
58   do {                                               \
59     if (!(_condition)) {                             \
60       RETURN_STATUS_UNEXPECTED(_e);                  \
61     }                                                \
62   } while (false)
63 
64 #define RETURN_SYNTAX_ERROR(_e)                                     \
65   do {                                                              \
66     RETURN_STATUS_ERROR(mindspore::StatusCode::kMDSyntaxError, _e); \
67   } while (false)
68 
69 #define CHECK_FAIL_RETURN_SYNTAX_ERROR(_condition, _e) \
70   do {                                                 \
71     if (!(_condition)) {                               \
72       RETURN_SYNTAX_ERROR(_e);                         \
73     }                                                  \
74   } while (false)
75 
76 #define LOG_AND_RETURN_STATUS_SYNTAX_ERROR(_e) \
77   do {                                         \
78     MS_LOG(ERROR) << _e;                       \
79     RETURN_SYNTAX_ERROR(_e);                   \
80   } while (false)
81 
82 #define RETURN_UNEXPECTED_IF_NULL(_ptr)                                         \
83   do {                                                                          \
84     if ((_ptr) == nullptr) {                                                    \
85       std::string err_msg = "The pointer[" + std::string(#_ptr) + "] is null."; \
86       RETURN_STATUS_UNEXPECTED(err_msg);                                        \
87     }                                                                           \
88   } while (false)
89 
90 #define RETURN_OK_IF_TRUE(_condition) \
91   do {                                \
92     if (_condition) {                 \
93       return mindspore::Status::OK(); \
94     }                                 \
95   } while (false)
96 
97 #define RETURN_SECOND_IF_ERROR(_s, _r)    \
98   do {                                    \
99     const mindspore::Status &__rc = (_s); \
100     if (__rc.IsError()) {                 \
101       MS_LOG(ERROR) << __rc;              \
102       return _r;                          \
103     }                                     \
104   } while (false)
105 
106 #define RETURN_STATUS_OOM(_e)                                       \
107   do {                                                              \
108     RETURN_STATUS_ERROR(mindspore::StatusCode::kMDOutOfMemory, _e); \
109   } while (false)
110 
111 #if !defined(_WIN32) && !defined(_WIN64) && !defined(__APPLE__)
112 const float MAX_MEMORY_USAGE_THRESHOLD = 0.8;
113 float GetMemoryUsage();
114 #endif
115 }  // namespace dataset
116 }  // namespace mindspore
117 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_STATUS_H_
118