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

Move Marshal memory allocation methods into CoreLib shared partition #41911

Merged
merged 2 commits into from
Sep 8, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,6 @@
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.HandleTypes.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.HandleTypes.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.LocalAlloc.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.LocalAlloc.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Ole32\Interop.CoTaskMemAlloc.cs">
<Link>Common\Interop\Windows\Ole32\Interop.CoTaskMemAlloc.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\OleAut32\Interop.SysAllocStringByteLen.cs">
<Link>Common\Interop\Windows\OleAut32\Interop.SysAllocStringByteLen.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ internal static partial class Interop
internal static partial class Libraries
{
internal const string Kernel32 = RuntimeHelpers.QCall;
internal const string User32 = RuntimeHelpers.QCall;
internal const string Ole32 = RuntimeHelpers.QCall;
internal const string OleAut32 = RuntimeHelpers.QCall;
internal const string Advapi32 = RuntimeHelpers.QCall;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,52 +250,6 @@ public static IntPtr GetHINSTANCE(Module m)
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Exception GetExceptionForHRInternal(int errorCode, IntPtr errorInfo);

public static IntPtr AllocHGlobal(IntPtr cb)
{
// For backwards compatibility on 32 bit platforms, ensure we pass values between
// int.MaxValue and uint.MaxValue to Windows. If the binary has had the
// LARGEADDRESSAWARE bit set in the PE header, it may get 3 or 4 GB of user mode
// address space. It is remotely that those allocations could have succeeded,
// though I couldn't reproduce that. In either case, that means we should continue
// throwing an OOM instead of an ArgumentOutOfRangeException for "negative" amounts of memory.
UIntPtr numBytes;
#if TARGET_64BIT
numBytes = new UIntPtr(unchecked((ulong)cb.ToInt64()));
#else // 32
numBytes = new UIntPtr(unchecked((uint)cb.ToInt32()));
#endif

IntPtr pNewMem = Interop.Kernel32.LocalAlloc(Interop.Kernel32.LMEM_FIXED, unchecked(numBytes));
if (pNewMem == IntPtr.Zero)
{
throw new OutOfMemoryException();
}

return pNewMem;
}

public static void FreeHGlobal(IntPtr hglobal)
{
if (!IsNullOrWin32Atom(hglobal))
{
if (IntPtr.Zero != Interop.Kernel32.LocalFree(hglobal))
{
ThrowExceptionForHR(GetHRForLastWin32Error());
}
}
}

public static IntPtr ReAllocHGlobal(IntPtr pv, IntPtr cb)
{
IntPtr pNewMem = Interop.Kernel32.LocalReAlloc(pv, cb, Interop.Kernel32.LMEM_MOVEABLE);
if (pNewMem == IntPtr.Zero)
{
throw new OutOfMemoryException();
}

return pNewMem;
}

#if FEATURE_COMINTEROP
/// <summary>
/// Converts the CLR exception to an HRESULT. This function also sets
Expand Down Expand Up @@ -481,83 +435,6 @@ public static IntPtr CreateAggregatedObject<T>(IntPtr pOuter, T o) where T : not
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern bool IsComObject(object o);

#endif // FEATURE_COMINTEROP

public static IntPtr AllocCoTaskMem(int cb)
{
IntPtr pNewMem = Interop.Ole32.CoTaskMemAlloc(new UIntPtr((uint)cb));
if (pNewMem == IntPtr.Zero)
{
throw new OutOfMemoryException();
}

return pNewMem;
}

public static void FreeCoTaskMem(IntPtr ptr)
{
if (!IsNullOrWin32Atom(ptr))
{
Interop.Ole32.CoTaskMemFree(ptr);
}
}

public static IntPtr ReAllocCoTaskMem(IntPtr pv, int cb)
{
IntPtr pNewMem = Interop.Ole32.CoTaskMemRealloc(pv, new UIntPtr((uint)cb));
if (pNewMem == IntPtr.Zero && cb != 0)
{
throw new OutOfMemoryException();
}

return pNewMem;
}

internal static IntPtr AllocBSTR(int length)
{
IntPtr bstr = Interop.OleAut32.SysAllocStringLen(null, length);
if (bstr == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
return bstr;
}

public static void FreeBSTR(IntPtr ptr)
{
if (!IsNullOrWin32Atom(ptr))
{
Interop.OleAut32.SysFreeString(ptr);
}
}

public static IntPtr StringToBSTR(string? s)
{
if (s is null)
{
return IntPtr.Zero;
}

IntPtr bstr = Interop.OleAut32.SysAllocStringLen(s, s.Length);
if (bstr == IntPtr.Zero)
{
throw new OutOfMemoryException();
}

return bstr;
}

public static string PtrToStringBSTR(IntPtr ptr)
{
if (ptr == IntPtr.Zero)
{
throw new ArgumentNullException(nameof(ptr));
}

return PtrToStringUni(ptr, (int)(SysStringByteLen(ptr) / sizeof(char)));
}

#if FEATURE_COMINTEROP
/// <summary>
/// Release the COM component and if the reference hits 0 zombie this object.
/// Further usage of this Object might throw an exception
Expand Down
21 changes: 6 additions & 15 deletions src/coreclr/src/System.Private.CoreLib/src/System/StubHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ internal static unsafe IntPtr ConvertToNative(int flags, string strManaged, IntP

internal static void ClearNative(IntPtr pNative)
{
Interop.Ole32.CoTaskMemFree(pNative);
Marshal.FreeCoTaskMem(pNative);
}

internal static unsafe void ConvertFixedToNative(int flags, string strManaged, IntPtr pNativeBuffer, int length)
Expand Down Expand Up @@ -257,10 +257,7 @@ internal static unsafe IntPtr ConvertToNative(int flags, string strManaged, IntP

internal static void ClearNative(IntPtr pNative)
{
if (pNative != IntPtr.Zero)
{
Interop.Ole32.CoTaskMemFree(pNative);
}
Marshal.FreeCoTaskMem(pNative);
}
}

Expand Down Expand Up @@ -412,10 +409,7 @@ internal static unsafe IntPtr ConvertToNative(string strManaged, IntPtr pNativeB

internal static void ClearNative(IntPtr pNative)
{
if (IntPtr.Zero != pNative)
{
Interop.OleAut32.SysFreeString(pNative);
}
Marshal.FreeBSTR(pNative);
}
} // class BSTRMarshaler

Expand Down Expand Up @@ -476,7 +470,7 @@ internal static void ClearNative(IntPtr pNative)
{
if (IntPtr.Zero != pNative)
{
Interop.Ole32.CoTaskMemFree((IntPtr)(((long)pNative) - sizeof(uint)));
Marshal.FreeCoTaskMem((IntPtr)(((long)pNative) - sizeof(uint)));
}
}
} // class VBByValStrMarshaler
Expand Down Expand Up @@ -518,10 +512,7 @@ internal static IntPtr ConvertToNative(int flags, string strManaged)

internal static void ClearNative(IntPtr pNative)
{
if (IntPtr.Zero != pNative)
{
Interop.OleAut32.SysFreeString(pNative);
}
Marshal.FreeBSTR(pNative);
}
} // class AnsiBSTRMarshaler

Expand Down Expand Up @@ -1067,7 +1058,7 @@ internal void ClearNative(IntPtr pNativeHome)
// this must happen regardless of BackPropAction
Marshal.DestroyStructure(pNativeHome, layoutType);
}
Interop.Ole32.CoTaskMemFree(pNativeHome);
Marshal.FreeCoTaskMem(pNativeHome);
}
StubHelpers.DestroyCleanupList(ref cleanupWorkList);
}
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/src/dlls/mscordac/mscordac_unixexports.src
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ nativeStringResourceTable_mscorrc
#LoadLibraryW
#LoadLibraryExW
#LocalAlloc
#LocalReAlloc
#LocalFree
#MapViewOfFile
#MoveFileExW
Expand Down
8 changes: 0 additions & 8 deletions src/coreclr/src/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2776,14 +2776,6 @@ LocalAlloc(
IN UINT uFlags,
IN SIZE_T uBytes);

