Pointer/Scope API questions
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Sun Dec 22 21:10:53 UTC 2019
On 21/12/2019 23:59, Ty Young wrote:
> Hi,
>
>
> There are a few questions I'd like to ask after using it for awhile:
>
>
> A. the implementation of equals on Pointers and Scopes... How is it
> implemented? In the case of Pointers, does the scope of which the
> pointer was allocated matter? Is comparing Pointers to a struct of a
> given type as reliable as comparing the structs themselves?
Pointer equality just compares the addresses - not the scopes; that is,
if two pointers p1 and p2 point to the same memory location, they are
considered equals. As to your question about differences w/r/t struct
comparison, I don't think the API support struct comparison.
>
>
> B. Is mixing allocated native types from different scopes and using
> them together safe? Will it result in any weird side affects?
You can allocate pointers from different scopes and mix them. The
general rule of thumb (described here [1]) is that you can only write a
pointer p1 into a memory pointed by p2 if p1 is guaranteed to outlive p2.
>
>
> C. What scope are Pointers from C from exactly? The libraries specific
> scope? Are there exceptions like If a function returns a Pointer which
> is derived from function argument Pointer data, is the scope the same
> as the input arguments?
Pointers that come from C have the 'unchecked' scope - as such they are
'unmanaged' pointers. As such you can do whatever you like with them, as
the Panama/foreign branch doesn't restrict use of native pointers in any
way - which also means that it's not easy to make them safer also (e.g.
if you happen to know more about the native pointer bounds). All the
work in the Pointer API predates some of the discussions on how to make
native pointer safer, which we had more recently.
Maurizio
[1] - http://cr.openjdk.java.net/~mcimadamore/panama/scopes.html
>
>
> Thanks for any clarity.
>
More information about the panama-dev
mailing list