On 10/04/2013 00:57, Marvin Humphrey wrote:
Similarly, in our Perl build, I'd like Clownfish::Obj to extend Perl's SV.
struct cfish_Obj {
void *sv_any;
U32 sv_refcnt;
U32 sv_flags;
union { void *ptr; IV iv; } sv_union;
cfish_VTable *vtable;
};
cfish_Obj*
cfish_VTable_make_obj(cfish_VTable *self) {
// Allocate an empty, undef SV but with a greater size.
char *raw;
Newxz(raw, self->obj_alloc_size, char); // Newxz to memzero object.
SV *obj_as_sv = (SV*)raw;
I don't think this will work as expected. The SV heads in Perl are
fixed-size and allocated from a special "arena". If an SV head is freed,
it's chained in a free list and possibly reused later. Allocating an SV
head via Newx might work technically, but when reusing the SV head,
memory beyond the standard SV struct will be wasted. Also, an SV
allocated this way will not be destroyed during global destruction.
Nick