To minimize memory use, Crossfire uses a shared string mechanism.
This is intended to be used for strings whose contents are 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)
To manipulate them, the following functions defined in
common/shstr.c ← file location
are available:
const char* add_string(const char *str)
sstring add_string(const char *str)
str
that can be shared with other objects.const char* add_refcount(const char *str)
sstring add_refcount(sstring str)
add_string
when the string is already a shared string. const char* find_string(const char *str)
sstring find_string(const char *str)
void free_string(const char *str)
void free_string(sstring str)
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.
sstring is defined in server/include/shstr.h since at least version 1.11.0 :
/** * One actual shared string. */ typedef struct _shared_string { union { struct _shared_string **array; struct _shared_string *previous; } u; struct _shared_string *next; /* The top bit of "refcount" is used to signify that "u.array" points * at the array entry. */ unsigned REFCOUNT_TYPE refcount; /* Padding will be unused memory, since we can't know how large * the padding when allocating memory. We assume here that * sizeof(long) is a good boundary. */ char string[PADDING]; } shared_string;
/**
* @file shstr.c
* This is a simple shared strings package with a simple interface.
*
* Author: Kjetil T. Homme, Oslo 1992.
*/