Skip to content

Commit bd18f5d

Browse files
committed
Removed all use-cases of Marshal.PtrToStructure in favor of unsafe ptrs.
1 parent 4a9a2c6 commit bd18f5d

6 files changed

Lines changed: 28 additions & 24 deletions

File tree

src/TensorFlowNET.Core/DisposableObject.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ private void internal_dispose(bool disposing)
5454
if (_handle != IntPtr.Zero)
5555
{
5656
DisposeUnmanagedResources(_handle);
57-
5857
_handle = IntPtr.Zero;
5958
}
6059
}

src/TensorFlowNET.Core/Graphs/Graph.Import.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ public unsafe TF_Output[] ImportGraphDefWithReturnOutputs(Buffer graph_def, Impo
3030
var return_output_handle = Marshal.AllocHGlobal(size * num_return_outputs);
3131

3232
c_api.TF_GraphImportGraphDefWithReturnOutputs(_handle, graph_def, opts, return_output_handle, num_return_outputs, s);
33-
for (int i = 0; i < num_return_outputs; i++)
34-
{
35-
var handle = return_output_handle + i * size;
36-
return_outputs[i] = Marshal.PtrToStructure<TF_Output>(handle);
37-
}
33+
34+
var tf_output_ptr = (TF_Output*) return_output_handle;
35+
for (int i = 0; i < num_return_outputs; i++)
36+
return_outputs[i] = *(tf_output_ptr + i);
3837

3938
Marshal.FreeHGlobal(return_output_handle);
4039

src/TensorFlowNET.Core/Graphs/Graph.Operation.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,20 @@ public OperationDescription NewOperation(string opType, string opName)
3939
return c_api.TF_NewOperation(_handle, opType, opName);
4040
}
4141

42-
public unsafe Operation[] ReturnOperations(IntPtr results)
42+
public Operation[] ReturnOperations(IntPtr results)
4343
{
4444
TF_Operation return_oper_handle = new TF_Operation();
4545
int num_return_opers = 0;
4646
c_api.TF_ImportGraphDefResultsReturnOperations(results, ref num_return_opers, ref return_oper_handle);
4747
Operation[] return_opers = new Operation[num_return_opers];
48+
var tf_op_size = Marshal.SizeOf<TF_Operation>();
4849
for (int i = 0; i < num_return_opers; i++)
4950
{
50-
var handle = return_oper_handle.node + Marshal.SizeOf<TF_Operation>() * i;
51-
return_opers[i] = new Operation(*(IntPtr*)handle);
51+
unsafe
52+
{
53+
var handle = return_oper_handle.node + tf_op_size * i;
54+
return_opers[i] = new Operation(*(IntPtr*)handle);
55+
}
5256
}
5357

5458
return return_opers;

src/TensorFlowNET.Core/Graphs/Graph.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ public string unique_name(string name, bool mark_as_used = true)
369369
var name_key = name.ToLower();
370370
int i = 0;
371371
if (_names_in_use.ContainsKey(name_key))
372-
i = _names_in_use[name_key];
372+
i = _names_in_use[name_key];
373373
// Increment the number for "name_key".
374374
if (mark_as_used)
375375
_names_in_use[name_key] = i + 1;
@@ -399,13 +399,13 @@ public TF_Output[] ReturnOutputs(IntPtr results)
399399
int num_return_outputs = 0;
400400
c_api.TF_ImportGraphDefResultsReturnOutputs(results, ref num_return_outputs, ref return_output_handle);
401401
TF_Output[] return_outputs = new TF_Output[num_return_outputs];
402-
for (int i = 0; i < num_return_outputs; i++)
402+
unsafe
403403
{
404-
var handle = return_output_handle + (Marshal.SizeOf<TF_Output>() * i);
405-
return_outputs[i] = Marshal.PtrToStructure<TF_Output>(handle);
404+
var tf_output_ptr = (TF_Output*) return_output_handle;
405+
for (int i = 0; i < num_return_outputs; i++)
406+
return_outputs[i] = *(tf_output_ptr + i);
407+
return return_outputs;
406408
}
407-
408-
return return_outputs;
409409
}
410410

411411
public string[] get_all_collection_keys()

src/TensorFlowNET.Core/Operations/Operation.Output.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,12 @@ public int OutputListLength(string name)
5050

5151
public unsafe TF_Input[] OutputConsumers(int index, int max_consumers)
5252
{
53-
int size = Marshal.SizeOf<TF_Input>();
54-
var handle = Marshal.AllocHGlobal(size);
53+
var handle = Marshal.AllocHGlobal(Marshal.SizeOf<TF_Input>());
5554
int num = c_api.TF_OperationOutputConsumers(new TF_Output(_handle, index), handle, max_consumers);
5655
var consumers = new TF_Input[num];
56+
var inputptr = (TF_Input*) handle;
5757
for (int i = 0; i < num; i++)
58-
{
59-
consumers[i] = Marshal.PtrToStructure<TF_Input>(handle + i * size);
60-
}
58+
consumers[i] = *(inputptr + i);
6159

6260
return consumers;
6361
}

src/TensorFlowNET.Core/Sessions/c_api.tf_session_helper.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ public static string[] TF_OperationOutputConsumers_wrapper(TF_Output oper_out)
2727
var handle = Marshal.AllocHGlobal(size * num_consumers);
2828
int num = TF_OperationOutputConsumers(oper_out, handle, num_consumers);
2929
var consumers = new string[num_consumers];
30-
for (int i = 0; i < num; i++)
30+
unsafe
3131
{
32-
TF_Input input = Marshal.PtrToStructure<TF_Input>(handle + i * size);
33-
consumers[i] = Marshal.PtrToStringAnsi(TF_OperationName(input.oper));
32+
var inputptr = (TF_Input*) handle;
33+
for (int i = 0; i < num; i++)
34+
{
35+
var oper = (inputptr + i)->oper;
36+
consumers[i] = Marshal.PtrToStringAnsi(TF_OperationName(oper));
37+
}
3438
}
3539

3640
return consumers;
3741
}
3842
}
39-
}
43+
}

0 commit comments

Comments
 (0)