4 ms·
Nim compiles to c, hence you can just call c functions. All you need is a signature, which can even be generated automatically from a c header. You can manuall
by pathsjs 10y ago
Nim compiles to c, hence you can just call c functions. All you need is a signature, which can even be generated automatically from a c header.
You can manually allocate memory if you want, and you can also pass pointers, either to manually allocated or gc memory. The gc will not run when c is working, since it is triggered by allocation
- jjnoakes 10y agoSo if I call from Nim to C (passing a gc'd Nim-owned pointer), then that C routine calls back into Nim (via a function pointer, or some other way through the FFI), the nested Nim routine may trigger gc and wipe out the pointer my C code is working with?
- dom96 10y agoYou can instruct the Nim GC to not do that using GC_ref (http://nim-lang.org/docs/system.html#GC_ref,string http://nim-lang.org/docs/system.html#GC_ref,string).
- jjnoakes 10y agoThat's what I figured. So not "trivial".
- dom96 10y agoAre you aware of any language's whose FFI is that trivial?
- jjnoakes 10y agoDo I have to be aware of some other language with such a trivial FFI in order to question the claim that Nim has such a trivial FFI? (C++ has such a trivial FFI, as well as non-GC languages which compile to C code... there may be more as well)
- dom96 10y agoSorry, it wasn't my intention to imply that. I'm genuinely curious, I haven't used many language's FFI to know which is best. You mention non-GC languages. Are there any GC languages that do? :)
- jjnoakes 10y agoI don't think so. I think by virtue of having a GC language you pretty much have to manually anchor pointers before calling code which knows nothing of the GC. But if anyone knows of counter examples, I would be interested in them as well.
- klibertp 10y ago> you pretty much have to manually anchor pointers before calling code which knows nothing of the GC. Which I'd still consider trivial...
- jjnoakes 10y agoI wouldn't. Trivial to me means a call to a C routine looks and feels like a call to a native routine. Manually managing the memory that the GC "owns" doesn't fit the bill in my definition.
- nickpsecurity 10y agoThe example for Go looks pretty straight-forward: https://golang.org/cmd/cgo/ https://golang.org/cmd/cgo/ Not an expert on these things by far but I remembered painless use of C in that language and it's GC'd. Thoughts?
- kibwen 10y agoThere are various restrictions on passing pointers between Go and C, see the "Passing pointers" section of your link.
- pathsjs 10y agoYou are right, this situation is not trivial. But it is also quite rare in practice
- pathsjs 10y agoThe situation that you describe could happen, but it is quite rare in practice. Usually, I call c functions that do their work and be done, such as BLAS. For such cases, you can either manually allocate the pointers you pass to c, or temporarily disable the gc. Each thread has its own heap, so gc for one thread does not break anything in other threads
- jjnoakes 10y agoIt's just those "quite rare in practice" bugs that I'm concerned about. Those tend to cost the most.