No segment method for MemoryAddress?

Ty Young youngty1997 at gmail.com
Tue Aug 25 21:22:29 UTC 2020


On 8/25/20 3:40 PM, Maurizio Cimadamore wrote:
>
>>
>> The libraries that I use(NVML, NVCtrl) rarely return pointers and 
>> even when they do they are immediately made safe by my abstraction 
>> layer:
>>
>>
>> public static NativeArray<Byte> nvmlErrorString(nvmlReturn_t value) 
>> throws Throwable
>> {
>>     return 
>> NativeArray.ofUnsafeSequenceLayout((MemoryAddress)ERROR_STRING.getMethodHandle().invoke(value.getNativeValue()), 
>> ERROR_LAYOUT);
>> }
>>
>>
>> So basically it's impossible for a MemoryAddress to not have segment. 
>> Of course functions are an exception.
>
> Ok - that's what I was hinting at.
>
> But if that's the case - you can simply replace MemoryAddress with 
> MemorySegment inside NativeObject and that should work as before?


Except that doesn't work because NativeFunction(extends NativeObject) 
doesn't have a segment, only a MemoryAddress and creating different code 
paths is extremely ugly. Creating a method like:


public interface NativeObject<A extends Addressable>

{

     public A getAddressable();

}


forces all classes that extends NativeObject to also specify their own 
generic arguments. You can't simply do MemorySegment because 
MappedMemorySegment exists, so NativeValue for example would need to be:


public interface NativeValue<A extends MemorySegment> extends 
NativeObject<A>

{

     public A getAddressable();

}


and of course any static method that returns a NativeValue will need to 
specify the generic type, making already long static methods even longer.


Oddly you can't just do:


public interface NativeObject

{

     public <A extends Addressable> A getAddressable();

}


public interface NativeValue extends NativeObject

{

     public <A extends MemorySegment> A getAddressable();

}


because they supposedly have the same erasure and don't override each 
other... but the NativeValue one does?!?! I don't really understand it. 
I would go this route if it worked, it's not super relevant where the 
segment is from except if it's on or off heap, but FMA doesn't have a 
"HeapMemorySegment" or "NativeMemorySegment" interfaces anyway.




>
> Maurizio
>


More information about the panama-dev mailing list