forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement NativeMemory (dotnet#54006)
* 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
1 parent
d764cb5
commit f721cf4
Showing
20 changed files
with
1,175 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/libraries/Common/src/Interop/Windows/Ucrtbase/Interop.MemAlloc.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.