[foreign] Panama EA - August 2019 edition
Henry Jen
henry.jen at oracle.com
Mon Sep 2 06:20:08 UTC 2019
> On Aug 31, 2019, at 4:57 AM, Jorn Vernee <JORN.VERNEE at ORACLE.COM> wrote:
>
>
> On 30/08/2019 22:34, Maurizio Cimadamore wrote:
>>
>> On 30/08/2019 21:22, Maurizio Cimadamore wrote:
>>>
>>> On 30/08/2019 21:12, Maurizio Cimadamore wrote:
>>>>
>>>>
>>>> So, let's say we have this:
>>>>
>>>> Pointer<Byte> c_str = scope.allocateCString("Hello");
>>>>
>>>> Now we have to create a pointer to that pointer. To do that:
>>>>
>>>> Pointer<Pointer<Byte>> p_c_str = scope.allocate(NativeTypes.UINT8.pointer());
>>>>
>>>> And then initialize the contents of the pointer to pointer:
>>>>
>>>> p_c_str.set(c_str);
>>>>
>>>> You can now pass p_c_str to your function.
>>>>
>>>> Is this what you wanted to do?
>>>
>>> And, I think here what bugs you is the need to allocate the outer pointer - you want something akin to C's & operator.
>>>
Uh, now I know what you really asking for, you can ignore my previous email. :)
>>> I made a proposal few months ago - which I've been able to find :-)
>>>
>>> https://mail.openjdk.java.net/pipermail/panama-dev/2019-February/004629.html
>>>
>>> Didn't get much traction back then - but maybe we should revisit?
>>
>> For the records, the proposal didn't go ahead as a result of an accidentally private discussion between me and Jorn. The conclusion can be summarized as follows:
>>
>> For Pointer, Arrays and Callback, it is hard to add an addressOf method because you need to allocate (so you need to take a scope) - e.g. to do something akin to what I showed you in the other email.
>>
>> Structs has already an addressof (called ptr), so, not much to gain there (other than the renaming)
>>
>> Which basically left us with the feeling that there's not much that can be improved.
>
> FWIW, Array also has a ptr() method nowadays that returns a Pointer<Array<X>>.
>
Now you mention this, it makes me feel that carrier type maybe a little misleading?
Note that the function is to have a Pointer with correct layout of an array, not as the element type.
But it’s still the same address, not really have the same meaning as Pointer<Pointer<X>>.
Cheers,
Henry
> Struct and Array are pretty easy to add such a method to, since they are implemented as a pointer to a block of memory that holds their value already. Pointer on the other hand stores it's value directly in a Java `long`. It is not really possible to return a 'stable' Pointer to that `long`; e.g. the Pointer object that holds the `long` might reside on the GC heap, and be moved from time to time, invalidating any Pointer that points to it. Even objects located on the stack are not guaranteed to be in the same place all the time (think of Loom's continuations).
>
> So, you'd need to put this value in a stable place, e.g. off-heap. But, this also means that any pointer to the 'stable' value is not actually a Pointer to the Pointer you already had, but rather to a copy of it. i.e. Updating the value through this new Pointer will not affect the value of the original.
>
> Maybe there is another way to get around these problems, but making things explicit in the written code also has it's merits, since you can _see_ what's going on, and don't have to memorize several corner cases in which things happen differently than you would expect.
>
> Jorn
>
>>
>> Maurizio
>>
>>
>>>
>>> Maurizio
>>>
>>>>
>>>> Maurizio
>>>>
>>>>>
>>>>>
>>>>>>
>>>>>> Maurizio
>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>>
>>>>>>>> Maurizio
>>>>>>>>
>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> Cheers
>>>>>>>>>> Maurizio
>>>>>>>>>>
>>>>>>>>>> On 19/08/2019 10:33, sundararajan.athijegannathan at oracle.com wrote:
>>>>>>>>>>>>
>>>>>>>>>>>> All that said, how close is Panama? Is this foreign memory API going to stay going forward or will the project take a major shift? I'd *really* like to start putting this to use and am willing to make adjustment where needed if minor changes are made, but if the entire foreign API is scrapped it isn't worth it.
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> Panama "memory access" ("memaccess" panama-dev branch) API is expected to become stable first and then other parts of java.foreign later ("foreign" branch stuff).
More information about the panama-dev
mailing list