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

types as C-structs #2818

Closed
wants to merge 1 commit into from
Closed

types as C-structs #2818

wants to merge 1 commit into from

Conversation

vtjnash
Copy link
Member

@vtjnash vtjnash commented Apr 10, 2013

This is a first pass at giving pointers to julia types easy compatibility with C. This is done by "hiding" the type and GC information before the data, and shifting all references in the julia source by 1 to match. What this would mean for the user is that

type A
  a::Int
end
type B
  a::A
end

can be passed to a C function expecting:

typedef struct _A {
  int a;
} A;
typedef struct _B {
  *A a;
} B;

If the user is receiving data from C (e.g. anything not created by Julia's GC), then they must instead declare it as Ptr{A} and access with unsafe_ref (I'm wondering if it might be more appropriate to introduce another type for this purpose, but thats a separate issue I'll go into later).

If @JeffBezanson approves of this, I'll go back through and clean this up some to make it ready to merge. TODO items include:

  • remove some casts that are now no-ops
  • figure out what emit_arraysize is doing (and replace with offsetof for better maintainability)
  • finish fixing OVERLAP_TUPLE_LEN
  • stop over-allocating types by sizeof(void*) bytes in alloc_big, and check if there's double counting anywhere else (or conversely, fully move it to alloc_big)
  • document how to use & how to avoid getting burned

@StefanKarpinski
Copy link
Member

I like it. We'll need some clear documentation on what becomes a pointer and what is just a field, but that's perfectly doable.

@stevengj
Copy link
Member

I agree that this would be a nice feature. In PyCall, for example, I needed precisely this feature of inserting a pointer to one structure in another structure, and I needed to use some hacks since & only works in ccall. Making jl_value_t* pointers compatible with C struct pointers (by offsetting the pointer) is a clean way to eliminate this sort of difficulty.

@StefanKarpinski
Copy link
Member

@JeffBezanson: can you please review? I think that's the hold up here.

@vtjnash
Copy link
Member Author

vtjnash commented Apr 27, 2013

@JeffBezanson Yes, I'm waiting for your input before I finish this. I'm on IRC too if you prefer to discuss there.

@JeffBezanson
Copy link
Member

Yes, I certainly approve the concept. Please continue fixing and ping me again when ready.

@StefanKarpinski
Copy link
Member

How's this going?

@vtjnash
Copy link
Member Author

vtjnash commented May 9, 2013

It's being held up by a thesis.

@StefanKarpinski
Copy link
Member

Boo! :-P

@stevengj
Copy link
Member

Any progress on this?

@Keno
Copy link
Member

Keno commented Aug 11, 2013

Depends on what you want. #3466 does some of it.

@vtjnash
Copy link
Member Author

vtjnash commented Aug 11, 2013

I'm waiting for after 0.2 gets tagged to merge both

@stevengj
Copy link
Member

@loladiro, I was specifically wondering about the capability to treat a jl_value_t * as a pointer to a C struct (by hiding the Julia stuff before the pointer address).

@JeffBezanson
Copy link
Member

Bump.

@vtjnash vtjnash mentioned this pull request Jan 29, 2014
@stevengj
Copy link
Member

Is this waiting on #5227?

@vtjnash
Copy link
Member Author

vtjnash commented Apr 21, 2014

Yes

@ViralBShah
Copy link
Member

As already said by many before, this capability would be really amazing to have.

@vtjnash
Copy link
Member Author

vtjnash commented Feb 13, 2015

almost done getting this recreated against my jn/ccall3 branch, so it should all be ready-to-merge at the same time: 58d3033

@vtjnash vtjnash changed the title WIP: types as C-structs types as C-structs Mar 14, 2015
@vtjnash
Copy link
Member Author

vtjnash commented Mar 14, 2015

it's amazing how much our usage of travis has changed since this PR was first submitted: https://travis-ci.org/JuliaLang/julia/builds/6230711

this is likely now ready to merge, pending any findings from CI or code review

@IainNZ
Copy link
Member

IainNZ commented Mar 14, 2015

Seems to have segfaulted building system image on 32bit linux?

@vtjnash
Copy link
Member Author

vtjnash commented Mar 14, 2015

yes, although, strangely, on windows it did the reverse (the i686 build succeeded and x86-64 failed). the llvm verifier locally pointed out my invalid pointer -> int64 cast though.

this makes references to types allocated in Julia recursively forward compatible with C structs
@JeffBezanson
Copy link
Member

moved to #10579

@vtjnash vtjnash deleted the jn/recurse_types branch August 27, 2015 04:23
kmsquire added a commit that referenced this pull request Aug 29, 2015
* Formatting changes/fixes
* Small wording updates
* Removed references to #2818
kmsquire added a commit that referenced this pull request Aug 29, 2015
* Formatting changes/fixes
* Small wording updates
* Removed references to #2818
kmsquire added a commit that referenced this pull request Aug 29, 2015
* Formatting changes/fixes
* Small wording updates
* Removed references to #2818
kmsquire added a commit that referenced this pull request Aug 29, 2015
* Formatting changes/fixes
* Small wording updates
* Removed references to #2818
jiahao added a commit that referenced this pull request Feb 4, 2016
- Remove references to #2818

- Explain when to use `Ptr{T}` and when to use `Ref{T}` correctly.
  `Ptr` is generally used for return types and fields of types mirroring
  C structs. `Ref` is generally used for input types, allowing memory
  managed by either C or Julia to be referenced by `ccall`.

- Provide some examples of C wrappers simplified from GSL.jl

- Other minor formatting change

Ref: JuliaMath/GSL.jl#43
jiahao added a commit that referenced this pull request Feb 4, 2016
- Remove references to #2818

- Explain when to use `Ptr{T}` and when to use `Ref{T}` correctly.
  `Ptr` is generally used for return types and fields of types mirroring
  C structs. `Ref` is generally used for input types, allowing memory
  managed by either C or Julia to be referenced by `ccall`.

- Provide some examples of C wrappers simplified from GSL.jl

- Other minor formatting change

Ref: JuliaMath/GSL.jl#43
jiahao added a commit that referenced this pull request Feb 4, 2016
- Remove references to #2818

- Explain when to use `Ptr{T}` and when to use `Ref{T}` correctly.
  `Ptr` is generally used for return types and fields of types mirroring
  C structs. `Ref` is generally used for input types, allowing memory
  managed by either C or Julia to be referenced by `ccall`.

- Provide some examples of C wrappers simplified from GSL.jl

- Fix description of `cconvert` in the Auto-conversion section

- Better cross-referencing to the standard library

- Other minor formatting changes

Ref: JuliaMath/GSL.jl#43
jiahao added a commit that referenced this pull request Feb 4, 2016
- Remove references to #2818

- Explain when to use `Ptr{T}` and when to use `Ref{T}` correctly.
  `Ptr` is generally used for return types and fields of types mirroring
  C structs. `Ref` is generally used for input types, allowing memory
  managed by either C or Julia to be referenced by `ccall`.

- Provide some examples of C wrappers simplified from GSL.jl, with
  comments delineating the various parts of the `ccall`.

- Fix description of `cconvert` in the Auto-conversion section

- Better cross-referencing to the standard library

- Other minor formatting changes

Ref: JuliaMath/GSL.jl#43
staticfloat added a commit that referenced this pull request May 1, 2016
tkelman added a commit that referenced this pull request May 5, 2016
Remove mention of issue #2818 from 0.4 manual
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

Successfully merging this pull request may close these issues.

7 participants