Ignore:
Timestamp:
Sep 18, 2006, 1:49:49 AM (19 years ago)
Author:
bird
Message:

skip some unnecessary copies and allocations.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gmake/variable.c

    r530 r531  
    314314   that it should be recursively re-expanded.  */
    315315
     316#ifdef CONFIG_WITH_VALUE_LENGTH
    316317struct variable *
    317318define_variable_in_set (const char *name, unsigned int length,
    318                         char *value, enum variable_origin origin,
    319                         int recursive, struct variable_set *set,
     319                        char *value, unsigned int value_length, int duplicate_value,
     320                        enum variable_origin origin, int recursive,
     321                        struct variable_set *set, const struct floc *flocp)
     322#else
     323struct variable *
     324define_variable_in_set (const char *name, unsigned int length,
     325                        char *value, enum variable_origin origin,
     326                        int recursive, struct variable_set *set,
    320327                        const struct floc *flocp)
     328#endif
    321329{
    322330  struct variable *v;
     
    352360        {
    353361#ifdef CONFIG_WITH_VALUE_LENGTH
    354           v->value_length = strlen (value);
    355           v->value = xrealloc (v->value, v->value_length + 1);
    356           bcopy (value, v->value, v->value_length + 1);
     362          if (value_length == ~0U)
     363            value_length = strlen (value);
     364          else
     365            assert (value_length == strlen (value));
     366          v->value_length = value_length;
     367          if (!duplicate_value)
     368            {
     369              if (v->value != 0)
     370                free (v->value);
     371              v->value = value;
     372            }
     373          else
     374            {
     375              v->value = xrealloc (v->value, v->value_length + 1);
     376              bcopy (value, v->value, v->value_length + 1);
     377            }
    357378#else
    358379          if (v->value != 0)
     
    381402  hash_insert_at (&set->table, v, var_slot);
    382403#ifdef CONFIG_WITH_VALUE_LENGTH
    383   v->value_length = strlen (value);
    384   v->value = xmalloc (v->value_length + 1);
    385   bcopy (value, v->value, v->value_length + 1);
     404  if (value_length == ~0U)
     405    value_length = strlen (value);
     406  else
     407    assert (value_length == strlen (value));
     408  v->value_length = value_length;
     409  if (!duplicate_value)
     410    v->value = value;
     411  else
     412    {
     413      v->value = xmalloc (v->value_length + 1);
     414      bcopy (value, v->value, v->value_length + 1);
     415    }
    386416#else
    387417  v->value = xstrdup (value);
     
    10441074      v->value = xstrdup (default_shell);
    10451075#ifdef CONFIG_WITH_VALUE_LENGTH
    1046       v->value_length = -1;
     1076      v->value_length = strlen (v->value);
    10471077#endif
    10481078    }
     
    12441274  int conditional = 0;
    12451275  const size_t varname_len = strlen (varname); /* bird */
     1276# ifdef CONFIG_WITH_VALUE_LENGTH
     1277  unsigned int value_len = ~0U;
     1278# endif
    12461279
    12471280  /* Calculate the variable's new value in VALUE.  */
     
    13081341
    13091342            val = value;
     1343#ifdef CONFIG_WITH_VALUE_LENGTH
     1344            if (v->recursive)
     1345              {
     1346                /* The previous definition of the variable was recursive.
     1347                   The new value is the unexpanded old and new values. */
     1348                flavor = f_recursive;
     1349                oldlen = v->value_length; assert (oldlen == strlen (v->value));
     1350                vallen = strlen (val);
     1351
     1352                value_len = oldlen + 1 + vallen;
     1353                p = alloc_value = xmalloc (value_len + 1);
     1354                memcpy (p, v->value, oldlen);
     1355                p[oldlen] = ' ';
     1356                memcpy (&p[oldlen + 1], val, vallen + 1);
     1357              }
     1358            else
     1359              {
     1360                /* The previous definition of the variable was simple.
     1361                   The new value comes from the old value, which was expanded
     1362                   when it was set; and from the expanded new value.  Allocate
     1363                   memory for the expansion as we may still need the rest of the
     1364                   buffer if we're looking at a target-specific variable.  */
     1365                char *buf;
     1366                unsigned int len;
     1367                install_variable_buffer (&buf, &len);
     1368
     1369                p = variable_buffer;
     1370                if ( v->value && *v->value )
     1371                  {
     1372                    p = variable_buffer_output (p, v->value, v->value_length);
     1373                    p = variable_buffer_output (p, " ", 1);
     1374                  }
     1375                p = variable_expand_string (p, val, (long)-1);
     1376
     1377                p = strchr (p, '\0'); /* returns adjusted start, not end. */
     1378                alloc_value = variable_buffer;
     1379                value_len = p - alloc_value;
     1380                p = alloc_value;
     1381
     1382                variable_buffer = NULL; /* hackish */
     1383                restore_variable_buffer (buf, len);
     1384              }
     1385#else /* !CONFIG_WITH_VALUE_LENGTH */
    13101386            if (v->recursive)
    13111387              /* The previous definition of the variable was recursive.
     
    13201396              val = alloc_value = allocated_variable_expand (val);
    13211397
    1322 #ifdef CONFIG_WITH_VALUE_LENGTH
    1323             oldlen = v->value_length;
    1324             assert(oldlen == strlen (v->value));
    1325 #endif
     1398            oldlen = strlen (v->value);
    13261399            vallen = strlen (val);
    13271400            p = (char *) alloca (oldlen + 1 + vallen + 1);
     
    13291402            p[oldlen] = ' ';
    13301403            bcopy (val, &p[oldlen + 1], vallen + 1);
     1404#endif /* !CONFIG_WITH_VALUE_LENGTH */
    13311405          }
    13321406      }
     
    14301504        {
    14311505          v = define_variable_in_set (varname, varname_len, default_shell,
     1506# ifdef CONFIG_WITH_VALUE_LENGTH
     1507                                      ~0U, 1 /* duplicate_value */,
     1508# endif
    14321509                                      origin, flavor == f_recursive,
    14331510                                      (target_var
     
    14391516      else
    14401517        v = lookup_variable (varname, varname_len);
     1518# ifdef CONFIG_WITH_VALUE_LENGTH
     1519      if (alloc_value)
     1520        free (alloc_value);
     1521# endif
    14411522    }
    14421523  else
     
    14501531
    14511532  v = define_variable_in_set (varname, varname_len, p,
     1533#ifdef CONFIG_WITH_VALUE_LENGTH
     1534                              value_len, !alloc_value,
     1535#endif
    14521536                              origin, flavor == f_recursive,
    14531537                              (target_var
     
    14571541  v->conditional = conditional;
    14581542
     1543#ifndef CONFIG_WITH_VALUE_LENGTH
    14591544  if (alloc_value)
    14601545    free (alloc_value);
     1546#endif
    14611547
    14621548  return v;
Note: See TracChangeset for help on using the changeset viewer.