Added cleanup for registered passes.
authorJustin Seyster <jseyster@cs.sunysb.edu>
Wed, 3 Feb 2010 00:21:58 +0000 (19:21 -0500)
committerJustin Seyster <jseyster@cs.sunysb.edu>
Wed, 3 Feb 2010 00:21:58 +0000 (19:21 -0500)
src/aop-main.c

index 1ed8274b3ed7a25c88f97e49babe7a751e36c629..547dc3821b3eda9b1019a0fdd167614943b54969 100644 (file)
 
 static const char *aop_plugin_name;
 
+/* Store a list of all struct opt_pass objects we create so that we
+   can free them at cleanup time. */
+typedef struct opt_pass *aop_pass;
+DEF_VEC_P (aop_pass);
+DEF_VEC_ALLOC_P (aop_pass, heap);
+static VEC (aop_pass, heap) *aop_pass_list;
+
 static struct opt_pass template_pass = {
   .type = GIMPLE_PASS,
   .name = NULL,
@@ -74,6 +81,8 @@ void aop_register_pass (const char *pass_name, pass_callback callback)
   struct register_pass_info pass_info;
 
   pass_aop = xmalloc (sizeof (struct opt_pass));
+  memcpy (pass_aop, &template_pass, sizeof (struct opt_pass));
+
   pass_aop->name = xstrdup (pass_name);
   pass_aop->execute = callback;
 
@@ -82,14 +91,34 @@ void aop_register_pass (const char *pass_name, pass_callback callback)
   pass_info.ref_pass_instance_number = 0;
   pass_info.pos_op = PASS_POS_INSERT_BEFORE;
 
+  /* Safe the pointer to this object so we can free it later. */
+  VEC_safe_push(aop_pass, heap, aop_pass_list, pass_aop);
+
   register_callback (aop_plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
                     &pass_info);
 }
 
+/* Free memory taken up by struct opt_pass objects saved in the
+   aop_pass_list vector as well as the vector itself. */
+static void cleanup_passes ()
+{
+  int i;
+  struct opt_pass *pass;
+
+  for (i = 0 ; VEC_iterate (aop_pass, aop_pass_list, i, pass) ; i++)
+    {
+      free ((char *)pass->name);
+      free (pass);
+    }
+  VEC_free(aop_pass, heap, aop_pass_list);
+  aop_pass_list = NULL;
+}
+
 /* This is the last plug-in function called before GCC exits.  Clean
    up all the memory we allocated. */
 static void aop_cleanup (void *event_date, void *data)
 {
+  cleanup_passes();
 }
 
 /* GCC calls this to initialize a plug-in.  Once InterAspect
@@ -100,7 +129,7 @@ int plugin_init (struct plugin_name_args *plugin_info,
 {
   fprintf (stderr, "InterAspect init.\n");
 
-  plugin_info->base_name;
+  aop_plugin_name = plugin_info->base_name;
 
   /* Register our cleanup function. */
   register_callback (aop_plugin_name, PLUGIN_FINISH, aop_cleanup, NULL);