Skip to content

[API Proposal]: Buffer.BlockCopy overload with long dstOffset #127971

@pfusik

Description

@pfusik

Background and motivation

https://learn.microsoft.com/en-us/dotnet/api/system.buffer.blockcopy?view=net-10.0 cannot access dst array above 2 GB because the byte offset is passed as int.
Looking at the current implementation, it already extends the offset to nuint internally, so this should be an easy addition.

API Proposal

public class Buffer
{
    public static void BlockCopy(Array src, int srcOffset, Array dst, long dstOffset, int count);
}

API Usage

	// Read `a` from `BR`
	void ReadWeights(BinaryReader BR, short[] a, int n)
	{
		const int bufSize = 4096;
		byte[] buf = new byte[bufSize * 2];
		for (int i = 0; i < n; i += bufSize) {
			int m = Math.Min(n - i, bufSize);
			if (BR.Read(buf, 0, m * 2) != m * 2)
				throw new IOException("Truncated read");
#if false
			for (int j = 0; j < m; j++)
				a[i + j] = (short) (buf[j * 2] | buf[j * 2 + 1] << 8);
#else
			Buffer.BlockCopy(buf, 0, a, i * 2L, m * 2);
#endif
		}
	}

Alternative Designs

nint or nuint - my experience with these is limited.

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.BuffersuntriagedNew issue has not been triaged by the area owner

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions