add a stringshare dumper so we can examine what's in there and improve usage.
authorraster <raster>
Mon, 27 Oct 2008 00:36:22 +0000 (00:36 +0000)
committerraster <raster@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Mon, 27 Oct 2008 00:36:22 +0000 (00:36 +0000)
git-svn-id: http://svn.enlightenment.org/svn/e/trunk/eina@37186 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/include/eina_stringshare.h
src/lib/eina_stringshare.c

index 420c3c6..d5e2165 100644 (file)
@@ -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);
+    
 /**
  * @}
  */
index 77a19b4..243ce81 100644 (file)
@@ -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);
+}
+
 /**
  * @}
  */