Skip to content

Commit d81b066

Browse files
authored
Move Marshal memory allocation methods into CoreLib shared partition (#41911)
* Move Marshal memory allocation methods into CoreLib shared partition * Unify behavior across platforms
1 parent c15455a commit d81b066

File tree

25 files changed

+305
-341
lines changed

25 files changed

+305
-341
lines changed

src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,6 @@
250250
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.HandleTypes.cs">
251251
<Link>Common\Interop\Windows\Kernel32\Interop.HandleTypes.cs</Link>
252252
</Compile>
253-
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.LocalAlloc.cs">
254-
<Link>Common\Interop\Windows\Kernel32\Interop.LocalAlloc.cs</Link>
255-
</Compile>
256-
<Compile Include="$(CommonPath)Interop\Windows\Ole32\Interop.CoTaskMemAlloc.cs">
257-
<Link>Common\Interop\Windows\Ole32\Interop.CoTaskMemAlloc.cs</Link>
258-
</Compile>
259253
<Compile Include="$(CommonPath)Interop\Windows\OleAut32\Interop.SysAllocStringByteLen.cs">
260254
<Link>Common\Interop\Windows\OleAut32\Interop.SysAllocStringByteLen.cs</Link>
261255
</Compile>

src/coreclr/src/System.Private.CoreLib/src/Interop/Unix/Interop.Libraries.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ internal static partial class Interop
88
internal static partial class Libraries
99
{
1010
internal const string Kernel32 = RuntimeHelpers.QCall;
11-
internal const string User32 = RuntimeHelpers.QCall;
12-
internal const string Ole32 = RuntimeHelpers.QCall;
1311
internal const string OleAut32 = RuntimeHelpers.QCall;
14-
internal const string Advapi32 = RuntimeHelpers.QCall;
1512
}
1613
}

src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -250,52 +250,6 @@ public static IntPtr GetHINSTANCE(Module m)
250250
[MethodImpl(MethodImplOptions.InternalCall)]
251251
internal static extern Exception GetExceptionForHRInternal(int errorCode, IntPtr errorInfo);
252252

253-
public static IntPtr AllocHGlobal(IntPtr cb)
254-
{
255-
// For backwards compatibility on 32 bit platforms, ensure we pass values between
256-
// int.MaxValue and uint.MaxValue to Windows. If the binary has had the
257-
// LARGEADDRESSAWARE bit set in the PE header, it may get 3 or 4 GB of user mode
258-
// address space. It is remotely that those allocations could have succeeded,
259-
// though I couldn't reproduce that. In either case, that means we should continue
260-
// throwing an OOM instead of an ArgumentOutOfRangeException for "negative" amounts of memory.
261-
UIntPtr numBytes;
262-
#if TARGET_64BIT
263-
numBytes = new UIntPtr(unchecked((ulong)cb.ToInt64()));
264-
#else // 32
265-
numBytes = new UIntPtr(unchecked((uint)cb.ToInt32()));
266-
#endif
267-
268-
IntPtr pNewMem = Interop.Kernel32.LocalAlloc(Interop.Kernel32.LMEM_FIXED, unchecked(numBytes));
269-
if (pNewMem == IntPtr.Zero)
270-
{
271-
throw new OutOfMemoryException();
272-
}
273-
274-
return pNewMem;
275-
}
276-
277-
public static void FreeHGlobal(IntPtr hglobal)
278-
{
279-
if (!IsNullOrWin32Atom(hglobal))
280-
{
281-
if (IntPtr.Zero != Interop.Kernel32.LocalFree(hglobal))
282-
{
283-
ThrowExceptionForHR(GetHRForLastWin32Error());
284-
}
285-
}
286-
}
287-
288-
public static IntPtr ReAllocHGlobal(IntPtr pv, IntPtr cb)
289-
{
290-
IntPtr pNewMem = Interop.Kernel32.LocalReAlloc(pv, cb, Interop.Kernel32.LMEM_MOVEABLE);
291-
if (pNewMem == IntPtr.Zero)
292-
{
293-
throw new OutOfMemoryException();
294-
}
295-
296-
return pNewMem;
297-
}
298-
299253
#if FEATURE_COMINTEROP
300254
/// <summary>
301255
/// Converts the CLR exception to an HRESULT. This function also sets
@@ -481,83 +435,6 @@ public static IntPtr CreateAggregatedObject<T>(IntPtr pOuter, T o) where T : not
481435
[MethodImpl(MethodImplOptions.InternalCall)]
482436
public static extern bool IsComObject(object o);
483437

