-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Implement NativeMemory #54006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Implement NativeMemory #54006
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b45bcbc
Implement NativeMemory
tannergooding d8920f7
Exposing additional APIs as approved
tannergooding 7c03e37
Ensure we have a test covering alignment and size being less than siz…
tannergooding 5cb44d9
Update src/libraries/System.Private.CoreLib/src/System/Runtime/Intero…
tannergooding f04bc2e
Responding to PR feedback
tannergooding 05fc047
Adding additional alignment test coverage for 1 to 16384
tannergooding 7edb6cc
Add coverage for 65k and 1/2/4MB alignments
tannergooding 924ea3c
Fixing the Native\Unix\System.Native\CMakeLists.txt
tannergooding 1e07515
Update src/libraries/System.Private.CoreLib/src/System/Runtime/Intero…
tannergooding 242ecca
Don't call Buffer.Memmove in NativeMemory.AlignedRealloc if ptr is null
tannergooding b2601fe
Updating NativeMemory.AlignedRealloc to correctly copy only the size …
tannergooding 2ee0409
Ensure check_symbol_exists(HAVE_ALIGNED_ALLOC) is under the non-apple…
tannergooding 1266f4d
Check for malloc_usable_size in malloc_np for FreeBSD and ensure test…
tannergooding 05f6b92
Fix the ReallocSmallerToLargerTest test
tannergooding 805882b
Handle that posix_memalign differs from aligned_alloc for size == 0
tannergooding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,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); | ||
tannergooding marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #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" | ||
jkotas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #endif | ||
| } | ||
|
|
||
| void SystemNative_AlignedFree(void* ptr) | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| 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); | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.