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

easier, clearer, faster ccall out args #2322

Closed
vtjnash opened this issue Feb 16, 2013 · 7 comments
Closed

easier, clearer, faster ccall out args #2322

vtjnash opened this issue Feb 16, 2013 · 7 comments

Comments

@vtjnash
Copy link
Sponsor Member

vtjnash commented Feb 16, 2013

ccall supports the following coding pattern. I propose adding this code to base/c.jl:

type ByRef{T} # T enforced to be any BitsKind
   p::T
   ByRef{S}(p::S) = (@assert isa(S,BitsKind); new(p))
end
ByRef{T}(::Type{T}) = ByRef{T}(zero(T))
typealias _out_{T} Ptr{ByRef{T}}

x = ByRef(Int)
ccall(:fname, Void, ( _out_{T}, ), &x)

edit: updated the example to use ByRef rather than OutArg

@vtjnash
Copy link
Sponsor Member Author

vtjnash commented Feb 17, 2013

@JeffBezanson thoughts?
note: OutArg could also be described as / named ByRef

@JeffBezanson
Copy link
Sponsor Member

I'm not sure heap-allocating an object to return an integer qualifies as fast! :)
It would be best to have some syntax for this that causes the space written to by the C function to be assigned back to x.

@vtjnash
Copy link
Sponsor Member Author

vtjnash commented Feb 17, 2013

compared to the current strategy of heap-allocating an array, it seems much faster to me to ask the gc for two words.
anyways, yes, ideally ByRef() would be dropped and the definition changed compatibly as follows:

ByRef{T}(::Type{T}) = zero(T)
typealias _out_{T} Ptr{T}

x = ByRef(Int)
ccall(:fname, Void, ( _out_{T}, ), &x)

which could also be written:

x::Int
ccall(:fname, Void, ( Ptr{Int}, ), &x)

@stevengj
Copy link
Member

+1 on having a syntax to cause the space written by the C function to be written back to x. Currently, this is probably the single biggest annoyance in ccall.

@vtjnash
Copy link
Sponsor Member Author

vtjnash commented Mar 7, 2015

implemented by 3387e77

@stevengj
Copy link
Member

stevengj commented Mar 7, 2015

Is it documented yet?

@vtjnash
Copy link
Sponsor Member Author

vtjnash commented Mar 7, 2015

most of the documentation is in #7906

@vtjnash vtjnash closed this as completed Mar 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants