diff options
Diffstat (limited to 'libraries/eina/src/include/eina_stringshare.h')
-rw-r--r-- | libraries/eina/src/include/eina_stringshare.h | 321 |
1 files changed, 321 insertions, 0 deletions
diff --git a/libraries/eina/src/include/eina_stringshare.h b/libraries/eina/src/include/eina_stringshare.h new file mode 100644 index 0000000..af58add --- /dev/null +++ b/libraries/eina/src/include/eina_stringshare.h | |||
@@ -0,0 +1,321 @@ | |||
1 | /* EINA - EFL data type library | ||
2 | * Copyright (C) 2002-2008 Carsten Haitzler, Jorge Luis Zapata Muga, Cedric Bail | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) any later version. | ||
8 | * | ||
9 | * This library is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | * Lesser General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU Lesser General Public | ||
15 | * License along with this library; | ||
16 | * if not, see <http://www.gnu.org/licenses/>. | ||
17 | * | ||
18 | * This file incorporates work covered by the following copyright and | ||
19 | * permission notice: | ||
20 | * | ||
21 | * Copyright (C) 2008 Peter Wehrfritz | ||
22 | * | ||
23 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
24 | * of this software and associated documentation files (the "Software"), to | ||
25 | * deal in the Software without restriction, including without limitation the | ||
26 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
27 | * sell copies of the Software, and to permit persons to whom the Software is | ||
28 | * furnished to do so, subject to the following conditions: | ||
29 | * | ||
30 | * The above copyright notice and this permission notice shall be included in | ||
31 | * all copies of the Software and its Copyright notices. In addition publicly | ||
32 | * documented acknowledgment must be given that this software has been used if no | ||
33 | * source code of this software is made available publicly. This includes | ||
34 | * acknowledgments in either Copyright notices, Manuals, Publicity and Marketing | ||
35 | * documents or any documentation provided with any product containing this | ||
36 | * software. This License does not apply to any software that links to the | ||
37 | * libraries provided by this software (statically or dynamically), but only to | ||
38 | * the software provided. | ||
39 | * | ||
40 | * Please see the OLD-COPYING.PLAIN for a plain-english explanation of this notice | ||
41 | * and it's intent. | ||
42 | * | ||
43 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
44 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
45 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
46 | * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
47 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
48 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
49 | */ | ||
50 | |||
51 | #ifndef EINA_STRINGSHARE_H_ | ||
52 | #define EINA_STRINGSHARE_H_ | ||
53 | |||
54 | #include <stdarg.h> | ||
55 | |||
56 | #include "eina_types.h" | ||
57 | |||
58 | /** | ||
59 | * @page eina_stringshare_example_01_page | ||
60 | * @dontinclude eina_stringshare_01.c | ||
61 | * | ||
62 | * Like all examples we start by including Eina: | ||
63 | * @skip #include | ||
64 | * @line #include | ||
65 | * | ||
66 | * Here we declare some variables and initialize eina: | ||
67 | * @until eina_init | ||
68 | * | ||
69 | * We start the substantive part of the example by showing how to make a part | ||
70 | * of a string shared and how to get the length of a shared string: | ||
71 | * @until stringshare_strlen | ||
72 | * As we add shared strings we also need to delete them when done using them: | ||
73 | * @line del | ||
74 | * | ||
75 | * There are many ways of creating shared strings including an equivalent to | ||
76 | * sprintf: | ||
77 | * @until del | ||
78 | * | ||
79 | * An equivalent to snprintf: | ||
80 | * @until printf | ||
81 | * | ||
82 | * But the simplest way of creating a shared string is through | ||
83 | * eina_stringshare_add(): | ||
84 | * @until printf | ||
85 | * | ||
86 | * Sometimes you already have a pointer to a shared string and want to use it, | ||
87 | * so to make sure the provider of the pointer won't free it while you're using | ||
88 | * it you can increase the shared string's ref count: | ||
89 | * @until printf | ||
90 | * | ||
91 | * Whenever you have a pointer to a shared string and would like to change it's | ||
92 | * value you should use eina_stringshare_replace(): | ||
93 | * @until printf | ||
94 | * @warning @b Don't use eina_stringshare_del() followed by | ||
95 | * eina_share_common_add(), under some circunstances you might end up deleting | ||
96 | * a shared string some other piece of code is using. | ||
97 | * | ||
98 | * We created str but haven't deleted it yet, and while we called | ||
99 | * eina_stringshare_del() on str2, we created it and then increased the ref | ||
100 | * count so it's still alive: | ||
101 | * @until str2 | ||
102 | * | ||
103 | * You can see the full source code @ref eina_stringshare_example_01 "here". | ||
104 | */ | ||
105 | /** | ||
106 | * @page eina_stringshare_example_01 | ||
107 | * @include eina_stringshare_01.c | ||
108 | * @example eina_stringshare_01.c | ||
109 | */ | ||
110 | /** | ||
111 | * @addtogroup Eina_Stringshare_Group Stringshare | ||
112 | * | ||
113 | * These functions allow you to store a single copy of a string, and use in | ||
114 | * multiple places throughout your program. | ||
115 | * | ||
116 | * This is a method to reduce the number of duplicated strings kept in | ||
117 | * memory. It's pretty common for the same strings to be dynamically | ||
118 | * allocated repeatedly between applications and libraries, especially in | ||
119 | * circumstances where you could have multiple copies of a structure that | ||
120 | * allocates the string. So rather than duplicating and freeing these | ||
121 | * strings, you request a read-only pointer to an existing string and | ||
122 | * only incur the overhead of a hash lookup. | ||
123 | * | ||
124 | * It sounds like micro-optimizing, but profiling has shown this can have | ||
125 | * a significant impact as you scale the number of copies up. It improves | ||
126 | * string creation/destruction speed, reduces memory use and decreases | ||
127 | * memory fragmentation, so a win all-around. | ||
128 | * | ||
129 | * The following diagram gives an idea of what happens as you create strings | ||
130 | * with eina_stringshare_add(): | ||
131 | * | ||
132 | * @image html eina_stringshare.png | ||
133 | * @image latex eina_stringshare.eps height=\textheight | ||
134 | * | ||
135 | * For more information, see @ref eina_stringshare_example_01_page | ||
136 | * "this example". | ||
137 | */ | ||
138 | |||
139 | /** | ||
140 | * @addtogroup Eina_Data_Types_Group Data Types | ||
141 | * | ||
142 | * @{ | ||
143 | */ | ||
144 | |||
145 | /** | ||
146 | * @defgroup Eina_Stringshare_Group Stringshare | ||
147 | * | ||
148 | * @{ | ||
149 | */ | ||
150 | |||
151 | |||
152 | /** | ||
153 | * @brief Retrieve an instance of a string for use in a program. | ||
154 | * | ||
155 | * @param str The string to retrieve an instance of. | ||
156 | * @param slen The string size (<= strlen(str)). | ||
157 | * @return A pointer to an instance of the string on success. | ||
158 | * @c NULL on failure. | ||
159 | * | ||
160 | * This function retrieves an instance of @p str. If @p str is | ||
161 | * @c NULL, then @c NULL is returned. If @p str is already stored, it | ||
162 | * is just returned and its reference counter is increased. Otherwise | ||
163 | * a duplicated string of @p str is returned. | ||
164 | * | ||
165 | * This function does not check string size, but uses the | ||
166 | * exact given size. This can be used to share_common part of a larger | ||
167 | * buffer or substring. | ||
168 | * | ||
169 | * @see eina_share_common_add() | ||
170 | */ | ||
171 | EAPI const char *eina_stringshare_add_length(const char *str, unsigned int slen) EINA_WARN_UNUSED_RESULT; | ||
172 | |||
173 | /** | ||
174 | * @brief Retrieve an instance of a string for use in a program. | ||
175 | * | ||
176 | * @param str The NULL terminated string to retrieve an instance of. | ||
177 | * @return A pointer to an instance of the string on success. | ||
178 | * @c NULL on failure. | ||
179 | * | ||
180 | * This function retrieves an instance of @p str. If @p str is | ||
181 | * @c NULL, then @c NULL is returned. If @p str is already stored, it | ||
182 | * is just returned and its reference counter is increased. Otherwise | ||
183 | * a duplicated string of @p str is returned. | ||
184 | * | ||
185 | * The string @p str must be NULL terminated ('@\0') and its full | ||
186 | * length will be used. To use part of the string or non-null | ||
187 | * terminated, use eina_stringshare_add_length() instead. | ||
188 | * | ||
189 | * @see eina_stringshare_add_length() | ||
190 | */ | ||
191 | EAPI const char *eina_stringshare_add(const char *str) EINA_WARN_UNUSED_RESULT; | ||
192 | |||
193 | /** | ||
194 | * @brief Retrieve an instance of a string for use in a program | ||
195 | * from a format string. | ||
196 | * | ||
197 | * @param fmt The NULL terminated format string to retrieve an instance of. | ||
198 | * @return A pointer to an instance of the string on success. | ||
199 | * @c NULL on failure. | ||
200 | * | ||
201 | * This function retrieves an instance of @p fmt. If @p fmt is | ||
202 | * @c NULL, then @c NULL is returned. If @p fmt is already stored, it | ||
203 | * is just returned and its reference counter is increased. Otherwise | ||
204 | * a duplicated string is returned. | ||
205 | * | ||
206 | * The format string @p fmt must be NULL terminated ('@\0') and its full | ||
207 | * length will be used. To use part of the format string or non-null | ||
208 | * terminated, use eina_stringshare_nprintf() instead. | ||
209 | * | ||
210 | * @see eina_stringshare_nprintf() | ||
211 | */ | ||
212 | EAPI const char *eina_stringshare_printf(const char *fmt, ...) EINA_WARN_UNUSED_RESULT EINA_PRINTF(1, 2); | ||
213 | |||
214 | /** | ||
215 | * @brief Retrieve an instance of a string for use in a program | ||
216 | * from a format string. | ||
217 | * | ||
218 | * @param fmt The NULL terminated format string to retrieve an instance of. | ||
219 | * @param args The va_args for @p fmt | ||
220 | * @return A pointer to an instance of the string on success. | ||
221 | * @c NULL on failure. | ||
222 | * | ||
223 | * This function retrieves an instance of @p fmt with @p args. If @p fmt is | ||
224 | * @c NULL, then @c NULL is returned. If @p fmt with @p args is already stored, it | ||
225 | * is just returned and its reference counter is increased. Otherwise | ||
226 | * a duplicated string is returned. | ||
227 | * | ||
228 | * The format string @p fmt must be NULL terminated ('@\0') and its full | ||
229 | * length will be used. To use part of the format string or non-null | ||
230 | * terminated, use eina_stringshare_nprintf() instead. | ||
231 | * | ||
232 | * @see eina_stringshare_nprintf() | ||
233 | */ | ||
234 | EAPI const char *eina_stringshare_vprintf(const char *fmt, va_list args) EINA_WARN_UNUSED_RESULT; | ||
235 | |||
236 | /** | ||
237 | * @brief Retrieve an instance of a string for use in a program | ||
238 | * from a format string with size limitation. | ||
239 | * @param len The length of the format string to use | ||
240 | * @param fmt The format string to retrieve an instance of. | ||
241 | * @return A pointer to an instance of the string on success. | ||
242 | * @c NULL on failure. | ||
243 | * | ||
244 | * This function retrieves an instance of @p fmt limited by @p len. If @p fmt is | ||
245 | * @c NULL or @p len is < 1, then @c NULL is returned. If the resulting string | ||
246 | * is already stored, it is returned and its reference counter is increased. | ||
247 | * Otherwise a duplicated string is returned. | ||
248 | * | ||
249 | * @p len length of the format string will be used. To use the | ||
250 | * entire format string, use eina_stringshare_printf() instead. | ||
251 | * | ||
252 | * @see eina_stringshare_printf() | ||
253 | */ | ||
254 | EAPI const char *eina_stringshare_nprintf(unsigned int len, const char *fmt, ...) EINA_WARN_UNUSED_RESULT EINA_PRINTF(2, 3); | ||
255 | |||
256 | /** | ||
257 | * Increment references of the given shared string. | ||
258 | * | ||
259 | * @param str The shared string. | ||
260 | * @return A pointer to an instance of the string on success. | ||
261 | * @c NULL on failure. | ||
262 | * | ||
263 | * This is similar to eina_share_common_add(), but it's faster since it will | ||
264 | * avoid lookups if possible, but on the down side it requires the parameter | ||
265 | * to be shared string. In other words, it must be the return of a previous | ||
266 | * call to one of the stringshare functions. | ||
267 | * | ||
268 | * There is no unref since this is the work of eina_share_common_del(). | ||
269 | */ | ||
270 | EAPI const char *eina_stringshare_ref(const char *str); | ||
271 | |||
272 | /** | ||
273 | * @brief Note that the given string has lost an instance. | ||
274 | * | ||
275 | * @param str string The given string. | ||
276 | * | ||
277 | * This function decreases the reference counter associated to @p str | ||
278 | * if it exists. If that counter reaches 0, the memory associated to | ||
279 | * @p str is freed. If @p str is NULL, the function returns | ||
280 | * immediately. | ||
281 | * | ||
282 | * Note that if the given pointer is not shared or NULL, bad things | ||
283 | * will happen, likely a segmentation fault. | ||
284 | */ | ||
285 | EAPI void eina_stringshare_del(const char *str); | ||
286 | |||
287 | /** | ||
288 | * @brief Note that the given string @b must be shared. | ||
289 | * | ||
290 | * @param str the shared string to know the length. It is safe to | ||
291 | * give NULL, in that case -1 is returned. | ||
292 | * | ||
293 | * This function is a cheap way to known the length of a shared | ||
294 | * string. Note that if the given pointer is not shared, bad | ||
295 | * things will happen, likely a segmentation fault. If in doubt, try | ||
296 | * strlen(). | ||
297 | */ | ||
298 | EAPI int eina_stringshare_strlen(const char *str) EINA_PURE EINA_WARN_UNUSED_RESULT; | ||
299 | |||
300 | /** | ||
301 | * @brief Dump the contents of the share_common. | ||
302 | * | ||
303 | * This function dumps all strings in the share_common to stdout with a | ||
304 | * DDD: prefix per line and a memory usage summary. | ||
305 | */ | ||
306 | EAPI void eina_stringshare_dump(void); | ||
307 | |||
308 | static inline Eina_Bool eina_stringshare_replace(const char **p_str, const char *news) EINA_ARG_NONNULL(1); | ||
309 | static inline Eina_Bool eina_stringshare_replace_length(const char **p_str, const char *news, unsigned int slen) EINA_ARG_NONNULL(1); | ||
310 | |||
311 | #include "eina_inline_stringshare.x" | ||
312 | |||
313 | /** | ||
314 | * @} | ||
315 | */ | ||
316 | |||
317 | /** | ||
318 | * @} | ||
319 | */ | ||
320 | |||
321 | #endif /* EINA_STRINGSHARE_H_ */ | ||