1 #include "ecore_private.h"
2 #include "Ecore_Data.h"
4 static void ecore_string_free_cb(void *data);
6 static Ecore_Hash *ecore_strings = NULL;
7 static int ecore_string_init_count = 0;
10 * @defgroup Ecore_String_Group String Instance Functions
12 * These functions allow you to store one copy of a string, and use it
13 * throughout your program.
15 * This is a method to reduce the number of duplicated strings kept in
16 * memory. It's pretty common for the same strings to be dynamically
17 * allocated repeatedly between applications and libraries, especially in
18 * circumstances where you could have multiple copies of a structure that
19 * allocates the string. So rather than duplicating and freeing these
20 * strings, you request a read-only pointer to an existing string and
21 * only incur the overhead of a hash lookup.
23 * It sounds like micro-optimizing, but profiling has shown this can have
24 * a significant impact as you scale the number of copies up. It improves
25 * string creation/destruction speed, reduces memory use and decreases
26 * memory fragmentation, so a win all-around.
30 * Initialize the ecore string internal structure.
31 * @return Zero on failure, non-zero on successful initialization.
37 * No strings have been loaded at this point, so create the hash
38 * table for storing string info for later.
40 if (!ecore_string_init_count)
42 ecore_strings = ecore_hash_new(ecore_str_hash, ecore_str_compare);
45 ecore_hash_free_value_cb_set(ecore_strings, ecore_string_free_cb);
47 ecore_string_init_count++;
53 * Retrieves an instance of a string for use in an ecore program.
54 * @param string The string to retrieve an instance of.
55 * @return A pointer to an instance of the string on success.
57 * @ingroup Ecore_String_Group
60 ecore_string_instance(const char *string)
64 CHECK_PARAM_POINTER_RETURN("string", string, NULL);
67 * Check for a previous instance of the string, if not found, create
70 str = ecore_hash_get(ecore_strings, string);
76 * Allocate and initialize a new string reference.
78 length = strlen(string) + 1;
80 str = (Ecore_String *)malloc(sizeof(Ecore_String) + length * sizeof(char));
82 str->string = (char*)(str + 1);
85 memcpy(str->string, string, length);
87 ecore_hash_set(ecore_strings, str->string, str);
96 * Notes that the given string has lost an instance.
98 * It will free the string if no other instances are left.
100 * @param string The given string.
101 * @ingroup Ecore_String_Group
104 ecore_string_release(const char *string)
108 CHECK_PARAM_POINTER("string", string);
110 str = ecore_hash_get(ecore_strings, (char *)string);
115 if (str->references < 1)
117 ecore_hash_remove(ecore_strings, (char *)string);
123 ecore_string_hash_dump_graph(void)
125 ecore_hash_dump_graph(ecore_strings);
129 ecore_string_hash_dump_stats(void)
131 ecore_hash_dump_stats(ecore_strings);
135 * Shutdown the ecore string internal structures
138 ecore_string_shutdown()
140 --ecore_string_init_count;
141 if (!ecore_string_init_count)
143 ecore_hash_destroy(ecore_strings);
144 ecore_strings = NULL;
149 ecore_string_free_cb(void *data)