gobject-introspection r158 - in trunk: . tools



Author: juergbi
Date: Wed Mar 12 11:45:28 2008
New Revision: 158
URL: http://svn.gnome.org/viewvc/gobject-introspection?rev=158&view=rev

Log:
2008-03-12  Juerg Billeter  <j bitron ch>

	* tools/scanner.c:
	* tools/scanner.h:
	* tools/scannerparser.y:
	Start fixing memory management in g-idl-scanner.


Modified:
   trunk/ChangeLog
   trunk/tools/scanner.c
   trunk/tools/scanner.h
   trunk/tools/scannerparser.y

Modified: trunk/tools/scanner.c
==============================================================================
--- trunk/tools/scanner.c	(original)
+++ trunk/tools/scanner.c	Wed Mar 12 11:45:28 2008
@@ -53,14 +53,14 @@
     g_ascii_strdown (igenerator->namespace, -1);
   igenerator->module = g_idl_module_new (namespace, shared_library);
 
-  igenerator->typedef_table = g_hash_table_new (g_str_hash, g_str_equal);
+  igenerator->typedef_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
   igenerator->struct_or_union_or_enum_table =
-    g_hash_table_new (g_str_hash, g_str_equal);
+    g_hash_table_new_full (g_str_hash, g_str_equal, g_free, csymbol_unref);
 
   igenerator->type_map = g_hash_table_new (g_str_hash, g_str_equal);
   igenerator->type_by_lower_case_prefix =
-    g_hash_table_new (g_str_hash, g_str_equal);
-  igenerator->symbols = g_hash_table_new (g_str_hash, g_str_equal);
+    g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+  igenerator->symbols = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
   igenerator->directives_map = g_hash_table_new (g_str_hash, g_str_equal);
 
   return igenerator;
@@ -72,22 +72,30 @@
   g_free (generator->namespace);
   g_free (generator->shared_library);
   g_free (generator->lower_case_namespace);
-#if 0
-  g_idl_module_free (generator->module);
-#endif  
+  
+  g_list_foreach (generator->filenames, (GFunc)g_free, NULL);
+  g_list_free (generator->filenames);
+
+  g_free (generator->current_filename);
+
+  g_list_foreach (generator->symbol_list, (GFunc)csymbol_unref, NULL);
+  g_list_free (generator->symbol_list);
+
   g_hash_table_destroy (generator->typedef_table);
   g_hash_table_destroy (generator->struct_or_union_or_enum_table);
+
+  g_idl_module_free (generator->module);
+
+  g_list_foreach (generator->get_type_symbols, (GFunc)g_free, NULL);
+  g_list_free (generator->get_type_symbols);
+
   g_hash_table_destroy (generator->type_map);
   g_hash_table_destroy (generator->type_by_lower_case_prefix);
+
   g_hash_table_destroy (generator->symbols);
-  g_list_foreach (generator->filenames, (GFunc)g_free, NULL);
+
   g_hash_table_destroy (generator->directives_map);
 
-  g_list_free (generator->filenames);
-#if 0
-  g_list_foreach (generator->symbol_list, (GFunc)csymbol_free, NULL);
-  g_list_free (generator->symbol_list);
-#endif
   g_free (generator);
 }
 
@@ -229,7 +237,7 @@
 	  continue;
 	}
       giprop = (GIdlNodeProperty *) g_idl_node_new (G_IDL_NODE_PROPERTY);
-      giprop->node.name = properties[i]->name;
+      giprop->node.name = g_strdup (properties[i]->name);
       node->members =
 	g_list_insert_sorted (node->members, giprop,
 			      (GCompareFunc) g_idl_node_cmp);
@@ -339,7 +347,7 @@
       g_free (alt_lower_case_prefix);
     }
 
-  node->gtype_name = node->node.name;
+  node->gtype_name = g_strdup (node->node.name);
   node->gtype_init = g_strdup (symbol_name);
   node->parent = g_strdup (lookup_symbol (igenerator,
 					    g_type_name (g_type_parent (type_id))));
@@ -397,7 +405,7 @@
 		       node);
   g_hash_table_insert (igenerator->type_by_lower_case_prefix,
 		       lower_case_prefix, node);
-  node->gtype_name = node->node.name;
+  node->gtype_name = g_strdup (node->node.name);
   node->gtype_init = g_strdup (symbol_name);
   
   iface_prereqs =
@@ -438,7 +446,7 @@
 		       node);
   g_hash_table_insert (igenerator->type_by_lower_case_prefix,
 		       lower_case_prefix, node);
