This is an old revision of the document!
To minimize memory use, Crossfire uses a shared string mechanism. This is intended to be used for strings whose contents is not expected to change during the server's life. Using shared strings saves memory and improved performance.
Those strings are used like regular const char*
, but should not be changed via strcpy
, snprintf
or other means because it would affect all structures using the shared string. Internally, they use a counter to track at how many places they are used. When it reaches 0, string is freed.
To alter such a string, the right behaviour is:
char[] new_value
variablefree_string(str)
str = add_string(new_value)
replace with sstring when done in code
To manipulate them, the following functions (defined in common/shstr.c
) are available:
const char* add_string(const char *str)
, used to share a string. Returned value is a string with the same contents that can be shared with other objectsconst char* add_refcount(const char *str)
, equivalent of add_string
when the string is already a shared stringconst char* find_string(const char *str)
, to search if a string is shared or notvoid free_string(const char *str)
, to decrement the use counter. After it's called, str
should not be used anymore.
Because shared strings will return a pointer to a string if it exists, one can see if the strings are equal by comparing the shared string address they point to (ie str == str2
directly).