Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
26 changes: 26 additions & 0 deletions THIRD-PARTY-NOTICES.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -952,3 +952,29 @@ by constants, including codegen instructions. The unsigned division incorporates
"round down" optimization per ridiculous_fish.

This is free and unencumbered software. Any copyright is dedicated to the Public Domain.


License notice for mimalloc
-----------------------------------

MIT License

Copyright (c) 2019 Microsoft Corporation, Daan Leijen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@

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

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

[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_MemFree")]
internal static extern void MemFree(IntPtr ptr);
[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_Calloc")]
internal static extern void* Calloc(nuint num, nuint size);

[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_Free")]
internal static extern void Free(void* ptr);

[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_Malloc")]
internal static extern void* Malloc(nuint size);

[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_Realloc")]
internal static extern void* Realloc(void* ptr, nuint new_size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ internal static partial class Libraries
internal const string GlobalizationNative = "System.Globalization.Native";
internal const string MsQuic = "msquic.dll";
internal const string HostPolicy = "hostpolicy.dll";
internal const string Ucrtbase = "ucrtbase.dll";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 static unsafe partial class Ucrtbase
{
[DllImport(Libraries.Ucrtbase, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern void* _aligned_malloc(nuint size, nuint alignment);

[DllImport(Libraries.Ucrtbase, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern void _aligned_free(void* ptr);

[DllImport(Libraries.Ucrtbase, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern void* _aligned_realloc(void* ptr, nuint size, nuint alignment);

[DllImport(Libraries.Ucrtbase, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern void* calloc(nuint num, nuint size);

[DllImport(Libraries.Ucrtbase, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern void free(void* ptr);

[DllImport(Libraries.Ucrtbase, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern void* malloc(nuint size);

[DllImport(Libraries.Ucrtbase, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern void* realloc(void* ptr, nuint new_size);
}
}
2 changes: 2 additions & 0 deletions src/libraries/Native/Unix/Common/pal_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@
#cmakedefine01 HAVE_GETGROUPLIST
#cmakedefine01 HAVE_SYS_PROCINFO_H
#cmakedefine01 HAVE_IOSS_H
#cmakedefine01 HAVE_ALIGNED_ALLOC
#cmakedefine01 HAVE_POSIX_MEMALIGN

// Mac OS X has stat64, but it is deprecated since plain stat now
// provides the same 64-bit aware struct when targeting OS X > 10.5
Expand Down
9 changes: 6 additions & 3 deletions src/libraries/Native/Unix/System.Native/entrypoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,13 @@ static const Entry s_sysNative[] =
DllImportEntry(SystemNative_LChflagsCanSetHiddenFlag)
DllImportEntry(SystemNative_ReadProcessStatusInfo)
DllImportEntry(SystemNative_Log)
DllImportEntry(SystemNative_MemAlloc)
DllImportEntry(SystemNative_MemReAlloc)
DllImportEntry(SystemNative_MemFree)
DllImportEntry(SystemNative_AlignedAlloc)
DllImportEntry(SystemNative_AlignedFree)
DllImportEntry(SystemNative_Calloc)
DllImportEntry(SystemNative_Free)
DllImportEntry(SystemNative_Malloc)
DllImportEntry(SystemNative_MemSet)
DllImportEntry(SystemNative_Realloc)
DllImportEntry(SystemNative_GetSpaceInfoForMountPoint)
DllImportEntry(SystemNative_GetFormatInfoForMountPoint)
DllImportEntry(SystemNative_GetAllMountPoints)
Expand Down
37 changes: 32 additions & 5 deletions src/libraries/Native/Unix/System.Native/pal_memory.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include "pal_config.h"
#include "pal_memory.h"

#include <stdlib.h>
#include <string.h>

void* SystemNative_MemAlloc(uintptr_t size)
void* SystemNative_AlignedAlloc(uintptr_t alignment, uintptr_t size)
{
return malloc(size);
#if HAVE_ALIGNED_ALLOC && !defined(__APPLE__)
// We want to prefer the standardized aligned_alloc function. However
// it cannot be used on __APPLE__ since we target 10.13 and it was
// only added in 10.15, but we might be compiling on a 10.15 box.
return aligned_alloc(alignment, size);
#elif HAVE_POSIX_MEMALIGN
void* result = NULL;
posix_memalign(&result, alignment, size);
return result;
#else
#error "Platform doesn't support aligned_alloc or posix_memalign"
#endif
}

void SystemNative_AlignedFree(void* ptr)
{
free(ptr);
}

void* SystemNative_MemReAlloc(void* ptr, uintptr_t size)
void* SystemNative_Calloc(uintptr_t num, uintptr_t size)
{
return realloc(ptr, size);
return calloc(num, size);
}

void SystemNative_MemFree(void* ptr)
void SystemNative_Free(void* ptr)
{
free(ptr);
}

void* SystemNative_Malloc(uintptr_t size)
{
return malloc(size);
}

void* SystemNative_MemSet(void* s, int c, uintptr_t n)
{
return memset(s, c, n);
}

void* SystemNative_Realloc(void* ptr, uintptr_t new_size)
{
return realloc(ptr, new_size);
}
25 changes: 20 additions & 5 deletions src/libraries/Native/Unix/System.Native/pal_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,36 @@
#include "pal_types.h"

/**
* C runtime malloc
* C runtime aligned_alloc
*/
PALEXPORT void* SystemNative_MemAlloc(uintptr_t size);
PALEXPORT void* SystemNative_AlignedAlloc(uintptr_t alignment, uintptr_t size);

/**
* C runtime realloc
* C runtime free for aligned_alloc
*/
PALEXPORT void* SystemNative_MemReAlloc(void* ptr, uintptr_t size);
PALEXPORT void SystemNative_AlignedFree(void* ptr);

/**
* C runtime calloc
*/
PALEXPORT void* SystemNative_Calloc(uintptr_t num, uintptr_t size);

/**
* C runtime free
*/
PALEXPORT void SystemNative_MemFree(void* ptr);
PALEXPORT void SystemNative_Free(void* ptr);

/**
* C runtime malloc
*/
PALEXPORT void* SystemNative_Malloc(uintptr_t size);

/**
* C runtime memset
*/
PALEXPORT void* SystemNative_MemSet(void* s, int c, uintptr_t n);

/**
* C runtime realloc
*/
PALEXPORT void* SystemNative_Realloc(void* ptr, uintptr_t size);
3 changes: 3 additions & 0 deletions src/libraries/Native/Unix/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,9 @@ check_c_source_compiles(
"
HAVE_BUILTIN_MUL_OVERFLOW)

check_symbol_exists(aligned_alloc stdlib.h HAVE_ALIGNED_ALLOC)
check_symbol_exists(posix_memalign stdlib.h HAVE_POSIX_MEMALIGN)

configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/Common/pal_config.h.in
${CMAKE_CURRENT_BINARY_DIR}/Common/pal_config.h)
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,9 @@
<data name="Argument_AdjustmentRulesOutOfOrder" xml:space="preserve">
<value>The elements of the AdjustmentRule array must be in chronological order and must not overlap.</value>
</data>
<data name="Argument_AlignmentMustBePow2" xml:space="preserve">
<value>The alignment must be a power of two.</value>
</data>
<data name="Argument_AlreadyACCW" xml:space="preserve">
<value>The object already has a CCW associated with it.</value>
</data>
Expand Down Expand Up @@ -3784,4 +3787,4 @@
<data name="Arg_MemberInfoNotFound" xml:space="preserve">
<value>A MemberInfo that matches '{0}' could not be found.</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\MarshalAsAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\MarshalDirectiveException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\MemoryMarshal.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\NativeMemory.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\UnmanagedCallConvAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\UnmanagedCallersOnlyAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\NativeLibrary.cs" />
Expand Down Expand Up @@ -1725,6 +1726,9 @@
<Compile Include="$(CommonPath)Interop\Windows\Shell32\Interop.SHGetKnownFolderPath.cs">
<Link>Common\Interop\Windows\Shell32\Interop.SHGetKnownFolderPath.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Ucrtbase\Interop.MemAlloc.cs">
<Link>Common\Interop\Windows\Ucrtbase\Interop.MemAlloc.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\User32\Interop.Constants.cs">
<Link>Common\Interop\Windows\User32\Interop.Constants.cs</Link>
</Compile>
Expand Down Expand Up @@ -1796,6 +1800,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Loader\LibraryNameVariation.Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\MemoryFailPoint.Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\Marshal.Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\NativeMemory.Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\StandardOleMarshalObject.Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Security\SecureString.Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\LowLevelMonitor.Windows.cs" />
Expand Down Expand Up @@ -2067,6 +2072,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Loader\LibraryNameVariation.Unix.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\MemoryFailPoint.Unix.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\Marshal.Unix.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\NativeMemory.Unix.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\StandardOleMarshalObject.Unix.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Security\SecureString.Unix.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\LowLevelMonitor.Unix.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,21 @@ public static class BitOperations
public static bool IsPow2(ulong value) => (value & (value - 1)) == 0 && value != 0;

/// <summary>
/// Round the given integral value up to a power of 2.
/// Evaluate whether a given integral value is a power of 2.
/// </summary>
/// <param name="value">The value.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsPow2(nint value) => (value & (value - 1)) == 0 && value > 0;

/// <summary>
/// Evaluate whether a given integral value is a power of 2.
/// </summary>
/// <param name="value">The value.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsPow2(nuint value) => (value & (value - 1)) == 0 && value != 0;

/// <summary>Round the given integral value up to a power of 2.</summary>
/// <param name="value">The value.</param>
/// <returns>
/// The smallest power of 2 which is greater than or equal to <paramref name="value"/>.
/// If <paramref name="value"/> is 0 or the result overflows, returns 0.
Expand Down
Loading