From 126637fee4b1c8ae216d51eef4a9108a7aca60f2 Mon Sep 17 00:00:00 2001 From: raster Date: Mon, 27 Oct 2008 00:36:22 +0000 Subject: [PATCH] add a stringshare dumper so we can examine what's in there and improve usage. git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@37186 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- src/include/eina_stringshare.h | 3 ++- src/lib/eina_stringshare.c | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/include/eina_stringshare.h b/src/include/eina_stringshare.h index 420c3c6..d5e2165 100644 --- a/src/include/eina_stringshare.h +++ b/src/include/eina_stringshare.h @@ -69,7 +69,8 @@ EAPI int eina_stringshare_init(void); EAPI int eina_stringshare_shutdown(void); EAPI const char *eina_stringshare_add(const char *str); EAPI void eina_stringshare_del(const char *str); - +EAPI void eina_stringshare_dump(void); + /** * @} */ diff --git a/src/lib/eina_stringshare.c b/src/lib/eina_stringshare.c index 77a19b4..243ce81 100644 --- a/src/lib/eina_stringshare.c +++ b/src/lib/eina_stringshare.c @@ -478,6 +478,64 @@ eina_stringshare_del(const char *str) if (getenv("EINA_ERROR_ABORT")) abort(); } +struct dumpinfo +{ + int used, saved, dups, unique; +}; + +static Eina_Bool +eina_iterator_array_check(const Eina_Rbtree *rbtree, Eina_Stringshare_Head *head, struct dumpinfo *fdata) +{ + Eina_Stringshare_Node *node; + + fdata->used += sizeof(Eina_Stringshare_Head); + for (node = head->head; node; node = node->next) + { + printf("DDD: %5i %5i ", node->length, node->references); + printf("'%s'\n", ((char *)node) + sizeof(Eina_Stringshare_Node)); + fdata->used += sizeof(Eina_Stringshare_Node); + fdata->used += node->length; + fdata->saved += (node->references - 1) * node->length; + fdata->dups += node->references - 1; + fdata->unique++; + } + return EINA_TRUE; +} + +/** + * @brief Dump the contents of the stringshare. + * + * This function dumps all strings in the stringshare to stdout with a + * DDD: prefix per line and a memory usage summary. + */ +EAPI void +eina_stringshare_dump(void) +{ + Eina_Iterator *it; + int i; + struct dumpinfo di; + + if (!share) return; + di.used = 0; + di.saved = 0; + di.dups = 0; + di.unique = 0; + printf("DDD: len ref string\n"); + printf("DDD:-------------------\n"); + for (i = 0; i < EINA_STRINGSHARE_BUCKETS; i++) + { + if (!share->buckets[i]) continue; +// printf("DDD: BUCKET # %i (HEAD=%i, NODE=%i)\n", i, +// sizeof(Eina_Stringshare_Head), sizeof(Eina_Stringshare_Node)); + it = eina_rbtree_iterator_prefix((Eina_Rbtree *)share->buckets[i]); + eina_iterator_foreach(it, EINA_EACH(eina_iterator_array_check), &di); + eina_iterator_free(it); + } + printf("DDD:-------------------\n"); + printf("DDD: usage (bytes) = %i, saved = %i (%i duplicates, %i unique)\n", + di.used, di.saved, di.dups, di.unique); +} + /** * @} */ -- 2.7.4