C Interface¶
ccall((symbol, library) or function_pointer, ReturnType, (ArgumentType1, ...), ArgumentValue1, ...)¶Call function in C-exported shared library, specified by
(functionname,library)tuple, where each component is a string or symbol.Note that the argument type tuple must be a literal tuple, and not a tuple-valued variable or expression. Alternatively,
ccallmay also be used to call a function pointer, such as one returned bydlsym.Each
ArgumentValueto theccallwill be converted to the correspondingArgumentType, by automatic insertion of calls tounsafe_convert(ArgumentType,cconvert(ArgumentType,ArgumentValue)). (See also the documentation for each of these functions for further details.) In most cases, this simply results in a call toconvert(ArgumentType,ArgumentValue).
cglobal((symbol, library)[, type=Void])¶Obtain a pointer to a global variable in a C-exported shared library, specified exactly as in
ccall. Returns aPtr{Type}, defaulting toPtr{Void}if no Type argument is supplied. The values can be read or written byunsafe_loadorunsafe_store!, respectively.
cfunction(function::Function, ReturnType::Type, (ArgumentTypes...))¶Generate C-callable function pointer from Julia function. Type annotation of the return value in the callback function is a must for situations where Julia cannot infer the return type automatically.
For example:
function foo()# bodyretval::Float64endbar=cfunction(foo,Float64,())
unsafe_convert(T, x)¶Convert
xto a value of typeTIn cases where
convertwould need to take a Julia object and turn it into aPtr, this function should be used to define and perform that conversion.Be careful to ensure that a Julia reference to
xexists as long as the result of this function will be used. Accordingly, the argumentxto this function should never be an expression, only a variable name or field reference. For example,x=a.b.cis acceptable, butx=[a,b,c]is not.The
unsafeprefix on this function indicates that using the result of this function after thexargument to this function is no longer accessible to the program may cause undefined behavior, including program corruption or segfaults, at any later time.
cconvert(T, x)¶Convert
xto a value of typeT, typically by callingconvert(T,x)In cases where
xcannot be safely converted toT, unlikeconvert,cconvertmay return an object of a type different fromT, which however is suitable forunsafe_convertto handle.Neither
convertnorcconvertshould take a Julia object and turn it into aPtr.
unsafe_load(p::Ptr{T}, i::Integer)¶Load a value of type
Tfrom the address of the ith element (1-indexed) starting atp. This is equivalent to the C expressionp[i-1].The
unsafeprefix on this function indicates that no validation is performed on the pointerpto ensure that it is valid. Incorrect usage may segfault your program or return garbage answers, in the same manner as C.
unsafe_store!(p::Ptr{T}, x, i::Integer)¶Store a value of type
Tto the address of the ith element (1-indexed) starting atp. This is equivalent to the C expressionp[i-1]=x.The
unsafeprefix on this function indicates that no validation is performed on the pointerpto ensure that it is valid. Incorrect usage may corrupt or segfault your program, in the same manner as C.
unsafe_copy!(dest::Ptr{T}, src::Ptr{T}, N)¶Copy
Nelements from a source pointer to a destination, with no checking. The size of an element is determined by the type of the pointers.The
unsafeprefix on this function indicates that no validation is performed on the pointersdestandsrcto ensure that they are valid. Incorrect usage may corrupt or segfault your program, in the same manner as C.
unsafe_copy!(dest::Array, do, src::Array, so, N)Copy
Nelements from a source array to a destination, starting at offsetsoin the source anddoin the destination (1-indexed).The
unsafeprefix on this function indicates that no validation is performed to ensure that N is inbounds on either array. Incorrect usage may corrupt or segfault your program, in the same manner as C.
copy!(dest, src)¶Copy all elements from collection
srcto arraydest. Returnsdest.
copy!(dest, do, src, so, N)Copy
Nelements from collectionsrcstarting at offsetso, to arraydeststarting at offsetdo. Returnsdest.
pointer(array[, index])¶Get the native address of an array or string element. Be careful to ensure that a Julia reference to
aexists as long as this pointer will be used. This function is “unsafe” likeunsafe_convert.Calling
Ref(array[,index])is generally preferable to this function.
pointer_to_array(pointer, dims[, take_ownership::Bool])¶Wrap a native pointer as a Julia Array object. The pointer element type determines the array element type.
ownoptionally specifies whether Julia should take ownership of the memory, callingfreeon the pointer when the array is no longer referenced.
pointer_from_objref(object_instance)¶Get the memory address of a Julia object as a
Ptr. The existence of the resultingPtrwill not protect the object from garbage collection, so you must ensure that the object remains referenced for the whole time that thePtrwill be used.
unsafe_pointer_to_objref(p::Ptr)¶Convert a
Ptrto an object reference. Assumes the pointer refers to a valid heap-allocated Julia object. If this is not the case, undefined behavior results, hence this function is considered “unsafe” and should be used with care.
disable_sigint(f::Function)¶Disable Ctrl-C handler during execution of a function, for calling external code that is not interrupt safe. Intended to be called using
doblock syntax as follows:disable_sigint()do# interrupt-unsafe code...end
reenable_sigint(f::Function)¶Re-enable Ctrl-C handler during execution of a function. Temporarily reverses the effect of
disable_sigint.
systemerror(sysfunc, iftrue)¶Raises a
SystemErrorforerrnowith the descriptive stringsysfuncififtrueistrue
Ptr{T}¶A memory address referring to data of type
T. However, there is no guarantee that the memory is actually valid, or that it actually represents data of the specified type.
Ref{T}¶An object that safely references data of type
T. This type is guaranteed to point to valid, Julia-allocated memory of the correct type. The underlying data is protected from freeing by the garbage collector as long as theRefitself is referenced.When passed as a
ccallargument (either as aPtrorReftype), aRefobject will be converted to a native pointer to the data it references.There is no invalid (NULL)
Ref.
Cchar¶Equivalent to the native
charc-type
Cuchar¶Equivalent to the native
unsignedcharc-type (UInt8)
Cshort¶Equivalent to the native
signedshortc-type (Int16)
Cushort¶Equivalent to the native
unsignedshortc-type (UInt16)
Cint¶Equivalent to the native
signedintc-type (Int32)
Cuint¶Equivalent to the native
unsignedintc-type (UInt32)
Clong¶Equivalent to the native
signedlongc-type
Culong¶Equivalent to the native
unsignedlongc-type
Clonglong¶Equivalent to the native
signedlonglongc-type (Int64)
Culonglong¶Equivalent to the native
unsignedlonglongc-type (UInt64)
Cintmax_t¶Equivalent to the native
intmax_tc-type (Int64)
Cuintmax_t¶Equivalent to the native
uintmax_tc-type (UInt64)
Csize_t¶Equivalent to the native
size_tc-type (UInt)
Cssize_t¶Equivalent to the native
ssize_tc-type
Cptrdiff_t¶Equivalent to the native
ptrdiff_tc-type (Int)
Coff_t¶Equivalent to the native
off_tc-type
Cwchar_t¶Equivalent to the native
wchar_tc-type (Int32)
Cfloat¶Equivalent to the native
floatc-type (Float32)
Cdouble¶Equivalent to the native
doublec-type (Float64)
LLVM Interface¶
llvmcall(IR::String, ReturnType, (ArgumentType1, ...), ArgumentValue1, ...)¶Call LLVM IR string in the first argument. Similar to an LLVM function
defineblock, arguments are available as consecutive unnamed SSA variables (%0, %1, etc.).Note that the argument type tuple must be a literal tuple, and not a tuple-valued variable or expression.
Each
ArgumentValuetollvmcallwill be converted to the correspondingArgumentType, by automatic insertion of calls tounsafe_convert(ArgumentType,cconvert(ArgumentType,ArgumentValue)). (see also the documentation for each of these functions for further details). In most cases, this simply results in a call toconvert(ArgumentType,ArgumentValue).See
test/llvmcall.jlfor usage examples.