1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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
27namespace tensorflow {
28namespace detail {
29
30struct TFTensorDeleter {
31 void operator()(TF_Tensor* p) const { TF_DeleteTensor(p); }
32};
33
34struct TFETensorHandleDeleter {
35 void operator()(TFE_TensorHandle* p) const { TFE_DeleteTensorHandle(p); }
36};
37
38struct TFStatusDeleter {
39 void operator()(TF_Status* p) const { TF_DeleteStatus(p); }
40};
41
42struct TFBufferDeleter {
43 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.
50using Safe_TF_TensorPtr = std::unique_ptr<TF_Tensor, detail::TFTensorDeleter>;
51Safe_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.
55using Safe_TFE_TensorHandlePtr =
56 std::unique_ptr<TFE_TensorHandle, detail::TFETensorHandleDeleter>;
57Safe_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.
61using Safe_TF_StatusPtr = std::unique_ptr<TF_Status, detail::TFStatusDeleter>;
62Safe_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.
66using Safe_TF_BufferPtr = std::unique_ptr<TF_Buffer, detail::TFBufferDeleter>;
67Safe_TF_BufferPtr make_safe(TF_Buffer* buffer);
68
69} // namespace tensorflow
70
71#endif // TENSORFLOW_PYTHON_LIB_CORE_SAFE_PTR_H_
72