From: <fr...@us...> - 2012-05-07 02:03:16
|
Revision: 4695 http://fuse-emulator.svn.sourceforge.net/fuse-emulator/?rev=4695&view=rev Author: fredm Date: 2012-05-07 02:03:10 +0000 (Mon, 07 May 2012) Log Message: ----------- Add g_hash_table_new_full() and libspectrum_end() for memory releasing (part of bug #3515269) (Sergio Baldov?\195?\173). Modified Paths: -------------- trunk/libspectrum/hacking/ChangeLog trunk/libspectrum/internals.h trunk/libspectrum/libspectrum.c trunk/libspectrum/libspectrum.h.in trunk/libspectrum/make-perl.c trunk/libspectrum/myglib/ghash.c trunk/libspectrum/myglib/gslist.c Modified: trunk/libspectrum/hacking/ChangeLog =================================================================== --- trunk/libspectrum/hacking/ChangeLog 2012-05-06 06:42:44 UTC (rev 4694) +++ trunk/libspectrum/hacking/ChangeLog 2012-05-07 02:03:10 UTC (rev 4695) @@ -882,3 +882,6 @@ 20120506 make-perl.c,myglib/gslist.c: fix some const qualifiers for the data argument to g_slist_remove and g_slist_find_custom (part of bug #3514721) (Sergio Baldoví). +20120507 internals.h,libspectrum.c,libspectrum.h.in,make-perl.c,myglib/{ghash.c, + gslist.c}: add g_hash_table_new_full() and libspectrum_end() for memory + releasing (part of bug #3515269) (Sergio Baldoví). Modified: trunk/libspectrum/internals.h =================================================================== --- trunk/libspectrum/internals.h 2012-05-06 06:42:44 UTC (rev 4694) +++ trunk/libspectrum/internals.h 2012-05-07 02:03:10 UTC (rev 4695) @@ -258,4 +258,14 @@ extern const int LIBSPECTRUM_BITS_IN_BYTE; +/* glib replacement functions */ + +#ifndef HAVE_LIB_GLIB /* Only if we are using glib replacement */ +void +libspectrum_slist_cleanup( void ); + +void +libspectrum_hashtable_cleanup( void ); +#endif /* #ifndef HAVE_LIB_GLIB */ + #endif /* #ifndef LIBSPECTRUM_INTERNALS_H */ Modified: trunk/libspectrum/libspectrum.c =================================================================== --- trunk/libspectrum/libspectrum.c 2012-05-06 06:42:44 UTC (rev 4694) +++ trunk/libspectrum/libspectrum.c 2012-05-07 02:03:10 UTC (rev 4695) @@ -121,6 +121,15 @@ return LIBSPECTRUM_ERROR_NONE; } +void +libspectrum_end( void ) +{ +#ifndef HAVE_LIB_GLIB + libspectrum_slist_cleanup(); + libspectrum_hashtable_cleanup(); +#endif /* #ifndef HAVE_LIB_GLIB */ +} + #ifdef HAVE_GCRYPT_H static void gcrypt_log_handler( void *opaque, int level, const char *format, va_list ap ) Modified: trunk/libspectrum/libspectrum.h.in =================================================================== --- trunk/libspectrum/libspectrum.h.in 2012-05-06 06:42:44 UTC (rev 4694) +++ trunk/libspectrum/libspectrum.h.in 2012-05-07 02:03:10 UTC (rev 4695) @@ -100,6 +100,7 @@ /* Initialisation */ WIN32_DLL libspectrum_error libspectrum_init( void ); +WIN32_DLL void libspectrum_end( void ); /* Version checking */ Modified: trunk/libspectrum/make-perl.c =================================================================== --- trunk/libspectrum/make-perl.c 2012-05-06 06:42:44 UTC (rev 4694) +++ trunk/libspectrum/make-perl.c 2012-05-07 02:03:10 UTC (rev 4695) @@ -150,7 +150,11 @@ "typedef gint (*GCompareFunc) (gconstpointer a,\n" " gconstpointer b);\n" "\n" +"typedef void (*GDestroyNotify) (gpointer data);\n" "\n" +"typedef void (*GFreeFunc) (gpointer data);\n" +"\n" +"\n" "WIN32_DLL GSList *g_slist_insert_sorted (GSList *list,\n" " gpointer data,\n" " GCompareFunc func);\n" @@ -216,6 +220,11 @@ "WIN32_DLL GHashTable *g_hash_table_new (GHashFunc hash_func,\n" " GCompareFunc key_compare_func);\n" "\n" +"WIN32_DLL GHashTable *g_hash_table_new_full (GHashFunc hash_func,\n" +" GCompareFunc key_equal_func,\n" +" GDestroyNotify key_destroy_func,\n" +" GDestroyNotify value_destroy_func);\n" +"\n" "WIN32_DLL void g_hash_table_destroy (GHashTable *hash_table);\n" "\n" "WIN32_DLL void g_hash_table_insert (GHashTable *hash_table,\n" Modified: trunk/libspectrum/myglib/ghash.c =================================================================== --- trunk/libspectrum/myglib/ghash.c 2012-05-06 06:42:44 UTC (rev 4694) +++ trunk/libspectrum/myglib/ghash.c 2012-05-07 02:03:10 UTC (rev 4695) @@ -57,9 +57,12 @@ GHashNode **nodes; GHashFunc hash_func; GCompareFunc key_equal_func; + GDestroyNotify key_destroy_func; + GDestroyNotify value_destroy_func; }; static GHashNode *node_free_list = NULL; +static GHashNode *node_allocated_list = NULL; guint g_direct_hash (gconstpointer v) @@ -71,6 +74,15 @@ g_hash_table_new (GHashFunc hash_func, GCompareFunc key_equal_func) { + return g_hash_table_new_full( hash_func, key_equal_func, NULL, NULL ); +} + +GHashTable* +g_hash_table_new_full (GHashFunc hash_func, + GCompareFunc key_equal_func, + GDestroyNotify key_destroy_func, + GDestroyNotify value_destroy_func) +{ GHashTable *hash_table; guint i; @@ -79,6 +91,8 @@ hash_table->nnodes = 0; hash_table->hash_func = hash_func? hash_func : g_direct_hash; hash_table->key_equal_func = key_equal_func; + hash_table->key_destroy_func = key_destroy_func; + hash_table->value_destroy_func = value_destroy_func; hash_table->nodes = libspectrum_malloc (HASH_TABLE_SIZE * sizeof (GHashNode*)); for (i = 0; i < HASH_TABLE_SIZE; i++) @@ -88,15 +102,28 @@ } static void -g_hash_nodes_destroy (GHashNode *hash_node) +g_hash_nodes_destroy (GHashNode *hash_node, + GFreeFunc key_destroy_func, + GFreeFunc value_destroy_func) { if (hash_node) { GHashNode *node = hash_node; - while (node->next) - node = node->next; + while (node->next) { + if (key_destroy_func) + key_destroy_func (node->key); + if (value_destroy_func) + value_destroy_func (node->value); + node = node->next; + } + + if (key_destroy_func) + key_destroy_func (node->key); + if (value_destroy_func) + value_destroy_func (node->value); + node->next = node_free_list; node_free_list = hash_node; } @@ -108,8 +135,10 @@ guint i; for (i = 0; i < HASH_TABLE_SIZE; i++) - g_hash_nodes_destroy (hash_table->nodes[i]); - + g_hash_nodes_destroy (hash_table->nodes[i], + hash_table->key_destroy_func, + hash_table->value_destroy_func); + libspectrum_free (hash_table->nodes); libspectrum_free (hash_table); } @@ -157,6 +186,7 @@ if (!node_free_list) { node_free_list = libspectrum_malloc (1024 * sizeof (GHashNode)); + node_allocated_list = node_free_list; for(i = 0; i < 1023; i++ ) node_free_list[i].next = &node_free_list[i+1]; @@ -185,6 +215,13 @@ if (*node) { + /* free the passed key */ + if (hash_table->key_destroy_func) + hash_table->key_destroy_func (key); + + if (hash_table->value_destroy_func) + hash_table->value_destroy_func ((*node)->value); + (*node)->value = value; } else @@ -195,8 +232,15 @@ } static void -g_hash_node_destroy (GHashNode *hash_node) +g_hash_node_destroy (GHashNode *hash_node, + GDestroyNotify key_destroy_func, + GDestroyNotify value_destroy_func) { + if (key_destroy_func) + key_destroy_func (hash_node->key); + if (value_destroy_func) + value_destroy_func (hash_node->value); + hash_node->next = node_free_list; node_free_list = hash_node; } @@ -227,13 +271,17 @@ if (prev) { prev->next = node->next; - g_hash_node_destroy (node); + g_hash_node_destroy (node, + hash_table->key_destroy_func, + hash_table->value_destroy_func); node = prev; } else { hash_table->nodes[i] = node->next; - g_hash_node_destroy (node); + g_hash_node_destroy (node, + hash_table->key_destroy_func, + hash_table->value_destroy_func); goto restart; } } @@ -299,4 +347,11 @@ return strcmp (string1, string2) == 0; } +void +libspectrum_hashtable_cleanup( void ) +{ + libspectrum_free( node_allocated_list ); + node_allocated_list = NULL; + node_free_list = NULL; +} #endif /* #ifndef HAVE_LIB_GLIB */ Modified: trunk/libspectrum/myglib/gslist.c =================================================================== --- trunk/libspectrum/myglib/gslist.c 2012-05-06 06:42:44 UTC (rev 4694) +++ trunk/libspectrum/myglib/gslist.c 2012-05-07 02:03:10 UTC (rev 4695) @@ -43,12 +43,14 @@ static int FREE_LIST_ALLOCATE_CHUNK = 1024; GSList * free_list = NULL; +GSList * allocated_list = NULL; static void allocate_free ( void ) { if(!free_list) { int i; free_list=libspectrum_malloc(FREE_LIST_ALLOCATE_CHUNK*sizeof(GSList)); + allocated_list = free_list; for(i=0;i<FREE_LIST_ALLOCATE_CHUNK-1;i++) free_list[i].next=&free_list[i+1]; free_list[FREE_LIST_ALLOCATE_CHUNK-1].next=NULL; @@ -352,4 +354,12 @@ return -1; } +void +libspectrum_slist_cleanup( void ) +{ + libspectrum_free( allocated_list ); + allocated_list = NULL; + free_list = NULL; +} + #endif /* #ifndef HAVE_LIB_GLIB */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |