Changeset 531 for trunk/src


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

skip some unnecessary copies and allocations.

Location:
trunk/src/gmake
Files:
4 edited

Legend:

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

    r530 r531  
    886886    {
    887887      char *result = 0;
    888 
     888#ifdef CONFIG_WITH_VALUE_LENGTH
     889      if (len > (unsigned int)var->value_length)
     890        {
     891          free (var->value);
     892          var->value = xmalloc (len + 1);
     893        }
     894      memcpy (var->value, p, len);
     895      var->value[len] = '\0';
     896      var->value_length = len;
     897#else
    889898      {
    890         char save = p[len];
    891 
    892         p[len] = '\0';
    893         free (var->value);
    894         var->value = (char *) xstrdup ((char*) p);
    895         p[len] = save;
     899        char save = p[len];
     900
     901        p[len] = '\0';
     902        free (var->value);
     903        var->value = (char *) xstrdup ((char*) p);
     904        p[len] = save;
    896905      }
     906#endif
    897907
    898908      result = allocated_variable_expand (body);
  • trunk/src/gmake/kbuild.c

    r530 r531  
    5151kbuild_get_variable(const char *pszName)
    5252{
     53#ifndef NDEBUG
     54    unsigned i;
     55#endif
    5356    struct variable *pVar = lookup_variable(pszName, strlen(pszName));
    5457    if (!pVar)
     
    5659    if (pVar->recursive)
    5760        fatal(NILF, _("variable `%s' is defined as `recursive' instead of `simple'!"), pszName);
    58     pVar->value_length = strlen(pVar->value);
     61#ifndef NDEBUG
     62    i = strlen(pVar->value);
     63    if (i != pVar->value_length)
     64    {
     65        printf("%d != %d %s\n", pVar->value_length, i, pVar->name);
     66        __asm int 3;
     67    }
     68#endif
    5969    return pVar;
    6070}
     
    7080kbuild_get_recursive_variable(const char *pszName)
    7181{
     82#ifndef NDEBUG
     83    unsigned i;
     84#endif
    7285    struct variable *pVar = lookup_variable(pszName, strlen(pszName));
    7386    if (!pVar)
    7487        fatal(NILF, _("variable `%s' isn't defined!"), pszName);
    75     pVar->value_length = strlen(pVar->value);
     88#ifndef NDEBUG
     89    i = strlen(pVar->value);
     90    if (i != pVar->value_length)
     91    {
     92        printf("%d != %d %s\n", pVar->value_length, i, pVar->name);
     93        __asm int 3;
     94    }
     95#endif
    7696    return pVar;
    7797}
     
    90110    struct variable *pVar = lookup_variable(pszName, strlen(pszName));
    91111    if (pVar)
    92         pVar->value_length = strlen(pVar->value);
     112    {
     113#ifndef NDEBUG
     114        unsigned i = strlen(pVar->value);
     115        if (i != pVar->value_length)
     116        {
     117            printf("%d != %d %s\n", pVar->value_length, i, pVar->name);
     118            __asm int 3;
     119        }
     120#endif
     121    }
    93122    return pVar;
    94123}
     
    230259            *pszEnd = '\0';
    231260            if (!strchr(pszEnd, '$'))
    232                 pVar = define_variable(pszVarName, strlen(pszVarName), psz, o_file, 0 /* !recursive */);
     261                pVar = define_variable_vl(pszVarName, strlen(pszVarName), psz, pszEnd - psz,
     262                                          1 /* duplicate */, o_file, 0 /* !recursive */);
    233263            else
    234264            {
     
    244274                {
    245275                    *pszExpEnd = '\0';
    246                     pVar = define_variable(pszVarName, strlen(pszVarName), pszExp, o_file, 0 /* !recursive */);
     276                    pVar = define_variable_vl(pszVarName, strlen(pszVarName), pszExp,
     277                                              pszExpEnd - pszExp, 1 /* duplicate */,
     278                                              o_file, 0 /* !recursive */);
    247279                }
    248280                else
     
    362394        + pszSrcEnd - pszSrc
    363395        + 1;
    364     psz = pszResult = alloca(cch);
     396    psz = pszResult = xmalloc(cch);
    365397
    366398    memcpy(psz, pPathTarget->value, pPathTarget->value_length); psz += pPathTarget->value_length;
     
    374406     * Define the variable in the current set and return it.
    375407     */
    376     return define_variable(pszVarName, strlen(pszVarName), pszResult, o_file, 0 /* !recursive */);
     408    return define_variable_vl(pszVarName, strlen(pszVarName), pszResult, cch - 1,
     409                              0 /* use pszResult */, o_file, 0 /* !recursive */);
    377410}
    378411
     
    440473            *pszEnd = '\0';
    441474            if (!strchr(pszEnd, '$'))
    442                 pVar = define_variable(pszVarName, strlen(pszVarName), psz, o_file, 0 /* !recursive */);
     475                pVar = define_variable_vl(pszVarName, strlen(pszVarName), psz, pszEnd - psz,
     476                                          1 /* duplicate */, o_file, 0 /* !recursive */);
    443477            else
    444478            {
     
    454488                {
    455489                    *pszExpEnd = '\0';
    456                     pVar = define_variable(pszVarName, strlen(pszVarName), pszExp, o_file, 0 /* !recursive */);
     490                    pVar = define_variable_vl(pszVarName, strlen(pszVarName), pszExp,
     491                                              pszExpEnd - pszExp, 1 /* duplicate */,
     492                                              o_file, 0 /* !recursive */);
    457493                }
    458494                else
     
    954990            free(paVars[iVar].pszExp);
    955991    }
    956     psz[-(cchTotal != 0)] = '\0';
    957 
    958     pVar = define_variable(pszVarName, strlen(pszVarName), pszResult, o_file, 0 /* !recursive */);
    959     free(pszResult);
     992    if (psz != pszResult)
     993        psz--;
     994    *psz = '\0';
     995    cchTotal = psz - pszResult;
     996
     997    pVar = define_variable_vl(pszVarName, strlen(pszVarName), pszResult, cchTotal,
     998                              0 /* take pszResult */ , o_file, 0 /* !recursive */);
    960999    return pVar;
    9611000}
     
    10211060    memcpy(psz, pObjSuff->value, pObjSuff->value_length);   psz += pObjSuff->value_length;
    10221061    memcpy(psz, pDepSuff->value, pDepSuff->value_length + 1);
    1023     *ppDep = define_variable("dep", 3, pszResult, o_file, 0 /* !recursive */);
     1062    *ppDep = define_variable_vl("dep", 3, pszResult, cch - 1, 1 /*dup*/, o_file, 0 /* !recursive */);
    10241063
    10251064    /*
     
    10271066     */
    10281067    *psz = '\0';
    1029     pObj = define_variable(pszVarName, strlen(pszVarName), pszResult, o_file, 0 /* !recursive */);
     1068    pObj = define_variable_vl(pszVarName, strlen(pszVarName), pszResult, psz - pszResult,
     1069                              1/* dup */, o_file, 0 /* !recursive */);
    10301070
    10311071    /*
     
    10771117
    10781118    /* set global variable */
    1079     define_variable_global(pszName, cch, pszResult, o_file, 0 /* !recursive */, NILF);
     1119    define_variable_vl_global(pszName, cch, pszResult, psz - pszResult, 1/*dup*/, o_file, 0 /* !recursive */, NILF);
    10801120
    10811121    /*
     
    10921132        *psz = '\0';
    10931133    }
    1094     *ppDirDep = define_variable("dirdep", 6, pszResult, o_file, 0 /* !recursive */);
     1134    *ppDirDep = define_variable_vl("dirdep", 6, pszResult, psz - pszResult, 1/*dup*/, o_file, 0 /* !recursive */);
    10951135
    10961136    return pObj;
  • 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;
  • trunk/src/gmake/variable.h

    r530 r531  
    162162                                                        const struct variable_set *set));
    163163
     164#ifdef CONFIG_WITH_VALUE_LENGTH
     165
     166extern struct variable *define_variable_in_set
     167    PARAMS ((const char *name, unsigned int length, char *value,
     168             unsigned int value_length, int duplicate_value,
     169             enum variable_origin origin, int recursive,
     170             struct variable_set *set, const struct floc *flocp));
     171
     172/* Define a variable in the current variable set.  */
     173
     174#define define_variable(n,l,v,o,r) \
     175          define_variable_in_set((n),(l),(v), ~0U,1,(o),(r),\
     176                                 current_variable_set_list->set,NILF)
     177
     178#define define_variable_vl(n,l,v,vl,dv,o,r) \
     179          define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),\
     180                                 current_variable_set_list->set,NILF)
     181
     182/* Define a variable with a location in the current variable set.  */
     183
     184#define define_variable_loc(n,l,v,o,r,f) \
     185          define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
     186                                 current_variable_set_list->set,(f))
     187
     188/* Define a variable with a location in the global variable set.  */
     189
     190#define define_variable_global(n,l,v,o,r,f) \
     191          define_variable_in_set((n),(l),(v),~0U,1,(o),(r),NULL,(f))
     192
     193#define define_variable_vl_global(n,l,v,vl,dv,o,r,f) \
     194          define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),NULL,(f))
     195
     196/* Define a variable in FILE's variable set.  */
     197
     198#define define_variable_for_file(n,l,v,o,r,f) \
     199          define_variable_in_set((n),(l),(v),~0U,1,(o),(r),(f)->variables->set,NILF)
     200
     201#else
     202
    164203extern struct variable *define_variable_in_set
    165204    PARAMS ((const char *name, unsigned int length, char *value,
     
    188227#define define_variable_for_file(n,l,v,o,r,f) \
    189228          define_variable_in_set((n),(l),(v),(o),(r),(f)->variables->set,NILF)
     229
     230#endif
    190231
    191232/* Warn that NAME is an undefined variable.  */
Note: See TracChangeset for help on using the changeset viewer.