Skip to content

Commit 5bb3b96

Browse files
committed
Try to improve cconvert/unsafe_convert doc
1 parent 7f0437f commit 5bb3b96

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

base/docs/helpdb/Base.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,14 @@ getindex(collection, key...)
115115
"""
116116
cconvert(T,x)
117117
118-
Convert `x` to a value of type `T`, typically by calling `convert(T,x)`
118+
Convert `x` to a value to be passed to C code as type `T`, typically by calling `convert(T, x)`.
119119
120120
In cases where `x` cannot be safely converted to `T`, unlike [`convert`](@ref), `cconvert` may
121121
return an object of a type different from `T`, which however is suitable for
122-
[`unsafe_convert`](@ref) to handle.
122+
[`unsafe_convert`](@ref) to handle. The result of this function should be kept valid (for the GC)
123+
until the result of [`unsafe_convert`](@ref) is not needed anymore.
124+
This can be used to allocate memory that will be accessed by the `ccall`.
125+
If multiple objects need to be allocated, a tuple of the objects can be used as return value.
123126
124127
Neither `convert` nor `cconvert` should take a Julia object and turn it into a `Ptr`.
125128
"""
@@ -881,7 +884,8 @@ trunc
881884
"""
882885
unsafe_convert(T,x)
883886
884-
Convert `x` to a value of type `T`
887+
Convert `x` to a C argument of type `T`
888+
where the input `x` must be the return value of `cconvert(T, ...)`.
885889
886890
In cases where [`convert`](@ref) would need to take a Julia object
887891
and turn it into a `Ptr`, this function should be used to define and perform
@@ -895,6 +899,8 @@ but `x=[a,b,c]` is not.
895899
The `unsafe` prefix on this function indicates that using the result of this function after
896900
the `x` argument to this function is no longer accessible to the program may cause undefined
897901
behavior, including program corruption or segfaults, at any later time.
902+
903+
See also [`cconvert`](@ref)
898904
"""
899905
unsafe_convert
900906

doc/src/manual/calling-c-and-fortran-code.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,9 @@ ccall((:foo, "libfoo"), Void, (Int32, Float64),
255255
```
256256

257257
[`Base.cconvert()`](@ref) normally just calls [`convert()`](@ref), but can be defined to return an
258-
arbitrary new object more appropriate for passing to C. For example, this is used to convert an
259-
`Array` of objects (e.g. strings) to an array of pointers.
258+
arbitrary new object more appropriate for passing to C.
259+
This should be used to perform all allocations of memory that will be accessed by the C code.
260+
For example, this is used to convert an `Array` of objects (e.g. strings) to an array of pointers.
260261

261262
[`Base.unsafe_convert()`](@ref) handles conversion to `Ptr` types. It is considered unsafe because
262263
converting an object to a native pointer can hide the object from the garbage collector, causing

0 commit comments

Comments
 (0)