3

根据Scalar::Util 的文档refaddr工作方式如下:

my $addr = refaddr( $ref );

如果 $ref 是引用,则引用值的内部内存地址作为纯整数返回。否则返回 undef。

但是,这并不能告诉我是否$addr是永久性的。refaddr参考值会随着时间而改变吗?例如,在 C 语言中,运行realloc可以更改存储在动态内存中的内容的位置。这与 Perl 5 类似吗?

我问是因为我想制作一个由内而外的对象,我想知道是否refaddr($object)会成为一把好钥匙。例如,在 XS 中编程时,这似乎是最简单的。

4

1 回答 1

6

首先,不要重新发明轮子;使用Class::InsideOut


它是永久性的。必须是,否则以下操作将失败:

my $x;
my $r = \$x;
... Do something with $x ...
say $$r;

标量在固定位置有一个“头”。如果 SV 需要升级(例如保存一个字符串),它是一个称为“主体”的第二个内存块,它将发生变化。字符串缓冲区是第三个内存块。

SVt_PVIV 标量的布局

$ perl -MDevel::Peek -MScalar::Util=refaddr -E'
   my $x=4;
   my $r=\$x;
   say sprintf "refaddr=0x%x", refaddr($r);
   Dump($$r);
   say "";

   say "Upgrade SV:";
   $x='abc';
   say sprintf "refaddr=0x%x", refaddr($r);
   Dump($$r);
   say "";

   say "Increase PV size:";
   $x="x"x20;
   say sprintf "refaddr=0x%x", refaddr($r);
   Dump($$r);
'

refaddr=0x2e1db58
SV = IV(0x2e1db48) at 0x2e1db58             <-- SVt_IV variables can't hold strings.
  REFCNT = 2
  FLAGS = (PADMY,IOK,pIOK)
  IV = 4

Upgrade SV:
refaddr=0x2e1db58
SV = PVIV(0x2e18b40) at 0x2e1db58           <-- Scalar upgrade to SVt_PVIV.
  REFCNT = 2                                    New body at new address,
  FLAGS = (PADMY,POK,IsCOW,pPOK)                but head still at same address.
  IV = 4
  PV = 0x2e86f20 "abc"\0                    <-- The scalar now has a string buffer.
  CUR = 3
  LEN = 10
  COW_REFCNT = 1

Increase PV size:
refaddr=0x2e1db58
SV = PVIV(0x2e18b40) at 0x2e1db58
  REFCNT = 2
  FLAGS = (PADMY,POK,pPOK)
  IV = 4
  PV = 0x2e5d7b0 "xxxxxxxxxxxxxxxxxxxx"\0   <-- Changing the address of the string buffer
  REFCNT = 2                                    doesn't change anything else.
  CUR = 20
  LEN = 22
于 2015-02-27T11:20:19.527 回答