forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuffer.cs
More file actions
60 lines (49 loc) · 1.35 KB
/
Buffer.cs
File metadata and controls
60 lines (49 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace Tensorflow
{
public class Buffer : IDisposable
{
private IntPtr _handle;
private TF_Buffer buffer => Marshal.PtrToStructure<TF_Buffer>(_handle);
public byte[] Data
{
get
{
var data = new byte[buffer.length];
if (buffer.length > 0)
Marshal.Copy(buffer.data, data, 0, (int)buffer.length);
return data;
}
}
public int Length => (int)buffer.length;
public Buffer()
{
_handle = c_api.TF_NewBuffer();
}
public Buffer(IntPtr handle)
{
_handle = handle;
}
public Buffer(byte[] data)
{
var dst = Marshal.AllocHGlobal(data.Length);
Marshal.Copy(data, 0, dst, data.Length);
_handle = c_api.TF_NewBufferFromString(dst, (ulong)data.Length);
}
public static implicit operator IntPtr(Buffer buffer)
{
return buffer._handle;
}
public static implicit operator byte[](Buffer buffer)
{
return buffer.Data;
}
public void Dispose()
{
c_api.TF_DeleteBuffer(_handle);
}
}
}