[foreign-memaccess] [Rev 01] RFR: Add MemorySegment::fill

John Rose john.r.rose at oracle.com
Wed May 13 23:02:04 UTC 2020


On May 13, 2020, at 9:40 AM, Chris Hegarty <chegar at openjdk.java.net> wrote:
> 
> On Wed, 13 May 2020 16:25:30 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
> 
>>> Nice and simple, but i think there is an open  design question here.
>>> 
>>> Notice that the bulk `copy` operation resides on `MemoryAddress`.  Fill is a little like a copy, e.g. if expanded later
>>> to fill one segment repeatedly with another (if the destination segment is smaller than the source then it's a
>>> truncated copy).  I think we need to consider grouping bulk operations in the same place.
>> 
>>> I think we need to consider grouping bulk operations in the same place.
>> 
>> I suggested the location to Chris. Since the method operates on segment it felt right to add it on MemorySegment. If we
>> add it on MemoryAddress it will look a tad odd IMHO, since it works on segments.
>> We could tweak fill to work on MemoryAddress, but that means that the user has to pass an address and a length (which
>> is what a segment is).
>> Or we could make fill an instance method on segment.
>> 
>> I'm not sure there's one answer that's better than the others here - but I don't have any strong feelings one way or
>> another, so I'm open to suggestions.
> 
> I agree with the logic of why this method fits better in MemorySegment, rather than MemoryAddress.
> 
> Fill seems more like a "utility", rather than an operation on the segment itself, so I marginally favour a static
> method.

We have an odd legacy from Java arrays that we are working with.
Arrays::fill is used to pack a scalar into (a slice of) an array.
But there’s no Arrays::copy.  Instead everybody uses System::arraycopy.

For Java arrays, some other designs would seem to be better,
in some hypothetical better world:

1. Add a static overloaded method Array::copy which provides
strongly-typed aliases for System::arraycopy (and deprecate that).

2. Add virtual methods to each array type which turn the receiver into
a source or destination operand: maybe copyIn, copyOut, fillIn.

3. Have a span type which reies a slice of such an array.  Add the
virtual methods of 2.  (This seems doable in Valhalla.)

I list these out because the MemorySegment work could use these
design patterns more readily than Java could for native arrays.

So, native array : MemorySegment :: System.arraycopy : (what?)
and native array : MemorySegment :: Arrays.fill : (what?)

(I suppose this game can be played with other ju.Arrays methods.)

— John


More information about the panama-dev mailing list