Skip to content

Commit 6256ed2

Browse files
Clean-up and take advantage of newer .NET APIs. (#556)
* Remove unused class. * Use non-boxing generic `Enum` APIs. * Use `NativeMemory.Alloc` to allocate native memory.
1 parent 56f1c60 commit 6256ed2

File tree

6 files changed

+22
-77
lines changed

6 files changed

+22
-77
lines changed

sources/TileDB.CSharp/Context.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ public string LastError()
146146
}
147147
else
148148
{
149-
var message = Enum.IsDefined(typeof(Status), status) ? ((Status)status).ToString() : ("Unknown error with code:" + status);
149+
var message = Enum.IsDefined((Status)status) ? ((Status)status).ToString() : ("Unknown error with code:" + status);
150150
sb_result.Append(" Context.last_error,caught exception:" + message);
151151
}
152152
}
153153
else
154154
{
155-
var message = Enum.IsDefined(typeof(Status), status) ? ((Status)status).ToString() : ("Unknown error with code:" + status);
155+
var message = Enum.IsDefined((Status)status) ? ((Status)status).ToString() : ("Unknown error with code:" + status);
156156
sb_result.Append(" Context.last_error,caught exception:" + message);
157157
}
158158
Methods.tiledb_error_free(&p_tiledb_error);

