1 /* Copyright 2022 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_OP_REQUIRES_H_ 17 #define TENSORFLOW_C_C_OP_REQUIRES_H_ 18 19 #include "tensorflow/core/platform/macros.h" 20 21 namespace tensorflow { 22 23 // Convenience macros for asserting and handling exceptional conditions, for 24 // C structs, including `TF_OpKernelContext`, `TF_Status`, etc. This is analogus 25 // to the macros in tensorflow/core/framework/op_requires.h. This is provided 26 // for plugin OpKernel developer's convenience. 27 28 #define C_OPKERNELCONTEXT_REQUIRES_OK(CTX, C_STATUS, __VA_ARGS__) \ 29 do { \ 30 ::tensorflow::Status _s(__VA_ARGS__); \ 31 if (!TF_PREDICT_TRUE(_s.ok())) { \ 32 ::tensorflow::Set_TF_Status_from_Status(C_STATUS, _s); \ 33 TF_OpKernelContext_Failure(CTX, C_STATUS); \ 34 TF_DeleteStatus(C_STATUS); \ 35 return; \ 36 } \ 37 } while (0) 38 39 #define TF_CLEANUP_AND_RETURN_IF_ERROR(C_STATUS, BUFFER, __VA_ARGS__) \ 40 do { \ 41 ::tensorflow::Status _s(__VA_ARGS__); \ 42 if (TF_PREDICT_TRUE(!_s.ok())) { \ 43 TF_DeleteStatus(C_STATUS); \ 44 TF_DeleteBuffer(BUFFER); \ 45 return _s; \ 46 } \ 47 } while (0) 48 49 } // namespace tensorflow 50 51 #endif // TENSORFLOW_C_C_OP_REQUIRES_H_ 52