Passing an array to C in RFFI
Hi all, Thanks for the help previously. I have another question regarding passing an array value to C function using RFFI. Suppose I have a function in C that’s like this: void fn(void** array_of_voips, int length) How do I in RPython call the function using RFFI passing an array value for it? it seems that I can’t just do fn([a, b, c], 3) Help appreciated. John Zhang ------------------------------------------------------ John Zhang Research Assistant Programming Languages, Design & Implementation Division Computer Systems Group ANU College of Engineering & Computer Science 108 North Rd The Australian National University Acton ACT 2601 john.zhang@anu.edu.au<mailto:john.zhang@anu.edu.au>
Hi John, On 28 June 2016 at 09:23, John Zhang <John.Zhang@anu.edu.au> wrote:
I have another question regarding passing an array value to C function using RFFI. Suppose I have a function in C that’s like this: void fn(void** array_of_voips, int length) How do I in RPython call the function using RFFI passing an array value for it? it seems that I can’t just do fn([a, b, c], 3)
Indeed, this works in CFFI, but RFFI is by far not as nice as that. You need to build the array value manually with "ar=lltype.new(ARRAY_OF_VOIDP.TO, length, flavor='raw'); ar[0]=a; " etc., remembering to do "lltype.free(ar, flavor='raw')" at the end. Or use "with lltype.scoped_alloc(...)", which replaces both the malloc at the start and the free at the end. A bientôt, Armin.
Hi Armin, Thanks for the help! The scoped_alloc really is quite handy in these situations. Cheers, John Zhang ------------------------------------------------------ John Zhang Research Assistant Programming Languages, Design & Implementation Division Computer Systems Group ANU College of Engineering & Computer Science 108 North Rd The Australian National University Acton ACT 2601 john.zhang@anu.edu.au<mailto:john.zhang@anu.edu.au> On 30 Jun 2016, at 16:05, Armin Rigo <arigo@tunes.org<mailto:arigo@tunes.org>> wrote: Hi John, On 28 June 2016 at 09:23, John Zhang <John.Zhang@anu.edu.au<mailto:John.Zhang@anu.edu.au>> wrote: I have another question regarding passing an array value to C function using RFFI. Suppose I have a function in C that’s like this: void fn(void** array_of_voips, int length) How do I in RPython call the function using RFFI passing an array value for it? it seems that I can’t just do fn([a, b, c], 3) Indeed, this works in CFFI, but RFFI is by far not as nice as that. You need to build the array value manually with "ar=lltype.new(ARRAY_OF_VOIDP.TO, length, flavor='raw'); ar[0]=a; " etc., remembering to do "lltype.free(ar, flavor='raw')" at the end. Or use "with lltype.scoped_alloc(...)", which replaces both the malloc at the start and the free at the end. A bientôt, Armin.
participants (2)
-
Armin Rigo
-
John Zhang