sources/TileDB.CSharp/Marshalling/MarshaledContiguousStringCollection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public MarshaledContiguousStringCollection(IReadOnlyCollection<string> strings,
2121
Encoding encoding = MarshaledString.GetEncoding(dataType);
2222
try
2323
{
24-
Offsets = (ulong*)Marshal.AllocHGlobal(strings.Count * sizeof(ulong));
24+
Offsets = (ulong*)NativeMemory.Alloc((nuint)strings.Count, sizeof(ulong));
2525
OffsetsCount = (ulong)strings.Count * sizeof(ulong);
2626
ulong currentOffset = 0;
2727
DataCount = 0;
@@ -31,7 +31,7 @@ public MarshaledContiguousStringCollection(IReadOnlyCollection<string> strings,
3131
DataCount += (ulong)encoding.GetByteCount(str);
3232
}
3333

34-
Data = (byte*)Marshal.AllocHGlobal((IntPtr)DataCount);
34+
Data = (byte*)NativeMemory.Alloc((nuint)DataCount);
3535
int i = 0;
3636
foreach (var str in strings)
3737
{
@@ -51,13 +51,13 @@ public void Dispose()
5151
{
5252
if (Data is not null)
5353
{
54-
Marshal.FreeHGlobal((IntPtr)Data);
54+
NativeMemory.Free(Data);
5555
Data = null;
5656
DataCount = 0;
5757
}
5858
if (Offsets is not null)
5959
{
60-
Marshal.FreeHGlobal((IntPtr)Offsets);
60+
NativeMemory.Free(Offsets);
6161
Offsets = null;
6262
OffsetsCount = 0;
6363
}

sources/TileDB.CSharp/Marshalling/MarshaledString.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public unsafe struct MarshaledString : IDisposable
1515
{
1616
public MarshaledString(string input)
1717
{
18-
(IntPtr ptr, Length) = AllocNullTerminated(input);
19-
Value = (sbyte*)ptr;
18+
Value = AllocNullTerminated(input, out int length);
19+
Length = length;
2020
}
2121

2222
internal static Encoding GetEncoding(DataType dataType)
@@ -36,22 +36,23 @@ internal static Encoding GetEncoding(DataType dataType)
3636
return null;
3737
}
3838

39-
internal static (IntPtr Pointer, int Length) AllocNullTerminated(string str)
39+
internal static sbyte* AllocNullTerminated(string str, out int length)
4040
{
4141
if (str is null)
4242
{
43-
return ((IntPtr)0, 0);
43+
length = 0;
44+
return null;
4445
}
4546

46-
int length = Encoding.ASCII.GetByteCount(str);
47-
var ptr = (sbyte*)Marshal.AllocHGlobal(length + 1);
47+
length = Encoding.ASCII.GetByteCount(str);
48+
var ptr = (sbyte*)NativeMemory.Alloc((nuint)length + 1);
4849
int bytesWritten = Encoding.ASCII.GetBytes(str, new Span<byte>(ptr, length));
4950
Debug.Assert(bytesWritten == length);
5051
ptr[length] = 0;
51-
return ((IntPtr)ptr, length);
52+
return ptr;
5253
}
5354

54-
internal static void FreeNullTerminated(IntPtr ptr) => Marshal.FreeHGlobal(ptr);
55+
internal static void FreeNullTerminated(void* ptr) => NativeMemory.Free(ptr);
5556

5657
public ReadOnlySpan<byte> AsSpan() => new ReadOnlySpan<byte>(Value, Length);
5758

@@ -63,7 +64,7 @@ public void Dispose()
6364
{
6465
if (Value != null)
6566
{
66-
Marshal.FreeHGlobal((IntPtr)Value);
67+
NativeMemory.Free(Value);
6768
Value = null;
6869
Length = 0;
6970
}

sources/TileDB.CSharp/Marshalling/MarshaledStringCollection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ namespace TileDB.CSharp.Marshalling;
77

88
internal unsafe ref struct MarshaledStringCollection
99
{
10-
public IntPtr* Strings { get; private set; }
10+
public sbyte** Strings { get; private set; }
1111

1212
public int Count { get; }
1313

1414
public MarshaledStringCollection(IReadOnlyList<string> strings)
1515
{
1616
Count = strings.Count;
17-
Strings = (IntPtr*)Marshal.AllocHGlobal(Count * sizeof(IntPtr));
17+
Strings = (sbyte**)NativeMemory.Alloc((nuint)Count, (nuint)sizeof(sbyte*));
1818

1919
try
2020
{
2121
for (int i = 0; i < Count; i++)
2222
{
23-
Strings[i] = MarshaledString.AllocNullTerminated(strings[i]).Pointer;
23+
Strings[i] = MarshaledString.AllocNullTerminated(strings[i], out _);
2424
}
2525
}
2626
catch
@@ -38,7 +38,7 @@ public void Dispose()
3838
{
3939
MarshaledString.FreeNullTerminated(Strings[i]);
4040
}
41-
Marshal.FreeHGlobal((IntPtr)Strings);
41+
NativeMemory.Free(Strings);
4242
Strings = null;
4343
}
4444
}

sources/TileDB.CSharp/Marshalling/SafeHandles/QueryHandle.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public BufferHandle(ref MemoryHandle handle, ulong size, int elementSize)
282282
bool successful = false;
283283
try
284284
{
285-
SizePointer = (ulong*)Marshal.AllocHGlobal(sizeof(ulong));
285+
SizePointer = (ulong*)NativeMemory.Alloc(1, sizeof(ulong));
286286
successful = true;
287287
}
288288
finally
@@ -300,7 +300,7 @@ public void Dispose()
300300
DataHandle.Dispose();
301301
if (SizePointer != null)
302302
{
303-
Marshal.FreeHGlobal((IntPtr)SizePointer);
303+
NativeMemory.Free(SizePointer);
304304
}
305305
SizePointer = null;
306306
}

sources/TileDB.CSharp/Query.cs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -965,60 +965,4 @@ private void CheckDataType<T>(string name)
965965
/// <exception cref="KeyNotFoundException">No validity buffer has
966966
/// been registered with <paramref name="name"/>.</exception>
967967
public ulong GetResultValidities(string name) => _handle.GetResultValidities(name);
968-
969-
private sealed class BufferHandle : IDisposable
970-
{
971-
private readonly int _elementSize;
972-
973-
private MemoryHandle DataHandle;
974-
975-
public ulong* SizePointer { get; private set; }
976-
977-
public void* DataPointer => DataHandle.Pointer;
978-
979-
public ulong SizeInBytes => *SizePointer;
980-
981-
public ulong SizeInElements
982-
{
983-
get
984-
{
985-
if (_elementSize == 0)
986-
{
987-
ThrowHelpers.ThrowBufferUnsafelySet();
988-
}
989-
return *SizePointer / (ulong)_elementSize;
990-
}
991-
}
992-
993-
public BufferHandle(ref MemoryHandle handle, ulong size, int elementSize)
994-
{
995-
DataHandle = handle;
996-
handle = default;
997-
_elementSize = elementSize;
998-
bool successful = false;
999-
try
1000-
{
1001-
SizePointer = (ulong*)Marshal.AllocHGlobal(sizeof(ulong));
1002-
successful = true;
1003-
}
1004-
finally
1005-
{
1006-
if (!successful)
1007-
{
1008-
DataHandle.Dispose();
1009-
}
1010-
}
1011-
*SizePointer = size;
1012-
}
1013-
1014-
public void Dispose()
1015-
{
1016-
DataHandle.Dispose();
1017-
if (SizePointer != null)
1018-
{
1019-
Marshal.FreeHGlobal((IntPtr)SizePointer);
1020-
}
1021-
SizePointer = null;
1022-
}
1023-
}
1024968
}

0 commit comments

Comments
 (0)