PALIMPORT
HLOCAL
PALAPI
LocalReAlloc(
IN HLOCAL hMem,
IN SIZE_T uBytes,
IN UINT uFlags);

PALIMPORT
HLOCAL
PALAPI
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/src/pal/inc/rt/palrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ typedef union _ULARGE_INTEGER {
/******************* OLE, BSTR, VARIANT *************************/

STDAPI_VIS(DLLEXPORT, LPVOID) CoTaskMemAlloc(SIZE_T cb);
STDAPI_VIS(DLLEXPORT, LPVOID) CoTaskMemRealloc(LPVOID pv, SIZE_T cb);
STDAPI_VIS(DLLEXPORT, void) CoTaskMemFree(LPVOID pv);

typedef SHORT VARIANT_BOOL;
Expand Down
46 changes: 0 additions & 46 deletions src/coreclr/src/pal/src/memory/local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,52 +69,6 @@ LocalAlloc(
return (HLOCAL) lpRetVal;
}

/*++
Function:
LocalReAlloc

See MSDN doc.
--*/
HLOCAL
PALAPI
LocalReAlloc(
IN HLOCAL hMem,
IN SIZE_T uBytes,
IN UINT uFlags)
{
LPVOID lpRetVal = NULL;
PERF_ENTRY(LocalReAlloc);
ENTRY("LocalReAlloc (hMem=%p, uBytes=%u, uFlags=%#x)\n", hMem, uBytes, uFlags);

if (uFlags != LMEM_MOVEABLE)
{
// Currently valid iff uFlags is LMEM_MOVEABLE
ASSERT("Invalid parameter uFlags=0x%x\n", uFlags);
SetLastError(ERROR_INVALID_PARAMETER);
goto done;
}

if (uBytes == 0)
{
// PAL's realloc behaves like free for a requested size of zero bytes. Force a nonzero size to get a valid pointer.
uBytes = 1;
}

lpRetVal = PAL_realloc(hMem, uBytes);

if (lpRetVal == NULL)
{
ERROR("Not enough memory\n");
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto done;
}

done:
LOGEXIT("LocalReAlloc returning %p.\n", lpRetVal);
PERF_EXIT(LocalReAlloc);
return (HLOCAL)lpRetVal;
}

/*++
Function:
LocalFree
Expand Down
9 changes: 2 additions & 7 deletions src/coreclr/src/palrt/comem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@

STDAPI_(LPVOID) CoTaskMemAlloc(SIZE_T cb)
{
return LocalAlloc(LMEM_FIXED, cb);
}

STDAPI_(LPVOID) CoTaskMemRealloc(LPVOID pv, SIZE_T cb)
{
return LocalReAlloc(pv, cb, LMEM_MOVEABLE);
return malloc(cb);
}

STDAPI_(void) CoTaskMemFree(LPVOID pv)
{
LocalFree(pv);
free(pv);
}
16 changes: 0 additions & 16 deletions src/coreclr/src/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -1054,15 +1054,9 @@ FCFuncStart(gPalKernel32Funcs)
QCFuncElement("CreateSemaphoreEx", CreateSemaphoreExW)
QCFuncElement("FormatMessage", FormatMessageW)
QCFuncElement("FreeEnvironmentStrings", FreeEnvironmentStringsW)
QCFuncElement("GetCurrentProcessId", GetCurrentProcessId)
QCFuncElement("GetCurrentThreadId", GetCurrentThreadId)
QCFuncElement("GetEnvironmentStrings", GetEnvironmentStringsW)
QCFuncElement("GetEnvironmentVariable", GetEnvironmentVariableW)
QCFuncElement("GetStdHandle", GetStdHandle)
QCFuncElement("GetSystemInfo", GetSystemInfo)
QCFuncElement("LocalAlloc", LocalAlloc)
QCFuncElement("LocalReAlloc", LocalReAlloc)
QCFuncElement("LocalFree", LocalFree)
QCFuncElement("OpenEvent", OpenEventW)
QCFuncElement("OpenMutex", OpenMutexW)
QCFuncElement("OpenSemaphore", OpenSemaphoreW)
Expand All @@ -1074,17 +1068,8 @@ FCFuncStart(gPalKernel32Funcs)
QCFuncElement("SetEvent", SetEvent)
QCFuncElement("WriteFile", WriteFile)
FCFuncEnd()

FCFuncStart(gPalOle32Funcs)
QCFuncElement("CoTaskMemAlloc", CoTaskMemAlloc)
QCFuncElement("CoTaskMemRealloc", CoTaskMemRealloc)
QCFuncElement("CoTaskMemFree", CoTaskMemFree)
FCFuncEnd()

FCFuncStart(gPalOleAut32Funcs)
QCFuncElement("SysAllocStringByteLen", SysAllocStringByteLen)
QCFuncElement("SysAllocStringLen", SysAllocStringLen)
QCFuncElement("SysFreeString", SysFreeString)
FCFuncEnd()
#endif

Expand Down Expand Up @@ -1199,7 +1184,6 @@ FCClassElement("Object", "System", gObjectFuncs)
FCClassElement("ObjectMarshaler", "System.StubHelpers", gObjectMarshalerFuncs)
#endif
#ifdef TARGET_UNIX
FCClassElement("Ole32", "", gPalOle32Funcs)
FCClassElement("OleAut32", "", gPalOleAut32Funcs)
#endif
FCClassElement("OverlappedData", "System.Threading", gOverlappedFuncs)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal unsafe partial class Sys
{
[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_MemAlloc")]
internal static extern IntPtr MemAlloc(nuint sizeInBytes);

[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_MemReAlloc")]
internal static extern IntPtr MemReAlloc(IntPtr ptr, nuint newSize);

[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_MemFree")]
internal static extern void MemFree(IntPtr ptr);
}
}
Loading