=head1 NAME

perlcall - Perl calling conventions from C

=head1 DESCRIPTION

The purpose of this document is to show you how to write I<callbacks>, 
i.e. how to call Perl from C. The main
focus is on how to interface back to Perl from a bit of C code that has itself
been run by Perl, i.e. the 'main' program is a Perl script; you are using it
to execute
a section of code written in C; that bit of C code wants you to do something
with a particular event, so you want a Perl sub to be executed whenever it
happens.

Examples where this is necessary include

=over 5

=item * 

You have created an XSUB interface to an application's C API.

A fairly common feature in applications is to allow you to define a C
function that will get called whenever something nasty occurs.
What we would like is for a Perl sub to be called instead.

=item *

The classic example of where callbacks are used is in an event driven program 
like for X-windows.
In this case your register functions to be called whenever a specific events
occur, e.g. a mouse button is pressed.

=back

Although the techniques described are applicable to embedding Perl
in a C program, this is not the primary goal of this document. For details
on embedding Perl in C refer to L<perlembed> (currently unwritten).

Before you launch yourself head first into the rest of this document, it would 
be a good idea to have read the following two documents - L<perlapi> and L<perlguts>.

This stuff is easier to explain using examples. But first here are a few
definitions anyway.

=head2 Definitions

Perl has a number of C functions which allow you to call Perl subs. They are

    I32 perl_call_sv(SV* sv, I32 flags) ;
    I32 perl_call_pv(char *subname, I32 flags) ;
    I32 perl_call_method(char *methname, I32 flags) ;
    I32 perl_call_argv(char *subname, I32 flags, register char **argv) ;

The key function is I<perl_call_sv>. All the other functions make use of
I<perl_call_sv> to do what they do.

I<perl_call_sv> takes two parameters, the first is an SV*. This allows you to 
specify the Perl sub to be called either as a C string (which has first been 
converted to an SV) or a reference to a 
sub. Example 7, shows you how you can make use of I<perl_call_sv>.
The second parameter, C<flags>, is a general purpose option command. 
This parameter is common to all the I<perl_call_*> functions. 
It is discussed in the next section.

The function, I<perl_call_pv>, is similar as I<perl_call_sv> except it 
expects it's first parameter to be a C char* which identifies the Perl 
sub you want to call, e.g. C<perl_call_pv("fred", 0)>.

The function I<perl_call_method> expects its first argument to contain a 
blessed reference to a class. Using that reference it looks up and calls C<methname> 
from that class. See example 9.

I<perl_call_argv> calls the Perl sub specified by the C<subname> parameter. 
It also takes the usual C<flags> parameter. 
The final parameter, C<argv>, consists of a 
list of C strings to be sent to the Perl sub. See example 8.

All the functions return a number. This is a count of the number of items
returned by the Perl sub on the stack.

As a general rule you should I<always> check the return value from these 
functions.
Even if you are only expecting a particular number of values to be returned 
from the Perl sub, there is nothing to stop someone from doing something
unexpected - don't say you haven't been warned.

=head2 Flag Values

The C<flags> parameter in all the I<perl_call_*> functions consists of any 
combination of the symbols defined below, OR'ed together.

=over 5

=item  G_SCALAR	

Calls the Perl sub in a scalar context.

Whatever the Perl sub actually returns, we only want a scalar. If the perl sub 
does return a scalar, the return value from the I<perl_call_*> function 
will be 1 or 0. If 1, then the value actually returned by the Perl sub will 
be contained on the top of the stack. 
If 0, then you have used the G_DISCARD flag.

It is important to note that
if the Perl sub returns a list, the I<perl_call_*> function will still
only return 1 or 0. If 1, then the number of elements in the list 
will be stored on top of the stack.
The actual values which were contained in the list will not be accessible. 


G_SCALAR is the default flag setting for all the functions.

=item G_ARRAY	

Calls the Perl sub in a list context.

The return code from the I<perl_call_*> functions will indicate how
many elements of the stack are used to store the array. See example 4.

=item G_DISCARD	

If you are not interested in the values returned by the Perl sub then setting
this flag will make Perl get rid of them automatically for you. This will take
precedence to either G_SCALAR or G_ARRAY.

If you do not set this flag then you need to make sure that any temporaries 
(i.e. parameters passed to the Perl sub and values returned from the sub) 
are disposed of yourself.
Example 3 gives details of how to explicitly dispose of these temporaries 
and the section I<Using Perl to dispose of temporaries> discusses the 
specific circumstances where you can ignore the problem and let Perl 
deal with it for you.

=item G_NOARGS	

