Skip to content

Commit

Permalink
Implement NativeMemory (#54006)
Browse files Browse the repository at this point in the history
* Implement NativeMemory

* Exposing additional APIs as approved

* Ensure we have a test covering alignment and size being less than sizeof(void*)

* Update src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeMemory.Unix.cs

Co-authored-by: Jan Kotas <jkotas@microsoft.com>

* Responding to PR feedback

* Adding additional alignment test coverage for 1 to 16384

* Add coverage for 65k and 1/2/4MB alignments

* Fixing the Native\Unix\System.Native\CMakeLists.txt

* Update src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeMemory.Unix.cs

Co-authored-by: Jan Kotas <jkotas@microsoft.com>

* Don't call Buffer.Memmove in NativeMemory.AlignedRealloc if ptr is null

* Updating NativeMemory.AlignedRealloc to correctly copy only the size of the last allocation

* Ensure check_symbol_exists(HAVE_ALIGNED_ALLOC) is under the non-apple paths

* Check for malloc_usable_size in malloc_np for FreeBSD and ensure tests compile

* Fix the ReallocSmallerToLargerTest test

* Handle that posix_memalign differs from aligned_alloc for size == 0

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
  • Loading branch information
tannergooding and jkotas committed Jun 18, 2021
1 parent d764cb5 commit f721cf4
Show file tree
Hide file tree
Showing 20 changed files with 1,175 additions and 81 deletions.
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,27 @@

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_AlignedRealloc")]
internal static extern void* AlignedRealloc(void* ptr, nuint alignment, nuint new_size);

[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);
}
}
5 changes: 5 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,11 @@
#cmakedefine01 HAVE_GETGROUPLIST
#cmakedefine01 HAVE_SYS_PROCINFO_H
#cmakedefine01 HAVE_IOSS_H
#cmakedefine01 HAVE_ALIGNED_ALLOC
#cmakedefine01 HAVE_MALLOC_SIZE
#cmakedefine01 HAVE_MALLOC_USABLE_SIZE
#cmakedefine01 HAVE_MALLOC_USABLE_SIZE_NP
#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
11 changes: 8 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,15 @@ 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_AlignedRealloc)
DllImportEntry(SystemNative_Calloc)
DllImportEntry(SystemNative_Free)
DllImportEntry(SystemNative_GetUsableSize)
DllImportEntry(SystemNative_Malloc)
DllImportEntry(SystemNative_MemSet)
DllImportEntry(SystemNative_Realloc)
DllImportEntry(SystemNative_GetSpaceInfoForMountPoint)
DllImportEntry(SystemNative_GetFormatInfoForMountPoint)
DllImportEntry(SystemNative_GetAllMountPoints)
Expand Down
75 changes: 70 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,92 @@
// 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 <assert.h>
#include <stdlib.h>
#include <string.h>

void* SystemNative_MemAlloc(uintptr_t size)
#if HAVE_MALLOC_SIZE
#include <malloc/malloc.h>
#elif HAVE_MALLOC_USABLE_SIZE
#include <malloc.h>
#elif HAVE_MALLOC_USABLE_SIZE_NP
#include <malloc_np.h>
#else
#error "Platform doesn't support malloc_usable_size or malloc_size"
#endif

void* SystemNative_AlignedAlloc(uintptr_t alignment, uintptr_t size)
{
return malloc(size);
#if HAVE_ALIGNED_ALLOC
// 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_AlignedRealloc(void* ptr, uintptr_t alignment, uintptr_t new_size)
{
void* result = SystemNative_AlignedAlloc(alignment, new_size);

if (result != NULL)
{
uintptr_t old_size = SystemNative_GetUsableSize(ptr);
assert((ptr != NULL) || (old_size == 0));

memcpy(result, ptr, (new_size < old_size) ? new_size : old_size);
SystemNative_AlignedFree(ptr);
}

return result;
}

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);
}

uintptr_t SystemNative_GetUsableSize(void* ptr)
{
#if HAVE_MALLOC_SIZE
return malloc_size(ptr);
#elif HAVE_MALLOC_USABLE_SIZE || HAVE_MALLOC_USABLE_SIZE_NP
return malloc_usable_size(ptr);
#else
#error "Platform doesn't support malloc_usable_size or malloc_size"
#endif
}

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);
}
35 changes: 30 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,46 @@
#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
* Free for C runtime aligned_alloc
*/
PALEXPORT void SystemNative_AlignedFree(void* ptr);

