Peta::FFI#
Κλήση συναρτήσεων βιβλιοθηκών C από την Perl χωρίς βήμα κατασκευής και χωρίς crate binding.
Peta::FFI is the dynamic FFI layer pperl ships for reaching into shared libraries at runtime. You open a library with dlopen, call any of its exported functions through call by giving a type signature string, and release the handle with dlclose. scan finds the libraries in a list of directories, which is useful for finding the right file before you open it.
This is how Perl code here reaches a C library: pperl loads no compiled extension modules, so anything living in a .so is called either through this module or through one of the bindings built on it. pperl’s FFI::Platypus is one of them: its calls, types, records and closures are this module’s, so whatever FFI::Platypus can express, Peta::FFI can, in its own signature language.
Καταφύγετε σε αυτό όταν:
Η συνάρτηση που χρειάζεστε βρίσκεται σε βιβλιοθήκη του συστήματος (
libm,libc,libuuid,libcrypto).Γνωρίζετε ήδη το πρωτότυπο C και θέλετε ένα μονογραμμικό, όχι βήμα κατασκευής.
Καλείτε ένα
.soτρίτου κατασκευαστή που δεν καλύπτει κανένα έτοιμο περιτύλιγμα.
C calling back into Perl is supported: closure wraps a coderef as a function pointer you can hand to any C API that wants one (see the Callbacks category). C structs are records: record declares one as a Perl class, and a signature passes and returns it by value or by pointer (see the Records category). A library’s global variables are reached with symbol, fetch/store and variable.
What it does not do is derive types from a header: you read the C prototype and write the matching signature by hand, and for a struct its members in order, since a signature that disagrees with the real prototype is undefined behaviour rather than an error.
Τα δύο συγγενή αρθρώματα Peta::FFI::Libc και Peta::FFI::UUID είναι έτοιμα βολικά περιτυλίγματα χτισμένα πάνω σε αυτό το στρώμα - χειρίζονται για λογαριασμό σας τον χορό dlopen / call / dlclose για ένα σταθερό σύνολο συναρτήσεων. Αν η ανάγκη σας καλύπτεται από ένα από αυτά, προτιμήστε το από ένα χειροποίητο FFI.
A call that can block for good - statfs on a dead NFS mount - runs on a Peta::FFI::Pool thread instead, with a deadline, and reports how it went as an event. A library that calls back on threads of its own gets a queued closure, whose calls wait in a Peta::FFI::Queue for Perl’s event loop.
Συμβολοσειρές υπογραφής#
Every call, function and closure takes a signature string of the form (args)ret, where each position is one type:
Type | Τύπος C | Πλευρά Perl |
|---|---|---|
|
| μόνο για επιστροφή· επιστρέφεται |
|
| ακέραιος |
|
| ακέραιος |
|
| unsigned integer |
|
| ακέραιος |
|
| unsigned integer |
|
| perl’s true or false |
|
| αριθμός κινητής υποδιαστολής |
|
| αριθμός κινητής υποδιαστολής |
|
| float (rounded to a double) |
|
|
|
|
| input string; a returned string is copied |
|
| a result, copied, then freed; |
| μεταβλητός ενταμιευτής | output buffer; |
| αδιαφανής δείκτης | ακατέργαστη διεύθυνση ως ακέραιος |
| a C object’s pointer | an object of a declared handle type |
|
| an object of a declared record class |
|
| the same object, by pointer |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| the same, copied, never freed |
|
|
|
result |
| an array reference, the array freed |
result |
| the bytes, freed |
result |
| an array reference of copies |
result |
| a copy of the bytes |
A signature with no arguments is written ()ret, e.g. ()i for an int-returning no-arg function. Types may be separated by blanks: (u64 <My::Pool>)b. A variadic function lists its named parameters, a ;, and the types this call passes through its ...: (iL;o)i is ioctl(int, unsigned long, ...) passing one pointer.
An unsigned type reads its argument as an unsigned integer, so 2**64-1 passes as itself, not as -1, and returns above 2**63 come back unsigned. s is for C functions whose returned string the caller owns; function names the free function that releases it. <Class> is a typed handle declared with handle_type, {Class} a record declared with record. An argument is read once, as an XS function reads it, so a regex capture or a tied scalar passes its value.
*T and T[N] take the numbers, o, p (an array of C strings, char **) and b: (d*i)d is frexp(double, int *), called with \my $exp, and (i32[]LLo)v is qsort sorting \@numbers in place. T[N] also takes handles and records, and C’s changes to the records are written back: ({My::Point}[]LLo)v sorts \@points. T[0] is an array of o, p or handles that C reads up to a NULL the call appends, as execv’s argv or a NULL-terminated list of handles.
Some C functions hand over what they allocate through a pointer the caller passes. *<Class> is a handle stored there, as sqlite3_open stores its sqlite3 *: called with \my $db, $db becomes the object, owned by the caller and destroyed with it; a handle already in $db goes in, and stays the same object if C leaves it. *s is a string stored there, copied into the scalar and freed.
*T[r], *T[#k] and *T[0] are an array of T the callee allocates and stores there: as long as the function’s integer result ([r]), as argument k ([#k], an integer or an * integer, counted from 1), or up to a NULL element ([0], for o, p, s and handles). (<My::Conn>*<My::Item>[r]u32)i lists a connection’s items: called with \my $items, $items becomes an array reference of objects, each the caller’s; a reference to an array is filled instead. Strings (*s[...]) are copied and each freed, numbers and o read, records copied. A negative length is the callee’s error and leaves undef. The array itself is freed with function’s free - or, for records, with its release, (array, count), the library’s function that also frees what the records point to. Records keep the array until the last of them is gone, so a record’s p member reads its string for as long as the record lives; a bare copy of its bytes does not keep it.
*P[r] and *P[#k] are bytes the callee allocates and stores there, with the length as for an array: one byte string, NULs included, where *u8[#k] would give an array of numbers. librados’s rados_mon_command hands over its reply this way, as a char ** and a size_t * freed with rados_buffer_free: (op[]LPL*P[#7]*L*P[#9]*L)i with free => "rados_buffer_free", the input buffer passed as \$inbuf and the reply taken with \my $out, \my $outlen, \my $status, \my $statuslen. A NULL leaves undef, and so does a negative length; the buffer is freed with function’s free. A buffer ended by a NUL is an *s. Bytes the callee only lends are &P[r] and &P[#k], and &P[#k] is also a result: copied, never freed, as the lent arrays below.
&T[r], &T[#k] and &T[0] are the same for an array the callee only lends - a pointer into memory it keeps, as libxkbcommon’s xkb_keymap_key_get_syms_by_level hands out a keymap’s keysyms: (<Keymap>u32u32u32&u32[r])i. The elements are copied and the array is never freed. They are numbers, o, p, b or records; an s, which the caller frees, and a handle, whose object owns its pointer, cannot be lent.
&T[#k] and &T[0] are also a result: the array C returns, as libxkbcommon’s xkb_compose_table_entry_sequence returns an entry’s keysyms and stores their count through a size_t *: (o*L)&u32[#2], called as $f->($entry, \my $n). The call returns an array reference of the copies, or undef for a negative length or a NULL with elements. The length is read after the call, from an integer or an * integer argument; [r] is no form here, since the result is the array itself.
*T[#k], *T[0] and *P[#k] are the same for an array or bytes C returns and the caller frees, as an argument’s are: liblxc’s get_ips returns a NULL-terminated char ** whose strings and array the caller frees, (<Container>ppi)*s[0] with free => "free". The elements are read as for the argument (each s copied and freed), then the array is freed with function’s free, or for records kept until the last record is gone and freed with its release.
P passes the bytes of the scalar it references, which hold what the callee wrote afterwards: my $buf = "\0" x 64; call(..., \$buf). Given a string or a number instead, it passes a zeroed buffer of that length or that many bytes (at least 16) that nobody reads back.
Το o είναι ο τύπος χειριστή για API της C χτισμένα πάνω σε αδιαφανή structs (PGconn*, sqlite3*, MYSQL*): η ακατέργαστη διεύθυνση ταξιδεύει ως ακέραιος της Perl, δεν αποαναφέρεται ποτέ από το runtime, και το undef/0 περνά NULL. Μη χρησιμοποιείτε ποτέ το p για τέτοιους δείκτες - το p επιστρέφει με σημασιολογία συμβολοσειράς αντιγραφής-μέχρι-NUL, κάτι λανθασμένο και μη ασφαλές για δεδομένα που δεν είναι συμβολοσειρές.
Πρωτογενή μνήμης#
For C APIs that traffic in caller-owned memory (out-parameters, pointer arrays, length-counted buffers) the module provides the pointer toolbox: alloc/free, peek/peek_cstr, poke, and pack_ptr/unpack_ptr for bytes, fetch/store for a value of any type at an address, and sizeof/alignof. Addresses are ordinary Perl integers as with o. A pointer array (e.g. char *argv[]) is built portably as join "", map pack_ptr($_), @addrs poked into an alloced block, or passed as a p[] array.
Σύνοψη#
use Peta::FFI;
my $libm = Peta::FFI::dlopen("libm.so.6");
my $root = Peta::FFI::call($libm, "sqrt", "(d)d", 2.0);
Peta::FFI::dlclose($libm);
print $root, "\n"; # 1.4142135623731
Peta::FFI::record("Div", quot => "i", rem => "i"); # div_t
my $d = Peta::FFI::call(undef, "div", "(ii){Div}", 17, 5);
print $d->quot, " r ", $d->rem, "\n"; # 3 r 2
Αρθρώματα#
Peta::FFI::EventSource- events from a C library’s own threads, drained on Perl’sPeta::FFI::LMDB- Βάσεις δεδομένων Lightning με αντιστοίχιση στη μνήμη: συναλλακτικές αποθήκες κλειδιού-τιμής σε ένα αρχείο, πάνω από τη liblmdb του συστήματος.Peta::FFI::Libc- Κλήση συνηθισμένων συναρτήσεων της libc από την Perl χωρίς να γράψετε ούτε μία γραμμή κώδικα σύνδεσης FFI.Peta::FFI::PCRE2- Μεταγλώττιση και εκτέλεση κανονικών εκφράσεων PCRE2 μέσω της βιβλιοθήκης libpcre2-8 του συστήματος.Peta::FFI::Pool- Run C functions on worker threads, so that a call which hangs cannot hang the program.Peta::FFI::Queue- Callbacks a C library makes from threads of its own, queued for Perl’s event loop.Peta::FFI::UUID- Δημιουργία και ανάλυση UUID κατά RFC 4122 μέσω της βιβλιοθήκης libuuid του συστήματος.
Συναρτήσεις#
Φόρτωση βιβλιοθηκών#
dlopen#
Ανοίγει μια κοινόχρηστη βιβλιοθήκη και επιστρέφει έναν χειριστή κατάλληλο για να δοθεί στην call.
dlclose#
Απελευθερώνει έναν χειριστή βιβλιοθήκης που είχε επιστραφεί προηγουμένως από την dlopen.
Κλήσεις συναρτήσεων#
call#
Καλεί μια συνάρτηση C από μια ανοιγμένη βιβλιοθήκη και επιστρέφει το αποτέλεσμά της.
function#
Prepare a C function once and return it as a code reference.
attach#
Prepare a C function once and install it as a named Perl sub.
errno#
errno as the last C call through Peta::FFI left it.
Ενδοσκόπηση συμβόλων#
symbol#
The address of a symbol - a function or a variable - in a library.
variable#
A library’s global variable as a Perl scalar.
scan#
Find the shared libraries in a list of directories.
Handles#
handle_type#
Declare a Perl class for a C object type, with its destructor.
release#
Destroy a handle’s C object now, ahead of its last reference.
Records#
record#
Declare a record class: a C struct, passed by value or by pointer.
union#
Declare a union class: a C union, reached by pointer and in memory.
sizeof#
The size of a type in bytes, as C’s sizeof.
alignof#
The alignment of a type in bytes, as C’s _Alignof.
Callbacks#
closure#
Τυλίγει ένα coderef της Perl ως δείκτη συνάρτησης που μπορεί να κληθεί από τη C.
closure_free#
Απελευθερώνει ένα closure που δημιουργήθηκε με την closure.
Μνήμη#
fetch#
Read the value of a type at an address.
store#
Write a value of a type at an address.
peek#
Διαβάζει $len ακατέργαστα bytes στη διεύθυνση $ptr σε μια συμβολοσειρά bytes της Perl.
peek_cstr#
Διαβάζει τη συμβολοσειρά C που τερματίζεται με NUL στη διεύθυνση $ptr.
poke#
Αντιγράφει μια συμβολοσειρά bytes της Perl στη μνήμη, στη διεύθυνση $ptr.
alloc#
Δεσμεύει $len μηδενισμένα bytes μνήμης C· επιστρέφει τη διεύθυνση.
free#
Απελευθερώνει μνήμη που αποκτήθηκε από την alloc.
pack_ptr#
Πακετάρει μια διεύθυνση σε συμβολοσειρά bytes εγγενούς μεγέθους δείκτη.
unpack_ptr#
Αποπακετάρει μια συμβολοσειρά bytes εγγενούς μεγέθους δείκτη σε διεύθυνση.