If you are not passing any parameters to the Perl sub, you can save a bit of 
time by setting this flag. It has the effect of not creating the C<@_> array 
for the Perl sub.

A point worth noting is that if this flag is specified the Perl sub you have
called can still access an C<@_> array from a previous Perl sub. 
This functionality can be illustrated with the perl code below

	sub fred
	  { print "@_\n"  }

	sub joe
	  { &fred }

	&joe(1,2,3) ;

This will print

	1 2 3

What has happened is that C<fred> accesses the C<@_> array which belongs to C<joe>.

=item G_EVAL	

It is possible for the Perl sub you are calling to terminate
abnormally, e.g. by calling I<die> explicitly  or by not actually existing. If 
you want to trap this type of event, specify the G_EVAL flag. 
It will put an I<eval { }> around the sub call.

Whenever control returns from the I<perl_call_*> function you need to
check the C<$@> variable as you would in a normal Perl script. 

The value returned from the I<perl_call_*> function is dependent on what other
flags have been specified and whether an error has occurred. 

If the I<perl_call_*> function returns normally, then the value returned is 
as specified in the previous sections.

If G_DISCARD is specified, the return value will always be 0 - this will 
override both G_SCALAR and G_ARRAY.

If G_ARRAY is specified I<and> an error has occurred, the return value will 
always be 0.

If G_SCALAR is specified I<and> an error has occurred, the return value will 
be 1 and the value on the top of the stack will be I<undef>. This means that if
you have already detected the error by checking C<$@> and you want the program
to continue, you must remember to pop the I<undef> from the stack.

See example 6 for details of using G_EVAL.

Please Note: If you are intending to make use of the G_EVAL flag in your
code use a version of Perl greater than 5.000.
There is a known bug in version 5.000 of Perl which means that G_EVAL will
not work as described above.



=back


=head1 EXAMPLES

Enough of the definition talk, let's have a few examples.

Perl provides many macros to assist in accessing the Perl stack. 
Wherever possible, these macros should always be used when interfacing to 
Perl internals.
Hopefully this should make the code less vulnerable to any changes made to
Perl in the future.

Another point worth noting is that in the first series of examples I have 
only made use of the I<perl_call_pv> function. 
This has only been done to ease you into the 
topic. Wherever possible, if the choice is between using I<perl_call_pv> 
and I<perl_call_sv>, you should always try to use I<perl_call_sv>. 
See example 7 for details.

=head2 Example1: No Parameters, Nothing returned

This first trivial example will call a Perl sub, I<PrintUID>, to print 
out the UID of the process. 

    sub PrintUID
    {
        print "UID is $<\n" ;
    }

and here is the C to call it

    void
    call_PrintUID()
    {
	dSP ;

	PUSHMARK(sp) ;
        perl_call_pv("PrintUID", G_DISCARD|G_NOARGS) ;
    }

Simple, eh. 

A few points to note about this example. 

=over 5

=item 1. 

We aren't passing any parameters to I<PrintUID> so G_NOARGS
can be specified.

=item 2.

Ignore C<dSP> and C<PUSHMARK(sp)> for now. They will be discussed in the next
example.

=item 3. 

We aren't interested in anything returned from I<PrintUID>, so
G_DISCARD is specified. Even if I<PrintUID> was changed to actually
return some value(s), having specified G_DISCARD will mean that they
will be wiped by the time control returns from I<perl_call_pv>.

=item 4. 

Because we specified G_DISCARD, it is not necessary to check 
the value returned from I<perl_call_pv>. It will always be 0.

=item 5.

As I<perl_call_pv> is being used, the Perl sub is specified as a C string.

=back

=head2 Example 2: Passing Parameters

Now let's make a slightly more complex example. This time we want 
to call a Perl sub, C<LeftString>,
which will take 2 parameters - a string (C<$s>) and an integer (C<$n>). 
The sub will simply print the first C<$n> characters of the string.

So the Perl sub would look like this

    sub LeftString
    {
        my($s, $n) = @_ ;
        print substr($s, 0, $n), "\n" ;
    }

The C function required to call I<LeftString> would look like this.

    static void
    call_LeftString(a, b)
    char * a ;
    int b ;
    {
        dSP ;

        PUSHMARK(sp) ;
        XPUSHs(sv_2mortal(newSVpv(a, 0)));
        XPUSHs(sv_2mortal(newSViv(b)));
        PUTBACK ;

        perl_call_pv("LeftString", G_DISCARD);
    }


Here are a few notes on the C function I<call_LeftString>.

=over 5

=item 1. 

The only flag specified this time is G_DISCARD. As we are passing 2 
parameters to the Perl sub this time, we have not specified G_NOARGS.

=item 2. 

Parameters are passed to the Perl sub using the Perl stack.
This is the purpose of the code beginning with the line C<dSP> and ending
with the line C<PUTBACK>.


=item 3.

If you are going to put something onto the Perl stack, you need to know
where to put it. This is the purpose of the macro C<dSP> -
it declares and initializes a I<local> copy of the Perl stack pointer.

All the other macros which will be used in this example require you to
have used this macro. 

If you are calling a Perl sub directly from an XSUB function, it is 
not necessary to explicitly use the C<dSP> macro - it will be declared for you.

=item 4.

Any parameters to be pushed onto the stack should be bracketed by the
C<PUSHMARK> and C<PUTBACK> macros. 
The purpose of these two macros, in this context, is to automatically count
the number of parameters you are pushing. Then whenever Perl is creating
the C<@_> array for the sub, it knows how big to make it.

The C<PUSHMARK> macro tells Perl to make a mental note of the current stack
pointer. Even if you aren't passing any parameters (like in Example 1) you must 
still call the C<PUSHMARK> macro before you can call any of 
the I<perl_call_*> functions - Perl still needs to know that there are 
no parameters.

The C<PUTBACK> macro sets the global copy of the stack pointer to be the
same as our local copy. If we didn't do this I<perl_call_pv> wouldn't
know where the two parameters we pushed were - remember that up to now
all the stack pointer manipulation we have done is with our local copy,
I<not> the global copy.

=item 5.

Next, we come to XPUSHs. This is where the parameters actually get
pushed onto the stack. In this case we are pushing a string and an integer.

See the section I<XSUB's AND  THE ARGUMENT STACK> in L<perlguts> for
details on how the XPUSH macros work.

=item 6.

Finally, I<LeftString> can now be called via the I<perl_call_pv> function.

=back

=head2 Example 3: Returning a Scalar

Now for an example of dealing with the values returned from a Perl sub.

Here is a Perl sub, I<Adder>,  which takes 2 integer parameters and simply 
returns their sum.

    sub Adder
    {
        my($a, $b) = @_ ;
        $a + $b ;
    }

Since we are now concerned with the return value from I<Adder>, the C
function required to call it is now a bit more complex.

    static void
    call_Adder(a, b)
    int a ;
    int b ;
    {
        dSP ;
        int count ;

        ENTER ;
        SAVETMPS;

        PUSHMARK(sp) ;
        XPUSHs(sv_2mortal(newSViv(a)));
        XPUSHs(sv_2mortal(newSViv(b)));
        PUTBACK ;

        count = perl_call_pv("Adder", G_SCALAR);

        SPAGAIN ;

	if (count != 1)
	    croak("Big trouble\n") ;

	printf ("The sum of %d and %d is %d\n", a, b, POPi) ;

        PUTBACK ;
        FREETMPS ;
        LEAVE ;
    }


Points to note this time are

=over 5

=item 1. 

The only flag specified this time was G_SCALAR. That means the C<@_> array
will be created and that the value returned by I<Adder> will still
exist after the call to I<perl_call_pv>.



=item 2.

Because we are interested in what is returned from I<Adder> we cannot specify
G_DISCARD. This means that we will have to tidy up the Perl stack and dispose
of any temporary values ourselves. This is the purpose of 

	ENTER ;
	SAVETMPS ;

at the start of the function, and

	FREETMPS ;
	LEAVE ;

at the end. The C<ENTER>/C<SAVETMPS> pair creates a boundary for any 
temporaries we create. 
This means that the temporaries we get rid of will be limited to those which
were created after these calls.

The C<FREETMPS>/C<LEAVE> pair will get rid of any values returned by the Perl 
sub, plus it will also dump the mortal SV's we have created. 
Having C<ENTER>/C<SAVETMPS> at the beginning
of the code makes sure that no other mortals are destroyed.

=item 3.

The purpose of the macro C<SPAGAIN> is to refresh the local copy of the
stack pointer. This is necessary because it is possible that the memory
allocated to the Perl stack has been re-allocated whilst in the I<perl_call_pv>
call.

If you are making use of the Perl stack pointer in your code you must always
refresh the your local copy using SPAGAIN whenever you make use of
of the I<perl_call_*> functions or any other Perl internal function.

=item 4. 

Although only a single value was expected to be returned from I<Adder>, it is
still good practice to check the return code from I<perl_call_pv> anyway.

Expecting a single value is not quite the same as knowing that there will
be one. If someone modified I<Adder> to return a list and we didn't check
for that possibility and take appropriate action the Perl stack would end 
up in an inconsistent state. That is something you I<really> don't want
to ever happen.

=item 5.

The C<POPi> macro is used here to pop the return value from the stack. In this
case we wanted an integer, so C<POPi> was used.


Here is the complete list of POP macros available, along with the types they 
return.

	POPs	SV
	POPp	pointer
	POPn	double
	POPi	integer
	POPl	long

=item 6.

The final C<PUTBACK> is used to leave the Perl stack in a consistent state 
before exiting the function. This is
necessary because when we popped the return value from the stack with C<POPi> it
only updated our local copy of the stack pointer. Remember, C<PUTBACK> sets the
global stack pointer to be the same as our local copy.

=back


=head2 Example 4: Returning a list of values

Now, let's extend the previous example to return both the sum of the parameters 
and the difference.

Here is the Perl sub

    sub AddSubtract
    {
       my($a, $b) = @_ ;
       ($a+$b, $a-$b) ;
    }

and this is the C function

    static void
    call_AddSubtract(a, b)
    int a ;
    int b ;
    {
        dSP ;
        int count ;

        ENTER ;
        SAVETMPS;

        PUSHMARK(sp) ;
        XPUSHs(sv_2mortal(newSViv(a)));
        XPUSHs(sv_2mortal(newSViv(b)));
        PUTBACK ;

        count = perl_call_pv("AddSubtract", G_ARRAY);

        SPAGAIN ;

        if (count != 2)
            croak("Big trouble\n") ;

        printf ("%d - %d = %d\n", a, b, POPi) ;
        printf ("%d + %d = %d\n", a, b, POPi) ;

        PUTBACK ;
        FREETMPS ;
        LEAVE ;
    }

If I<call_AddSubtract> is called like this

    call_AddSubtract(7, 4) ;

then here is the output

    7 - 4 = 3
    7 + 4 = 11

Notes

=over 5

=item 1.

We wanted array context, so we used G_ARRAY.

=item 2.

Not surprisingly C<POPi> is used twice this time because we were retrieving 2
values from the stack. The main point to note is that they came off the stack in
reverse order.

=back

=head2 Example 5: Returning Data from Perl via the parameter list

It is also possible to return values directly via the parameter list -
whether it is actually desirable to do it is another matter entirely.

The Perl sub, I<Inc>, below takes 2 parameters and increments each directly.

    sub Inc
    {
        ++ $_[0] ;
        ++ $_[1] ;
    }

and here is a C function to call it.

    static void
    call_Inc(a, b)
    int a ;
    int b ;
    {
        dSP ;
        int count ;
        SV * sva ;
        SV * svb ;

        ENTER ;
        SAVETMPS;

        sva = sv_2mortal(newSViv(a)) ;
        svb = sv_2mortal(newSViv(b)) ;

        PUSHMARK(sp) ;
        XPUSHs(sva);
        XPUSHs(svb);
        PUTBACK ;

        count = perl_call_pv("Inc", G_DISCARD);

        if (count != 0)
            croak ("call_Inc: expected 0 values from 'Inc', got %d\n",
                   count) ;

        printf ("%d + 1 = %d\n", a, SvIV(sva)) ;
        printf ("%d + 1 = %d\n", b, SvIV(svb)) ;

    	FREETMPS ;
        LEAVE ;
    }

To be able to access the two parameters that were pushed onto the stack
after they return from I<perl_call_pv> it is necessary to make a note
of their addresses - thus the two variables C<sva> and C<svb>.

The reason this is necessary is that the area of the Perl stack which
held them will very likely have been overwritten by something else by
the time control returns from I<perl_call_pv>.




=head2 Using G_EVAL

Now an example using G_EVAL. Below is a Perl subroutine which computes
the difference of its 2 parameters. If this would result in a negative
result, the subroutine calls I<die>.

    sub Subtract
    {
        my ($a, $b) = @_ ;

        die "death can be fatal\n" if $a < $b ;

        $a - $b ;
    }

and some C to call it

    static void
    call_Subtract(a, b)
    int a ;
    int b ;
    {
        dSP ;
        int count ;
        SV * sv ;

        ENTER ;
        SAVETMPS;

        PUSHMARK(sp) ;
        XPUSHs(sv_2mortal(newSViv(a)));
        XPUSHs(sv_2mortal(newSViv(b)));
        PUTBACK ;

        count = perl_call_pv("Subtract", G_EVAL|G_SCALAR);

        SPAGAIN ;
	
	/* Check the eval first */
        sv = GvSV(gv_fetchpv("@", TRUE, SVt_PV));
        if (SvTRUE(sv))
	{
            printf ("Uh oh - %s\n", SvPV(sv, na)) ;
	    POPs ;
	}
	else
	{
            if (count != 1)
                croak ("call_Subtract : expected 1 value from 'Subtract', got %d\n", count) ;
	
	    printf ("%d - %d = %d\n", a, b, POPi) ;
	}

        PUTBACK ;
        FREETMPS ;
        LEAVE ;

    }

