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

library global variables #961

Closed
nolta opened this issue Jun 22, 2012 · 5 comments
Closed

library global variables #961

nolta opened this issue Jun 22, 2012 · 5 comments

Comments

@nolta
Copy link
Member

nolta commented Jun 22, 2012

I'd like to add a function to access library global variables, e.g.,

x = cglobal(dlsym(lib,:var), Int32)

The following seems to work,

cglobal(p::Ptr, T::Type) = pointer_to_array(convert(Ptr{T}, p), (1,))[1]

but i'm opening this issue because there's probably a better way to do this.

@JeffBezanson
Copy link
Sponsor Member

Clever. While this may seem like a hack at first, maybe it's totally fine --- the interface to C variables can be creating a 1-element array that you read and write. I think cglobal should return that array, so it can be reused for each access.

@StefanKarpinski
Copy link
Sponsor Member

Yeah, that is very clever. Seems like a reasonable way to do this to me. The only issue is the interface to it — manipulating C globals via indexing with 1 seems pretty annoying.

@nolta
Copy link
Member Author

nolta commented Jun 29, 2012

Some interface options:

g = cglobal(ptr, typ)
x = g[1]
g[1] = y
g = cglobal(ptr, typ)
x = deref(g)
assign(g, y)
x = cglobal(ptr, typ)
cglobal(ptr, typ, y)

@StefanKarpinski
Copy link
Sponsor Member

Could also use a zero-dimensional array:

julia> g = Array(Int)
0-dimensional Int64 Array:
-4

julia> g[] = 1
1

Has the benefit that 0-d arrays always have a single unique value and can't be grown. Writing g[] and g[] = 2 doesn't seem terrible although it's not quite beautiful either.

@nolta
Copy link
Member Author

nolta commented Sep 13, 2012

Superseded by #1270.

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

3 participants