Changeset 2591 for trunk/src/kmk/misc.c


Ignore:
Timestamp:
Jun 17, 2012, 10:45:31 PM (13 years ago)
Author:
bird
Message:

kmk: Merged in changes from GNU make 3.82. Previous GNU make base version was gnumake-2008-10-28-CVS.

Location:
trunk/src/kmk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk

    • Property svn:ignore
      •  

        old new  
        1313stamp-*
        1414makebook*
         15
        1516.*gdbinit
         17.gdb_history
         18
        1619*.dep
        1720*.dvi
         
        3134*.pg
        3235*.pgs
         36
        3337README
        3438README.DOS
        3539README.W32
         40README.OS2
        3641aclocal.m4
        3742autom4te.cache
         
        5257config.h.W32
        5358config.h-vms
         59
        5460loadavg
        5561loadavg.c
        5662make
         63
        5764.deps
        5865.dep_segment
         66ID
         67TAGS
         68
        5969_*
        6070sun4
         
        7282sol2
        7383i486-linux
         84
        7485customs
         86
        7587install-sh
        7688mkinstalldirs
         89
         90.directive.asc
  • trunk/src/kmk/misc.c

    r2548 r2591  
    11/* Miscellaneous generic support functions for GNU Make.
    22Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
    3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
    4 Foundation, Inc.
     31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
     42010 Free Software Foundation, Inc.
    55This file is part of GNU Make.
    66
     
    166166      ++in;
    167167
    168       /* If the newline is quoted, discard following whitespace
    169          and any preceding whitespace; leave just one space.  */
     168      /* If the newline is escaped, discard following whitespace leaving just
     169         one space.  POSIX requires that each backslash/newline/following
     170         whitespace sequence be reduced to a single space.  */
    170171      if (backslash)
    171172        {
    172173          in = next_token (in);
     174          /* Removing this loop will fix Savannah bug #16670: do we want to? */
    173175          while (out > line && isblank ((unsigned char)out[-1]))
    174176            --out;
     
    218220
    219221
    220 /* Return a string whose contents concatenate those of s1, s2, s3.
     222/* Return a string whose contents concatenate the NUM strings provided
    221223   This string lives in static, re-used memory.  */
    222224
    223 char *
    224 concat (const char *s1, const char *s2, const char *s3)
    225 {
    226   unsigned int len1, len2, len3;
     225const char *
     226#if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
     227concat (unsigned int num, ...)
     228#else
     229concat (num, va_alist)
     230     unsigned int num;
     231     va_dcl
     232#endif
     233{
    227234  static unsigned int rlen = 0;
    228235  static char *result = NULL;
    229 
    230   len1 = (s1 && *s1 != '\0') ? strlen (s1) : 0;
    231   len2 = (s2 && *s2 != '\0') ? strlen (s2) : 0;
    232   len3 = (s3 && *s3 != '\0') ? strlen (s3) : 0;
    233 
    234   if (len1 + len2 + len3 + 1 > rlen)
    235     result = xrealloc (result, (rlen = len1 + len2 + len3 + 10));
    236 
    237   if (len1)
    238     memcpy (result, s1, len1);
    239   if (len2)
    240     memcpy (result + len1, s2, len2);
    241   if (len3)
    242     memcpy (result + len1 + len2, s3, len3);
    243 
    244   result[len1+len2+len3] = '\0';
     236  unsigned int ri = 0; /* bird: must be unsigned */
     237
     238#if USE_VARIADIC
     239  va_list args;
     240#endif
     241
     242  VA_START (args, num);
     243
     244  while (num-- > 0)
     245    {
     246      const char *s = va_arg (args, const char *);
     247      unsigned int l = s ? strlen (s) : 0;
     248
     249      if (l == 0)
     250        continue;
     251
     252      if (ri + l > rlen)
     253        {
     254          rlen = ((rlen ? rlen : 60) + l) * 2;
     255          result = xrealloc (result, rlen);
     256        }
     257
     258      memcpy (result + ri, s, l);
     259      ri += l;
     260    }
     261
     262  VA_END (args);
     263
     264  /* Get some more memory if we don't have enough space for the
     265     terminating '\0'.   */
     266  if (ri == rlen)
     267    {
     268      rlen = (rlen ? rlen : 60) * 2;
     269      result = xrealloc (result, rlen);
     270    }
     271
     272  result[ri] = '\0';
    245273
    246274  return result;
     
    397425
    398426#undef xmalloc
     427#undef xcalloc
    399428#undef xrealloc
    400429#undef xstrdup
     
    403432xmalloc (unsigned int size)
    404433{
    405   /* Make sure we don't allocate 0, for pre-ANSI libraries.  */
     434  /* Make sure we don't allocate 0, for pre-ISO implementations.  */
    406435  void *result = malloc (size ? size : 1);
    407436  if (result == 0)
     
    415444    make_stats_allocated += size;
    416445#endif
     446  return result;
     447}
     448
     449
     450void *
     451xcalloc (unsigned int size)
     452{
     453  /* Make sure we don't allocate 0, for pre-ISO implementations.  */
     454  void *result = calloc (size ? size : 1, 1);
     455  if (result == 0)
     456    fatal (NILF, _("virtual memory exhausted"));
    417457  return result;
    418458}
     
    432472#endif
    433473
    434   /* Some older implementations of realloc() don't conform to ANSI.  */
     474  /* Some older implementations of realloc() don't conform to ISO.  */
    435475  if (! size)
    436476    size = 1;
     
    480520
    481521char *
    482 savestring (const char *str, unsigned int length)
    483 {
    484   char *out = xmalloc (length + 1);
     522xstrndup (const char *str, unsigned int length)
     523{
     524  char *result;
     525
     526#if defined(HAVE_STRNDUP) && !defined(KMK)
     527  result = strndup (str, length);
     528  if (result == 0)
     529    fatal (NILF, _("virtual memory exhausted"));
     530#else
     531  result = xmalloc (length + 1);
    485532  if (length > 0)
    486     memcpy (out, str, length);
    487   out[length] = '\0';
    488   return out;
     533    strncpy (result, str, length);
     534  result[length] = '\0';
     535#endif
     536
     537  return result;
    489538}
    490539
     
    786835
    787836
    788 /* Allocate a new `struct dep' with all fields initialized to 0.   */
    789 
    790 struct dep *
    791 alloc_dep ()
    792 {
    793 #ifndef CONFIG_WITH_ALLOC_CACHES
    794   struct dep *d = xmalloc (sizeof (struct dep));
    795   memset (d, '\0', sizeof (struct dep));
    796   return d;
    797 #else
    798   return (struct dep *) alloccache_calloc (&dep_cache);
    799 #endif
    800 }
    801 
    802 
    803 /* Free `struct dep' along with `name' and `stem'.   */
    804 
    805 void
    806 free_dep (struct dep *d)
    807 {
    808 #ifndef CONFIG_WITH_ALLOC_CACHES
    809   free (d);
    810 #else
    811   alloccache_free (&dep_cache, d);
    812 #endif
    813 }
    814 
    815 /* Copy a chain of `struct dep', making a new chain
    816    with the same contents as the old one.  */
     837/* Copy a chain of `struct dep'.  For 2nd expansion deps, dup the name.  */
    817838
    818839struct dep *
     
    827848      struct dep *c = xmalloc (sizeof (struct dep));
    828849#else
    829       struct dep *c = (struct dep *) alloccache_alloc (&dep_cache);
     850      struct dep *c = alloccache_alloc(&dep_cache);
    830851#endif
    831852      memcpy (c, d, sizeof (struct dep));
     853
     854      /** @todo KMK: Check if we need this duplication! */
     855      if (c->need_2nd_expansion)
     856        c->name = xstrdup (c->name);
    832857
    833858      c->next = 0;
     
    852877      struct dep *df = d;
    853878      d = d->next;
    854 #ifndef CONFIG_WITH_ALLOC_CACHES
    855879      free_dep (df);
    856 #else
    857       alloccache_free (&dep_cache, df);
    858 #endif
    859880    }
    860881}
     
    903924      return (c1 - c2);
    904925    }
     926}
     927#endif
     928
     929#if !HAVE_STRNCASECMP && !HAVE_STRNICMP && !HAVE_STRNCMPI
     930
     931/* If we don't have strncasecmp() (from POSIX), or anything that can
     932   substitute for it, define our own version.  */
     933
     934int
     935strncasecmp (const char *s1, const char *s2, int n)
     936{
     937  while (n-- > 0)
     938    {
     939      int c1 = (int) *(s1++);
     940      int c2 = (int) *(s2++);
     941
     942      if (isalpha (c1))
     943        c1 = tolower (c1);
     944      if (isalpha (c2))
     945        c2 = tolower (c2);
     946
     947      if (c1 != '\0' && c1 == c2)
     948        continue;
     949
     950      return (c1 - c2);
     951    }
     952
     953  return 0;
    905954}
    906955#endif
     
    12341283
    12351284  malloc_zone_statistics (NULL, &s);
    1236   printf (_("\n# CRT Heap: %zu bytes in use, in %u blocks, avg %zu bytes/block\n"),
    1237           s.size_in_use, s.blocks_in_use, s.size_in_use / s.blocks_in_use);
    1238   printf (_("#           %zu bytes max in use (high water mark)\n"),
    1239           s.max_size_in_use);
    1240   printf (_("#           %zu bytes reserved,  %zu bytes free (estimate)\n"),
    1241           s.size_allocated, s.size_allocated - s.size_in_use);
     1285  printf (_("\n# CRT Heap: %u bytes in use, in %u blocks, avg %u bytes/block\n"),
     1286          (unsigned)s.size_in_use, (unsigned)s.blocks_in_use,
     1287          (unsigned)(s.size_in_use / s.blocks_in_use));
     1288  printf (_("#           %u bytes max in use (high water mark)\n"),
     1289          (unsigned)s.max_size_in_use);
     1290  printf (_("#           %u bytes reserved,  %u bytes free (estimate)\n"),
     1291          (unsigned)s.size_allocated,
     1292          (unsigned)(s.size_allocated - s.size_in_use));
    12421293# endif /* __APPLE__ */
    12431294
     
    13741425format_elapsed_nano (char *buf, size_t size, big_int ts)
    13751426{
    1376   int sz;
     1427  unsigned sz;
    13771428  if (ts < 1000)
    13781429    sz = sprintf (buf, "%uns", (unsigned)ts);
     
    14071458    }
    14081459  if (sz >= size)
    1409     fatal (NILF, _("format_elapsed_nano buffer overflow: %d written, %d buffer"),
    1410            sz, size);
     1460    fatal (NILF, _("format_elapsed_nano buffer overflow: %u written, %lu buffer"),
     1461           sz, (unsigned long)size);
    14111462  return sz;
    14121463}
Note: See TracChangeset for help on using the changeset viewer.