If I<call_Subtract> is called thus

	call_Subtract(4, 5)

the following will be printed

	Uh oh - death can be fatal

Notes

=over 5

=item 1.

We want to be able to catch the I<die> so we have used the G_EVAL flag.
Not specifying this flag would mean that the program would terminate
immediately at the I<die>.

=item 2.

The code 

        sv = GvSV(gv_fetchpv("@", TRUE, SVt_PV));
        if (SvTRUE(sv))
            printf ("Uh oh - %s\n", SvPVx(sv, na)) ;

is the equivalent of this bit of Perl

	print "Uh oh - $@\n" if $@ ;


=item 3.

Note that the stack is popped using C<POPs> in the block where C<SvTRUE(sv)> 
is true.
This is necessary because whenever a I<perl_call_*> function invoked with
G_EVAL|G_SCALAR returns an error, the top of the stack holds the value
I<undef>. As we want the program to continue after detecting this error, it
is essential that the stack is tidied up by removing the I<undef>.

=back


=head2 Example 7: Using perl_call_sv

In all the previous examples I have 'hard-wired' the name of the Perl sub to
be called from C. 
Most of the time though, it is more convenient to be able to specify the name
of the Perl sub from within the Perl script.

Consider the Perl code below

	sub fred
	{
	    print "Hello there\n" ;
	}

	CallSub("fred") ;


here is a snippet of XSUB which defines I<CallSub>.

	void
	CallSub(name)
		char *	name
		CODE:
		PUSHMARK(sp) ;
		perl_call_pv(name, G_DISCARD|G_NOARGS) ;

That is fine as far as it goes. The thing is, it only allows the Perl sub to be
specified as a string. 
For perl 4 this was adequate, but Perl 5 allows references to 
subs and anonymous subs. This is where I<perl_call_sv> is useful.

The code below for I<CallSub> is identical to the previous time except that the
C<name> parameter is now defined as an SV* and we use I<perl_call_sv> instead of
I<perl_call_pv>.

	void
	CallSub(name)
		SV*	name
		CODE:
		PUSHMARK(sp) ;
		perl_call_sv(name, G_DISCARD|G_NOARGS) ;

As we are using an SV to call I<fred> the following can all be used

	CallSub("fred") ;
	Callsub(\&fred) ;
	$ref = \&fred ;
	CallSub($ref) ;
	CallSub( sub { print "Hello there\n" } ) ;

As you can see, I<perl_call_sv> gives you much greater flexibility in how you 
can specify the Perl sub.

=head2 Example 8: Using perl_call_argv

Here is a Perl sub which prints whatever parameters are passed to it.

	sub PrintList
	{
	    my(@list) = @_ ;

	    foreach (@list) { print "$_\n" }
	}

and here is an example of I<perl_call_argv> which will call I<PrintList>.

	call_PrintList
	{
	    dSP ;
	    char * words[] = {"alpha", "beta", "gamma", "delta", NULL } ;

	    perl_call_argv("PrintList", words, G_DISCARD) ;
	}

Note that it is not necessary to call C<PUSHMARK> in this instance. This is
because I<perl_call_argv> will do it for you.

=head2 Example 9: Using perl_call_method

Consider the following Perl code

	{
	    package Mine ;

	    sub new
	    { 
	        my($type) = shift ;
	        bless [@_] 
	    }
	
	    sub Display 
	    { 
	        my ($self, $index) = @_ ;
	        print "$index: $$self[$index]\n" ;
	    }
	}


It just implements a very simple class to manage an array. The only method
defined is C<Display> which prints out a single element of the array.
Here is an all Perl example of using it.

	$a = new Mine ('red', 'green', 'blue') ;
	$a->Display(1) ;

will print

	1: green

Calling a Perl method from C is fairly straightforward - a reference to 
the object, the name of the method and any other parameters specific to
the method are all that is required.

Here is a simple XSUB which illustrates the mechanics of calling the
C<Display> method from C.

	void
	call_Display(ref, method, index)
            SV *    ref
            char *  method
	    int	    index
            CODE:
            PUSHMARK(sp);
            XPUSHs(ref);
	    XPUSHs(sv_2mortal(newSViv(index))) ;
            PUTBACK;
	
            perl_call_method(method, G_DISCARD) ;

