]> git.refcnt.org Git - colorize.git/commitdiff
Stack and release color names memory
authorSteven Schubiger <stsc@refcnt.org>
Tue, 25 Oct 2016 12:18:09 +0000 (14:18 +0200)
committerSteven Schubiger <stsc@refcnt.org>
Tue, 25 Oct 2016 12:18:09 +0000 (14:18 +0200)
Don't call the color_names memory freeing code explicitly in the
cleanup function as it is taken care of via {STACK,RELEASE}_VAR().
This implies the memory is "garbage collected" like other uses of
those macros, too.

Also, declare and initialize the color_names array in a non-global,
tighter scope.

colorize.c

index 05fd84639763a84768f1a5b8bc999c623fd889a7..a891135fdd4772710481ff8607f9d7782cee7ff6 100644 (file)
@@ -125,8 +125,6 @@ struct color_name {
     char *orig;
 };
 
-static struct color_name *color_names[3] = { NULL, NULL, NULL };
-
 struct color {
     const char *name;
     const char *code;
@@ -510,8 +508,6 @@ print_version (void)
 static void
 cleanup (void)
 {
-    free_color_names (color_names);
-
     if (stream && fileno (stream) != STDIN_FILENO)
       fclose (stream);
 #if DEBUG
@@ -534,9 +530,9 @@ free_color_names (struct color_name **color_names)
     unsigned int i;
     for (i = 0; color_names[i]; i++)
       {
-        free (color_names[i]->name);
-        free (color_names[i]->orig);
-        free_null (color_names[i]);
+        RELEASE_VAR (color_names[i]->name);
+        RELEASE_VAR (color_names[i]->orig);
+        RELEASE_VAR (color_names[i]);
       }
 }
 
@@ -546,6 +542,7 @@ process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct
     int ret;
     char *p;
     struct stat sb;
+    struct color_name *color_names[3] = { NULL, NULL, NULL };
 
     const char *color_string = arg_cnt >= 1 ? arg_strings[0] : NULL;
     const char *file_string  = arg_cnt == 2 ? arg_strings[1] : NULL;
@@ -743,13 +740,16 @@ gather_color_names (const char *color_string, bool *bold, struct color_name **co
           }
 
         color_names[index] = xcalloc (1, sizeof (struct color_name));
+        STACK_VAR (color_names[index]);
 
         color_names[index]->orig = xstrdup (color);
+        STACK_VAR (color_names[index]->orig);
 
         for (ch = color; *ch; ch++)
           *ch = tolower (*ch);
 
         color_names[index]->name = xstrdup (color);
+        STACK_VAR (color_names[index]->name);
       }
 
     RELEASE_VAR (str);