-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[API Proposal]: Expose Buffer.ZeroMemory(void*, nuint) #63547
Comments
Tagging subscribers to this area: @dotnet/area-system-memory Issue DetailsBackground and motivationThere are two APIs currently "missing" from the
API Proposalnamespace System;
public static class Buffer
{
public static void MemoryCopy(void* source, void* destination, nuint destinationSizeInBytes, nuint sourceBytesToCopy);
public static void ZeroMemory(void* buffer, nuint sizeInBytes);
} API Usage// When dealing with a short buffer
new Span<byte>(p, size).Clear(); // Before
Buffer.ZeroMemory(p, size); // After
// When dealing with a large buffer
// new Span<byte>(p, size).Clear(); // Unusable
Buffer.ZeroMemory(p, size); // Works just fine Alternative DesignsNo response RisksNone that I can see, really.
|
I like the idea of making
What would be the gain? The following code compiles just fine: nuint byteCount = nuint.MaxValue;
void* ptr = IntPtr.Zero.ToPointer();
Buffer.MemoryCopy(ptr, ptr, byteCount, byteCount); And I can't see any scenario where we could loose some data by casting @bartonjs do we have any guidance for adding |
The gain would be making it more consistent with other modern APIs (eg. all the ones in |
If we already had 32 and 64 bit overloads then I could be convinced that adding an nuint overload would make sense, to avoid using a double-register on 32 bit systems... but we don't, it's just a 64-bit input (overloaded as signed and unsigned). Since casting isn't required then I'd guess the review process would say something like "this is just a stylistic overload that feels like it costs more in the size increase to System.Private.Corelib than it has benefit to the ecosystem". But if (e.g.) Jan K wants to endorse it as good because of register allocations on 32-bit systems, then we'd nod and take his word on it. |
The preferred way to copy and clear memory is These overloads are working around the fact that Span length is |
Ideally not a lot, I think we end up with "at most" 1 "nuint" alternative to each That being said, an alternative would be to just expose |
Noting that if we don't expose |
Agree with #63547 (comment) that there's not any real benefit to exposing nuint overloads of existing methods, which are typed as [u]long. Just leave those as legacy and introduce a new nuint API for the clear method. |
I've updated the API proposal as requested 👍 |
namespace System.Runtime.InteropServices;
public static class NativeMemory
{
public static void ZeroMemory(void* ptr, nuint byteCount);
} |
@tannergooding @GrabYourPitchforks I'm happy to take this if it's up for grabs 😄 |
Background and motivation
Buffer.ZeroMemory
is currently internal (why?). This API just tunnels toSpanHelpers.ClearWithoutReferences
, which is otherwise only accessible throughnew Span<byte>(void*, int).Clear()
. This has two drawbacks: it's arguably clunkier than just doingBuffer.ZeroMemory(p, size)
, and (most importantly) caps the length of the buffer toint
, which makes it immediately a no-go when dealing with larger buffers. This proposal is to makeBuffer.ZeroMemory
public to solve both issues.API Proposal
API Usage
Risks
None that I can see, really.
The text was updated successfully, but these errors were encountered: