4 #include "Ecore_Data.h"
6 static void ecore_string_free_cb(void *data);
8 static Ecore_Hash *ecore_strings = NULL;
9 static int ecore_string_init_count = 0;
12 * @defgroup Ecore_String_Group String Instance Functions
14 * These functions allow you to store one copy of a string, and use it
15 * throughout your program.
17 * This is a method to reduce the number of duplicated strings kept in
18 * memory. It's pretty common for the same strings to be dynamically
19 * allocated repeatedly between applications and libraries, especially in
20 * circumstances where you could have multiple copies of a structure that
21 * allocates the string. So rather than duplicating and freeing these
22 * strings, you request a read-only pointer to an existing string and
23 * only incur the overhead of a hash lookup.
25 * It sounds like micro-optimizing, but profiling has shown this can have
26 * a significant impact as you scale the number of copies up. It improves
27 * string creation/destruction speed, reduces memory use and decreases
28 * memory fragmentation, so a win all-around.
32 * Initialize the ecore string internal structure.
33 * @return Zero on failure, non-zero on successful initialization.
39 * No strings have been loaded at this point, so create the hash
40 * table for storing string info for later.
42 if (!ecore_string_init_count)
44 ecore_strings = ecore_hash_new(ecore_str_hash, ecore_str_compare);
48 ecore_hash_free_value_cb_set(ecore_strings, ecore_string_free_cb);
51 ecore_string_init_count++;
57 * Retrieves an instance of a string for use in an ecore program.
58 * @param string The string to retrieve an instance of.
59 * @return A pointer to an instance of the string on success.
61 * @ingroup Ecore_String_Group
64 ecore_string_instance(const char *string)
68 CHECK_PARAM_POINTER_RETURN("string", string, NULL);
71 * Check for a previous instance of the string, if not found, create
74 str = ecore_hash_get(ecore_strings, string);
80 * Allocate and initialize a new string reference.
82 length = strlen(string) + 1;
85 (Ecore_String *)malloc(sizeof(Ecore_String) + length * sizeof(char));
87 str->string = (char *)(str + 1);
90 memcpy(str->string, string, length);
92 ecore_hash_set(ecore_strings, str->string, str);
101 * Notes that the given string has lost an instance.
103 * It will free the string if no other instances are left.
105 * @param string The given string.
106 * @ingroup Ecore_String_Group
109 ecore_string_release(const char *string)
113 CHECK_PARAM_POINTER("string", string);
115 str = ecore_hash_get(ecore_strings, (char *)string);
120 if (str->references < 1)
122 ecore_hash_remove(ecore_strings, (char *)string);
128 ecore_string_hash_dump_graph(void)
130 ecore_hash_dump_graph(ecore_strings);
134 ecore_string_hash_dump_stats(void)
136 ecore_hash_dump_stats(ecore_strings);
140 * Shutdown the ecore string internal structures
143 ecore_string_shutdown()
145 --ecore_string_init_count;
146 if (!ecore_string_init_count)
148 ecore_hash_destroy(ecore_strings);
149 ecore_strings = NULL;
154 ecore_string_free_cb(void *data)