VirtualBox

Changeset 1928 in kBuild


Ignore:
Timestamp:
Oct 24, 2008 5:19:23 AM (16 years ago)
Author:
bird
Message:

expand.c: added allocated_variable_expand_3 and recycle_variable_buffer in order to avoid some frequent free() and xmalloc() calls during variable expansion in variable_expand_string_2.

File:
1 edited

Legend:

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

    r1927 r1928  
    5151#endif
    5252char *variable_buffer;
     53
     54
     55#ifdef CONFIG_WITH_VALUE_LENGTH
     56struct recycled_buffer
     57{
     58  struct recycled_buffer *next;
     59  unsigned int length;
     60};
     61struct recycled_buffer *recycled_head;
     62static char *
     63allocated_variable_expand_3 (const char *line, unsigned int length,
     64                             unsigned int *value_lenp,
     65                             unsigned int *buffer_lengthp);
     66static void
     67recycle_variable_buffer (char *buffer, unsigned int length);
     68
     69#endif
     70
     71
    5372
    5473#ifndef KMK
     
    87106  /* If we don't have a variable output buffer yet, get one.  */
    88107
     108#ifdef CONFIG_WITH_VALUE_LENGTH
    89109  if (variable_buffer == 0)
    90110    {
    91 #ifdef KMK
    92       variable_buffer_length = 384;
     111      struct recycled_buffer *recycled = recycled_head;
     112      if (recycled)
     113        {
     114          recycled_head = recycled->next;
     115          variable_buffer_length = recycled->length;
     116          variable_buffer = (char *)recycled;
     117        }
     118      else
     119        {
     120          variable_buffer_length = 384;
     121          variable_buffer = xmalloc (variable_buffer_length);
     122        }
     123      variable_buffer[0] = '\0';
     124    }
    93125#else
     126  if (variable_buffer == 0)
     127    {
    94128      variable_buffer_length = 200;
    95 #endif
    96129      variable_buffer = xmalloc (variable_buffer_length);
    97130      variable_buffer[0] = '\0';
    98131    }
     132#endif
    99133
    100134  return variable_buffer;
     
    545579            char *op;
    546580            char *abeg = NULL;
     581            unsigned int alen = 0;
    547582            const char *end, *colon;
    548583
     
    590625                     /* Expand the name.  */
    591626                    saved = *p;
    592                     *(char *)p = '\0'; /* XXX: proove that this is safe! */
    593                     abeg = allocated_variable_expand_2 (beg, p - beg, &len);
     627                    *(char *)p = '\0'; /* XXX: proove that this is safe! XXX2: shouldn't be necessary any longer! */
     628                    abeg = allocated_variable_expand_3 (beg, p - beg, &len, &alen);
    594629                    beg = abeg;
    595630                    end = beg + len;
     
    687722
    688723          if (abeg)
    689             free (abeg);
     724            recycle_variable_buffer (abeg, alen);
    690725          }
    691726          break;
     
    10351070  return value;
    10361071}
     1072
     1073/* Handles a special case for variable_expand_string2 where the variable
     1074   name is expanded.  This variant allows the variable_buffer to
     1075   be recycled and thus avoid bothering with a slow free implementation
     1076   (darwin is horrible here).  */
     1077
     1078static char *
     1079allocated_variable_expand_3 (const char *line, unsigned int length,
     1080                             unsigned int *value_lenp,
     1081                             unsigned int *buffer_lengthp)
     1082{
     1083  char        *obuf = variable_buffer;
     1084  unsigned int olen = variable_buffer_length;
     1085  long         len  = (long)length;
     1086  char *value;
     1087  char *eol;
     1088
     1089  variable_buffer = 0;
     1090
     1091  value = variable_expand_string_2 (NULL, line, len, &eol);
     1092  *value_lenp = eol - value;
     1093  *buffer_lengthp = variable_buffer_length;
     1094
     1095  variable_buffer = obuf;
     1096  variable_buffer_length = olen;
     1097
     1098  return value;
     1099}
     1100
     1101/* recycle a buffer. */
     1102static void
     1103recycle_variable_buffer (char *buffer, unsigned int length)
     1104{
     1105    struct recycled_buffer *recycled = (struct recycled_buffer *)buffer;
     1106
     1107    assert (!(length & 31));
     1108    assert (length >= 384);
     1109    recycled->length = length;
     1110    recycled->next = recycled_head;
     1111    recycled_head = recycled;
     1112}
     1113
    10371114#endif /* CONFIG_WITH_VALUE_LENGTH */
    10381115
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