From 028b97734dff0a48232795f9edb11ccf48a6a41b Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 6 Jul 2020 08:14:37 -0700 Subject: [PATCH] Implement SafeResourceVariableHandle as a wrapper for TFE_ResourceVariable --- .../Variables/SafeResourceVariableHandle.cs | 40 +++++++++++++++++++ .../Variables/c_api.variable.cs | 9 ++--- 2 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/TensorFlowNET.Core/Variables/SafeResourceVariableHandle.cs diff --git a/src/TensorFlowNET.Core/Variables/SafeResourceVariableHandle.cs b/src/TensorFlowNET.Core/Variables/SafeResourceVariableHandle.cs new file mode 100644 index 00000000..dc3f09df --- /dev/null +++ b/src/TensorFlowNET.Core/Variables/SafeResourceVariableHandle.cs @@ -0,0 +1,40 @@ +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; +using Tensorflow.Util; + +namespace Tensorflow.Variables +{ + public sealed class SafeResourceVariableHandle : SafeTensorflowHandle + { + private SafeResourceVariableHandle() + { + } + + public SafeResourceVariableHandle(IntPtr handle) + : base(handle) + { + } + + protected override bool ReleaseHandle() + { + c_api.TFE_DeleteResourceVariable(handle); + SetHandle(IntPtr.Zero); + return true; + } + } +} diff --git a/src/TensorFlowNET.Core/Variables/c_api.variable.cs b/src/TensorFlowNET.Core/Variables/c_api.variable.cs index 7f9fcfb5..78075f61 100644 --- a/src/TensorFlowNET.Core/Variables/c_api.variable.cs +++ b/src/TensorFlowNET.Core/Variables/c_api.variable.cs @@ -1,22 +1,21 @@ using System; -using System.Collections.Generic; using System.Runtime.InteropServices; -using System.Text; +using Tensorflow.Variables; namespace Tensorflow { public partial class c_api { [DllImport(TensorFlowLibName)] - public static extern IntPtr TFE_NewResourceVariable(); + public static extern SafeResourceVariableHandle TFE_NewResourceVariable(); [DllImport(TensorFlowLibName)] public static extern void TFE_DeleteResourceVariable(IntPtr variable); [DllImport(TensorFlowLibName)] - public static extern void TFE_SetResourceVariableHandle(IntPtr variable, IntPtr tensor); + public static extern void TFE_SetResourceVariableHandle(SafeResourceVariableHandle variable, IntPtr tensor); [DllImport(TensorFlowLibName)] - public static extern void TFE_SetResourceVariableName(IntPtr variable, string name); + public static extern void TFE_SetResourceVariableName(SafeResourceVariableHandle variable, string name); } }