Passing a void** to C

已查看 158 次
跳至第一个未读帖子

Dominique Orban

未读,
2015年10月8日 21:36:042015/10/8
收件人 julia-users
I'm trying to call the C interface to the HSL MA97 from Julia. It's a symmetric indefinite factorization library that uses OpenMP. One of the main C functions expects a void** as argument. The example that ships with the library declares a void* variable named akeep. The main program doesn't do anything with it except that it passes &akeep (i.e., a void**) to some of the library functions. Presumably, those functions allocate memory pointed to by akeep. It's a bit perplexing, but that's how it is. At the end, there's a call of the form free(&akeep).

My question is: what syntax should I use in Julia to perform the same operations? Naively, I'm tempted to try something like

akeep = Ptr{Void}
ccall((:some_function, "libwhatever"), Void, (Ptr{Void},), akeep)

but that returns the error message: `convert` has no method matching convert(::Type{Ptr{None}}, ::Type{Ptr{None}}).

Replacing akeep with &akeep in the ccall returns: expected Ptr{None}, got Type{Ptr{None}}.

What might be the appropriate syntax here? And more importantly, is this a recipe for a segfault?'

This is with Julia 0.3.11.

Thanks!

Isaiah Norton

未读,
2015年10月8日 22:08:332015/10/8
收件人 julia...@googlegroups.com
akeep = Ptr{Void}

This is declaring a "DataType" variable (try `typeof(Ptr)` to see what I mean). You need to construct an instance, so try: `akeep = Ptr{Ptr{Void}}(0)`

Then the ccall signature should be `(Ptr{Ptr{Void}},)`.

Steven G. Johnson

未读,
2015年10月8日 23:58:442015/10/8
收件人 julia-users


On Thursday, October 8, 2015 at 10:08:33 PM UTC-4, Isaiah wrote:
akeep = Ptr{Void}

This is declaring a "DataType" variable (try `typeof(Ptr)` to see what I mean). You need to construct an instance, so try: `akeep = Ptr{Ptr{Void}}(0)`

Then the ccall signature should be `(Ptr{Ptr{Void}},)`.

Tony Kelman

未读,
2015年10月9日 05:55:362015/10/9
收件人 julia-users
I hope you're willing/able to publish this as a package when it's ready. An HSLSparse.jl would be a great addition to JuliaSparse!

Dominique Orban

未读,
2015年10月9日 07:03:512015/10/9
收件人 julia-users
Thanks for the responses! No luck so far though:

julia> akeep = Ptr{Ptr{Void}}(0)
ERROR: type cannot be constructed

I changed the signature of the ccall to Ptr{Ptr{Void}}, but declaring akeep=Ptr{Void} and passing &akeep yields: ERROR: `convert` has no method matching convert(::Type{Ptr{None}}, ::Type{Ptr{None}})

Declaring akeep=Ptr{Ptr{Void}} and passing akeep directly yields: ERROR: `convert` has no method matching convert(::Type{Ptr{Ptr{None}}}, ::Type{Ptr{Ptr{None}}})

I don't understand what there is to convert. The two types mentioned in either error message are the same...

Haven't tried 0.4 yet.

@tkelman, I'll be very happy to release this when/if I can make it work.

On Thursday, October 8, 2015 at 10:08:33 PM UTC-4, Isaiah wrote:

Tony Kelman

未读,
2015年10月9日 07:11:352015/10/9
收件人 julia-users
On 0.3, try convert(Ptr{Ptr{Void}}, 0)

Dominique Orban

未读,
2015年10月10日 10:37:442015/10/10
收件人 julia-users
Thanks. It's all working now.

Dominique Orban

未读,
2015年10月12日 12:26:072015/10/12
收件人 julia-users
回复全部
回复作者
转发
0 个新帖子