1 /* Copyright 2017 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_PYTHON_LIB_CORE_SAFE_PTR_H_ 17 #define TENSORFLOW_PYTHON_LIB_CORE_SAFE_PTR_H_ 18 19 #include <Python.h> 20 21 #include <memory> 22 23 #include "tensorflow/c/c_api.h" 24 #include "tensorflow/c/eager/c_api.h" 25 #include "tensorflow/python/lib/core/safe_pyobject_ptr.h" 26 27 namespace tensorflow { 28 namespace detail { 29 30 struct TFTensorDeleter { operatorTFTensorDeleter31 void operator()(TF_Tensor* p) const { TF_DeleteTensor(p); } 32 }; 33 34 struct TFETensorHandleDeleter { operatorTFETensorHandleDeleter35 void operator()(TFE_TensorHandle* p) const { TFE_DeleteTensorHandle(p); } 36 }; 37 38 struct TFStatusDeleter { operatorTFStatusDeleter39 void operator()(TF_Status* p) const { TF_DeleteStatus(p); } 40 }; 41 42 struct TFBufferDeleter { operatorTFBufferDeleter43 void operator()(TF_Buffer* p) const { TF_DeleteBuffer(p); } 44 }; 45 46 } // namespace detail 47 48 // Safe containers for an owned TF_Tensor. On destruction, the tensor will be 49 // deleted by TF_DeleteTensor. 50 using Safe_TF_TensorPtr = std::unique_ptr<TF_Tensor, detail::TFTensorDeleter>; 51 Safe_TF_TensorPtr make_safe(TF_Tensor* tensor); 52 53 // Safe containers for an owned TFE_TensorHandle. On destruction, the handle 54 // will be deleted by TFE_DeleteTensorHandle. 55 using Safe_TFE_TensorHandlePtr = 56 std::unique_ptr<TFE_TensorHandle, detail::TFETensorHandleDeleter>; 57 Safe_TFE_TensorHandlePtr make_safe(TFE_TensorHandle* handle); 58 59 // Safe containers for an owned TF_Status. On destruction, the handle 60 // will be deleted by TF_DeleteStatus. 61 using Safe_TF_StatusPtr = std::unique_ptr<TF_Status, detail::TFStatusDeleter>; 62 Safe_TF_StatusPtr make_safe(TF_Status* status); 63 64 // Safe containers for an owned TF_Buffer. On destruction, the handle 65 // will be deleted by TF_DeleteBuffer. 66 using Safe_TF_BufferPtr = std::unique_ptr<TF_Buffer, detail::TFBufferDeleter>; 67 Safe_TF_BufferPtr make_safe(TF_Buffer* buffer); 68 69 } // namespace tensorflow 70 71 #endif // TENSORFLOW_PYTHON_LIB_CORE_SAFE_PTR_H_ 72