-  node->gtype_name = node->node.name;
+  node->gtype_name = g_strdup (node->node.name);
   node->gtype_init = g_strdup (symbol_name);
 }
 
@@ -461,7 +469,7 @@
 		       node);
   g_hash_table_insert (igenerator->type_by_lower_case_prefix,
 		       lower_case_prefix, node);
-  node->gtype_name = node->node.name;
+  node->gtype_name = g_strdup (node->node.name);
   node->gtype_init = g_strdup (symbol_name);
   
   type_class = g_type_class_ref (type_id);
@@ -496,7 +504,7 @@
 		       node);
   g_hash_table_insert (igenerator->type_by_lower_case_prefix,
 		       lower_case_prefix, node);
-  node->gtype_name = node->node.name;
+  node->gtype_name = g_strdup (node->node.name);
   node->gtype_init = g_strdup (symbol_name);
   
   type_class = g_type_class_ref (type_id);
@@ -660,7 +668,7 @@
   /* create a namespace function if no prefix matches */
   if (func->node.name == NULL)
     {
-      func->node.name = sym->ident;
+      func->node.name = g_strdup (sym->ident);
       func->is_constructor = FALSE;
       func->is_method = FALSE;
       igenerator->module->entries =
@@ -668,7 +676,7 @@
 			      (GCompareFunc) g_idl_node_cmp);
     }
 
-  func->symbol = sym->ident;
+  func->symbol = g_strdup (sym->ident);
   func->result = (GIdlNodeParam *) g_idl_node_new (G_IDL_NODE_PARAM);
   func->result->type = create_node_from_ctype (sym->base_type->base_type);
 
@@ -734,7 +742,7 @@
       if (param_sym->ident == NULL)
 	param->node.name = g_strdup_printf ("p%d", i);
       else
-	param->node.name = param_sym->ident;
+	param->node.name = g_strdup (param_sym->ident);
 
       func->parameters = g_list_append (func->parameters, param);
     }
@@ -755,7 +763,7 @@
   GList *member_l;
   char *lower_case_prefix;
 
-  node->node.name = sym->ident;
+  node->node.name = g_strdup (sym->ident);
   igenerator->module->entries =
     g_list_insert_sorted (igenerator->module->entries, node,
 			  (GCompareFunc) g_idl_node_cmp);
@@ -772,7 +780,7 @@
 	(GIdlNodeField *) g_idl_node_new (G_IDL_NODE_FIELD);
 
       node->members = g_list_append (node->members, gifield);
-      gifield->node.name = member->ident;
+      gifield->node.name = g_strdup (member->ident);
       gifield->type = create_node_from_ctype (member->base_type);
     }
 }
@@ -826,7 +834,7 @@
 	      GIdlNodeField *gifield =
 		(GIdlNodeField *) g_idl_node_new (G_IDL_NODE_FIELD);
 	      node->members = g_list_append (node->members, gifield);
-	      gifield->node.name = member->ident;
+	      gifield->node.name = g_strdup (member->ident);
 	      gifield->type = create_node_from_ctype (member->base_type);
 	    }
 	}
@@ -841,7 +849,7 @@
 	      GIdlNodeField *gifield =
 		(GIdlNodeField *) g_idl_node_new (G_IDL_NODE_FIELD);
 	      node->members = g_list_append (node->members, gifield);
-	      gifield->node.name = member->ident;
+	      gifield->node.name = g_strdup (member->ident);
 	      gifield->type = create_node_from_ctype (member->base_type);
 	    }
 	}
@@ -936,7 +944,7 @@
 		}
 
 	      givfunc = (GIdlNodeVFunc *) g_idl_node_new (G_IDL_NODE_VFUNC);
-	      givfunc->node.name = member->ident;
+	      givfunc->node.name = g_strdup (member->ident);
 	      node->members =
 		g_list_insert_sorted (node->members, givfunc,
 				      (GCompareFunc) g_idl_node_cmp);
@@ -956,7 +964,7 @@
 		    }
 		  else
 		    {
-		      param->node.name = param_sym->ident;
+		      param->node.name = g_strdup (param_sym->ident);
 		    }
 		  param->type = create_node_from_ctype (param_sym->base_type);
 		  givfunc->parameters =
@@ -1012,7 +1020,7 @@
 	  GIdlNodeField *gifield =
 	    (GIdlNodeField *) g_idl_node_new (G_IDL_NODE_FIELD);
 	  node->members = g_list_append (node->members, gifield);
-	  gifield->node.name = member->ident;
+	  gifield->node.name = g_strdup (member->ident);
 	  gifield->type = create_node_from_ctype (member->base_type);
 	}
     }
