Skip to content

Commit

Permalink
Don't use mmap in the frozen object manager for WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
SingleAccretion committed Jan 21, 2024
1 parent 0cf1432 commit 1e2fe0d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#pragma warning disable IDE0060 // Remove unused parameter

using System.Runtime.InteropServices;

namespace Internal.Runtime
{
internal unsafe partial class FrozenObjectHeapManager
{
private static void* ClrVirtualReserve(nuint size)
{
void* alloc = Interop.Sys.AlignedAlloc(8, size);
if (alloc != null)
{
NativeMemory.Clear(alloc, size);
}
return alloc;
}

private static void* ClrVirtualCommit(void* pBase, nuint size)
{
// Already 'commited'.
return pBase;
}

private static void ClrVirtualFree(void* pBase, nuint size)
{
// This will only be called before an OOM. We have no way do a partial unmap, so do not try.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ internal unsafe partial class FrozenObjectHeapManager
private readonly LowLevelLock m_Crst = new LowLevelLock();
private FrozenObjectSegment m_CurrentSegment;

#if TARGET_WASM
// WASM doesn't support reserving memory so we use a smaller size.
private const nuint FOH_SEGMENT_DEFAULT_SIZE = 64 * 1024;
#else
// Default size to reserve for a frozen segment
private const nuint FOH_SEGMENT_DEFAULT_SIZE = 4 * 1024 * 1024;
#endif
// Size to commit on demand in that reserved space
private const nuint FOH_COMMIT_SIZE = 64 * 1024;

Expand Down Expand Up @@ -119,7 +124,7 @@ public FrozenObjectSegment(nuint sizeHint)
{
m_Size = sizeHint;

Debug.Assert(m_Size > FOH_COMMIT_SIZE);
Debug.Assert(m_Size >= FOH_COMMIT_SIZE);
Debug.Assert(m_Size % FOH_COMMIT_SIZE == 0);

void* alloc = ClrVirtualReserve(m_Size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@
<Compile Include="System\Threading\ThreadPoolCallbackWrapper.NativeAot.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsUnix)'=='true' or '$(TargetsBrowser)'=='true' or '$(TargetsWasi)'=='true'">
<Compile Include="Internal\Runtime\FrozenObjectHeapManager.Unix.cs" />
<Compile Include="Internal\Runtime\FrozenObjectHeapManager.Unix.cs" Condition="'$(TargetsWasm)' != 'true'" />
<Compile Include="Internal\Runtime\FrozenObjectHeapManager.Wasm.cs" Condition="'$(TargetsWasm)' == 'true'" />
<Compile Include="System\Environment.NativeAot.Unix.cs" />
<Compile Include="System\Runtime\InteropServices\NativeLibrary.NativeAot.Unix.cs" />
<Compile Include="System\Runtime\InteropServices\PInvokeMarshal.Unix.cs" />
Expand Down

0 comments on commit 1e2fe0d

Please sign in to comment.