Skip to content
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

Implement NativeMemory #54006

Merged
merged 15 commits into from
Jun 18, 2021
Merged

Implement NativeMemory #54006

merged 15 commits into from
Jun 18, 2021

Conversation

tannergooding
Copy link
Member

This resolves #33244

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@tannergooding
Copy link
Member Author

CC. @GrabYourPitchforks, @jkotas

I've put this up to validate it builds/passes on all architectures, but it should be generally in the "right" shape (barring any feedback).

src/coreclr/vm/corelib.cpp Outdated Show resolved Hide resolved
@tannergooding tannergooding force-pushed the native-memory branch 2 times, most recently from 1da201c to bd5e64e Compare June 10, 2021 17:55
@tannergooding
Copy link
Member Author

tannergooding commented Jun 15, 2021

@jkotas, any other feedback here. If not, I think this is "good" and ready for taking the open questions (like renaming Calloc to AllocZeroed) back to API review.

@tannergooding
Copy link
Member Author

tannergooding commented Jun 16, 2021

CI job is here: https://dev.azure.com/dnceng/public/_build/results?buildId=1191094&view=results (it is running now, the push notifications are backed up however)

…pServices/NativeMemory.Unix.cs

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

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.


if (ptr != null)
{
Buffer.Memmove(ref *(byte*)newPtr, ref *(byte*)ptr, byteCount);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this works. byteCount is the new size, but we can only copy old size (the rest should be left uninitialized).

Copy link
Member Author

@tannergooding tannergooding Jun 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right. I'll need to either expose malloc_usable_size/malloc_size or make this a P/Invoke to a wrapper SystemNative.AlignedRealloc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to be a P/Invoke to a SystemNative_AlignedRealloc to avoid needing 3-4 P/Invoke transitions.

It now correctly does:

  • Allocate new space
  • If allocation was successful:
    • Get usable size of previous allocation
    • Copy min of old/new size from old to new
    • Free old allocation
  • Return result

I've also added a test that explicitly tests going from a small to large allocation and that the bytes are as expected.

@tannergooding
Copy link
Member Author

Turns out posix_memalign can differ from aligned_alloc specifically for size == 0 (its considered invalid for the latter but may return a valid sizeless allocation for the former) and so the overflow isn't "harmless" for those platforms.

I've fixed it to track the adjustedByteCount and handle adjustedByteCount < byteCount as returning null

@tannergooding
Copy link
Member Author

@jkotas, @stephentoub This required a couple additional fixes since your sign-off to ensure it compiled on FreeBSD and so tests were passing everywhere. Would appreciate if you could take a look.

I believe it is otherwise good now and is just waiting on the last couple legs to complete

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tannergooding tannergooding merged commit f721cf4 into dotnet:main Jun 18, 2021
@adamsitnik adamsitnik added this to the 6.0.0 milestone Jun 21, 2021
@adamsitnik adamsitnik added the tenet-performance Performance related issue label Jun 21, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jul 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide Marshal.Alloc api that accepts alignment argument
6 participants