@@ -1023,7 +1031,7 @@
       char *lower_case_prefix;
       GList *member_l;
       
-      node->node.name = sym->ident;
+      node->node.name = g_strdup (sym->ident);
       igenerator->module->entries =
 	g_list_insert_sorted (igenerator->module->entries, node,
 			      (GCompareFunc) g_idl_node_cmp);
@@ -1032,7 +1040,7 @@
       g_hash_table_insert (igenerator->type_by_lower_case_prefix,
 			   lower_case_prefix, node);
 
-      node->node.name = sym->ident;
+      node->node.name = g_strdup (sym->ident);
       for (member_l = union_type->child_list; member_l != NULL;
 	   member_l = member_l->next)
 	{
@@ -1040,7 +1048,7 @@
 	  GIdlNodeField *gifield =
 	    (GIdlNodeField *) g_idl_node_new (G_IDL_NODE_FIELD);
 	  node->members = g_list_append (node->members, gifield);
-	  gifield->node.name = member->ident;
+	  gifield->node.name = g_strdup (member->ident);
 	  gifield->type = create_node_from_ctype (member->base_type);
 	}
     }
@@ -1079,7 +1087,7 @@
     }
 
   node = (GIdlNodeEnum *) g_idl_node_new (G_IDL_NODE_ENUM);
-  node->node.name = sym->ident;
+  node->node.name = g_strdup (sym->ident);
   igenerator->module->entries =
     g_list_insert_sorted (igenerator->module->entries, node,
 			  (GCompareFunc) g_idl_node_cmp);
@@ -1091,7 +1099,7 @@
       GIdlNodeValue *gival =
 	(GIdlNodeValue *) g_idl_node_new (G_IDL_NODE_VALUE);
       node->values = g_list_append (node->values, gival);
-      gival->node.name = member->ident;
+      gival->node.name = g_strdup (member->ident);
       gival->value = member->const_int;
     }
 }
@@ -1107,12 +1115,12 @@
   GIdlNodeFunction *gifunc =
     (GIdlNodeFunction *) g_idl_node_new (G_IDL_NODE_CALLBACK);
 
-  gifunc->node.name = sym->ident;
+  gifunc->node.name = g_strdup (sym->ident);
   igenerator->module->entries =
     g_list_insert_sorted (igenerator->module->entries, gifunc,
 			  (GCompareFunc) g_idl_node_cmp);
 
-  gifunc->symbol = sym->ident;
+  gifunc->symbol = g_strdup (sym->ident);
   gifunc->result = (GIdlNodeParam *) g_idl_node_new (G_IDL_NODE_PARAM);
   gifunc->result->type =
     create_node_from_ctype (sym->base_type->base_type->base_type);
@@ -1129,7 +1137,7 @@
 	}
       else
 	{
-	  param->node.name = param_sym->ident;
+	  param->node.name = g_strdup (param_sym->ident);
 	}
       param->type = create_node_from_ctype (param_sym->base_type);
       gifunc->parameters = g_list_append (gifunc->parameters, param);
@@ -1141,7 +1149,7 @@
 {
   GIdlNodeConstant *giconst =
     (GIdlNodeConstant *) g_idl_node_new (G_IDL_NODE_CONSTANT);
-  giconst->node.name = sym->ident;
+  giconst->node.name = g_strdup (sym->ident);
   igenerator->module->entries =
     g_list_insert_sorted (igenerator->module->entries, giconst,
 			  (GCompareFunc) g_idl_node_cmp);
@@ -1198,7 +1206,7 @@
 		(GIdlNodeStruct *) g_idl_node_new (G_IDL_NODE_STRUCT);
 	      char *lower_case_prefix;
 	      
-	      node->node.name = sym->ident;
+	      node->node.name = g_strdup (sym->ident);
 	      igenerator->module->entries =
 		g_list_insert_sorted (igenerator->module->entries, node,
 				      (GCompareFunc) g_idl_node_cmp);
@@ -1248,20 +1256,20 @@
   if (found_filename || igenerator->macro_scan)
     {
       igenerator->symbol_list =
-	g_list_prepend (igenerator->symbol_list, symbol);
+	g_list_prepend (igenerator->symbol_list, csymbol_ref (symbol));
     }
 
   if (symbol->type == CSYMBOL_TYPE_TYPEDEF)
 
     {
-      g_hash_table_insert (igenerator->typedef_table, symbol->ident, symbol);
+      g_hash_table_insert (igenerator->typedef_table, g_strdup (symbol->ident), GINT_TO_POINTER (TRUE));
     }
   else if (symbol->type == CSYMBOL_TYPE_STRUCT
 	   || symbol->type == CSYMBOL_TYPE_UNION
 	   || symbol->type == CSYMBOL_TYPE_ENUM)
     {
       g_hash_table_insert (igenerator->struct_or_union_or_enum_table,
-			   symbol->ident, symbol);
+			   g_strdup (symbol->ident), csymbol_ref (symbol));
     }
 }
 
