Patchwork [2/4] Unify common LTO section hash code and fix minor memory leak

login
register
mail settings
Submitter Andi Kleen
Date July 10, 2010, 11:41 a.m.
Message ID <20100710114152.DB24CB24FF@basil.firstfloor.org>
Download mbox | patch
Permalink /patch/58476/
State New
Headers show

Comments

Andi Kleen - July 10, 2010, 11:41 a.m.
Move the common hash table support code in the ELF/MACHO/COFF lto
modules into lto.c. Also fix a minor memory leak, in that the section
strings were no freed on hash table destruction.

2010-07-10  Andi Kleen <ak@linux.intel.com>

	* lto-coff.c (hash_name, eq_name): Move.
	(lto_obj_build_section_table): Call lto_obj_create_section_hash_table.
	* lto-elf.c: (hash_name, eq_name): Move.
	(lto_obj_build_section_table): Call lto_obj_create_section_hash_table.
	* lto-macho.c: (hash_name, eq_name): Move.
	(lto_obj_build_section_table): Call lto_obj_create_section_hash_table.
	* lto.c: (hash_name, eq_name): Move from lto-*.c
	(lto_obj_create_section_hash_table): Add.
	(free_with_string): Add.

Patch

diff --git a/gcc/lto/lto-coff.c b/gcc/lto/lto-coff.c
index 176c322..f5aaff8 100644
--- a/gcc/lto/lto-coff.c
+++ b/gcc/lto/lto-coff.c
@@ -134,29 +134,6 @@  lto_file_init (lto_file *file, const char *filename, off_t offset)
   file->offset = offset;
 }
 
-/* Returns a hash code for P.  */
-
-static hashval_t
-hash_name (const void *p)
-{
-  const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
-  return (hashval_t) htab_hash_string (ds->name);
-}
-
-/* Returns nonzero if P1 and P2 are equal.  */
-
-static int
-eq_name (const void *p1, const void *p2)
-{
-  const struct lto_section_slot *s1 =
-    (const struct lto_section_slot *) p1;
-  const struct lto_section_slot *s2 =
-    (const struct lto_section_slot *) p2;
-
-  return strcmp (s1->name, s2->name) == 0;
-}
-
-
 /* Build a hash table whose key is the section names and whose data is
    the start and size of each section in the .o file.  */
 
@@ -169,7 +146,7 @@  lto_obj_build_section_table (lto_file *lto_file)
   ssize_t strtab_size;
   char *strtab;
 
