Ignore:
Timestamp:
May 8, 2008, 5:36:35 AM (17 years ago)
Author:
bird
Message:

Fixed prepend w/ expansion (would append instead of prepend). Optimized append_expanded_string_to_variable().

File:
1 edited

Legend:

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

    r1554 r1610  
    15911591
    15921592#ifdef CONFIG_WITH_VALUE_LENGTH
     1593/* Worker function for do_variable_definition_append() and
     1594   append_expanded_string_to_variable().
     1595   The APPEND argument indicates whether it's an append or prepend operation. */
     1596void append_string_to_variable (struct variable *v, const char *value, unsigned int value_len, int append)
     1597{
     1598  /* The previous definition of the variable was recursive.
     1599     The new value is the unexpanded old and new values. */
     1600  unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
     1601  int done_1st_prepend_copy = 0;
     1602
     1603  /* adjust the size. */
     1604  if ((unsigned)v->value_alloc_len <= new_value_len + 1)
     1605    {
     1606      v->value_alloc_len *= 2;
     1607      if (v->value_alloc_len < new_value_len + 1)
     1608          v->value_alloc_len = (new_value_len + 1 + value_len + 0x7f) + ~0x7fU;
     1609      if (append || !v->value_length)
     1610        v->value = xrealloc (v->value, v->value_alloc_len);
     1611      else
     1612        {
     1613          /* avoid the extra memcpy the xrealloc may have to do */
     1614          char *new_buf = xmalloc (v->value_alloc_len);
     1615          memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
     1616          done_1st_prepend_copy = 1;
     1617          free (v->value);
     1618          v->value = new_buf;
     1619        }
     1620    }
     1621
     1622  /* insert the new bits */
     1623  if (v->value_length != 0)
     1624    {
     1625      if (append)
     1626        {
     1627          v->value[v->value_length] = ' ';
     1628          memcpy (&v->value[v->value_length + 1], value, value_len + 1);
     1629        }
     1630      else
     1631        {
     1632          if (!done_1st_prepend_copy)
     1633            memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
     1634          v->value[value_len] = ' ';
     1635          memcpy (v->value, value, value_len);
     1636        }
     1637    }
     1638  else
     1639    memcpy (v->value, value, value_len + 1);
     1640  v->value_length = new_value_len;
     1641}
     1642
    15931643static struct variable *
    15941644do_variable_definition_append (const struct floc *flocp, struct variable *v, const char *value,
     
    16171667     This is a heavily exercised code path in kBuild. */
    16181668  if (v->recursive)
    1619     {
    1620       /* The previous definition of the variable was recursive.
    1621          The new value is the unexpanded old and new values. */
    1622       unsigned int value_len = strlen (value);
    1623       unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
    1624       int done_1st_prepend_copy = 0;
    1625 
    1626       /* adjust the size. */
    1627       if ((unsigned)v->value_alloc_len <= new_value_len + 1)
    1628         {
    1629           v->value_alloc_len *= 2;
    1630           if (v->value_alloc_len < new_value_len + 1)
    1631               v->value_alloc_len = (new_value_len + 1 + value_len + 0x7f) + ~0x7fU;
    1632           if (append || !v->value_length)
    1633             v->value = xrealloc (v->value, v->value_alloc_len);
    1634           else
    1635             {
    1636               /* avoid the extra memcpy the xrealloc may have to do */
    1637               char *new_buf = xmalloc (v->value_alloc_len);
    1638               memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
    1639               done_1st_prepend_copy = 1;
    1640               free (v->value);
    1641               v->value = new_buf;
    1642             }
    1643         }
    1644 
    1645       /* insert the new bits */
    1646       if (v->value_length != 0)
    1647         {
    1648           if (append)
    1649             {
    1650               v->value[v->value_length] = ' ';
    1651               memcpy (&v->value[v->value_length + 1], value, value_len + 1);
    1652             }
    1653           else
    1654             {
    1655               if (!done_1st_prepend_copy)
    1656                 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
    1657               v->value[value_len] = ' ';
    1658               memcpy (v->value, value, value_len);
    1659             }
    1660         }
    1661       else
    1662         memcpy (v->value, value, value_len + 1);
    1663       v->value_length = new_value_len;
    1664     }
     1669    append_string_to_variable (v, value, strlen (value), append);
    16651670  else
    1666     {
    1667       /* The previous definition of the variable was simple.
    1668          The new value comes from the old value, which was expanded
    1669          when it was set; and from the expanded new value. */
    1670       append_expanded_string_to_variable(v, value);
    1671     }
     1671    /* The previous definition of the variable was simple.
     1672       The new value comes from the old value, which was expanded
     1673       when it was set; and from the expanded new value. */
     1674    append_expanded_string_to_variable (v, value, append);
    16721675
    16731676  /* update the variable */
Note: See TracChangeset for help on using the changeset viewer.