perlsystem-callsrhelperl-xs

Writing a Sys::Getpagesize Perl module for system call getpagesize (man page GETPAGESIZE(2))


I have been tasked to write a Perl module that requires that I use Perl XS. I have not ever used Perl XS but I have looked at the documentation here: http://perldoc.perl.org/perlxs.html and it looks like I will need the C source code for the getpagesize system call. I tried looking for getpagesize.c via yum like so ...

$ sudo yum provides */getpagesize.c 

... but I do not seem to have any RPMs that would provide getpagesize.c.

Can anyone out there help me out of the ditch I seem to have driven into?

Thanks


Solution

  • Why do you think you need the source code to the getpagesize function? You just link to the system's version. I haven't tried it, but something like this should work:

    #include "EXTERN.h"
    #include "perl.h"
    #include "XSUB.h"
    #include <unistd.h> /* man 2 getpagesize says to use this */
    
    MODULE = Sys::Getpagesize PACKAGE = Sys::Getpagesize
    
    int
    getpagesize()
    

    But in this case, you shouldn't need to write XS at all. man 2 getpagesize says "Portable applications should employ sysconf(_SC_PAGESIZE) instead of getpagesize()."

    Perl's standard POSIX module has sysconf already:

    use POSIX qw(sysconf _SC_PAGESIZE);
    print sysconf( _SC_PAGESIZE );