-  section_hash_table = htab_create (37, hash_name, eq_name, free);
+  section_hash_table = lto_obj_create_section_hash_table ();
 
   /* Seek to start of string table.  */
   if (coff_file->strtab_offs != lseek (coff_file->fd,
diff --git a/gcc/lto/lto-elf.c b/gcc/lto/lto-elf.c
index 3798feb..ad49621 100644
--- a/gcc/lto/lto-elf.c
+++ b/gcc/lto/lto-elf.c
@@ -158,31 +158,6 @@  lto_elf_free_shdr (Elf64_Shdr *shdr)
     free (shdr);
 }
 
-
-/* Returns a hash code for P.  */
-
-static hashval_t
-hash_name (const void *p)
-{
-  const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
-  return (hashval_t) htab_hash_string (ds->name);
-}
-
-
-/* Returns nonzero if P1 and P2 are equal.  */
-
-static int
-eq_name (const void *p1, const void *p2)
-{
-  const struct lto_section_slot *s1 =
-    (const struct lto_section_slot *) p1;
-  const struct lto_section_slot *s2 =
-    (const struct lto_section_slot *) p2;
-
-  return strcmp (s1->name, s2->name) == 0;
-}
-
-
 /* Build a hash table whose key is the section names and whose data is
    the start and size of each section in the .o file.  */
 
@@ -194,7 +169,7 @@  lto_obj_build_section_table (lto_file *lto_file)
   Elf_Scn *section;
   size_t base_offset;
 
-  section_hash_table = htab_create (37, hash_name, eq_name, free);
+  section_hash_table = lto_obj_create_section_hash_table ();
 
   base_offset = elf_getbase (elf_file->elf);
   /* We are reasonably sure that elf_getbase does not fail at this
diff --git a/gcc/lto/lto-macho.c b/gcc/lto/lto-macho.c
index 0541145..9f89e8e 100644
--- a/gcc/lto/lto-macho.c
+++ b/gcc/lto/lto-macho.c
@@ -141,28 +141,6 @@  lto_file_init (lto_file *file, const char *filename, off_t offset)
   file->offset = offset;
 }
 
-/* Returns a hash code for P.  */
-
-static hashval_t
-hash_name (const void *p)
-{
-  const struct lto_section_slot *s = (const struct lto_section_slot *) p;
-  return (hashval_t) htab_hash_string (s->name);
-}
-
-/* Returns nonzero if P1 and P2 are equal.  */
-
-static int
-eq_name (const void *p1, const void *p2)
-{
-  const struct lto_section_slot *s1 =
-    (const struct lto_section_slot *) p1;
-  const struct lto_section_slot *s2 =
-    (const struct lto_section_slot *) p2;
-
-  return strcmp (s1->name, s2->name) == 0;
-}
-
 /* Build a hash table whose key is the section names and whose data is
    the start and size of each section in the .o file.  */
 
@@ -177,7 +155,7 @@  lto_obj_build_section_table (lto_file *lto_file)
   char *strtab = NULL;
   int i;
 
-  section_hash_table = htab_create (37, hash_name, eq_name, free);
+  section_hash_table = lto_obj_create_section_hash_table ();
 
   /* Seek the string table.  */
   /* FIXME The segment name should be in darwin.h, but can we include it
diff --git a/gcc/lto/lto.c b/gcc/lto/lto.c
index 4ac1ac1..e4ee214 100644
--- a/gcc/lto/lto.c
+++ b/gcc/lto/lto.c
@@ -60,6 +61,47 @@  along with GCC; see the file COPYING3.  If not see
 
 static GTY(()) tree first_personality_decl;
 
+/* Returns a hash code for P.  */
+
+static hashval_t
+hash_name (const void *p)
+{
+  const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
+  return (hashval_t) htab_hash_string (ds->name);
+}
+
+
+/* Returns nonzero if P1 and P2 are equal.  */
+
+static int
+eq_name (const void *p1, const void *p2)
+{
+  const struct lto_section_slot *s1 =
+    (const struct lto_section_slot *) p1;
+  const struct lto_section_slot *s2 =
+    (const struct lto_section_slot *) p2;
+
+  return strcmp (s1->name, s2->name) == 0;
+}
+
+/* Free lto_section_slot */
+
+static void
+free_with_string (void *arg)
+{
+  struct lto_section_slot *s = (struct lto_section_slot *)arg;
+
+  free (CONST_CAST (char *, s->name));
+  free (arg);
+}
+
+/* Create section hash table */
+
+htab_t 
+lto_obj_create_section_hash_table (void)
+{
+  return htab_create (37, hash_name, eq_name, free_with_string);
+}
 
 /* Read the constructors and inits.  */
 
diff --git a/gcc/lto/lto.h b/gcc/lto/lto.h
index cdd30d6..47d9973 100644
--- a/gcc/lto/lto.h
+++ b/gcc/lto/lto.h
@@ -44,6 +44,7 @@  extern void lto_read_all_file_options (void);
 extern lto_file *lto_obj_file_open (const char *filename, bool writable);
 extern void lto_obj_file_close (lto_file *file);
 extern htab_t lto_obj_build_section_table (lto_file *file);
+extern htab_t lto_obj_create_section_hash_table (void);
 extern void lto_obj_begin_section (const char *name);
 extern void lto_obj_append_data (const void *data, size_t len, void *block);
 extern void lto_obj_end_section (void);