Ignore:
Timestamp:
Oct 11, 2008, 9:12:10 AM (17 years ago)
Author:
bird
Message:

kmk: more length optimizations.

File:
1 edited

Legend:

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

    r1819 r1827  
    108108
    109109char *
     110#ifndef CONFIG_WITH_VALUE_LENGTH
    110111recursively_expand_for_file (struct variable *v, struct file *file)
     112#else
     113recursively_expand_for_file (struct variable *v, struct file *file,
     114                             unsigned int *value_lenp)
     115#endif
    111116{
    112117  char *value;
     
    149154
    150155  v->expanding = 1;
     156#ifndef CONFIG_WITH_VALUE_LENGTH
    151157  if (v->append)
    152158    value = allocated_variable_append (v);
    153159  else
    154160    value = allocated_variable_expand (v->value);
     161#else  /* CONFIG_WITH_VALUE_LENGTH */
     162  if (!v->append)
     163    value = allocated_variable_expand_2 (v->value, v->value_length, value_lenp);
     164  else
     165    {
     166      value = allocated_variable_append (v);
     167      if (value_lenp)
     168        *value_lenp = strlen (value);
     169    }
     170#endif /* CONFIG_WITH_VALUE_LENGTH */
    155171  v->expanding = 0;
    156172
     
    187203
    188204#ifdef CONFIG_WITH_VALUE_LENGTH
     205  assert ((unsigned int)v->value_length == strlen (v->value));
    189206  if (!v->recursive)
    190     {
    191       assert (v->value_length == strlen (v->value));
    192       o = variable_buffer_output (o, v->value, v->value_length);
    193     }
     207    o = variable_buffer_output (o, v->value, v->value_length);
    194208  else
    195209   {
    196      value = recursively_expand (v);
    197      o = variable_buffer_output (o, value, strlen (value));
     210     unsigned int value_len;
     211
     212     value = recursively_expand_for_file (v, NULL, &value_len);
     213     o = variable_buffer_output (o, value, value_len);
    198214     free (value);
    199215   }
     
    211227
    212228
     229#ifndef CONFIG_WITH_VALUE_LENGTH /* Only using variable_expand_string_2! */
    213230/* Scan STRING for variable references and expansion-function calls.  Only
    214231   LENGTH bytes of STRING are actually scanned.  If LENGTH is -1, scan until
     
    228245  char *o;
    229246  unsigned int line_offset;
    230 #ifdef CONFIG_WITH_VALUE_LENGTH
    231   const char *eos;
    232 #endif
    233247
    234248  if (!line)
     
    239253  if (length == 0)
    240254    {
    241 #ifdef KMK /* this is a bug fix. The 2 vs 1 thing is the strlen + 1 in the loop below. */
    242       variable_buffer_output (o, "\0", 2);
    243       return (variable_buffer + line_offset);
    244 #else
    245255      variable_buffer_output (o, "", 1);
    246256      return (variable_buffer);
    247 #endif
    248     }
    249 
    250 #ifdef CONFIG_WITH_VALUE_LENGTH
    251   /* Simple first, 50% of the kBuild calls to this function does
    252      not need any expansion at all. Should be worth a special case. */
    253   if (length < 0)
    254     length = strlen (string);
    255   p1 = (const char *)memchr (string, '$', length);
    256   if (p1 == 0)
    257     {
    258       o = variable_buffer_output (o, string, length);
    259       variable_buffer_output (o, "\0", 2);
    260       return (variable_buffer + line_offset);
    261     }
    262   eos = string + length;
    263 #endif /* CONFIG_WITH_VALUE_LENGTH */
     257    }
    264258
    265259  /* If we want a subset of the string, allocate a temporary buffer for it.
     
    270264      memcpy(abuf, string, length);
    271265      abuf[length] = '\0';
    272 #ifdef CONFIG_WITH_VALUE_LENGTH
    273       p1 += abuf - string;
    274       eos += abuf - string;
    275 #endif /* CONFIG_WITH_VALUE_LENGTH */
    276266      string = abuf;
    277267    }
     
    284274         at the next $ or the end of the input.  */
    285275
    286 #ifndef CONFIG_WITH_VALUE_LENGTH
    287276      p1 = strchr (p, '$');
    288 #endif
    289277
    290278      o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);
     
    327315               If so, expand it before expanding the entire reference.  */
    328316
    329 #ifndef CONFIG_WITH_VALUE_LENGTH
    330317            end = strchr (beg, closeparen);
    331 #else
    332             end = memchr (beg, closeparen, eos - beg);
    333 #endif
    334318            if (end == 0)
    335319              /* Unterminated variable reference.  */
     
    470454      else
    471455        ++p;
    472 #ifdef CONFIG_WITH_VALUE_LENGTH
    473       p1 = memchr (p, '$', eos - p);
    474 #endif
    475456    }
    476457
     
    481462  return (variable_buffer + line_offset);
    482463}
    483 #ifdef CONFIG_WITH_VALUE_LENGTH
    484 
    485 
    486 /* Same as variable_expand_string except that the pointer at EOL will
    487    point to the end of the returned string. */
     464
     465#else /* CONFIG_WITH_VALUE_LENGTH */
     466/* Scan STRING for variable references and expansion-function calls.  Only
     467   LENGTH bytes of STRING are actually scanned.  If LENGTH is -1, scan until
     468   a null byte is found.
     469
     470   Write the results to LINE, which must point into `variable_buffer'.  If
     471   LINE is NULL, start at the beginning of the buffer.
     472   Return a pointer to LINE, or to the beginning of the buffer if LINE is
     473   NULL. Set EOLP to point to the string terminator.
     474 */
    488475char *
    489 variable_expand_string_2 (char *line, const char *string, long length, char **eol)
     476variable_expand_string_2 (char *line, const char *string, long length, char **eolp)
    490477{
    491478  struct variable *v;
    492   const char *p, *p1;
    493   char *abuf = NULL;
     479  const char *p, *p1, *eos;
    494480  char *o;
    495481  unsigned int line_offset;
    496 #ifdef CONFIG_WITH_VALUE_LENGTH
    497   const char *eos;
    498 #endif
    499482
    500483  if (!line)
     
    503486  line_offset = line - variable_buffer;
    504487
    505   if (length == 0)
    506     {
    507       o = variable_buffer_output (o, "\0", 2);
    508       *eol = o - 2;
    509 #ifdef KMK /* this is a bug fix. */
    510       return (variable_buffer + line_offset);
    511 #else
    512       return (variable_buffer);
    513 #endif
    514     }
    515 
    516 #ifdef CONFIG_WITH_VALUE_LENGTH
    517   /* Simple first, 50% of the kBuild calls to this function does
    518      not need any expansion at all. Should be worth a special case. */
    519488  if (length < 0)
    520489    length = strlen (string);
     490
     491  /* Simple 1: Emptry string. */
     492
     493  if (length == 0)
     494    {
     495      o = variable_buffer_output (o, "\0", 2);
     496      *eolp = o - 2;
     497      return (variable_buffer + line_offset);
     498    }
     499
     500  /* Simple 2: Nothing to expand. ~50% if the kBuild calls. */
     501
    521502  p1 = (const char *)memchr (string, '$', length);
    522503  if (p1 == 0)
     
    524505      o = variable_buffer_output (o, string, length);
    525506      o = variable_buffer_output (o, "\0", 2);
    526       *eol = o - 2;
     507      *eolp = o - 2;
    527508      return (variable_buffer + line_offset);
    528509    }
    529   eos = string + length;
    530 #endif /* CONFIG_WITH_VALUE_LENGTH */
    531 
    532   /* If we want a subset of the string, allocate a temporary buffer for it.
    533      Most of the functions we use here don't work with length limits.  */
    534   if (length > 0 && string[length] != '\0')
    535     {
    536       abuf = xmalloc(length+1);
    537       memcpy(abuf, string, length);
    538       abuf[length] = '\0';
    539 #ifdef CONFIG_WITH_VALUE_LENGTH
    540       p1 += abuf - string;
    541       eos += abuf - string;
    542 #endif
    543       string = abuf;
    544     }
     510
    545511  p = string;
     512  eos = p + length;
    546513
    547514  while (1)
     
    551518         at the next $ or the end of the input.  */
    552519
    553 #ifndef CONFIG_WITH_VALUE_LENGTH
    554       p1 = strchr (p, '$');
    555 #endif
    556 
    557       /*o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1); - why +1 ? */
    558       o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p));
     520      o = variable_buffer_output (o, p, p1 != 0 ? (p1 - p) : (eos - p));
    559521
    560522      if (p1 == 0)
     
    585547            op = o;
    586548            begp = p;
    587             if (handle_function (&op, &begp))
     549            if (handle_function (&op, &begp, eos))
    588550              {
    589551                o = op;
     
    595557               If so, expand it before expanding the entire reference.  */
    596558
    597 #ifndef CONFIG_WITH_VALUE_LENGTH
    598             end = strchr (beg, closeparen);
    599 #else
    600559            end = memchr (beg, closeparen, eos - beg);
    601 #endif
    602560            if (end == 0)
    603561              /* Unterminated variable reference.  */
     
    609567                   Count parens or braces until it is matched.  */
    610568                int count = 0;
    611                 for (p = beg; *p != '\0'; ++p)
     569                for (p = beg; p < eos; ++p)
    612570                  {
    613571                    if (*p == openparen)
     
    621579                if (count < 0)
    622580                  {
    623                     abeg = expand_argument (beg, p); /* Expand the name.  */
    624                     beg = abeg;
    625                     end = strchr (beg, '\0');
     581                    unsigned int len;
     582                    char saved;
     583
     584                     /* Expand the name.  */
     585                    saved = *p;
     586                    *(char *)p = '\0'; /* XXX: proove that this is safe! */
     587                    abeg = allocated_variable_expand_2 (beg, p - beg, &len);
     588                    beg = abeg;
     589                    end = beg + len;
     590                    *(char *)p = saved;
    626591                  }
    627592              }
     
    721686
    722687        case '\0':
    723           break;
     688          assert (p == eos);
     689          break;
    724690
    725691        default:
    726           if (isblank ((unsigned char)p[-1]))
     692          if (isblank ((unsigned char)p[-1])) /* XXX: This looks incorrect, previous is '$' */
    727693            break;
    728694
     
    734700        }
    735701
    736       if (*p == '\0')
     702      if (++p >= eos)
    737703        break;
    738       else
    739         ++p;
    740 #ifdef CONFIG_WITH_VALUE_LENGTH
    741704      p1 = memchr (p, '$', eos - p);
    742 #endif
    743     }
    744 
    745   if (abuf)
    746     free (abuf);
     705    }
    747706
    748707  o = variable_buffer_output (o, "\0", 2); /* KMK: compensate for the strlen + 1 that was removed above. */
    749   *eol = o - 2;
     708  *eolp = o - 2;
    750709  return (variable_buffer + line_offset);
    751710}
     
    761720variable_expand (const char *line)
    762721{
     722#ifndef CONFIG_WITH_VALUE_LENGTH
    763723  return variable_expand_string(NULL, line, (long)-1);
     724#else
     725  char *s;
     726
     727  /* this function is abused a lot like this: variable_expand(""). */
     728  if (!*line)
     729    {
     730      s = variable_buffer_output (initialize_variable_output (), "\0", 2);
     731      return s - 2;
     732    }
     733  return variable_expand_string_2 (NULL, line, (long)-1, &s);
     734#endif
    764735}
    765736
     
    779750    return xstrdup("");
    780751
     752#ifndef CONFIG_WITH_VALUE_LENGTH
    781753  if (!end || *end == '\0')
    782754    return allocated_variable_expand (str);
     755#else
     756  if (!end)
     757      return allocated_variable_expand_2 (str, ~0U, NULL);
     758  if (*end == '\0')
     759    return allocated_variable_expand_2 (str, end - str, NULL);
     760#endif
    783761
    784762#ifdef CONFIG_WITH_OPTIMIZATION_HACKS
     
    786764    const char saved_char = *end;
    787765    *(char *)end = '\0';
     766# ifndef CONFIG_WITH_VALUE_LENGTH
    788767    tmp = allocated_variable_expand ((char *)str);
     768# else
     769    tmp = allocated_variable_expand_2 ((char *)str, end - str, NULL);
     770# endif
    789771    *(char *)end = saved_char;
    790772    return tmp;
     
    795777  tmp[end - str] = '\0';
    796778
     779# ifndef CONFIG_WITH_VALUE_LENGTH
    797780  return allocated_variable_expand (tmp);
     781# else
     782  return allocated_variable_expand_2 (tmp, end - str, NULL);
     783# endif
    798784#endif
    799785}
     
    839825  struct variable_set_list *save;
    840826  const struct floc *reading_file_saved;
     827  char *eol;
    841828
    842829  if (file == 0)
    843     return variable_expand_string (o, line, (long)-1);
     830    return variable_expand_string_2 (o, line, (long)-1, &eol);
    844831
    845832  save = current_variable_set_list;
     
    850837  else
    851838    reading_file = 0;
    852   result = variable_expand_string (o, line, (long)-1);
     839  result = variable_expand_string_2 (o, line, (long)-1, &eol);
    853840  current_variable_set_list = save;
    854841  reading_file = reading_file_saved;
     
    893880    buf = variable_buffer_output (buf, " ", 1);
    894881#ifdef CONFIG_WITH_VALUE_LENGTH
    895   assert (v->value_length == strlen (v->value));
     882  assert ((unsigned int)v->value_length == strlen (v->value)); /* FIXME */
    896883#endif
    897884
     
    10261013char *
    10271014allocated_variable_expand_2 (const char *line, unsigned int length,
    1028                              unsigned int *value_len)
     1015                             unsigned int *value_lenp)
    10291016{
    10301017  char *value;
     
    10321019  unsigned int olen = variable_buffer_length;
    10331020  long len = length == ~0U ? -1L : (long)length;
     1021  char *eol;
    10341022
    10351023  variable_buffer = 0;
    10361024
    1037   if (!value_len)
    1038     value = variable_expand_string (NULL, line, len);
    1039   else
    1040     {
    1041       char *eol;
    1042       value = variable_expand_string_2 (NULL, line, len, &eol);
    1043       *value_len = eol - value;
    1044     }
     1025  value = variable_expand_string_2 (NULL, line, len, &eol);
     1026  if (value_lenp)
     1027    *value_lenp = eol - value;
    10451028
    10461029  variable_buffer = obuf;
Note: See TracChangeset for help on using the changeset viewer.