Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use NativeMemory in System.Data.Odbc #85966

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,14 @@ internal abstract class DbBuffer : SafeHandle
{
private readonly int _bufferLength;

protected DbBuffer(int initialSize) : base(IntPtr.Zero, true)
protected unsafe DbBuffer(int initialSize) : base(IntPtr.Zero, true)
{
Debug.Assert(initialSize % IntPtr.Size == 0, $"Expected aligned {nameof(initialSize)}.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What guarantees this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DbBuffer is is only instantiated from the derived class CNativeBuffer.

The buffer size is either 4096 bytes, or a larger value calculated in CalcParameterBufferSize.

internal int CalcParameterBufferSize(OdbcCommand command)
{
// Calculate the size of the buffer we need
int parameterBufferSize = 0;
for (int i = 0; i < Count; ++i)
{
if (_rebindCollection)
{
this[i].HasChanged = true;
}
this[i].PrepareForBind(command, (short)(i + 1), ref parameterBufferSize);
parameterBufferSize = (parameterBufferSize + (IntPtr.Size - 1)) & ~(IntPtr.Size - 1); // align buffer;
}
return parameterBufferSize;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it makes sense to calculate aligned buffer size in the ctor instead.


if (0 < initialSize)
{
_bufferLength = initialSize;

try { }
finally
{
base.handle = SafeNativeMethods.LocalAlloc((IntPtr)initialSize);
}
if (IntPtr.Zero == base.handle)
{
throw new OutOfMemoryException();
}
base.handle = (IntPtr)NativeMemory.AllocZeroed((uint)initialSize);
}
}

Expand Down Expand Up @@ -368,15 +361,12 @@ internal unsafe float ReadSingle(int offset)
return BitConverter.Int32BitsToSingle(value);
}

protected override bool ReleaseHandle()
protected override unsafe bool ReleaseHandle()
{
// NOTE: The SafeHandle class guarantees this will be called exactly once.
IntPtr ptr = base.handle;
base.handle = IntPtr.Zero;
if (IntPtr.Zero != ptr)
{
SafeNativeMethods.LocalFree(ptr);
}
NativeMemory.Free((void*)ptr);
return true;
}

Expand Down Expand Up @@ -639,7 +629,7 @@ internal unsafe void WriteSingle(int offset, float value)
WriteInt32(offset, BitConverter.SingleToInt32Bits(value));
}

internal void ZeroMemory()
internal unsafe void ZeroMemory()
{
bool mustRelease = false;

Expand All @@ -648,7 +638,7 @@ internal void ZeroMemory()
DangerousAddRef(ref mustRelease);

IntPtr ptr = DangerousGetHandle();
SafeNativeMethods.ZeroMemory(ptr, Length);
Unsafe.InitBlock((void*)ptr, 0, (uint)Length);
}
finally
{
Expand Down
1 change: 0 additions & 1 deletion src/libraries/System.Data.Odbc/src/System.Data.Odbc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ System.Data.Odbc.OdbcTransaction</PackageDescription>
<Compile Include="Common\System\Data\Common\DbConnectionStringCommon.cs" />
<Compile Include="$(CommonPath)System\Data\Common\NameValuePair.cs"
Link="Common\System\Data\Common\NameValuePair.cs" />
<Compile Include="Common\System\Data\Common\SafeNativeMethods.cs" />
<Compile Include="Common\System\Data\DataStorage.cs" />
<Compile Include="Common\System\Data\ProviderBase\DbBuffer.cs" />
<Compile Include="$(CommonPath)System\Data\Common\FieldNameLookup.cs"
Expand Down