/**
* Realloc for C runtime aligned_alloc
*/
PALEXPORT void* SystemNative_MemReAlloc(void* ptr, uintptr_t size);
PALEXPORT void* SystemNative_AlignedRealloc(void* ptr, uintptr_t alignment, uintptr_t size);

/**
* 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);

/**
* Get usable size of C runtime malloc
*/
PALEXPORT uintptr_t SystemNative_GetUsableSize(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 new_size);
32 changes: 31 additions & 1 deletion src/libraries/Native/Unix/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -529,33 +529,63 @@ if (CLR_CMAKE_TARGET_LINUX)
set(HAVE_SUPPORT_FOR_DUAL_MODE_IPV4_PACKET_INFO 1)
endif ()

check_symbol_exists(
malloc_size
malloc/malloc.h
HAVE_MALLOC_SIZE)
check_symbol_exists(
malloc_usable_size
malloc.h
HAVE_MALLOC_USABLE_SIZE)
check_symbol_exists(
malloc_usable_size
malloc_np.h
HAVE_MALLOC_USABLE_SIZE_NP)
check_symbol_exists(
posix_memalign
stdlib.h
HAVE_POSIX_MEMALIGN)

if(CLR_CMAKE_TARGET_IOS)
# Manually set results from check_c_source_runs() since it's not possible to actually run it during CMake configure checking
unset(HAVE_SHM_OPEN_THAT_WORKS_WELL_ENOUGH_WITH_MMAP)
unset(HAVE_ALIGNED_ALLOC) # only exists on iOS 13+
unset(HAVE_CLOCK_MONOTONIC) # only exists on iOS 10+
unset(HAVE_CLOCK_REALTIME) # only exists on iOS 10+
unset(HAVE_FORK) # exists but blocked by kernel
elseif(CLR_CMAKE_TARGET_MACCATALYST)
# Manually set results from check_c_source_runs() since it's not possible to actually run it during CMake configure checking
# TODO: test to see if these all actually hold true on Mac Catalyst
unset(HAVE_SHM_OPEN_THAT_WORKS_WELL_ENOUGH_WITH_MMAP)
unset(HAVE_ALIGNED_ALLOC) # only exists on iOS 13+
unset(HAVE_CLOCK_MONOTONIC) # only exists on iOS 10+
unset(HAVE_CLOCK_REALTIME) # only exists on iOS 10+
unset(HAVE_FORK) # exists but blocked by kernel
elseif(CLR_CMAKE_TARGET_TVOS)
# Manually set results from check_c_source_runs() since it's not possible to actually run it during CMake configure checking
unset(HAVE_SHM_OPEN_THAT_WORKS_WELL_ENOUGH_WITH_MMAP)
unset(HAVE_ALIGNED_ALLOC) # only exists on iOS 13+
unset(HAVE_CLOCK_MONOTONIC) # only exists on iOS 10+
unset(HAVE_CLOCK_REALTIME) # only exists on iOS 10+
unset(HAVE_FORK) # exists but blocked by kernel
elseif(CLR_CMAKE_TARGET_ANDROID)
# Manually set results from check_c_source_runs() since it's not possible to actually run it during CMake configure checking
unset(HAVE_SHM_OPEN_THAT_WORKS_WELL_ENOUGH_WITH_MMAP)
unset(HAVE_ALIGNED_ALLOC) # only exists on newer Android
set(HAVE_CLOCK_MONOTONIC 1)
set(HAVE_CLOCK_REALTIME 1)
elseif (CLR_CMAKE_TARGET_BROWSER)
elseif(CLR_CMAKE_TARGET_BROWSER)
set(HAVE_FORK 0)
else()
if(CLR_CMAKE_TARGET_OSX)
unset(HAVE_ALIGNED_ALLOC) # only exists on OSX 10.15+
else()
check_symbol_exists(
aligned_alloc
stdlib.h
HAVE_ALIGNED_ALLOC)
endif()

check_c_source_runs(
"
#include <sys/mman.h>
Expand Down
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>
Loading

0 comments on commit f721cf4

Please sign in to comment.