Browse Source

Implement SafeTensorflowHandle with extensions

tags/v0.20
Sam Harwell 5 years ago
parent
commit
79f3d7617f
3 changed files with 133 additions and 0 deletions
  1. +54
    -0
      src/TensorFlowNET.Core/Util/SafeHandleExtensions.cs
  2. +36
    -0
      src/TensorFlowNET.Core/Util/SafeHandleLease.cs
  3. +43
    -0
      src/TensorFlowNET.Core/Util/SafeTensorflowHandle.cs

+ 54
- 0
src/TensorFlowNET.Core/Util/SafeHandleExtensions.cs View File

@@ -0,0 +1,54 @@
/*****************************************************************************
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 System.Runtime.InteropServices;

namespace Tensorflow.Util
{
internal static class SafeHandleExtensions
{
/// <summary>
/// Acquires a lease on a safe handle. This method is intended to be used in the initializer of a <c>using</c>
/// statement.
/// </summary>
/// <param name="handle">The <see cref="SafeHandle"/> to lease.</param>
/// <returns>A <see cref="SafeHandleLease"/>, which must be disposed to release the resource.</returns>
/// <exception cref="ObjectDisposedException">If the lease could not be acquired.</exception>
public static SafeHandleLease Lease(this SafeHandle handle)
{
if (handle is null)
throw new ArgumentNullException(nameof(handle));

var success = false;
try
{
handle.DangerousAddRef(ref success);
if (!success)
throw new ObjectDisposedException(handle.GetType().FullName);

return new SafeHandleLease(handle);
}
catch
{
if (success)
handle.DangerousRelease();

throw;
}
}
}
}

+ 36
- 0
src/TensorFlowNET.Core/Util/SafeHandleLease.cs View File

@@ -0,0 +1,36 @@
/*****************************************************************************
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 System.Runtime.InteropServices;

namespace Tensorflow.Util
{
/// <summary>
/// Represents a lease of a <see cref="SafeHandle"/>.
/// </summary>
/// <seealso cref="SafeHandleExtensions.Lease"/>
public readonly struct SafeHandleLease : IDisposable
{
private readonly SafeHandle _handle;

internal SafeHandleLease(SafeHandle handle)
=> _handle = handle;

public void Dispose()
=> _handle?.DangerousRelease();
}
}

+ 43
- 0
src/TensorFlowNET.Core/Util/SafeTensorflowHandle.cs View File

@@ -0,0 +1,43 @@
/*****************************************************************************
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 System.Runtime.InteropServices;

namespace Tensorflow.Util
{
public abstract class SafeTensorflowHandle : SafeHandle
{
private protected SafeTensorflowHandle()
: base(IntPtr.Zero, ownsHandle: true)
{
}

private protected SafeTensorflowHandle(IntPtr handle)
: base(IntPtr.Zero, ownsHandle: true)
{
SetHandle(handle);
}

private protected SafeTensorflowHandle(IntPtr handle, bool ownsHandle)
: base(IntPtr.Zero, ownsHandle)
{
SetHandle(handle);
}

public override bool IsInvalid => handle == IntPtr.Zero;
}
}

Loading…
Cancel
Save