Discussion:
Inline::C-Cookbook's "Object Oriented Inline"
(too old to reply)
s***@optusnet.com.au
2007-08-29 02:23:02 UTC
Permalink
Hi,
The Inline::C cookbook presents an example that contains the following new() function which creates the Soldier object:

----------------------------------
SV* new(char* class, char* name, char* rank, long serial) {
Soldier* soldier;
New(42, soldier, 1, Soldier);
SV* obj_ref = newSViv(0);
SV* obj = newSVrv(obj_ref, class);

soldier->name = savepv(name);
soldier->rank = savepv(rank);
soldier->serial = serial;

sv_setiv(obj, (IV)soldier);
SvREADONLY_on(obj);
return obj_ref;
}
---------------------------------

A while back, at http://www.mail-archive.com/***@perl.org/msg01370.html Ken Williams suggested the following (more succinct) rewrite:

---------------------------------
SV* new(char* class, char* name, char* rank, long serial) {
Soldier* soldier;
New(42, soldier, 1, Soldier);

soldier->name = savepv(name);
soldier->rank = savepv(rank);
soldier->serial = serial;

return sv_setref_pv(newSViv(0), class, (void *)soldier);
}
----------------------------------

But the 2 versions of new() return slightly different objects. The original (cookbook) version returns this:

----------------------------------
SV = PVIV(0x2f5b8c) at 0x2f4940
REFCNT = 1
FLAGS = (PADBUSY,PADMY,ROK)
IV = 0
RV = 0x2f3ca4
SV = PVMG(0x2febbc) at 0x2f3ca4
REFCNT = 1
FLAGS = (OBJECT,IOK,READONLY,pIOK)
IV = 3143860
NV = 0
PV = 0
STASH = 0x45fca8 "Soldier"
PV = 0x2f3ca4 ""
CUR = 0
LEN = 0
----------------------------------

But, in Ken's rewrite, that READONLY flag is absent in the returned object.

1) What are the likely/possible ramifications of that READONLY flag being unset ? (I note that the cookbook version explicitly sets that READONLY flag ... presumably for a good reason.)

2) How does one modify Ken's rewrite so that it sets that READONLY flag ?

I recently asked about this at perlmonks ( http://www.perlmonks.org/index.pl?node_id=635113 ) but didn't receive any responses on the matter.

It's doubtful that I really *need* to know the answers (as I'm quite happy to stick with the cookbook approach) but I am, nevertheless, curious about it.

Note: I've also changed a couple of things in the versions of new() that I've presented above. The original versions used strdup(), which I've replaced with savepv() - and they used malloc(), which I've replaced with new().

Cheers,
Rob
Sisyphus
2007-09-06 02:34:18 UTC
Permalink
----- Original Message -----
From: "Steve Fink" <***@gmail.com>
To: <***@optusnet.com.au>
Cc: <perl-***@perl.org>
Sent: Thursday, September 06, 2007 6:22 AM
Subject: Re: Inline::C-Cookbook's "Object Oriented Inline"
http://www.perlmonks.org/index.pl?node_id=637251
Thanks for that Steve. It answers the questions I asked quite nicely. I'll
probably post a reply to perlmonks after I've properly digested your post.

The entire thread (including Steve's reply) is visible at
http://www.perlmonks.org/index.pl?node_id=635113 for anyone interested.
From what I've been able to figure out (or fool myself into thinking
I've figured out), the Cookbook needs an update.
Yes - it, along with the rest of the Inline::C distro, is definitely in need
of an update.

Cheers,
Rob
Steve Fink
2007-09-05 20:22:18 UTC
Permalink
I replied to this on PerlMonks: http://www.perlmonks.org/index.pl?node_id=637251

From what I've been able to figure out (or fool myself into thinking
I've figured out), the Cookbook needs an update.

Loading...