@@ -1289,7 +1297,7 @@
 	    {
 	      // ignore get_type functions with parameters
 	      igenerator->get_type_symbols =
-		g_list_prepend (igenerator->get_type_symbols, sym->ident);
+		g_list_prepend (igenerator->get_type_symbols, g_strdup (sym->ident));
 	    }
 	}
     }

Modified: trunk/tools/scanner.h
==============================================================================
--- trunk/tools/scanner.h	(original)
+++ trunk/tools/scanner.h	Wed Mar 12 11:45:28 2008
@@ -76,6 +76,7 @@
 
 struct _CSymbol
 {
+  int ref_count;
   CSymbolType type;
   int id;
   char *ident;
@@ -153,7 +154,8 @@
 
 CSymbol *    csymbol_new               (CSymbolType  type);
 gboolean     csymbol_get_const_boolean (CSymbol     *symbol);
-void         csymbol_free              (CSymbol     *symbol);
+CSymbol *    csymbol_ref               (CSymbol     *symbol);
+void         csymbol_unref             (CSymbol     *symbol);
 CDirective * cdirective_new            (const gchar *name,
 					const gchar *value,
 					GSList *options);

Modified: trunk/tools/scannerparser.y
==============================================================================
--- trunk/tools/scannerparser.y	(original)
+++ trunk/tools/scannerparser.y	Wed Mar 12 11:45:28 2008
@@ -46,7 +46,8 @@
 CSymbol *
 csymbol_new (CSymbolType type)
 {
-  CSymbol *s = g_new0 (CSymbol, 1);
+  CSymbol *s = g_slice_new0 (CSymbol);
+  s->ref_count = 1;
   s->type = type;
   return s;
 }
@@ -54,19 +55,32 @@
 static void
 ctype_free (CType * type)
 {
-  g_free (type);
   g_free (type->name);
-  g_list_foreach (type->child_list, (GFunc)ctype_free, NULL);
+  g_list_foreach (type->child_list, (GFunc)csymbol_unref, NULL);
   g_list_free (type->child_list);
+  g_slice_free (CType, type);
+}
+
+CSymbol *
+csymbol_ref (CSymbol * symbol)
+{
+  symbol->ref_count++;
 }
 
 void
-csymbol_free (CSymbol * symbol)
+csymbol_unref (CSymbol * symbol)
 {
-  g_free (symbol->ident);
-  ctype_free (symbol->base_type);
-  g_free (symbol->const_string);
-  g_free (symbol);
+  symbol->ref_count--;
+  if (symbol->ref_count == 0)
+    {
+      g_free (symbol->ident);
+      if (symbol->base_type)
+        ctype_free (symbol->base_type);
+      g_free (symbol->const_string);
+      g_slist_foreach (symbol->directives, (GFunc)cdirective_free, NULL);
+      g_slist_free (symbol->directives);
+      g_slice_free (CSymbol, symbol);
+    }
 }
  
 gboolean
@@ -78,7 +92,7 @@
 CType *
 ctype_new (CTypeType type)
 {
-  CType *t = g_new0 (CType, 1);
+  CType *t = g_slice_new0 (CType);
   t->type = type;
   return t;
 }
@@ -86,7 +100,19 @@
 CType *
 ctype_copy (CType * type)
 {
-  return g_memdup (type, sizeof (CType));
+  GList *l;
+  CType *result = g_slice_new0 (CType);
+  result->type = type->type;
+  result->storage_class_specifier = type->storage_class_specifier;
+  result->type_qualifier = type->type_qualifier;
+  result->function_specifier = type->function_specifier;
+  if (type->name)
+    result->name = g_strdup (type->name);
+  if (type->base_type)
+    result->base_type = ctype_copy (type->base_type);
+  for (l = type->child_list; l; l = l->next)
+    result->child_list = g_list_append (result->child_list, csymbol_ref (l->data));
+  return result;
 }
 
 CType *
@@ -133,7 +159,8 @@
 cpointer_new (CType * base_type)
 {
   CType *pointer = ctype_new (CTYPE_POINTER);
-  pointer->base_type = ctype_copy (base_type);
+  if (base_type != NULL)
+    pointer->base_type = ctype_copy (base_type);
   return pointer;
 }
 
@@ -159,7 +186,7 @@
   while (*foundation_type != NULL) {
     foundation_type = &((*foundation_type)->base_type);
   }
-  *foundation_type = ctype_copy (type);
+  *foundation_type = type;
 }
 
 CDirective *