C<call_Display> expects it's first parameter to be a reference to 
an object, 
the second to be the name of the method, and the third is the index.
So the method C<Display> method can be invoked like this


	$a = new Mine ('red', 'green', 'blue') ;
	call_Method($a, 'Display', 2) ;

The only thing to note is that the reference to the 
object (C<ref>) and the C<index> parameter are is pushed onto 
the stack before calling I<perl_call_method>. The method name
C<method> does not get pushed onto the stack, it is used as the 
first parameter to I<perl_call_method>.



=head2 Using Perl to dispose of temporaries

In the examples given to date, any temporaries created in the callback (
i.e parameters passed on the stack to the I<perl_call_*> function or
values returned via the stack) have been freed by

=over 5

=item *

specifying the G_DISCARD flag with I<perl_call_*>.

=item *

explicitly disposed of using the C<ENTER>/C<SAVETMPS> - C<FREETMPS>/C<LEAVE> 
pairing.

=back

There is another method which can be
used, namely letting Perl do it for you automatically whenever it 
regains control after the callback has terminated. 
This is done by simply not using the

	ENTER ;
	SAVETMPS ;
	...
	FREETMPS ;
	LEAVE ;

sequence in the callback (and not, of course, specifying the G_DISCARD flag).

If you are going to use this method you have to be aware of a possible 
memory leak which can arise under very specific circumstances.
To explain these circumstances you need to know a bit about the flow of
control between Perl and the callback routine.

The examples given at the start of the document (an error handler and
an event driven program) are typical of 
the two main sorts 
of flow control that you are likely to encounter with callbacks. 
There is a very important distinction between them, so pay attention.

In the first example, an error handler, the flow of control could be as
follows. You have created an interface to an external library and control 
has reached the library thus

	perl --> XSUB --> external library

Whilst control is in the library, an error condition occurs. You have 
previously set up a Perl callback to handle this situation, so the flow
of control will be


	                  external library --> XSUB --> perl_call --+
	                                                            |
	perl <-- XSUB <-- external library <-- XSUB <---------------+


After processing of the error using I<perl_call_*> is completed, 
control reverts back to Perl more or less immediately. 
The important thing to note is that it is only now that control is back 
with Perl that any temporaries you have left hanging around will be freed. 
				
In the second example, an event driven program, the flow of control will 
be more like this

	event handler --> XSUB --> perl_call --+
			       		       |
	              +--------<---------------+
		      |
	event handler +-> XSUB --> perl_call --+
			       		       |
	              +----------<-------------+
		      |
	event handler +-> XSUB --> perl_call --+
			       		       |
	              +----------<-------------+
		      |
	event handler +-> XSUB --> perl_call --+
	        
In this case the flow of control can consist only of the repeated sequence

	event handler -> XSUB -> perl_call 

for the practically the complete duration of the program.
This means that control may never drop back to Perl proper.

So what is the big problem? Well, if you are expecting Perl to tidy up 
those temporaries for you, you might be in for a long wait. 
For Perl to actually dispose
of your temporaries, control must drop back to it at some stage. In the
event driven scenario that may never happen. This means that as time goes on,
your program will create more and more temporaries, none of which will ever
be freed. As each of these temporaries consumes some memory you program will
eventually consume all the available memory in your system - kapow!

So here is the bottom line - if you are sure that control will revert 
back to Perl fairly quickly after the end of your callback, then it 
isn't absolutely necessary to explicitly dispose of any temporaries you 
may have created. Mind you, if you are at all uncertain about what to do,
it doesn't do any harm to tidy up anyway.


=head2 Strategies for storing Callback Context Information


Potentially one of the trickiest problems to overcome when designing a 
callback interface can be figuring 
out how to store the mapping between the C callback function and the 
Perl equivalent.

To help understand why this can be a real problem first consider how a
callback is set up in an all C environment. 
Typically a C API will provide a function to register a callback. 
This will expect a pointer to a function as one of it's parameters.
Below is a call to a hypothetical function C<register_fatal> which 
registers the C function to get called when a fatal error occurs.

	register_fatal(cb1) ;

The single parameter C<cb1> is a pointer to a function, so you must have
defined C<cb1> in your code, say something like this

	void
	cb1()
	{
	    printf ("Fatal Error\n") ;
	    exit(1) ;
	}
	

