From: ramza2
Subject: FFI and function pointers
Date: 
Message-ID: <1102976743.398731.165890@c13g2000cwb.googlegroups.com>
is it possible to represent pointers to function using FFI in a struct.
(ffi:def-c-struct lisp-func
   (???? the-function-pointer))

From: Edi Weitz
Subject: Re: FFI and function pointers
Date: 
Message-ID: <u4qipq0tx.fsf@agharta.de>
On 13 Dec 2004 14:25:43 -0800, "ramza2" <············@gmail.com> wrote:

> is it possible to represent pointers to function using FFI in a struct.
> (ffi:def-c-struct lisp-func
>    (???? the-function-pointer))

You don't say which FFI (i.e. which Lisp) you are using. But why don't
you just use a pointer to void?

Edi.

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: ramza2
Subject: Re: FFI and function pointers
Date: 
Message-ID: <1102980018.078292.48320@f14g2000cwb.googlegroups.com>
I am using CLisp(most recent version) win32
From: Ken Dyck
Subject: Re: FFI and function pointers
Date: 
Message-ID: <1103027958.731044.73840@f14g2000cwb.googlegroups.com>
> But why don't you just use a pointer to void?

It is my understanding that casting a function pointer to a void
pointer results in undefined behaviour according to the C standard.
Void pointers may be cast to object pointers and back; function
pointers may be cast to other function pointers; but function pointers
and void pointers apparently don't mix.

I refer you to a thread on comp.lang.c entitled "Casting function
pointer to void*" (circa July 2001).

Ken
From: Edi Weitz
Subject: Re: FFI and function pointers
Date: 
Message-ID: <ur7ltroht.fsf@agharta.de>
On 14 Dec 2004 04:39:18 -0800, "Ken Dyck" <······@gmail.com> wrote:

> It is my understanding that casting a function pointer to a void
> pointer results in undefined behaviour according to the C standard.
> Void pointers may be cast to object pointers and back; function
> pointers may be cast to other function pointers; but function
> pointers and void pointers apparently don't mix.
>
> I refer you to a thread on comp.lang.c entitled "Casting function
> pointer to void*" (circa July 2001).

But that's not relevant for this discussion because the foreign
function interface of a Lisp that compiles to native code doesn't
create C code - it creates machine code to interface with foreign code
and for that it only needs to know the size of the pointer to compute
the correct offset into the struct, and the size of a function pointer
and a void pointer are the same.

I didn't ask the OP to change his C code.

Cheers,
Edi.

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Ken Dyck
Subject: Re: FFI and function pointers
Date: 
Message-ID: <1103034843.609562.184640@f14g2000cwb.googlegroups.com>
>But that's not relevant for this discussion because the foreign
>function interface of a Lisp that compiles to native code doesn't
>create C code - it creates machine code to interface with foreign code
>and for that it only needs to know the size of the pointer to compute
>the correct offset into the struct, and the size of a function pointer
>and a void pointer are the same.

I think it is relevant. As I understand it, one of the reasons that the
C standard does not define conversion between void and function
pointers is to allow them to differ in size. On a platform where they
aren't the same size, using a void pointer to store a function pointer
can result in an erroneous structure layout.

Ken
From: Edi Weitz
Subject: Re: FFI and function pointers
Date: 
Message-ID: <u7jnkewt8.fsf@agharta.de>
On 14 Dec 2004 06:34:03 -0800, "Ken Dyck" <······@gmail.com> wrote:

> I think it is relevant. As I understand it, one of the reasons that
> the C standard does not define conversion between void and function
> pointers is to allow them to differ in size. On a platform where
> they aren't the same size, using a void pointer to store a function
> pointer can result in an erroneous structure layout.

OK, you might have a point here.  But is there really a platform out
there which /has/ different sizes for these two kinds of pointers?
And does a Common Lisp implementation with a foreign function
interface exist for this platform?  If not, then it isn't relevant, as
I said... :)

Edi.

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Cameron MacKinnon
Subject: Re: FFI and function pointers
Date: 
Message-ID: <x7ydnSn3zKzFqSLcRVn-tw@golden.net>
Edi Weitz wrote:
> On 14 Dec 2004 06:34:03 -0800, "Ken Dyck" <······@gmail.com> wrote:
> 
> 
>>I think it is relevant. As I understand it, one of the reasons that
>>the C standard does not define conversion between void and function
>>pointers is to allow them to differ in size. On a platform where
>>they aren't the same size, using a void pointer to store a function
>>pointer can result in an erroneous structure layout.
> 
> 
> OK, you might have a point here.  But is there really a platform out
> there which /has/ different sizes for these two kinds of pointers?
> And does a Common Lisp implementation with a foreign function
> interface exist for this platform?  If not, then it isn't relevant, as
> I said... :)

IBM's AS/400 might fall into this category (of machines where pointers 
can't be cast between code and data). Think about how many tag bits 
might hide in a sixteen byte pointer! Validating the C compiler on that 
platform was more fun than a barrel full of type check exceptions.
From: Friedrich Dominicus
Subject: Re: FFI and function pointers
Date: 
Message-ID: <87sm69kky1.fsf@fbigm.here>
"Ken Dyck" <······@gmail.com> writes:

>> But why don't you just use a pointer to void?
>
> It is my understanding that casting a function pointer to a void
> pointer results in undefined behaviour according to the C standard.
> Void pointers may be cast to object pointers and back; function
> pointers may be cast to other function pointers; but function pointers
> and void pointers apparently don't mix.
I can't imagine that this was written. The standards says:
| J.5.7 Function pointer casts 
| 1 A pointer to an object or to void may
| be cast to a pointer to a function, allowing data to be invoked as a
| function (6.5.4). 

Regards
Friedrich


-- 
Please remove just-for-news- to reply via e-mail.
From: Ken Dyck
Subject: Re: FFI and function pointers
Date: 
Message-ID: <1103033786.211248.117580@f14g2000cwb.googlegroups.com>
>I can't imagine that this was written. The standards says:
> ...

The paragraph you are quoting comes from the section J.5, Common
Extensions. There is no guarantee from the standard that a particular C
compiler supports this extension.

Ken