diotalevi has asked for the wisdom of the Perl Monks concerning the following question:
I have a B::*OP object which is a pointer to an OP object in a specific location in memory ($$op). I would like to copy the OP object to some other location, I don't really care where, and then install something else into the location that I just copied the OP out of. Other things that already have a pointer to this op would now have a pointer to the new thing which might itself point to the old thing.
Not all OP objects are the same size so do I have to be concerned that a 28 byte hole would have to accomodate a 56 byte op? Is there slack space built in?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Moving an OP and replacing it
by tachyon (Chancellor) on Jun 01, 2004 at 02:56 UTC | |
Not all OP objects are the same size so do I have to be concerned that a 28 byte hole would have to accomodate a 56 byte op? Is there slack space built in? Put the question in C terms. If you wanted 28 bytes to store a C struct and had an array of such structs in memory would you: Now I don't know the real answer but I would be a little suprised if writing 56 bytes into a 28 byte hole did not overwrite the neighbouring 28 byte struct. Why have 28 and 56 byte structs if you always allocate 56 (which is what you are asking for)? The only way I could see it working is if you replace the old object with a new object (less than or equal in size to the old) that points to a freshly allocated OP object in a free chunk of memory. cheers tachyon | [reply] |
by diotalevi (Canon) on Jun 01, 2004 at 02:59 UTC | |
| [reply] |
by tachyon (Chancellor) on Jun 01, 2004 at 03:43 UTC | |
If you have a look at memory allocation and the op tree with -MO=Debug then you can see that Perl appears to be allocating space in line with requirements. You can see ops 60 32 40 32 40 124 40 76 bytes appart in physical memory.... Pot luck not overwriting a neighbouring op I would have thought:
cheers tachyon | [reply] [d/l] |
|
Re: Moving an OP and replacing it
by TomDLux (Vicar) on Jun 01, 2004 at 02:07 UTC | |
In C, it is sometimes appropriate to fiddle with the allocation of objects. In perl, the natural paradigm is to allow garbage collection to eliminate obsolete data, and new objects to be allocated where they will. Why do you want to allocate things in particular locations? Maybe you should create an object with Inline::C handling the details, if you REALLY need it ... but I bet you don't need it. -- | [reply] |
by diotalevi (Canon) on Jun 01, 2004 at 02:19 UTC | |
| [reply] |
|
Re: Moving an OP and replacing it
by tilly (Archbishop) on Jun 01, 2004 at 07:33 UTC | |
How are you going to recognize pointers to the thing that you're adjusting? Are you just going to do a substitution and pray that anything whose bytes are the address of that location really is a pointer to that location? Sure it is only 1 in 4 billion odds of being wrong, for each place you start a set of 4 bytes. Which means that if you did this on a process that was at 40 MB, you'd have 1% odds of randomly going wrong. And when you did go wrong, good luck debugging it! | [reply] |
by diotalevi (Canon) on Jun 01, 2004 at 13:28 UTC | |
Oh that'd be silly - to just substr the process memory and just look for strings that were identical to the packed pointer. Here's I've half-copied, half-sketched the code that I'm running in http://lik.grenekatz.org/downloads/perl/Devel/UncheckedOps.pm.
| [reply] [d/l] |