• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
20 
21 #include <Python.h>
22 
23 #include "tensorflow/c/c_api.h"
24 #include "tensorflow/c/eager/c_api.h"
25 
26 namespace tensorflow {
27 namespace detail {
28 
29 struct PyDecrefDeleter {
operatorPyDecrefDeleter30   void operator()(PyObject* p) const { Py_DECREF(p); }
31 };
32 
33 struct TFTensorDeleter {
operatorTFTensorDeleter34   void operator()(TF_Tensor* p) const { TF_DeleteTensor(p); }
35 };
36 
37 struct TFETensorHandleDeleter {
operatorTFETensorHandleDeleter38   void operator()(TFE_TensorHandle* p) const { TFE_DeleteTensorHandle(p); }
39 };
40 
41 struct TFStatusDeleter {
operatorTFStatusDeleter42   void operator()(TF_Status* p) const { TF_DeleteStatus(p); }
43 };
44 
45 }  // namespace detail
46 
47 // Safe container for an owned PyObject. On destruction, the reference count of
48 // the contained object will be decremented.
49 using Safe_PyObjectPtr = std::unique_ptr<PyObject, detail::PyDecrefDeleter>;
50 Safe_PyObjectPtr make_safe(PyObject* o);
51 
52 // Safe containers for an owned TF_Tensor. On destruction, the tensor will be
53 // deleted by TF_DeleteTensor.
54 using Safe_TF_TensorPtr = std::unique_ptr<TF_Tensor, detail::TFTensorDeleter>;
55 Safe_TF_TensorPtr make_safe(TF_Tensor* tensor);
56 
57 // Safe containers for an owned TFE_TensorHandle. On destruction, the handle
58 // will be deleted by TFE_DeleteTensorHandle.
59 using Safe_TFE_TensorHandlePtr =
60     std::unique_ptr<TFE_TensorHandle, detail::TFETensorHandleDeleter>;
61 Safe_TFE_TensorHandlePtr make_safe(TFE_TensorHandle* handle);
62 
63 // Safe containers for an owned TF_Status. On destruction, the handle
64 // will be deleted by TF_DeleteStatus.
65 using Safe_TF_StatusPtr = std::unique_ptr<TF_Status, detail::TFStatusDeleter>;
66 Safe_TF_StatusPtr make_safe(TF_Status* status);
67 
68 }  // namespace tensorflow
69 
70 #endif  // TENSORFLOW_PYTHON_LIB_CORE_SAFE_PTR_H_
71