@@ -222,6 +249,7 @@
 %type <ctype> enum_specifier
 %type <ctype> pointer
 %type <ctype> specifier_qualifier_list
+%type <ctype> type_name
 %type <ctype> struct_or_union
 %type <ctype> struct_or_union_specifier
 %type <ctype> type_specifier
@@ -281,6 +309,8 @@
 		$$ = g_hash_table_lookup (const_table, $1);
 		if ($$ == NULL) {
 			$$ = csymbol_new (CSYMBOL_TYPE_INVALID);
+		} else {
+			$$ = csymbol_ref ($$);
 		}
 	  }
 	| INTEGER
@@ -419,6 +449,7 @@
 	  }
 	| SIZEOF '(' type_name ')'
 	  {
+		ctype_free ($3);
 		$$ = csymbol_new (CSYMBOL_TYPE_INVALID);
 	  }
 	;
@@ -454,6 +485,7 @@
 	: unary_expression
 	| '(' type_name ')' cast_expression
 	  {
+		ctype_free ($2);
 		$$ = $4;
 	  }
 	;
@@ -658,7 +690,7 @@
 		GList *l;
 		for (l = $2; l != NULL; l = l->next) {
 			CSymbol *sym = l->data;
-			csymbol_merge_type (sym, $1);
+			csymbol_merge_type (sym, ctype_copy ($1));
 			if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF) {
 				sym->type = CSYMBOL_TYPE_TYPEDEF;
 			} else if (sym->base_type->type == CTYPE_FUNCTION) {
@@ -667,9 +699,14 @@
 				sym->type = CSYMBOL_TYPE_OBJECT;
 			}
 			g_igenerator_add_symbol (igenerator, sym);
+			csymbol_unref (sym);
 		}
+		ctype_free ($1);
 	  }
 	| declaration_specifiers ';'
+	  {
+		ctype_free ($1);
+	  }
 	;
 
 declaration_specifiers
@@ -796,6 +833,7 @@
 	| typedef_name
 	  {
 		$$ = ctypedef_new ($1);
+		g_free ($1);
 	  }
 	;
 
@@ -817,6 +855,7 @@
 		sym->ident = g_strdup ($$->name);
 		sym->base_type = ctype_copy ($$);
 		g_igenerator_add_symbol (igenerator, sym);
+		csymbol_unref (sym);
 	  }
 	| struct_or_union '{' struct_declaration_list '}'
 	  {
@@ -859,9 +898,10 @@
 			if ($1->storage_class_specifier & STORAGE_CLASS_TYPEDEF) {
 				sym->type = CSYMBOL_TYPE_TYPEDEF;
 			}
-			csymbol_merge_type (sym, $1);
+			csymbol_merge_type (sym, ctype_copy ($1));
 			$$ = g_list_append ($$, sym);
 		}
+		ctype_free ($1);
 	  }
 	;
 
@@ -957,7 +997,7 @@
 		$$->ident = $1;
 		$$->const_int_set = TRUE;
 		$$->const_int = ++last_enum_value;
-		g_hash_table_insert (const_table, g_strdup ($$->ident), $$);
+		g_hash_table_insert (const_table, g_strdup ($$->ident), csymbol_ref ($$));
 	  }
 	| identifier '=' constant_expression
 	  {
@@ -966,7 +1006,7 @@
 		$$->const_int_set = TRUE;
 		$$->const_int = $3->const_int;
 		last_enum_value = $$->const_int;
-		g_hash_table_insert (const_table, g_strdup ($$->ident), $$);
+		g_hash_table_insert (const_table, g_strdup ($$->ident), csymbol_ref ($$));
 	  }
 	;
 
@@ -1333,6 +1373,7 @@
 		if ($2->const_int_set || $2->const_string != NULL) {
 			$2->ident = $1;
 			g_igenerator_add_symbol (igenerator, $2);
+			csymbol_unref ($2);
 		}
 	  }
 	;
@@ -1363,7 +1404,7 @@
   g_return_val_if_fail (file != NULL, FALSE);
   
   const_table = g_hash_table_new_full (g_str_hash, g_str_equal,
-				       g_free, NULL);
+				       g_free, csymbol_unref);
   
   lineno = 1;
   yyin = file;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]