484-
#endif // FEATURE_COMINTEROP
485-
486-
public static IntPtr AllocCoTaskMem(int cb)
487-
{
488-
IntPtr pNewMem = Interop.Ole32.CoTaskMemAlloc(new UIntPtr((uint)cb));
489-
if (pNewMem == IntPtr.Zero)
490-
{
491-
throw new OutOfMemoryException();
492-
}
493-
494-
return pNewMem;
495-
}
496-
497-
public static void FreeCoTaskMem(IntPtr ptr)
498-
{
499-
if (!IsNullOrWin32Atom(ptr))
500-
{
501-
Interop.Ole32.CoTaskMemFree(ptr);
502-
}
503-
}
504-
505-
public static IntPtr ReAllocCoTaskMem(IntPtr pv, int cb)
506-
{
507-
IntPtr pNewMem = Interop.Ole32.CoTaskMemRealloc(pv, new UIntPtr((uint)cb));
508-
if (pNewMem == IntPtr.Zero && cb != 0)
509-
{
510-
throw new OutOfMemoryException();
511-
}
512-
513-
return pNewMem;
514-
}
515-
516-
internal static IntPtr AllocBSTR(int length)
517-
{
518-
IntPtr bstr = Interop.OleAut32.SysAllocStringLen(null, length);
519-
if (bstr == IntPtr.Zero)
520-
{
521-
throw new OutOfMemoryException();
522-
}
523-
return bstr;
524-
}
525-
526-
public static void FreeBSTR(IntPtr ptr)
527-
{
528-
if (!IsNullOrWin32Atom(ptr))
529-
{
530-
Interop.OleAut32.SysFreeString(ptr);
531-
}
532-
}
533-
534-
public static IntPtr StringToBSTR(string? s)
535-
{
536-
if (s is null)
537-
{
538-
return IntPtr.Zero;
539-
}
540-
541-
IntPtr bstr = Interop.OleAut32.SysAllocStringLen(s, s.Length);
542-
if (bstr == IntPtr.Zero)
543-
{
544-
throw new OutOfMemoryException();
545-
}
546-
547-
return bstr;
548-
}
549-
550-
public static string PtrToStringBSTR(IntPtr ptr)
551-
{
552-
if (ptr == IntPtr.Zero)
553-
{
554-
throw new ArgumentNullException(nameof(ptr));
555-
}
556-
557-
return PtrToStringUni(ptr, (int)(SysStringByteLen(ptr) / sizeof(char)));
558-
}
559-
560-
#if FEATURE_COMINTEROP
561438
/// <summary>
562439
/// Release the COM component and if the reference hits 0 zombie this object.
563440
/// Further usage of this Object might throw an exception

src/coreclr/src/System.Private.CoreLib/src/System/StubHelpers.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ internal static unsafe IntPtr ConvertToNative(int flags, string strManaged, IntP
127127

128128
internal static void ClearNative(IntPtr pNative)
129129
{
130-
Interop.Ole32.CoTaskMemFree(pNative);
130+
Marshal.FreeCoTaskMem(pNative);
131131
}
132132

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

258258
internal static void ClearNative(IntPtr pNative)
259259
{
260-
if (pNative != IntPtr.Zero)
261-
{
262-
Interop.Ole32.CoTaskMemFree(pNative);
263-
}
260+
Marshal.FreeCoTaskMem(pNative);
264261
}
265262
}
266263

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

413410
internal static void ClearNative(IntPtr pNative)
414411
{
415-
if (IntPtr.Zero != pNative)
416-
{
417-
Interop.OleAut32.SysFreeString(pNative);
418-
}
412+
Marshal.FreeBSTR(pNative);
419413
}
420414
} // class BSTRMarshaler
421415

