From: Neil W. <neilw@ActiveState.com> - 2001-12-03 20:33:11
|
> How do perl extensions handle: > > 1. Memory management Perl provides its own memory allocation macros. Read all about them in the perlapi manpage (or the bottom of the perlguts manpage for Perl < 5.6.0). void Newz(int id, void *ptr, int nitems, type); 'id' is just an integer. The idea is you just pick some constant and use it everywhere in your extension. I've never seen this used for anything. 'ptr' will be initialized to the allocated memory. 'nitems' tells perl how many 'type's it should allocate room for. char **lines; Newz(1352, lines, 20, char*); This example will make 'lines' point at an array of 20 char *s. void Safefree(void *ptr); The corresponding free() macro. I believe Safefree is NULL-safe. > 2. Object life-time Perl uses reference counting, just like Python. > 3. Exceptions From XS, you have to use the G_EVAL flag to trap the exception. After every call or eval, you have to check the "$@" variable, just as you would in a Perl script. Perl's call() macros don't return any special flag to signify that an error occurred - you have to check for it yourself. See the perlcall manpage for more information. |