You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Buffer.cs 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using NumSharp.Backends.Unmanaged;
  14. using System;
  15. using System.Runtime.CompilerServices;
  16. using Tensorflow.Util;
  17. using static Tensorflow.c_api;
  18. namespace Tensorflow
  19. {
  20. /// <summary>
  21. /// Represents a TF_Buffer that can be passed to Tensorflow.
  22. /// </summary>
  23. public sealed class Buffer : IDisposable
  24. {
  25. public SafeBufferHandle Handle { get; }
  26. /// <remarks>
  27. /// <inheritdoc cref="SafeHandleLease" path="/devdoc/usage"/>
  28. /// </remarks>
  29. private unsafe ref readonly TF_Buffer DangerousBuffer
  30. => ref Unsafe.AsRef<TF_Buffer>(Handle.DangerousGetHandle().ToPointer());
  31. /// <summary>
  32. /// The memory block representing this buffer.
  33. /// </summary>
  34. /// <remarks>
  35. /// <para>The deallocator is set to null.</para>
  36. ///
  37. /// <inheritdoc cref="SafeHandleLease" path="/devdoc/usage"/>
  38. /// </remarks>
  39. public unsafe UnmanagedMemoryBlock<byte> DangerousMemoryBlock
  40. {
  41. get
  42. {
  43. ref readonly TF_Buffer buffer = ref DangerousBuffer;
  44. return new UnmanagedMemoryBlock<byte>((byte*)buffer.data.ToPointer(), (long)buffer.length);
  45. }
  46. }
  47. /// <summary>
  48. /// The bytes length of this buffer.
  49. /// </summary>
  50. public ulong Length
  51. {
  52. get
  53. {
  54. using (Handle.Lease())
  55. {
  56. return DangerousBuffer.length;
  57. }
  58. }
  59. }
  60. public Buffer()
  61. => Handle = TF_NewBuffer();
  62. public Buffer(SafeBufferHandle handle)
  63. => Handle = handle;
  64. public Buffer(byte[] data)
  65. => Handle = _toBuffer(data);
  66. private static SafeBufferHandle _toBuffer(byte[] data)
  67. {
  68. if (data == null)
  69. throw new ArgumentNullException(nameof(data));
  70. unsafe
  71. {
  72. fixed (byte* src = data)
  73. return TF_NewBufferFromString(new IntPtr(src), (ulong)data.LongLength);
  74. }
  75. }
  76. /// <summary>
  77. /// Copies this buffer's contents onto a <see cref="byte"/> array.
  78. /// </summary>
  79. public byte[] ToArray()
  80. {
  81. using (Handle.Lease())
  82. {
  83. var block = DangerousMemoryBlock;
  84. var len = block.Count;
  85. if (len == 0)
  86. return Array.Empty<byte>();
  87. var data = new byte[len];
  88. block.CopyTo(data, 0);
  89. return data;
  90. }
  91. }
  92. public void Dispose()
  93. => Handle.Dispose();
  94. }
  95. }