Now change that to call a Perl sub instead

	static SV * callback ;

	void
	register_fatal(fn)
	    CODE:
	    SV *	fn
	    {
		/* Remember the Perl sub */
		callback = fn ;
	
		/* register the callback with the external library */
	        register_fatal(cb1) ;
	    }
	
	void
	cb1()
	{
	    ...
	    /* Call the Perl sub to process the callback */
	    perl_call_sv(callback) ;
	    ...
	}
	

where the Perl equivalent of C<register_fatal> and the callback it 
registers ,C<pcb1>, might look like this

	# Register the sub pcb1
	register_fatal(\&pcb1) ;

	sub pcb1
	{
	    die "I'm dying...\n" ;
	}

The mapping between the C callback and the Perl equivalent is stored in the
global variable C<callback>.

This will be adequate if you only ever need to have 1 callback 
registered at any time. An example could be an error handler like
the code sketched out above. Remember though, repeated calls to 
C<register_fatal> will
replace the previously registered callback function with the new one.

Say though you want to interface to a library which allows asynchronous 
file i/o.
In this case you may be able to register a callback whenever a read operation
has completed. To be of any use we want to be able to call separate Perl
subs for each file that is opened. As it stands, the error handler example
above would not be adequate as it only allows a single callback
to be defined at any time. What we require is a means of storing the mapping 
between the opened file and the Perl sub we want to be called for that file.

Say the i/o library has a function C<asynch_read> which associates a
C function C<callback> with a file handle C<fh> - this assumes that it has
also provided some routine to open file file and so obtain the file handle.

	asynch_read(fh, callback)


This may expect the C callback function of this form

	void
	callback(fh, buffer)
	int	fh ;
	char *	buffer ;
	{
	    ...
	}


To provide a Perl interface to this library we need to be able to map
between the C<fh> parameter and the Perl sub we want called. A hash is a 
convenient mechanism for storing this mapping. The code below shows a
possible implementation

	HV * Mapping = (HV*)NULL ;

	void
	asynch_read(fh, callback)
	int	fh
	SV *	callback
	CODE:
	{
	  /* If the hash doesn't already exist, create it */
	  if (Mapping == (HV*)NULL)
	      Mapping = newHV() ;
	
	  /* Save the fh -> callback mapping */
	  hv_store(Mapping, &fh, sizeof(fh), callback, 0) ;
	
	  /* Register with the C Library */
	  asynch_read(fh, asynch_read_if) ;
	}

where C<asynch_read_if> could look like this

	static void
	asynch_read_if(fh, buffer)
	int	fh ;
	char *	buffer ;
	{
	    dSP ;
	    SV ** sv ;
	
	    /* Get the callback associated with fh */
	    sv =  hv_fetch(Mapping, &fh , sizeof(fh), FALSE) ;
	    if (sv == (SV**)NULL)
	        croak("Internal error...\n") ;
	
	    PUSHMARK(sp) ;
	    XPUSHs(sv_2mortal(newSViv(fh))) ;
	    XPUSHs(sv_2mortal(newSVpv(buffer, 0))) ;
	    PUTBACK ;
	
	    /* Call the Perl sub */
	    perl_call_sv(*sv, G_DISCARD) ;
	}

So the Perl interface would look like this

	# Register the Perl callback
	asynch_read(fh, \&callback1) ;
	
	sub callback1
	{
	    my($handle, $buffer) = @_ ;
	    ...
	}


The mapping between the C callback and Perl is stored in the global hash 
C<Mapping> this time. Using a hash has the advantage that it allows an 
unlimited number of callbacks to be registered.

What if the interface provided by the C callback doesn't contain a parameter
which allows the file handle to Perl sub mapping? Say in the asynchronous i/o
package, the callback function only gets passed the C<buffer> parameter 
like this 

	void
	callback(buffer)
	char *	buffer ;
	{
	    ...
	}

Without the file handle there is no straightforward way to map from the C 
callback to the Perl sub.

