NAME
Devel::PtrTable - Interface to perl's old-new pointer mapping for cloning
DESCRIPTION
This provides an interface to map memory addresses of duplicated/cloned perl variables between threads.
Perl duplicates each variable when a new thread is spawned, and thus the new underlying SV object has a new memory address.
Internally, perl maintains a struct ptr_tbl during cloning, which allows the interpreter to properly copy objects, as well as keep track of which objects were copied.
SYNOPSIS
use threads;
use Devel::PtrTable;
my $someref = \"somestring";
my $refhash = {
$someref + 0 => $someref
};
my $thr = $thr->create(
sub {
while (my ($addr,$v) = each %$refhash) {
my $newval = PtrTable_get($addr);
die("oops!") unless $newval == $addr;
}
}
);
FUNCTIONS
PtrTable_get($memaddr)
Returns a reference to whatever happened to have been stored under $memaddr in the parent thread, or undef if there is no such entry.
PtrTable_freecopied()
Free the copied pointer table
GUTS
For those who might be interested, this module does the following
Using
CLONE_SKIPto initialize an opaque SV and magic structure with aMGVTBLincludingsvt_dupInside the
svt_duphook, the stash for this module is unshifted to the beginning of thestashesfields of theCLONE_PARAMSargument. This ensures that no other module will destroy the globalPL_ptr_tablebefore us. This also means that we won't be able to see any new pointers possibly added by other packages in theirCLONEmethods - though this is unlikelyAfter the interpreter has been cloned, our
CLONEmethod is the first thing called. In this method, we make a duplicatestruct ptr_tblobject, which is available until the perl-levelPtrTable_freecopiedis called
CAVEATS
This module cannot be too smart about which entries in the pointer table are valid SVs, which are PerlIO objects, and which are random junk. Users of this module are expected to have a list of valid addresses to use.
Additionally, modules which may insert other entries into the pointer table during their own CLONE methods will not have those entries available to us. See "GUTS" for the reason
SEE ALSO
perlapi and perl.h
AUTHOR AND COPYRIGHT
Copyright (C) 2011 by M. Nunberg.
You may use and distribute this module under the same terms and license as Perl itself