Thread: RE: [Plib-devel] psl copy constructor.
Brought to you by:
sjbaker
From: Fay J. F C. AAC/W. <joh...@eg...> - 2004-04-19 15:07:19
|
Jonathan, I put your changes into my copy of PSL and it compiles nicely. The existing code around the changes mentions a need for reference counting and so it appears that what you have done is probably half the solution. Half is better than none, though, and I thank you for your contribution. The combination of your "is_copy" and the mentions of reference counting have gotten me thinking. Would it improve matters to declare "is_copy" to be a static integer (and probably rename it to something like "number_of_objects"), increment it in every constructor, decrement it in the destructor, and delete the compiler and code when its value is zero? I know nothing of PSL and really can't test this out at all. John F. Fay joh...@eg... -----Original Message----- From: pli...@li... [mailto:pli...@li...]On Behalf Of Jonathan Wheare Sent: Sunday, April 18, 2004 6:26 AM To: pli...@li... Subject: [Plib-devel] psl copy constructor. I've been tinkering with psl scripting and written myself a small manager class that will automatically create script instances using the copy constructor if there is already a compiled psl script in existance. this seems to work quite well until it is time to free the script instances. the first one works OK, but subsequent ones have problems. Looking through the code I found that there is no differentiation between instances and the scripts theey are created from when freeing the compiler and code structures. I've written a little bit of code that changes the pslProgram destructor so that it only frees the pslCode and pslCompiler objects on the master copy of a script. it will still cause problems if someone deletes the master copy of a script and continues to use instances created from it, but I can now create and delete ten instances of a script created using the copy constructor without problems. I have attatched a copy of my script at the end of this E-Mail. I would appreciate any feedback that people have. Thanks, Jonathan. <snip> |
From: Jonathan W. <jtw...@in...> - 2004-04-20 08:55:41
|
On Tue, 2004-04-20 at 00:34, Fay John F Contr AAC/WMG wrote: > Jonathan, > > I put your changes into my copy of PSL and it compiles > nicely. The existing code around the changes mentions a need for > reference counting and so it appears that what you have done is > probably half the solution. Half is better than none, though, and I > thank you for your contribution. > John F. Fay > joh...@eg... Thanks for the feedback. I thought about doing a reference count and could implement it for the pslCompiler class fairly easily, but there is also a static array of type pslOpcode containing the bytecode, I've thought about wrapping it in a class or making it a part of pslCompiler, but that would require changing all the references in the pslProgram class to be indirect. I'm willing to have a go at it if no-one has a better idea though. Jonathan |
From: Steve B. <sjb...@ai...> - 2004-04-20 12:01:41
|
Jonathan Wheare wrote: > I thought about doing a reference count and could implement it for the > pslCompiler class fairly easily, but there is also a static array of > type pslOpcode containing the bytecode, I've thought about wrapping it > in a class or making it a part of pslCompiler, but that would require > changing all the references in the pslProgram class to be indirect. We need to be careful around the bytecode stuff because it would be very easy to introduce something that would clobber performance. Reading the bytecode array happens at the innermost levels of the runtime interpreter - so tread VERY carefully! ---------------------------- Steve Baker ------------------------- HomeEmail: <sjb...@ai...> WorkEmail: <sj...@li...> HomePage : http://www.sjbaker.org Projects : http://plib.sf.net http://tuxaqfh.sf.net http://tuxkart.sf.net http://prettypoly.sf.net -----BEGIN GEEK CODE BLOCK----- GCS d-- s:+ a+ C++++$ UL+++$ P--- L++++$ E--- W+++ N o+ K? w--- !O M- V-- PS++ PE- Y-- PGP-- t+ 5 X R+++ tv b++ DI++ D G+ e++ h--(-) r+++ y++++ -----END GEEK CODE BLOCK----- |
From: Jonathan W. <jtw...@in...> - 2004-04-20 13:30:24
|
On Tue, 2004-04-20 at 21:29, Steve Baker wrote: > We need to be careful around the bytecode stuff because it would be > very easy to introduce something that would clobber performance. > Reading the bytecode array happens at the innermost levels of the > runtime interpreter - so tread VERY carefully! OK, I've got a patch that implements ref counting for the pslCompiler class and i've set it to delete the code array when it's destructor is called - This does feel like a hack though, cleaning up an array created by a seperate class. Could the array creation be moved into the pslCompiler constructor? I've written a program that compiles a simple psl script, runs it, makes a copy, deletes the first one and then repeats the process. All three scripts seem to run without a hitch, so I guess the patch does work. Once again I'd appreciate any feedback. Jonathan. Index: pslCompiler.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslCompiler.h,v retrieving revision 1.33 diff -u -r1.33 pslCompiler.h --- pslCompiler.h 6 Jan 2003 05:10:13 -0000 1.33 +++ pslCompiler.h 20 Apr 2004 12:37:52 -0000 @@ -51,6 +51,8 @@ class pslCompiler { + int ref_count; // reference count. Clean up when this reaches zero + /* File I/O and preprocessor */ int getChar () ; @@ -313,6 +315,7 @@ const pslExtension *_extn, const char *_progName ) { + ref_count = 1; program = prog ; progName = ulStrDup ( _progName ) ; @@ -341,6 +344,21 @@ delete [] symtab [ i ] . symbol ; symtab [ i ] . symbol = NULL ; delete [] code_symtab [ i ] . symbol ; code_symtab [ i ] . symbol = NULL ; delete [] forward_ref [ i ] . symbol ; forward_ref [ i ] . symbol = NULL ; + } + } + + void ref() + { + ref_count++; + } + + void deref() + { + ref_count--; + if(ref_count < 1) + { + delete [] code; + delete this; } } Index: pslProgram.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslProgram.cxx,v retrieving revision 1.17 diff -u -r1.17 pslProgram.cxx --- pslProgram.cxx 6 Jan 2003 05:10:14 -0000 1.17 +++ pslProgram.cxx 20 Apr 2004 12:37:52 -0000 @@ -29,7 +29,7 @@ if ( ! _pslInitialised ) ulSetError ( UL_FATAL, "PSL: You didn't call pslInit() before using PSL functions." ) ; - + code = new pslOpcode [ MAX_CODE ] ; extensions = ext ; @@ -64,17 +64,17 @@ progName = NULL ; if ( _prgnm == NULL ) _prgnm = src -> getProgName () ; - + setProgName ( _prgnm ) ; /* This will fail if this pslProgram is ever deleted */ /* We need ref-counting on code/compiler */ - + code = src -> getCode () ; compiler = src -> getCompiler () ; extensions = src -> getExtensions () ; userData = src -> getUserData () ; - + compiler -> ref(); context = new pslContext ( this ) ; context -> reset () ; } @@ -86,9 +86,9 @@ /* We need ref-counting on code/compiler */ /* DEBUG-ME! */ delete [] progName ; - delete compiler ; delete context ; - delete [] code ; + compiler -> deref(); + } |