@@ -476,7 +470,7 @@ internal static void ClearNative(IntPtr pNative)
476470
{
477471
if (IntPtr.Zero != pNative)
478472
{
479-
Interop.Ole32.CoTaskMemFree((IntPtr)(((long)pNative) - sizeof(uint)));
473+
Marshal.FreeCoTaskMem((IntPtr)(((long)pNative) - sizeof(uint)));
480474
}
481475
}
482476
} // class VBByValStrMarshaler
@@ -518,10 +512,7 @@ internal static IntPtr ConvertToNative(int flags, string strManaged)
518512

519513
internal static void ClearNative(IntPtr pNative)
520514
{
521-
if (IntPtr.Zero != pNative)
522-
{
523-
Interop.OleAut32.SysFreeString(pNative);
524-
}
515+
Marshal.FreeBSTR(pNative);
525516
}
526517
} // class AnsiBSTRMarshaler
527518

@@ -1067,7 +1058,7 @@ internal void ClearNative(IntPtr pNativeHome)
10671058
// this must happen regardless of BackPropAction
10681059
Marshal.DestroyStructure(pNativeHome, layoutType);
10691060
}
1070-
Interop.Ole32.CoTaskMemFree(pNativeHome);
1061+
Marshal.FreeCoTaskMem(pNativeHome);
10711062
}
10721063
StubHelpers.DestroyCleanupList(ref cleanupWorkList);
10731064
}

src/coreclr/src/dlls/mscordac/mscordac_unixexports.src

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ nativeStringResourceTable_mscorrc
138138
#LoadLibraryW
139139
#LoadLibraryExW
140140
#LocalAlloc
141-
#LocalReAlloc
142141
#LocalFree
143142
#MapViewOfFile
144143
#MoveFileExW

src/coreclr/src/pal/inc/pal.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,14 +2776,6 @@ LocalAlloc(
27762776
IN UINT uFlags,
27772777
IN SIZE_T uBytes);
27782778

2779-
PALIMPORT
2780-
HLOCAL
2781-
PALAPI
2782-
LocalReAlloc(
2783-
IN HLOCAL hMem,
2784-
IN SIZE_T uBytes,
2785-
IN UINT uFlags);
2786-
27872779
PALIMPORT
27882780
HLOCAL
27892781
PALAPI

