bison-patches
[Top][All Lists]
Advanced

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

[PATCH 01/12] diagnostics: style: avoid allocating memory when not neede


From: Akim Demaille
Subject: [PATCH 01/12] diagnostics: style: avoid allocating memory when not needed
Date: Sun, 13 Jan 2019 15:23:57 +0100

* src/muscle-tab.c (muscle_percent_variable_update): Avoid allocating
memory when it is not needed, which should be most of the time (when
there's no update to perform).
Adjust callers.
---
 src/muscle-tab.c | 44 ++++++++++++++++++++++++++------------------
 1 file changed, 26 insertions(+), 18 deletions(-)

diff --git a/src/muscle-tab.c b/src/muscle-tab.c
index 351fb964..4c17baa4 100644
--- a/src/muscle-tab.c
+++ b/src/muscle-tab.c
@@ -429,9 +429,10 @@ define_directive (char const *assignment,
 /** If the \a variable name is obsolete, return the name to use,
  * otherwise \a variable.  If the \a value is obsolete, update it too.
  *
- * Allocates the returned value.  */
+ * Allocates the returned value if needed, otherwise the returned
+ * value is exactly \a variable.  */
 static
-char *
+char const *
 muscle_percent_variable_update (char const *variable, location variable_loc,
                                 muscle_kind kind,
                                 char const **value)
@@ -474,22 +475,28 @@ muscle_percent_variable_update (char const *variable, 
location variable_loc,
              && STREQ (eq + 1, *value))
           : STREQ (c->obsolete, variable))
         {
-          char *old = define_directive (c->obsolete, kind, *value);
-          char *upd = define_directive (c->updated, c->kind, *value);
-          deprecated_directive (&variable_loc, old, upd);
-          free (old);
-          free (upd);
-          char *res = xstrdup (c->updated);
-          char *eq2 = strchr (res, '=');
-          if (eq2)
-            {
-              *eq2 = '\0';
-              *value = eq2 + 1;
-            }
-          return res;
+          /* Generate the deprecation warning. */
+          {
+            char *old = define_directive (c->obsolete, kind, *value);
+            char *upd = define_directive (c->updated, c->kind, *value);
+            deprecated_directive (&variable_loc, old, upd);
+            free (old);
+            free (upd);
+          }
+          /* Update the variable and its value.  */
+          {
+            char *res = xstrdup (c->updated);
+            char *eq2 = strchr (res, '=');
+            if (eq2)
+              {
+                *eq2 = '\0';
+                *value = eq2 + 1;
+              }
+            return res;
+          }
         }
     }
-  return xstrdup (variable);
+  return variable;
 }
 
 void
@@ -499,7 +506,7 @@ muscle_percent_define_insert (char const *var, location 
variable_loc,
                               muscle_percent_define_how how)
 {
   /* Backward compatibility.  */
-  char *variable
+  char const *variable
     = muscle_percent_variable_update (var, variable_loc, kind, &value);
   uniqstr name = muscle_name (variable, NULL);
   uniqstr loc_name = muscle_name (variable, "loc");
@@ -533,7 +540,8 @@ muscle_percent_define_insert (char const *var, location 
variable_loc,
   MUSCLE_INSERT_INT (how_name, how);
   MUSCLE_INSERT_STRING (kind_name, muscle_kind_string (kind));
  end:
-  free (variable);
+  if (variable != var)
+    free ((char *) variable);
 }
 
 /* This is used for backward compatibility, e.g., "%define api.pure"
-- 
2.20.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]