Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re^3: Inline::C and NULL pointersby syphilis (Archbishop) |
on Dec 19, 2021 at 21:42 UTC ( [id://11139743]=note: print w/replies, xml ) | Need Help?? |
but I need to pass "NULL" pointers to the functions How to do that is an interesting puzzle. I've been playing with this little Inline::C demo: The question is: "How can I call foo() directly from perl such that it will output "False" ? One way is to call bar() which then calls foo() - and I think that the OP's existing enctypex_msname function could be accessed (as is) via this technique of calling a wrapper function . But that's still not a direct call to foo(). AFAICT, the issue is the typemapping of "unsigned char *" (which can be found in perl's lib/ExtUtils/typemap file). This default typemapping types "unsigned char *" to T_PV, but if we change that to T_PTR, then calling foo(undef) in my little demo will have it output "False" as desired. (I expect this is what SWIG, in effect, does.) Inline::C allows us to use our own typemaps, so I wrote an alternative typemap for unsigned char * and I placed that file (named "nullmap.txt") in the same directory as the script, and pointed the script to it (in the inline configuration section). Unfortunately, the default typemapping was still used. So ... as proof of concept, I replaced "unsigned char *" in nullmap.txt with "unmapped". It looks like this: Then, in the script, I typedeffed the "unsigned char *" to "unmapped", and replaced "unsigned char *" with "unmapped" in the function declaration: So we have 2 essentially identical scripts that output different results. One of them specifies an "unmapped" type where the other specifies "unsigned char *" - yet the two types are the same thing. The difference occurs because the two types employ different typemapping. Note that it should not be necessary to replace *all* occurrences of "unsigned char *" with "unmapped" (or whatever replacement name is chosen) - just renaming those occurrences found in the declaration would be all that's needed. And then there's the question of whether both the return type and the argument type should be rewritten to "unmapped". (Perhaps it's only the argument type that needs to be renamed.) Also, when fiddling around with Inline scripts, we need to remember that a change to the Inline Config section will not trigger a recompilation of the C code unless that Config section specifies FORCE_BUILD => 1. Otherwise, it's necessary to alter the actual code section. I'm surprised that specifying an alternative Inline::C typemap doesn't override the default typemap for any types that are specified in both. Is that an Inline::C bug ? Or a design flaw ? Maybe it's the way it has to be. Cheers, Rob
In Section
Seekers of Perl Wisdom
|
|