Re: [Plib-devel] psl copy constructor.
Brought to you by:
sjbaker
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(); + } |