1 /* EINA - EFL data type library
2 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2010
4 * Jorge Luis Zapata Muga,
6 * Gustavo Sverzut Barbieri
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library;
22 * if not, see <http://www.gnu.org/licenses/>.
31 #elif defined __GNUC__
32 # define alloca __builtin_alloca
34 # define alloca __alloca
35 #elif defined _MSC_VER
37 # define alloca _alloca
43 void *alloca (size_t);
54 #include "eina_config.h"
55 #include "eina_private.h"
56 #include "eina_error.h"
58 #include "eina_lock.h"
59 #include "eina_share_common.h"
61 /* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
62 #include "eina_safety_checks.h"
63 #include "eina_stringshare.h"
69 #define CRITICAL(...) EINA_LOG_DOM_CRIT(_eina_share_stringshare_log_dom, __VA_ARGS__)
74 #define ERR(...) EINA_LOG_DOM_ERR(_eina_share_stringshare_log_dom, __VA_ARGS__)
79 #define DBG(...) EINA_LOG_DOM_DBG(_eina_share_stringshare_log_dom, __VA_ARGS__)
81 static int _eina_share_stringshare_log_dom = -1;
83 /* The actual share */
84 static Eina_Share *stringshare_share;
85 static const char EINA_MAGIC_STRINGSHARE_NODE_STR[] = "Eina Stringshare Node";
87 extern Eina_Bool _share_common_threads_activated;
88 static Eina_Lock _mutex_small;
90 /* Stringshare optimizations */
91 static const unsigned char _eina_stringshare_single[512] = {
92 0,0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,14,0,15,0,
93 16,0,17,0,18,0,19,0,20,0,21,0,22,0,23,0,24,0,25,0,26,0,27,0,28,0,29,0,30,0,
94 31,0,32,0,33,0,34,0,35,0,36,0,37,0,38,0,39,0,40,0,41,0,42,0,43,0,44,0,45,0,
95 46,0,47,0,48,0,49,0,50,0,51,0,52,0,53,0,54,0,55,0,56,0,57,0,58,0,59,0,60,0,
96 61,0,62,0,63,0,64,0,65,0,66,0,67,0,68,0,69,0,70,0,71,0,72,0,73,0,74,0,75,0,
97 76,0,77,0,78,0,79,0,80,0,81,0,82,0,83,0,84,0,85,0,86,0,87,0,88,0,89,0,90,0,
98 91,0,92,0,93,0,94,0,95,0,96,0,97,0,98,0,99,0,100,0,101,0,102,0,103,0,104,0,
100 106,0,107,0,108,0,109,0,110,0,111,0,112,0,113,0,114,0,115,0,116,0,117,0,118,
102 121,0,122,0,123,0,124,0,125,0,126,0,127,0,128,0,129,0,130,0,131,0,132,0,133,
104 136,0,137,0,138,0,139,0,140,0,141,0,142,0,143,0,144,0,145,0,146,0,147,0,148,
106 151,0,152,0,153,0,154,0,155,0,156,0,157,0,158,0,159,0,160,0,161,0,162,0,163,
108 166,0,167,0,168,0,169,0,170,0,171,0,172,0,173,0,174,0,175,0,176,0,177,0,178,
110 181,0,182,0,183,0,184,0,185,0,186,0,187,0,188,0,189,0,190,0,191,0,192,0,193,
112 196,0,197,0,198,0,199,0,200,0,201,0,202,0,203,0,204,0,205,0,206,0,207,0,208,
114 211,0,212,0,213,0,214,0,215,0,216,0,217,0,218,0,219,0,220,0,221,0,222,0,223,
116 226,0,227,0,228,0,229,0,230,0,231,0,232,0,233,0,234,0,235,0,236,0,237,0,238,
118 241,0,242,0,243,0,244,0,245,0,246,0,247,0,248,0,249,0,250,0,251,0,252,0,253,
122 typedef struct _Eina_Stringshare_Small Eina_Stringshare_Small;
123 typedef struct _Eina_Stringshare_Small_Bucket Eina_Stringshare_Small_Bucket;
125 struct _Eina_Stringshare_Small_Bucket
127 /* separate arrays for faster lookups */
128 const char **strings;
129 unsigned char *lengths;
130 unsigned short *references;
135 struct _Eina_Stringshare_Small
137 Eina_Stringshare_Small_Bucket *buckets[256];
140 #define EINA_STRINGSHARE_SMALL_BUCKET_STEP 8
141 static Eina_Stringshare_Small _eina_small_share;
144 _eina_stringshare_small_cmp(const Eina_Stringshare_Small_Bucket *bucket,
147 unsigned char plength)
149 /* pstr and plength are from second char and on, since the first is
152 * First string being always the same, size being between 2 and 3
153 * characters (there is a check for special case length==1 and then
154 * small stringshare is applied to strings < 4), we just need to
155 * compare 2 characters of both strings.
157 const unsigned char cur_plength = bucket->lengths[i] - 1;
158 const char *cur_pstr;
160 if (cur_plength > plength)
162 else if (cur_plength < plength)
165 cur_pstr = bucket->strings[i] + 1;
167 if (cur_pstr[0] > pstr[0])
169 else if (cur_pstr[0] < pstr[0])
175 if (cur_pstr[1] > pstr[1])
177 else if (cur_pstr[1] < pstr[1])
184 _eina_stringshare_small_bucket_find(const Eina_Stringshare_Small_Bucket *bucket,
186 unsigned char length,
189 const char *pstr = str + 1; /* skip first letter, it's always the same */
190 unsigned char plength = length - 1;
193 if (bucket->count == 0)
200 high = bucket->count;
206 i = (low + high - 1) / 2;
208 r = _eina_stringshare_small_cmp(bucket, i, pstr, plength);
216 return bucket->strings[i];
225 _eina_stringshare_small_bucket_resize(Eina_Stringshare_Small_Bucket *bucket,
230 tmp = realloc((void *)bucket->strings, size * sizeof(bucket->strings[0]));
233 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
237 bucket->strings = tmp;
239 tmp = realloc(bucket->lengths, size * sizeof(bucket->lengths[0]));
242 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
246 bucket->lengths = tmp;
248 tmp = realloc(bucket->references, size * sizeof(bucket->references[0]));
251 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
255 bucket->references = tmp;
262 _eina_stringshare_small_bucket_insert_at(
263 Eina_Stringshare_Small_Bucket **p_bucket,
265 unsigned char length,
268 Eina_Stringshare_Small_Bucket *bucket = *p_bucket;
274 *p_bucket = bucket = calloc(1, sizeof(*bucket));
277 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
282 if (bucket->count + 1 >= bucket->size)
284 int size = bucket->size + EINA_STRINGSHARE_SMALL_BUCKET_STEP;
285 if (!_eina_stringshare_small_bucket_resize(bucket, size))
289 snew = malloc(length + 1);
292 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
296 memcpy(snew, str, length);
300 todo = bucket->count - idx;
303 memmove((void *)(bucket->strings + off), bucket->strings + idx,
304 todo * sizeof(bucket->strings[0]));
305 memmove(bucket->lengths + off, bucket->lengths + idx,
306 todo * sizeof(bucket->lengths[0]));
307 memmove(bucket->references + off, bucket->references + idx,
308 todo * sizeof(bucket->references[0]));
311 bucket->strings[idx] = snew;
312 bucket->lengths[idx] = length;
313 bucket->references[idx] = 1;
320 _eina_stringshare_small_bucket_remove_at(
321 Eina_Stringshare_Small_Bucket **p_bucket,
324 Eina_Stringshare_Small_Bucket *bucket = *p_bucket;
327 if (bucket->references[idx] > 1)
329 bucket->references[idx]--;
333 free((char *)bucket->strings[idx]);
335 if (bucket->count == 1)
337 free((void *)bucket->strings);
338 free(bucket->lengths);
339 free(bucket->references);
346 if (idx == bucket->count)
350 todo = bucket->count - idx;
352 memmove((void *)(bucket->strings + idx), bucket->strings + off,
353 todo * sizeof(bucket->strings[0]));
354 memmove(bucket->lengths + idx, bucket->lengths + off,
355 todo * sizeof(bucket->lengths[0]));
356 memmove(bucket->references + idx, bucket->references + off,
357 todo * sizeof(bucket->references[0]));
360 if (bucket->count + EINA_STRINGSHARE_SMALL_BUCKET_STEP < bucket->size)
362 int size = bucket->size - EINA_STRINGSHARE_SMALL_BUCKET_STEP;
363 _eina_stringshare_small_bucket_resize(bucket, size);
368 _eina_stringshare_small_add(const char *str, unsigned char length)
370 Eina_Stringshare_Small_Bucket **bucket;
373 bucket = _eina_small_share.buckets + (unsigned char)str[0];
379 ret = _eina_stringshare_small_bucket_find(*bucket, str, length, &i);
382 (*bucket)->references[i]++;
387 return _eina_stringshare_small_bucket_insert_at(bucket, str, length, i);
391 _eina_stringshare_small_del(const char *str, unsigned char length)
393 Eina_Stringshare_Small_Bucket **bucket;
397 bucket = _eina_small_share.buckets + (unsigned char)str[0];
401 ret = _eina_stringshare_small_bucket_find(*bucket, str, length, &i);
405 _eina_stringshare_small_bucket_remove_at(bucket, i);
409 CRITICAL("EEEK trying to del non-shared stringshare \"%s\"", str);
413 _eina_stringshare_small_init(void)
415 eina_lock_new(&_mutex_small);
416 memset(&_eina_small_share, 0, sizeof(_eina_small_share));
420 _eina_stringshare_small_shutdown(void)
422 Eina_Stringshare_Small_Bucket **p_bucket, **p_bucket_end;
424 p_bucket = _eina_small_share.buckets;
425 p_bucket_end = p_bucket + 256;
427 for (; p_bucket < p_bucket_end; p_bucket++)
429 Eina_Stringshare_Small_Bucket *bucket = *p_bucket;
435 s = (char **)bucket->strings;
436 s_end = s + bucket->count;
437 for (; s < s_end; s++)
440 free((void *)bucket->strings);
441 free(bucket->lengths);
442 free(bucket->references);
447 eina_lock_free(&_mutex_small);
451 _eina_stringshare_small_bucket_dump(Eina_Stringshare_Small_Bucket *bucket,
454 const char **s = bucket->strings;
455 unsigned char *l = bucket->lengths;
456 unsigned short *r = bucket->references;
459 di->used += sizeof(*bucket);
460 di->used += bucket->count * sizeof(*s);
461 di->used += bucket->count * sizeof(*l);
462 di->used += bucket->count * sizeof(*r);
463 di->unique += bucket->count;
465 for (i = 0; i < bucket->count; i++, s++, l++, r++)
469 printf("DDD: %5hhu %5hu '%s'\n", *l, *r, *s);
474 di->saved += *l * dups;
480 _eina_stringshare_small_dump(struct dumpinfo *di)
482 Eina_Stringshare_Small_Bucket **p_bucket, **p_bucket_end;
484 p_bucket = _eina_small_share.buckets;
485 p_bucket_end = p_bucket + 256;
487 for (; p_bucket < p_bucket_end; p_bucket++)
489 Eina_Stringshare_Small_Bucket *bucket = *p_bucket;
494 _eina_stringshare_small_bucket_dump(bucket, di);
499 /*============================================================================*
501 *============================================================================*/
505 * @brief Initialize the share_common module.
507 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
509 * This function sets up the share_common module of Eina. It is called by
515 eina_stringshare_init(void)
519 if (_eina_share_stringshare_log_dom < 0)
521 _eina_share_stringshare_log_dom = eina_log_domain_register
522 ("eina_stringshare", EINA_LOG_COLOR_DEFAULT);
524 if (_eina_share_stringshare_log_dom < 0)
526 EINA_LOG_ERR("Could not register log domain: eina_stringshare");
531 ret = eina_share_common_init(&stringshare_share,
532 EINA_MAGIC_STRINGSHARE_NODE,
533 EINA_MAGIC_STRINGSHARE_NODE_STR);
535 _eina_stringshare_small_init();
538 eina_log_domain_unregister(_eina_share_stringshare_log_dom);
539 _eina_share_stringshare_log_dom = -1;
547 * @brief Shut down the share_common module.
549 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
551 * This function shuts down the share_common module set up by
552 * eina_share_common_init(). It is called by eina_shutdown().
554 * @see eina_shutdown()
557 eina_stringshare_shutdown(void)
560 _eina_stringshare_small_shutdown();
561 ret = eina_share_common_shutdown(&stringshare_share);
563 if (_eina_share_stringshare_log_dom >= 0)
565 eina_log_domain_unregister(_eina_share_stringshare_log_dom);
566 _eina_share_stringshare_log_dom = -1;
572 /*============================================================================*
574 *============================================================================*/
577 eina_stringshare_del(Eina_Stringshare *str)
587 else if (str[1] == '\0')
589 else if (str[2] == '\0')
591 else if (str[3] == '\0')
594 slen = 4; /* handled later */
600 eina_share_common_population_del(stringshare_share, slen);
601 eina_lock_take(&_mutex_small);
602 _eina_stringshare_small_del(str, slen);
603 eina_lock_release(&_mutex_small);
607 if (!eina_share_common_del(stringshare_share, str))
608 CRITICAL("EEEK trying to del non-shared stringshare \"%s\"", str);
611 EAPI Eina_Stringshare *
612 eina_stringshare_add_length(const char *str, unsigned int slen)
614 if ((!str) || (slen <= 0))
617 return (Eina_Stringshare *) _eina_stringshare_single + ((*str) << 1);
622 eina_lock_take(&_mutex_small);
623 s = _eina_stringshare_small_add(str, slen);
624 eina_lock_release(&_mutex_small);
628 return eina_share_common_add_length(stringshare_share, str, slen *
629 sizeof(char), sizeof(char));
632 EAPI Eina_Stringshare *
633 eina_stringshare_add(const char *str)
641 else if (str[1] == '\0')
643 else if (str[2] == '\0')
645 else if (str[3] == '\0')
648 slen = 3 + (int)strlen(str + 3);
650 return eina_stringshare_add_length(str, slen);
653 EAPI Eina_Stringshare *
654 eina_stringshare_printf(const char *fmt, ...)
665 len = vasprintf(&tmp, fmt, args);
671 ret = eina_stringshare_add_length(tmp, len);
677 EAPI Eina_Stringshare *
678 eina_stringshare_vprintf(const char *fmt, va_list args)
687 len = vasprintf(&tmp, fmt, args);
692 ret = eina_stringshare_add_length(tmp, len);
698 EAPI Eina_Stringshare *
699 eina_stringshare_nprintf(unsigned int len, const char *fmt, ...)
711 tmp = alloca(sizeof(char) * len + 1);
714 size = vsnprintf(tmp, len, fmt, args);
720 return eina_stringshare_add_length(tmp, len);
723 EAPI Eina_Stringshare *
724 eina_stringshare_ref(Eina_Stringshare *str)
729 return eina_share_common_ref(stringshare_share, str);
734 else if (str[1] == '\0')
736 else if (str[2] == '\0')
738 else if (str[3] == '\0')
741 slen = 3 + (int)strlen(str + 3);
745 eina_share_common_population_add(stringshare_share, slen);
752 eina_share_common_population_add(stringshare_share, slen);
754 eina_lock_take(&_mutex_small);
755 s = _eina_stringshare_small_add(str, slen);
756 eina_lock_release(&_mutex_small);
761 return eina_share_common_ref(stringshare_share, str);
765 eina_stringshare_strlen(Eina_Stringshare *str)
784 len = eina_share_common_length(stringshare_share, (Eina_Stringshare *) str);
785 len = (len > 0) ? len / (int)sizeof(char) : -1;
790 eina_stringshare_dump(void)
792 eina_share_common_dump(stringshare_share,
793 _eina_stringshare_small_dump,
794 sizeof(_eina_stringshare_single));