In this case a possible way around this problem is to pre-define a series 
of C functions to act as the interface to Perl, thus


	#define MAX_CB	3
	typedef	void (*FnMap)() ;
	static void fn1() ;
	static void fn2() ;
	static void fn3() ;
	FnMap	Functions[MAX_CB] = { fn1, fn2, fn3 } ;
	SV *	PerlSubs [MAX_CB] = { NULL, NULL, NULL } ;
	
	static void
	Pcb(index, buffer)
	int index ;
	char * buffer ;
	{
	    dSP ;
	    
	    PUSHMARK(sp) ;
	    XPUSHs(sv_2mortal(newSVpv(buffer, 0))) ;
	    PUTBACK ;
	
	    /* Call the Perl sub */
	    perl_call_sv(PerlSubs[index], G_DISCARD) ;
	}
	
	static void
	fn1(buffer)
	char * buffer ;
	{
	    Pcb(0, buffer) ;
	}
	
	static void
	fn2(buffer)
	char * buffer ;
	{
	    Pcb(1, buffer) ;
	}
	
	static void
	fn3(buffer)
	char * buffer ;
	{
	    Pcb(2, buffer) ;
	}

	void
	asynch_read(fh, callback)
	int	fh
	SV *	callback
	CODE:
	{
	  int index ;
	  /* Find an empty entry */
	  for (index = 0 ; index < MAX_CB ; ++index)
	  {
	      if (PerlSubs[index] == NULL)
		  break ;
	  }
	
	  if (index < MAX_CB)
	  {
	      /* Remember the Perl sub */
	      PerlSubs[index] = callback ;
	      asynch_read(fh, Functions[index]) ;
	  }
	  else
	      croak ("Too many callback functions registered\n") ;
	}


In this case the functions C<fn1>, C<fn2> and C<fn3> are used to remember
the Perl sub to be called. Each of the functions holds a separate 
hard-wired index which is used in the function C<Pcb> to access 
the C<PerlSubs> array and actually call the Perl sub. 

The obvious disadvantage of this technique is that there is a hard-wired 
limit (in this case 3) to the number of callbacks that can exist 
simultaneously. The only way to increase the limit is to modify the
code to add more functions and then re-compile.
None the less, as long as the number of functions is chosen with some
care, it is still a workable solution and in some cases is the only
one available.

To summarize, here are a number of possible methods for you to consider
for storing the mapping between C and the Perl callback

=over 5

=item 1. Ignore the problem - Only allow 1 callback

For a lot of situations, like interfacing to a error handler, this may be a 
perfectly adequate solution.

=item 2. Create a sequence of callbacks - hard wired limit

If it is impossible to tell from the parameters passed back from the 
C callback what the context is, then you
need to create a sequence of C callback interface functions, 
and store pointers to each in an array.

=item 3. Use a parameter to map to the Perl callback

Use a hash to store the mapping between C and Perl.

=back


=head2 Alternate Stack Manipulation


Although I have only made use of the C<POP*> macros to access values returned 
from Perl subs, it is also possible to bypass these macros and read the 
stack using the C<ST> macro (See L<perlapi> for a full description 
of the C<ST> macro). 

Most of the time the C<POP*> macros should be adequate, the main
problem with them is that they force you to process the returned values 
in sequence. This may not be the most suitable way to process the values
in some cases. Using the C<ST> macro will allow random access to the stack.

The code below is example 4 recoded to use C<ST> instead of C<POP*>.

    static void
    call_AddSubtract2(a, b)
    int a ;
    int b ;
    {
        dSP ;
	I32 ax ;
        int count ;

        ENTER ;
        SAVETMPS;

        PUSHMARK(sp) ;
        XPUSHs(sv_2mortal(newSViv(a)));
        XPUSHs(sv_2mortal(newSViv(b)));
        PUTBACK ;

        count = perl_call_pv("AddSubtract", G_ARRAY);

        SPAGAIN ;
	sp -= count ;
	ax = (sp - stack_base) + 1 ;

	if (count != 2)
	    croak("Big trouble\n") ;

	printf ("%d - %d = %d\n", a, b, SViv(ST(0))) ;
	printf ("%d + %d = %d\n", a, b, SViv(ST(1))) ;

        PUTBACK ;
        FREETMPS ;
        LEAVE ;
    }


Notes

=over 5

=item 1.

Notice that it was necessary to define the variable C<ax>. 
This is because the C<ST> macro expects it to exist.
If we were in an XSUB it would not be necessary to define C<ax> as it is
already defined for you.

=item 2.

The code
	
        SPAGAIN ;
	sp -= count ;
	ax = (sp - stack_base) + 1 ;

sets the stack up so that we can use the C<ST> macro.

=item 3.

Remember that as with example 4, the returned values are 
still indexed in reverse order. 
So C<ST(0)> refers to the last value and C<ST(count-1)> refers to the first.

=back

=head1 SEE ALSO

L<perlapi>, L<perlguts>, L<perlembed>

=head1 AUTHOR

Paul Marquess <pmarquess@bfsec.bt.co.uk>

Special thanks to the following people who assisted in the creation of the 
document.

Jeff Okamoto, Tim Bunce, Nick Gianniotis.

=head1 DATE

Version 0.5, 6th January 1995
