/
MAILING #2

-=- New String Manager for Envy (and Merc) -=-

Replacement code for :
str_dup(), free_string(), fread_string()


The new functions are in ssm.c, or you may be extracting it from mail
so name it what you want.

- Edit the Makefile and add ssm.o

As I recall there is only 1 file that needs to be modified and that
is db.c, now let me do the patch and find out...

<<<<<   db.c  >>>>>
  ----
(1) - Comment out the functions str_dup(), free_string(), and fread_string()
      fread_string has comments so be careful to comment in blocks

(2) - Add the function prototype for init_string_space() to the
      Semi-locals. - string search for 'Semi-loc'
      Also add extern for MAX_STRING which is no lonfer a #define

      Add lines:

===========
extern  int MAX_STRING;
void	init_string_space	args( ( void ) );
===========

      You can now remove MAX_STRING #define and string_hash table.

Line 189 or so:

===========
#define			MAX_STRING	150000
===========

Line 164 or so:

===========
char *			string_hash		[ MAX_KEY_HASH	     ];
===========

      Or you may want to keep them in case you decide to go back to old
      code (If you do your nuts :P)

(3) - Go to boot_db() , the first thing in boot_db() is where
      the read-only string space is calloc'd :

===========
    /*
     * Init some data space stuff.
     */
    { 
>       if ( !( string_space = calloc( 1, MAX_STRING ) ) )
>       {
>           bug( "Boot_db: can't alloc %d string space.", MAX_STRING );
>           exit( 1 );
>       }
>       top_string	= string_space;
        fBootDb		= TRUE;
    }
===========

Changes to: 

===========
    /*
     * Init some data space stuff.
     */
    { 
>       init_string_space();
        fBootDb		= TRUE;
    }
===========

init_string_space() either succeeds or exits.


(4) - Now we have the functions in place.
      There are still some reliances on strings never being freed.
      Functions like create_mobile() just assign the pointers
      instead of calling str_dup(). That USED to be OK but now the
      str_dup() is real so we have to call it. All this really does
      is updates the usage counter on the string so it won't be freed
      with references still pointing to it. fread_string() is fine since
      the new fread_string() returns a str_dup'd string.

First in create_mobile():

===========
>   mob->name 		= pMobIndex->player_name;
>   mob->short_descr    = pMobIndex->short_descr;
>   mob->long_descr     = pMobIndex->long_descr;
>   mob->description    = pMobIndex->description;
===========

changes to str_dup's

===========
    mob->name 		= str_dup( pMobIndex->player_name );
    mob->short_descr    = str_dup( pMobIndex->short_descr );
    mob->long_descr     = str_dup( pMobIndex->long_descr );
    mob->description    = str_dup( pMobIndex->description );
===========

Then in create_object():

===========
>   obj->name		= pObjIndex->name;
>   obj->short_descr	= pObjIndex->short_descr;
>   obj->description	= pObjIndex->description;
===========

changes to str_dup's

===========
    obj->name		= str_dup( pObjIndex->name );
    obj->short_descr	= str_dup( pObjIndex->short_descr );
    obj->description	= str_dup( pObjIndex->description );
===========

NOTE:  In ssm.c you probably need to tweak MAX_STRING some.
       I set it to 2000000 because not only does my code take a little
       more space on the heap but it is can now be used in place of
       the real C strdup() so leave some work space. If you are running
       a low RAM machine, you can get by with something like 1300000
       or so. Tweak it until you get a BUG log, then you know you are
       tight, and back off a little. Remember you lose all benefits
       of the manager as soon as it switches to allocation mode
       at which time no further sharing is done for new allocations.

This is all that needs to be changed in the base Envy and it should be
identical for Merc. I have no idea what ROM's string code is like so
I can't say, but if it is the standard no-freeing Merc code then you
may want to think about adding my patch. Using OLC now should have
no bearing on the runtime (gradual buildup of read-only strings).
I am adding the code to Jason's Envy release after this post but
Im thinking that there is nothing that needs changing.

Let me know if you have problems or if I missed something.
Lost Realms has been running for months with this code without
any data corruption or dangling pointers.

Fusion
msmith@falcon.mercer.peachnet.edu