src/coreclr/src/pal/inc/rt/palrt.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ typedef union _ULARGE_INTEGER {
303303
/******************* OLE, BSTR, VARIANT *************************/
304304

305305
STDAPI_VIS(DLLEXPORT, LPVOID) CoTaskMemAlloc(SIZE_T cb);
306-
STDAPI_VIS(DLLEXPORT, LPVOID) CoTaskMemRealloc(LPVOID pv, SIZE_T cb);
307306
STDAPI_VIS(DLLEXPORT, void) CoTaskMemFree(LPVOID pv);
308307

309308
typedef SHORT VARIANT_BOOL;

src/coreclr/src/pal/src/memory/local.cpp

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -69,52 +69,6 @@ LocalAlloc(
6969
return (HLOCAL) lpRetVal;
7070
}
7171

72-
/*++
73-
Function:
74-
LocalReAlloc
75-
76-
See MSDN doc.
77-
--*/
78-
HLOCAL
79-
PALAPI
80-
LocalReAlloc(
81-
IN HLOCAL hMem,
82-
IN SIZE_T uBytes,
83-
IN UINT uFlags)
84-
{
85-
LPVOID lpRetVal = NULL;
86-
PERF_ENTRY(LocalReAlloc);
87-
ENTRY("LocalReAlloc (hMem=%p, uBytes=%u, uFlags=%#x)\n", hMem, uBytes, uFlags);
88-
89-
if (uFlags != LMEM_MOVEABLE)
90-
{
91-
// Currently valid iff uFlags is LMEM_MOVEABLE
92-
ASSERT("Invalid parameter uFlags=0x%x\n", uFlags);
93-
SetLastError(ERROR_INVALID_PARAMETER);
94-
goto done;
95-
}
96-
97-
if (uBytes == 0)
98-
{
99-
// PAL's realloc behaves like free for a requested size of zero bytes. Force a nonzero size to get a valid pointer.
100-
uBytes = 1;
101-
}
102-
103-
lpRetVal = PAL_realloc(hMem, uBytes);
104-
105-
if (lpRetVal == NULL)
106-
{
107-
ERROR("Not enough memory\n");
108-
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
109-
goto done;
110-
}
111-
112-
done:
113-
LOGEXIT("LocalReAlloc returning %p.\n", lpRetVal);
114-
PERF_EXIT(LocalReAlloc);
115-
return (HLOCAL)lpRetVal;
116-
}
117-
11872
/*++
11973
Function:
12074
LocalFree

src/coreclr/src/palrt/comem.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,10 @@
1212

1313
STDAPI_(LPVOID) CoTaskMemAlloc(SIZE_T cb)
1414
{
15-
return LocalAlloc(LMEM_FIXED, cb);
16-
}
17-
18-
STDAPI_(LPVOID) CoTaskMemRealloc(LPVOID pv, SIZE_T cb)
19-
{
20-
return LocalReAlloc(pv, cb, LMEM_MOVEABLE);
15+
return malloc(cb);
2116
}
2217

2318
STDAPI_(void) CoTaskMemFree(LPVOID pv)
2419
{
25-
LocalFree(pv);
20+
free(pv);
2621
}

src/coreclr/src/vm/ecalllist.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,15 +1054,9 @@ FCFuncStart(gPalKernel32Funcs)
10541054
QCFuncElement("CreateSemaphoreEx", CreateSemaphoreExW)
10551055
QCFuncElement("FormatMessage", FormatMessageW)
10561056
QCFuncElement("FreeEnvironmentStrings", FreeEnvironmentStringsW)
1057-
QCFuncElement("GetCurrentProcessId", GetCurrentProcessId)
1058-
QCFuncElement("GetCurrentThreadId", GetCurrentThreadId)
10591057
QCFuncElement("GetEnvironmentStrings", GetEnvironmentStringsW)
10601058
QCFuncElement("GetEnvironmentVariable", GetEnvironmentVariableW)
10611059
QCFuncElement("GetStdHandle", GetStdHandle)
1062-
QCFuncElement("GetSystemInfo", GetSystemInfo)
1063-
QCFuncElement("LocalAlloc", LocalAlloc)
1064-
QCFuncElement("LocalReAlloc", LocalReAlloc)
1065-
QCFuncElement("LocalFree", LocalFree)
10661060
QCFuncElement("OpenEvent", OpenEventW)
10671061
QCFuncElement("OpenMutex", OpenMutexW)
10681062
QCFuncElement("OpenSemaphore", OpenSemaphoreW)
@@ -1074,17 +1068,8 @@ FCFuncStart(gPalKernel32Funcs)
10741068
QCFuncElement("SetEvent", SetEvent)
10751069
QCFuncElement("WriteFile", WriteFile)
10761070
FCFuncEnd()
1077-
1078-
FCFuncStart(gPalOle32Funcs)
1079-
QCFuncElement("CoTaskMemAlloc", CoTaskMemAlloc)
1080-
QCFuncElement("CoTaskMemRealloc", CoTaskMemRealloc)
1081-
QCFuncElement("CoTaskMemFree", CoTaskMemFree)
1082-
FCFuncEnd()
1083-
10841071
FCFuncStart(gPalOleAut32Funcs)
10851072
QCFuncElement("SysAllocStringByteLen", SysAllocStringByteLen)
1086-
QCFuncElement("SysAllocStringLen", SysAllocStringLen)
1087-
QCFuncElement("SysFreeString", SysFreeString)
10881073
FCFuncEnd()
10891074
#endif
10901075

@@ -1199,7 +1184,6 @@ FCClassElement("Object", "System", gObjectFuncs)
11991184
FCClassElement("ObjectMarshaler", "System.StubHelpers", gObjectMarshalerFuncs)
12001185
#endif
12011186
#ifdef TARGET_UNIX
1202-
FCClassElement("Ole32", "", gPalOle32Funcs)
12031187
FCClassElement("OleAut32", "", gPalOleAut32Funcs)
12041188
#endif
12051189
FCClassElement("OverlappedData", "System.Threading", gOverlappedFuncs)

0 commit comments

Comments
 (0)