VirtualBox

Changeset 1805 in kBuild for trunk


Ignore:
Timestamp:
Oct 9, 2008 4:12:33 AM (16 years ago)
Author:
bird
Message:

kmk: Some minor optimizations.

Location:
trunk/src/kmk
Files:
4 edited

Legend:

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

    r1617 r1805  
    236236  if (length == 0)
    237237    {
     238#ifdef KMK /* this is a bug fix. The 2 vs 1 thing is the strlen + 1 in the loop below. */
     239      variable_buffer_output (o, "\0", 2);
     240      return (variable_buffer + line_offset);
     241#else
    238242      variable_buffer_output (o, "", 1);
    239243      return (variable_buffer);
    240     }
     244#endif
     245    }
     246
     247  /* Simple first, 50% of the kBuild calls to this function does
     248     not need any expansion at all. Should be worth a special case. */
     249#ifdef KMK
     250  if (length > 0)
     251    {
     252      p1 = (const char *)memchr (string, '$', length);
     253      if (p1 == 0)
     254        {
     255          if (string[length] == '\0')
     256            o = variable_buffer_output (o, string, length);
     257          else
     258            o = variable_buffer_output (o, string, length);
     259          variable_buffer_output (o, "\0", 2);
     260          return (variable_buffer + line_offset);
     261        }
     262    }
     263  else
     264    {
     265      p1 = strchr (string, '$');
     266      if (p1 == 0)
     267      {
     268          length = strlen (string);
     269          o = variable_buffer_output (o, string, length);
     270          variable_buffer_output (o, "\0", 2);
     271          return (variable_buffer + line_offset);
     272      }
     273    }
     274#endif /* KMK - optimization */
    241275
    242276  /* If we want a subset of the string, allocate a temporary buffer for it.
     
    247281      memcpy(abuf, string, length);
    248282      abuf[length] = '\0';
     283#ifdef KMK
     284      p1 += abuf - string;
     285#endif /* KMK - optimization */
    249286      string = abuf;
    250287    }
     
    257294         at the next $ or the end of the input.  */
    258295
     296#ifndef KMK
    259297      p1 = strchr (p, '$');
     298#endif /* !KMK - optimization  */
    260299
    261300      o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);
     
    437476      else
    438477        ++p;
     478#ifdef KMK
     479      p1 = strchr (p, '$');
     480#endif /* KMK - optimization */
    439481    }
    440482
     
    445487  return (variable_buffer + line_offset);
    446488}
     489#ifdef KMK
     490
     491
     492/* Same as variable_expand_string except that the pointer at EOL will
     493   point to the end of the returned string. */
     494char *
     495variable_expand_string_2 (char *line, const char *string, long length, char **eol)
     496{
     497  struct variable *v;
     498  const char *p, *p1;
     499  char *abuf = NULL;
     500  char *o;
     501  unsigned int line_offset;
     502
     503  if (!line)
     504    line = initialize_variable_output();
     505  o = line;
     506  line_offset = line - variable_buffer;
     507
     508  if (length == 0)
     509    {
     510      o = variable_buffer_output (o, "\0", 2);
     511      *eol = o - 2;
     512#ifdef KMK /* this is a bug fix. */
     513      return (variable_buffer + line_offset);
     514#else
     515      return (variable_buffer);
     516#endif
     517    }
     518
     519  /* Simple first, 50% of the kBuild calls to this function does
     520     not need any expansion at all. Should be worth a special case. */
     521#ifdef KMK
     522  if (length > 0)
     523    {
     524      p1 = (const char *)memchr (string, '$', length);
     525      if (p1 == 0)
     526        {
     527          if (string[length] == '\0')
     528            o = variable_buffer_output (o, string, length);
     529          else
     530            o = variable_buffer_output (o, string, length);
     531          o = variable_buffer_output (o, "\0", 2);
     532          *eol = o - 2;
     533          return (variable_buffer + line_offset);
     534        }
     535    }
     536  else
     537    {
     538      p1 = strchr (string, '$');
     539      if (p1 == 0)
     540      {
     541          length = strlen (string);
     542          o = variable_buffer_output (o, string, length + 1);
     543          o = variable_buffer_output (o, "\0", 2);
     544          *eol = o - 2;
     545          return (variable_buffer + line_offset);
     546      }
     547    }
     548#endif /* KMK */
     549
     550  /* If we want a subset of the string, allocate a temporary buffer for it.
     551     Most of the functions we use here don't work with length limits.  */
     552  if (length > 0 && string[length] != '\0')
     553    {
     554      abuf = xmalloc(length+1);
     555      memcpy(abuf, string, length);
     556      abuf[length] = '\0';
     557#ifdef KMK
     558      p1 += abuf - string;
     559#endif
     560      string = abuf;
     561    }
     562  p = string;
     563
     564  while (1)
     565    {
     566      /* Copy all following uninteresting chars all at once to the
     567         variable output buffer, and skip them.  Uninteresting chars end
     568         at the next $ or the end of the input.  */
     569
     570#ifndef KMK
     571      p1 = strchr (p, '$');
     572#endif /* !KMK */
     573
     574      /*o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1); - why +1 ? */
     575      o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p));
     576
     577      if (p1 == 0)
     578        break;
     579      p = p1 + 1;
     580
     581      /* Dispatch on the char that follows the $.  */
     582
     583      switch (*p)
     584        {
     585        case '$':
     586          /* $$ seen means output one $ to the variable output buffer.  */
     587          o = variable_buffer_output (o, p, 1);
     588          break;
     589
     590        case '(':
     591        case '{':
     592          /* $(...) or ${...} is the general case of substitution.  */
     593          {
     594            char openparen = *p;
     595            char closeparen = (openparen == '(') ? ')' : '}';
     596            const char *begp;
     597            const char *beg = p + 1;
     598            char *op;
     599            char *abeg = NULL;
     600            const char *end, *colon;
     601
     602            op = o;
     603            begp = p;
     604            if (handle_function (&op, &begp))
     605              {
     606                o = op;
     607                p = begp;
     608                break;
     609              }
     610
     611            /* Is there a variable reference inside the parens or braces?
     612               If so, expand it before expanding the entire reference.  */
     613
     614            end = strchr (beg, closeparen);
     615            if (end == 0)
     616              /* Unterminated variable reference.  */
     617              fatal (*expanding_var, _("unterminated variable reference"));
     618            p1 = lindex (beg, end, '$');
     619            if (p1 != 0)
     620              {
     621                /* BEG now points past the opening paren or brace.
     622                   Count parens or braces until it is matched.  */
     623                int count = 0;
     624                for (p = beg; *p != '\0'; ++p)
     625                  {
     626                    if (*p == openparen)
     627                      ++count;
     628                    else if (*p == closeparen && --count < 0)
     629                      break;
     630                  }
     631                /* If COUNT is >= 0, there were unmatched opening parens
     632                   or braces, so we go to the simple case of a variable name
     633                   such as `$($(a)'.  */
     634                if (count < 0)
     635                  {
     636                    abeg = expand_argument (beg, p); /* Expand the name.  */
     637                    beg = abeg;
     638                    end = strchr (beg, '\0');
     639                  }
     640              }
     641            else
     642              /* Advance P to the end of this reference.  After we are
     643                 finished expanding this one, P will be incremented to
     644                 continue the scan.  */
     645              p = end;
     646
     647            /* This is not a reference to a built-in function and
     648               any variable references inside are now expanded.
     649               Is the resultant text a substitution reference?  */
     650
     651            colon = lindex (beg, end, ':');
     652            if (colon)
     653              {
     654                /* This looks like a substitution reference: $(FOO:A=B).  */
     655                const char *subst_beg, *subst_end, *replace_beg, *replace_end;
     656
     657                subst_beg = colon + 1;
     658                subst_end = lindex (subst_beg, end, '=');
     659                if (subst_end == 0)
     660                  /* There is no = in sight.  Punt on the substitution
     661                     reference and treat this as a variable name containing
     662                     a colon, in the code below.  */
     663                  colon = 0;
     664                else
     665                  {
     666                    replace_beg = subst_end + 1;
     667                    replace_end = end;
     668
     669                    /* Extract the variable name before the colon
     670                       and look up that variable.  */
     671                    v = lookup_variable (beg, colon - beg);
     672                    if (v == 0)
     673                      warn_undefined (beg, colon - beg);
     674
     675                    /* If the variable is not empty, perform the
     676                       substitution.  */
     677                    if (v != 0 && *v->value != '\0')
     678                      {
     679                        char *pattern, *replace, *ppercent, *rpercent;
     680                        char *value = (v->recursive
     681                                       ? recursively_expand (v)
     682                                       : v->value);
     683
     684                        /* Copy the pattern and the replacement.  Add in an
     685                           extra % at the beginning to use in case there
     686                           isn't one in the pattern.  */
     687                        pattern = alloca (subst_end - subst_beg + 2);
     688                        *(pattern++) = '%';
     689                        memcpy (pattern, subst_beg, subst_end - subst_beg);
     690                        pattern[subst_end - subst_beg] = '\0';
     691
     692                        replace = alloca (replace_end - replace_beg + 2);
     693                        *(replace++) = '%';
     694                        memcpy (replace, replace_beg,
     695                               replace_end - replace_beg);
     696                        replace[replace_end - replace_beg] = '\0';
     697
     698                        /* Look for %.  Set the percent pointers properly
     699                           based on whether we find one or not.  */
     700                        ppercent = find_percent (pattern);
     701                        if (ppercent)
     702                          {
     703                            ++ppercent;
     704                            rpercent = find_percent (replace);
     705                            if (rpercent)
     706                              ++rpercent;
     707                          }
     708                        else
     709                          {
     710                            ppercent = pattern;
     711                            rpercent = replace;
     712                            --pattern;
     713                            --replace;
     714                          }
     715
     716                        o = patsubst_expand_pat (o, value, pattern, replace,
     717                                                 ppercent, rpercent);
     718
     719                        if (v->recursive)
     720                          free (value);
     721                      }
     722                  }
     723              }
     724
     725            if (colon == 0)
     726              /* This is an ordinary variable reference.
     727                 Look up the value of the variable.  */
     728                o = reference_variable (o, beg, end - beg);
     729
     730          if (abeg)
     731            free (abeg);
     732          }
     733          break;
     734
     735        case '\0':
     736          break;
     737
     738        default:
     739          if (isblank ((unsigned char)p[-1]))
     740            break;
     741
     742          /* A $ followed by a random char is a variable reference:
     743             $a is equivalent to $(a).  */
     744          o = reference_variable (o, p, 1);
     745
     746          break;
     747        }
     748
     749      if (*p == '\0')
     750        break;
     751      else
     752        ++p;
     753#ifdef KMK
     754      p1 = strchr (p, '$');
     755#endif
     756    }
     757
     758  if (abuf)
     759    free (abuf);
     760
     761  o = variable_buffer_output (o, "\0", 2); /* KMK: compensate for the strlen + 1 that was removed above. */
     762  *eol = o - 2;
     763  return (variable_buffer + line_offset);
     764}
     765#endif /* KMK - optimization */
    447766
    448767
     
    598917#endif
    599918
    600 #ifdef CONFIG_WITH_VALUE_LENGTH
     919#ifndef KMK
     920# ifdef CONFIG_WITH_VALUE_LENGTH
    601921  buf = variable_expand_string (buf, v->value, v->value_length);
    602 #else
     922# else
    603923  buf = variable_expand_string (buf, v->value, strlen (v->value));
    604 #endif
     924# endif
    605925  return (buf + strlen (buf));
     926#else  /* KMK - optimization */
     927# ifdef CONFIG_WITH_VALUE_LENGTH
     928  variable_expand_string_2 (buf, v->value, v->value_length, &buf);
     929# else
     930  variable_expand_string_2 (buf, v->value, strlen (v->value), &buf);
     931#  error "huh, this is supposed to be defined"
     932# endif
     933  assert (*buf == '\0');
     934  return buf;
     935#endif /* KMK - optimization */
    606936}
    607937
     
    611941append_expanded_string_to_variable (struct variable *v, const char *value, int append)
    612942{
     943char *tmp;
    613944  unsigned int value_len = strlen (value);
    614945  char *p = (char *) memchr (value, '$', value_len);
     
    637968          /* Append the assignment value. */
    638969          p = variable_buffer_output (p, value, off_dollar);
     970# ifndef KMK
    639971          p = variable_expand_string (p, value + off_dollar, value_len - off_dollar);
    640972          p = strchr (p, '\0');
     973# else
     974          tmp = variable_expand_string_2 (p, value + off_dollar, value_len - off_dollar, &p);
     975          assert (*p == '\0');
     976          tmp = strchr (tmp, '\0');
     977          assert (tmp == p);
     978# endif
    641979        }
    642980      else
     
    644982          /* Expand the assignemnt value. */
    645983          p = variable_buffer_output (p, value, off_dollar);
     984#ifndef KMK
    646985          p = variable_expand_string (p, value + off_dollar, value_len - off_dollar);
    647986          p = strchr (p, '\0');
     987#else
     988          tmp = variable_expand_string_2 (p, value + off_dollar, value_len - off_dollar, &p);
     989          assert (*p == '\0');
     990          tmp = strchr (tmp, '\0');
     991          assert (tmp == p);
     992#endif
    648993
    649994          /* Append a space followed by the old value. */
     
    7141059}
    7151060
     1061#ifdef KMK /* possible optimization... */
     1062/* The special, and most comment case, of
     1063   allocated_variable_expand_for_file. */
     1064
     1065char *
     1066allocated_variable_expand_2 (const char *line, long length)
     1067{
     1068  char *value;
     1069  char *obuf = variable_buffer;
     1070  unsigned int olen = variable_buffer_length;
     1071
     1072  variable_buffer = 0;
     1073
     1074#if 0 /* for profiling */
     1075  if (length < 0)
     1076    length = strlen (line);
     1077#endif
     1078
     1079  value = variable_expand_string (NULL, line, length);
     1080
     1081  variable_buffer = obuf;
     1082  variable_buffer_length = olen;
     1083
     1084  return value;
     1085}
     1086
     1087#endif
    7161088/* Install a new variable_buffer context, returning the current one for
    7171089   safe-keeping.  */
  • trunk/src/kmk/function.c

    r1793 r1805  
    931931  char *list = expand_argument (argv[1], NULL);
    932932  const char *body = argv[2];
     933#ifdef KMK
     934  long body_len = strlen (body);
     935#endif /* KMK - optimization */
    933936
    934937  int doneany = 0;
     
    944947  while ((p = find_next_token (&list_iterator, &len)) != 0)
    945948    {
     949#ifndef KMK
    946950      char *result = 0;
     951#endif /* KMK - optimization */
    947952#ifdef CONFIG_WITH_VALUE_LENGTH
    948953      if (len >= (unsigned int)var->value_alloc_len)
     
    960965#endif
    961966
     967#ifndef KMK
    962968      result = allocated_variable_expand (body);
    963969
     
    966972      doneany = 1;
    967973      free (result);
     974#else  /* KMK - optimization */
     975      variable_expand_string_2 (o, body, body_len, &o);
     976      o = variable_buffer_output (o, " ", 1);
     977      doneany = 1;
     978#endif /* KMK - optimization */
    968979    }
    969980
     
    43034314
    43044315      v->exp_count = EXP_COUNT_MAX;
     4316#ifndef KMK
    43054317      o = variable_expand_string (o, body, flen+3);
    43064318      v->exp_count = 0;
    43074319
    43084320      o += strlen (o);
     4321#else  /* KMK - optimization */
     4322      variable_expand_string_2 (o, body, flen+3, &o);
     4323      v->exp_count = 0;
     4324#endif /* KMK - optimization */
    43094325#ifdef CONFIG_WITH_EVALPLUS
    43104326    }
  • trunk/src/kmk/variable.c

    r1797 r1805  
    19961996  register char *end;
    19971997  enum variable_flavor flavor = f_bogus;
     1998#ifndef KMK
    19981999  char *name;
     2000#endif /* KMK - optimization */
    19992001
    20002002  while (1)
     
    20802082
    20812083  /* Expand the name, so "$(foo)bar = baz" works.  */
     2084#ifndef KMK
    20822085  name = alloca (end - beg + 1);
    20832086  memcpy (name, beg, end - beg);
    20842087  name[end - beg] = '\0';
    20852088  v->name = allocated_variable_expand (name);
     2089#else  /* KMK - optimizations */
     2090  //if (memchr (beg, '$', end - beg)) /* (Mostly for cleaning up the profiler result.) */
     2091      v->name = allocated_variable_expand_2 (beg, end - beg);
     2092  //else
     2093  //  {
     2094  //    v->name = memcpy (xmalloc (end - beg + 1), beg, end - beg);
     2095  //    v->name[end - beg] = '\0';
     2096  //  }
     2097#endif /* KMK - optimizations */
    20862098
    20872099  if (v->name[0] == '\0')
  • trunk/src/kmk/variable.h

    r1622 r1805  
    136136#endif
    137137char *allocated_variable_expand_for_file (const char *line, struct file *file);
     138#ifndef KMK
    138139#define allocated_variable_expand(line) \
    139140  allocated_variable_expand_for_file (line, (struct file *) 0)
     141#else  /* KMK */
     142# define allocated_variable_expand(line) \
     143  allocated_variable_expand_2 (line, -1)
     144char *allocated_variable_expand_2(const char *line, long length);
     145#endif
    140146char *expand_argument (const char *str, const char *end);
    141147char *variable_expand_string (char *line, const char *string, long length);
     148#ifdef KMK
     149char *variable_expand_string_2 (char *line, const char *string, long length, char **eol);
     150#endif
    142151void install_variable_buffer (char **bufp, unsigned int *lenp);
    143152void restore_variable_buffer (char *buf, unsigned int len);
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette