freenode/lisp - IRC Chatlog
Search
16:44:25
jmercouris
when I use cffi:callback to pass a callback to some C code, how can I store that in the C code? what is it a void *?
16:45:36
pjb
jmercouris: in any case, the cffi declaration of the C function, and the C function itself don't need to have the same type of parameters.
16:45:42
jackdaniel
pjb: in C standard casting function pointers to data pointers is undefined behavior
16:48:37
_death
so, struct { void (*fp)(void); } stuff; void store(void (*fp)(void)) { stuff.fp = fp; }
16:49:07
jackdaniel
jmercouris: please read this piece https://www.cprogramming.com/tutorial/function-pointers.html, I think that discussing C pointers is not very ontopic
16:49:28
Bike
function pointers and data pointers aren't interchangeable but you can use function pointers fine. declare a global variable with function pointer type, put it in a struct, whatever
16:50:01
_death
you'd also want to take a context pointer, i.e. some void pointer to store as well and pass along when calling the function pointer
16:50:08
pjb
jackdaniel: I see only a prevention of casting (void*) to float, in 6.5.4 of ISO/IEC 9899:201x Committee Draft — April 12, 2011 N1570
16:53:29
TMA
pjb: you need to consider the whole standard; undefined behavior need not be explicitely specified, the necessary condition is that the standard does not explicitely or implicitely specify it
16:54:17
pjb
TMA: yes, but then the C standard doesn't specify a Turing Complete language either. So we could stop there.
16:54:46
pjb
TMA: but if it's not formally forbidden, and all C compilers do the right thing, where's the objection?
16:55:30
TMA
pjb: embedded platforms can have function pointers bigger or smaller than data pointers
16:56:54
jackdaniel
pjb: in 6.3.2.3 (same document), pointes 7. and 8.; there is no specification about casting between object and function pointers
17:00:12
jackdaniel
it is certainly nice that it "usually works", but I think that there are architectures where normal data pointer is insufficient to store function address
17:00:26
pjb
Ok, so it could work or not, it's implementation dependent. So we'd need to cast to something like int(*)() or void(*)() perhaps. typedef void(*function_ptr)();
17:02:27
pjb
Anyways, my point was that the CFFI declaration of _death's store function above doesn't need to use the type of void(*fp)(void) as argument, but can take the cffi:callback type.
17:03:22
pjb
Said otherwise, FFIs don't validate the function signatures across languages. If it could, we wouldn't have to declare the C functions in lisp!
17:13:47
jmercouris
not to fuel the off-topic discussion, but I read somewhere that it is apparently a posix specification that it should work
17:56:15
flip214
_Ark_: SBCL shows the CPU cycles in (time), (sb-ext:call-with-timing) can give that without parsing text, and then there's (sb-impl::read-cycle-counter) ...
18:30:16
jackdaniel
that is correct, dlsym returns a void pointer, it is briefly discussed here https://en.wikipedia.org/wiki/Dynamic_loading
3:55:25
ihals
I need some help with EVAL... How can I use variable bindings that I already have, within the *calc-input* string? (let ((form (read-from-string *calc-input*)))