@@ -16,44 +16,36 @@ limitations under the License.
1616
1717using Google . Protobuf ;
1818using System ;
19- using System . Runtime . InteropServices ;
2019
2120namespace Tensorflow
2221{
23- internal class SessionOptions : DisposableObject
22+ internal sealed class SessionOptions : IDisposable
2423 {
24+ public SafeSessionOptionsHandle Handle { get ; }
25+
2526 public SessionOptions ( string target = "" , ConfigProto config = null )
2627 {
27- _handle = c_api . TF_NewSessionOptions ( ) ;
28- c_api . TF_SetTarget ( _handle , target ) ;
28+ Handle = c_api . TF_NewSessionOptions ( ) ;
29+ c_api . TF_SetTarget ( Handle , target ) ;
2930 if ( config != null )
3031 SetConfig ( config ) ;
3132 }
3233
33- public SessionOptions ( IntPtr handle )
34- {
35- _handle = handle ;
36- }
37-
38- protected override void DisposeUnmanagedResources ( IntPtr handle )
39- => c_api . TF_DeleteSessionOptions ( handle ) ;
34+ public void Dispose ( )
35+ => Handle . Dispose ( ) ;
4036
41- private void SetConfig ( ConfigProto config )
37+ private unsafe void SetConfig ( ConfigProto config )
4238 {
4339 var bytes = config . ToByteArray ( ) ;
44- var proto = Marshal . AllocHGlobal ( bytes . Length ) ;
45- Marshal . Copy ( bytes , 0 , proto , bytes . Length ) ;
4640
47- using ( var status = new Status ( ) )
41+ fixed ( byte * proto2 = bytes )
4842 {
49- c_api . TF_SetConfig ( _handle , proto , ( ulong ) bytes . Length , status . Handle ) ;
50- status . Check ( false ) ;
43+ using ( var status = new Status ( ) )
44+ {
45+ c_api . TF_SetConfig ( Handle , ( IntPtr ) proto2 , ( ulong ) bytes . Length , status . Handle ) ;
46+ status . Check ( false ) ;
47+ }
5148 }
52-
53- Marshal . FreeHGlobal ( proto ) ;
5449 }
55-
56- public static implicit operator IntPtr ( SessionOptions opts ) => opts . _handle ;
57- public static implicit operator SessionOptions ( IntPtr handle ) => new SessionOptions ( handle ) ;
5850 }
5951}
0 commit comments