Skip to content

Commit

Permalink
document cglobal (from issue #1815), and fix deprecated unsafe_ref/as…
Browse files Browse the repository at this point in the history
…sign -> unsafe_load/store! in the manual
  • Loading branch information
stevengj committed May 13, 2013
1 parent 3ee9803 commit f7e4d3d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
14 changes: 12 additions & 2 deletions doc/helpdb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5278,6 +5278,16 @@ RetType, (ArgType1, ...), ArgVar1, ...)
"),

("C Interface","Base","cglobal","cglobal((symbol, library) or ptr [, Type=Void])
Obtain a pointer to a global variable in a C-exported shared library,
specified exactly as in ``ccall``. Returns a ``Ptr{Type}``, defaulting
to ``Ptr{Void}`` if no Type argument is supplied. The values can be
read or written by ``unsafe_load`` or ``unsafe_store!``, respectively.
"),

("C Interface","Base","cfunction","cfunction(fun::Function,
RetType::Type, (ArgTypes...))
Expand Down Expand Up @@ -5349,15 +5359,15 @@ flags::Integer])
"),

("C Interface","Base","unsafe_ref","unsafe_ref(p::Ptr{T}, i::Integer)
("C Interface","Base","unsafe_load","unsafe_load(p::Ptr{T}, i::Integer)
Dereference the pointer \"p[i]\" or \"*p\", returning a copy of
type T.
"),

("C Interface","Base","unsafe_assign","unsafe_assign(p::Ptr{T}, x,
("C Interface","Base","unsafe_store!","unsafe_store!(p::Ptr{T}, x,
i::Integer)
Expand Down
6 changes: 3 additions & 3 deletions doc/manual/calling-c-and-fortran-code.rst
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ to terminate abruptly or corrupt arbitrary process memory due to a bad pointer
or type declaration.

Given a ``Ptr{T}``, the contents of type ``T`` can generally be copied from
the referenced memory into a Julia object using ``unsafe_ref(ptr, [index])``. The
the referenced memory into a Julia object using ``unsafe_load(ptr, [index])``. The
index argument is optional (default is 1), and performs 1-based indexing. This
function is intentionally similar to the behavior of ``getindex()`` and ``setindex!()``
(e.g. ``[]`` access syntax).
Expand All @@ -330,7 +330,7 @@ can be converted to ``jl_value_t*`` pointers, as ``Ptr{Void}``, by calling
``pointer_from_objref(v)``.)

The reverse operation (writing data to a Ptr{T}), can be performed using
``unsafe_assign(ptr, value, [index])``. Currently, this is only supported
``unsafe_store!(ptr, value, [index])``. Currently, this is only supported
for bitstypes or other pointer-free (``isbits``) immutable types.

Any operation that throws an error is probably currently unimplemented
Expand Down Expand Up @@ -358,7 +358,7 @@ it is finished with them.

Whenever you have created a pointer to Julia data, you must ensure the original data
exists until you are done with using the pointer. Many methods in Julia such as
``unsafe_ref()`` and ``bytestring()`` make copies of data instead of taking ownership
``unsafe_load()`` and ``bytestring()`` make copies of data instead of taking ownership
of the buffer, so that it is safe to free (or alter) the original data without
affecting Julia. A notable exception is ``pointer_to_array()`` which, for performance
reasons, shares (or can be told to take ownership of) the underlying buffer.
Expand Down
11 changes: 9 additions & 2 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3031,6 +3031,13 @@ C Interface

Call function in C-exported shared library, specified by (function name, library) tuple (String or :Symbol). Alternatively, ccall may be used to call a function pointer returned by dlsym, but note that this usage is generally discouraged to facilitate future static compilation.

.. function:: cglobal((symbol, library) or ptr [, Type=Void])

Obtain a pointer to a global variable in a C-exported shared library,
specified exactly as in ``ccall``. Returns a ``Ptr{Type}``, defaulting
to ``Ptr{Void}`` if no Type argument is supplied. The values can be
read or written by ``unsafe_load`` or ``unsafe_store!``, respectively.

.. function:: cfunction(fun::Function, RetType::Type, (ArgTypes...))

Generate C-callable function pointer from Julia function. Type annotation of the return value in the
Expand Down Expand Up @@ -3079,11 +3086,11 @@ C Interface

Call free() from C standard library.

.. function:: unsafe_ref(p::Ptr{T},i::Integer)
.. function:: unsafe_load(p::Ptr{T},i::Integer)

Dereference the pointer ``p[i]`` or ``*p``, returning a copy of type T.

.. function:: unsafe_assign(p::Ptr{T},x,i::Integer)
.. function:: unsafe_store!(p::Ptr{T},x,i::Integer)

Assign to the pointer ``p[i] = x`` or ``*p = x``, making a copy of object x into the memory at p.

Expand Down

0 comments on commit f7e4d3d

Please sign in to comment.