Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manual: attempt to explain when to use Ptr{T} vs Ref{T} #14930

Merged
merged 1 commit into from
Feb 5, 2016

Conversation

jiahao
Copy link
Member

@jiahao jiahao commented Feb 4, 2016

I've attempted to update the part of the ccall documentation in the manual
according to my best understanding of current good practices for using Ptr{T}
vs Ref{T} in ccall type declarations.
The current wording summarizes my experiences from maintaining the GSL.jl
wrappers and a better understanding of what Ref does from offline discussions
with @jakebolewski, @JeffBezanson and @vtjnash.

  • Remove references to types as C-structs #2818
  • Explain when to use Ptr{T} and when to use Ref{T} correctly.
    Ptr is generally used for return types and fields of types mirroring
    C structs. Ref is generally used for input types, allowing memory
    managed by either C or Julia to be referenced by ccall.
  • Provide some examples of C wrappers simplified from GSL.jl
  • Other minor formatting changes

Ref: JuliaMath/GSL.jl#43

@jiahao jiahao force-pushed the cjh/ccall-examples branch from 115cbd0 to 3b52cb6 Compare February 4, 2016 05:03
The input ``n`` is passed by value, and so the function's input signature is
simply declared as ``(Csize_t,)`` without any ``Ref`` or ``Ptr`` necessary.
Furthermore, ``n`` can be any type that is convertable to a ``Csize_t``
integer; the ``ccall`` implicitly calls ``convert(Csize_t, n)``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Base.cconvert(Csize_t, n) to be more accurate.

@jiahao jiahao force-pushed the cjh/ccall-examples branch from 3b52cb6 to 626e7bb Compare February 4, 2016 05:31
without requiring as much manual management of the ``Ptr`` conversions.
After #2818 is implemented, it will be true that an ``Vector{T}`` will be equivalent to
an ``Ptr{Ptr{T}}``. That is currently not true, and the conversion must be explicitly.
In C wrapper code that must handle pointers, ``Ref{T}`` should generally be
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I first read "C wrapper code" as wrapper code written in C. Maybe say "In code wrapping C function calls" or something like that?

- Remove references to #2818

- Explain when to use `Ptr{T}` and when to use `Ref{T}` correctly.
  `Ptr` is generally used for return types and fields of types mirroring
  C structs. `Ref` is generally used for input types, allowing memory
  managed by either C or Julia to be referenced by `ccall`.

- Provide some examples of C wrappers simplified from GSL.jl, with
  comments delineating the various parts of the `ccall`.

- Fix description of `cconvert` in the Auto-conversion section

- Better cross-referencing to the standard library

- Other minor formatting changes

Ref: JuliaMath/GSL.jl#43
@jiahao jiahao force-pushed the cjh/ccall-examples branch from 626e7bb to a70ecdb Compare February 4, 2016 18:55
@nalimilan
Copy link
Member

Thanks, the additions are really helpful!

@jiahao jiahao added the docs This change adds or pertains to documentation label Feb 5, 2016
jiahao added a commit that referenced this pull request Feb 5, 2016
Manual: attempt to explain when to use Ptr{T} vs Ref{T}
@jiahao jiahao merged commit a990a73 into master Feb 5, 2016
@jiahao jiahao deleted the cjh/ccall-examples branch February 5, 2016 15:47
are passed by value. For C code accepting pointers, ``Ref{T}`` should
generally be used for the types of input arguments, allowing the use of
pointers to memory managed by either Julia or C through the implicit call to
:func:``cconvert``. In contrast, pointers returned by the C function called
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these be single backticks with the :func: syntax?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, yes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's just that one

jiahao added a commit that referenced this pull request Feb 5, 2016
In Julia code wrapping calls to external Fortran routines, all input arguments
should be declared as of type ``Ref{T}``, as Fortran passes all variables by
reference. The return type should either be ``Void`` for Fortran subroutines,
or a ``Ptr{T}`` for Fortran functions returning the type ``T``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the return type should be T for Fortran functions that return T

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting the error; fixed in 3256421

jiahao added a commit that referenced this pull request Feb 6, 2016
Return type of Fortran function should be Void or T, not Ptr{T}

ref: #14930 (comment)
result_array) <cconvert>` unpacks the Julia pointer to a Julia array data
structure into a form understandable by C.

Note that for this code to work correctly, ``result_array`` must be declared to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't quite accurate – the gc-rooting is performed on the arguments without regards to the signature

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What wording would you suggest instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants