xref: /aosp_15_r20/external/tensorflow/tensorflow/c/c_api_macros_internal.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_C_C_API_MACROS_INTERNAL_H_
17 #define TENSORFLOW_C_C_API_MACROS_INTERNAL_H_
18 
19 #ifdef __cplusplus
20 #include "tensorflow/core/platform/status.h"
21 
22 // Macro to verify that the field `struct_size` of STRUCT_OBJ is initialized.
23 // `struct_size` is used for struct member compatibility check between core TF
24 // and plug-ins with the same C API minor version. More info here:
25 // https://github.com/tensorflow/community/blob/master/rfcs/20200612-stream-executor-c-api/C_API_versioning_strategy.md
26 #define TF_VALIDATE_STRUCT_SIZE(STRUCT_NAME, STRUCT_OBJ, SIZE_VALUE_NAME) \
27   do {                                                                    \
28     if (STRUCT_OBJ.struct_size == 0) {                                    \
29       return tensorflow::Status(tensorflow::error::FAILED_PRECONDITION,   \
30                                 "Expected initialized `" #STRUCT_NAME     \
31                                 "` structure with `struct_size` field "   \
32                                 "set to " #SIZE_VALUE_NAME                \
33                                 ". Found `struct_size` = 0.");            \
34     }                                                                     \
35   } while (0)
36 
37 // Macro to verify that the field NAME of STRUCT_OBJ is not null.
38 #define TF_VALIDATE_NOT_NULL(STRUCT_NAME, STRUCT_OBJ, NAME)             \
39   do {                                                                  \
40     if (STRUCT_OBJ.NAME == 0) {                                         \
41       return tensorflow::Status(tensorflow::error::FAILED_PRECONDITION, \
42                                 "'" #NAME "' field in " #STRUCT_NAME    \
43                                 " must be set.");                       \
44     }                                                                   \
45   } while (0)
46 
47 #endif  // __cplusplus
48 #endif  // TENSORFLOW_C_C_API_MACROS_INTERNAL_H_
49