emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] trunk r117806: Minor cleanup of recent strlen-avoiding pat


From: Paul Eggert
Subject: [Emacs-diffs] trunk r117806: Minor cleanup of recent strlen-avoiding patch.
Date: Tue, 02 Sep 2014 18:05:08 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 117806
revision-id: address@hidden
parent: address@hidden
committer: Paul Eggert <address@hidden>
branch nick: trunk
timestamp: Tue 2014-09-02 11:05:00 -0700
message:
  Minor cleanup of recent strlen-avoiding patch.
  
  * src/fileio.c (CHECK_LENGTH): Remove.
  Rewrite callers so that they don't need it.
  (Fexpand_file_name) [DOS_NT]: Fix a case where directory length
  variable wasn't set.
modified:
  src/ChangeLog                  changelog-20091113204419-o5vbwnq5f7feedwu-1438
  src/buffer.c                   buffer.c-20091113204419-o5vbwnq5f7feedwu-264
  src/data.c                     data.c-20091113204419-o5vbwnq5f7feedwu-251
  src/fileio.c                   fileio.c-20091113204419-o5vbwnq5f7feedwu-210
  src/lisp.h                     lisp.h-20091113204419-o5vbwnq5f7feedwu-253
=== modified file 'src/ChangeLog'
--- a/src/ChangeLog     2014-09-02 11:41:22 +0000
+++ b/src/ChangeLog     2014-09-02 18:05:00 +0000
@@ -1,3 +1,11 @@
+2014-09-02  Paul Eggert  <address@hidden>
+
+       Minor cleanup of recent strlen-avoiding patch.
+       * fileio.c (CHECK_LENGTH): Remove.
+       Rewrite callers so that they don't need it.
+       (Fexpand_file_name) [DOS_NT]: Fix a case where directory length
+       variable wasn't set.
+
 2014-09-02  Dmitry Antipov  <address@hidden>
 
        * fileio.c (CHECK_LENGTH): New macro.

=== modified file 'src/buffer.c'
--- a/src/buffer.c      2014-09-02 11:41:22 +0000
+++ b/src/buffer.c      2014-09-02 18:05:00 +0000
@@ -1276,10 +1276,10 @@
 For a symbol that is locally unbound, just the symbol appears in the value.
 Note that storing new VALUEs in these elements doesn't change the variables.
 No argument or nil as argument means use current buffer as BUFFER.  */)
-  (register Lisp_Object buffer)
+  (Lisp_Object buffer)
 {
-  register struct buffer *buf = decode_buffer (buffer);
-  register Lisp_Object result = buffer_lisp_local_variables (buf, 0);
+  struct buffer *buf = decode_buffer (buffer);
+  Lisp_Object result = buffer_lisp_local_variables (buf, 0);
 
   /* Add on all the variables stored in special slots.  */
   {
@@ -1306,9 +1306,9 @@
        0, 1, 0,
        doc: /* Return t if BUFFER was modified since its file was last read or 
saved.
 No argument or nil as argument means use current buffer as BUFFER.  */)
-  (register Lisp_Object buffer)
+  (Lisp_Object buffer)
 {
-  register struct buffer *buf = decode_buffer (buffer);
+  struct buffer *buf = decode_buffer (buffer);
   return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
 }
 

=== modified file 'src/data.c'
--- a/src/data.c        2014-09-02 11:41:22 +0000
+++ b/src/data.c        2014-09-02 18:05:00 +0000
@@ -1952,9 +1952,9 @@
        1, 2, 0,
        doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
 BUFFER defaults to the current buffer.  */)
-  (register Lisp_Object variable, Lisp_Object buffer)
+  (Lisp_Object variable, Lisp_Object buffer)
 {
-  register struct buffer *buf = decode_buffer (buffer);
+  struct buffer *buf = decode_buffer (buffer);
   struct Lisp_Symbol *sym;
 
   CHECK_SYMBOL (variable);

=== modified file 'src/fileio.c'
--- a/src/fileio.c      2014-09-02 11:41:22 +0000
+++ b/src/fileio.c      2014-09-02 18:05:00 +0000
@@ -847,15 +847,6 @@
   return make_temp_name (prefix, 0);
 }
 
-/* The following function does a lot of work with \0-terminated strings.
-   To avoid extra calls to strlen and strcat, we maintain an important
-   lengths explicitly.  This macro is used to check whether we're in sync.  */
-#ifdef ENABLE_CHECKING
-#define CHECK_LENGTH(str, len) (eassert (strlen (str) == len), len)
-#else
-#define CHECK_LENGTH(str, len) (len)
-#endif /* ENABLE_CHECKING */
-
 DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
        doc: /* Convert filename NAME to absolute, and canonicalize it.
 Second arg DEFAULT-DIRECTORY is directory to start with if NAME is relative
@@ -885,7 +876,9 @@
   /* These point to SDATA and need to be careful with string-relocation
      during GC (via DECODE_FILE).  */
   char *nm;
+  char *nmlim;
   const char *newdir;
+  const char *newdirlim;
   /* This should only point to alloca'd data.  */
   char *target;
 
@@ -893,10 +886,10 @@
   struct passwd *pw;
 #ifdef DOS_NT
   int drive = 0;
-  bool collapse_newdir = 1;
+  bool collapse_newdir = true;
   bool is_escaped = 0;
 #endif /* DOS_NT */
-  ptrdiff_t length, newdirlen, nmlen, nbytes;
+  ptrdiff_t length, nbytes;
   Lisp_Object handler, result, handled_name;
   bool multibyte;
   Lisp_Object hdir;
@@ -1027,14 +1020,13 @@
 
   /* Make a local copy of NAME to protect it from GC in DECODE_FILE below.  */
   nm = xlispstrdupa (name);
-  nmlen = SBYTES (name);
+  nmlim = nm + SBYTES (name);
 
 #ifdef DOS_NT
   /* Note if special escape prefix is present, but remove for now.  */
   if (nm[0] == '/' && nm[1] == ':')
     {
       is_escaped = 1;
-      nmlen -= 2;
       nm += 2;
     }
 
@@ -1044,7 +1036,6 @@
   if (IS_DRIVE (nm[0]) && IS_DEVICE_SEP (nm[1]))
     {
       drive = (unsigned char) nm[0];
-      nmlen -= 2;
       nm += 2;
     }
 
@@ -1053,7 +1044,7 @@
      colon when stripping the drive letter.  Otherwise, this expands to
      "//somedir".  */
   if (drive && IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1]))
-    nmlen--, nm++;
+    nm++;
 
   /* Discard any previous drive specifier if nm is now in UNC format.  */
   if (IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1])
@@ -1114,8 +1105,7 @@
          if (IS_DIRECTORY_SEP (nm[1]))
            {
              if (strcmp (nm, SSDATA (name)) != 0)
-               name = make_specified_string
-                 (nm, -1, CHECK_LENGTH (nm, nmlen), multibyte);
+               name = make_specified_string (nm, -1, nmlim - nm, multibyte);
            }
          else
 #endif
@@ -1136,8 +1126,7 @@
 #else /* not DOS_NT */
          if (strcmp (nm, SSDATA (name)) == 0)
            return name;
-         return make_specified_string
-           (nm, -1, CHECK_LENGTH (nm, nmlen), multibyte);
+         return make_specified_string (nm, -1, nmlim - nm, multibyte);
 #endif /* not DOS_NT */
        }
     }
@@ -1158,8 +1147,7 @@
      return an absolute name, if the final prefix is not absolute we
      append it to the current working directory.  */
 
-  newdir = 0;
-  newdirlen = -1;
+  newdir = newdirlim = 0;
 
   if (nm[0] == '~')            /* prefix ~ */
     {
@@ -1169,8 +1157,8 @@
          Lisp_Object tem;
 
          if (!(newdir = egetenv ("HOME")))
-           newdir = "";
-         nmlen--, nm++;
+           newdir = newdirlim = "";
+         nm++;
          /* `egetenv' may return a unibyte string, which will bite us since
             we expect the directory to be multibyte.  */
 #ifdef WINDOWSNT
@@ -1184,15 +1172,15 @@
          else
 #endif
            tem = build_string (newdir);
-         newdirlen = SBYTES (tem);
+         newdirlim = newdir + SBYTES (tem);
          if (multibyte && !STRING_MULTIBYTE (tem))
            {
              hdir = DECODE_FILE (tem);
              newdir = SSDATA (hdir);
-             newdirlen = SBYTES (hdir);
+             newdirlim = newdir + SBYTES (hdir);
            }
 #ifdef DOS_NT
-         collapse_newdir = 0;
+         collapse_newdir = false;
 #endif
        }
       else                     /* ~user/filename */
@@ -1216,17 +1204,16 @@
                 bite us since we expect the directory to be
                 multibyte.  */
              tem = build_string (newdir);
-             newdirlen = SBYTES (tem);
+             newdirlim = newdir + SBYTES (tem);
              if (multibyte && !STRING_MULTIBYTE (tem))
                {
                  hdir = DECODE_FILE (tem);
                  newdir = SSDATA (hdir);
-                 newdirlen = SBYTES (hdir);
+                 newdirlim = newdir + SBYTES (hdir);
                }
-             nmlen -= (p - nm);
              nm = p;
 #ifdef DOS_NT
-             collapse_newdir = 0;
+             collapse_newdir = false;
 #endif
            }
 
@@ -1252,8 +1239,8 @@
              Lisp_Object tem = build_string (adir);
 
              tem = DECODE_FILE (tem);
-             newdirlen = SBYTES (tem);
-             memcpy (adir, SSDATA (tem), newdirlen + 1);
+             newdirlim = adir + SBYTES (tem);
+             memcpy (adir, SSDATA (tem), SBYTES (tem) + 1);
            }
        }
       if (!adir)
@@ -1264,7 +1251,7 @@
          adir[1] = ':';
          adir[2] = '/';
          adir[3] = 0;
-         newdirlen = 3;
+         newdirlim = adir + 3;
        }
       newdir = adir;
     }
@@ -1285,13 +1272,12 @@
       && !newdir)
     {
       newdir = SSDATA (default_directory);
-      newdirlen = SBYTES (default_directory);
+      newdirlim = newdir + SBYTES (default_directory);
 #ifdef DOS_NT
       /* Note if special escape prefix is present, but remove for now.  */
       if (newdir[0] == '/' && newdir[1] == ':')
        {
          is_escaped = 1;
-         newdirlen -= 2;
          newdir += 2;
        }
 #endif
@@ -1327,18 +1313,19 @@
          if (IS_DRIVE (newdir[0]) && IS_DEVICE_SEP (newdir[1]))
            {
              drive = (unsigned char) newdir[0];
-             newdirlen -= 2;
              newdir += 2;
            }
          if (!IS_DIRECTORY_SEP (nm[0]))
            {
+             ptrdiff_t nmlen = nmlim - nm;
+             ptrdiff_t newdirlen = newdirlim - newdir;
              char *tmp = alloca (newdirlen + file_name_as_directory_slop
-                                 + CHECK_LENGTH (nm, nmlen) + 1);
-             nbytes = file_name_as_directory (tmp, newdir, newdirlen,
-                                              multibyte);
-             memcpy (tmp + nbytes, nm, nmlen + 1);
-             nmlen += nbytes;
+                                 + nmlen + 1);
+             ptrdiff_t dlen = file_name_as_directory (tmp, newdir, newdirlen,
+                                                      multibyte);
+             memcpy (tmp + dlen, nm, nmlen + 1);
              nm = tmp;
+             nmlim = nm + dlen + nmlen;
            }
          adir = alloca (adir_size);
          if (drive)
@@ -1353,11 +1340,11 @@
              Lisp_Object tem = build_string (adir);
 
              tem = DECODE_FILE (tem);
-             newdirlen = SBYTES (tem);
-             memcpy (adir, SSDATA (tem), newdirlen + 1);
+             newdirlim = adir + SBYTES (tem);
+             memcpy (adir, SSDATA (tem), SBYTES (tem) + 1);
            }
          else
-           newdirlen = strlen (adir);
+           newdirlim = adir + strlen (adir);
          newdir = adir;
        }
 
@@ -1365,7 +1352,6 @@
       if (IS_DRIVE (newdir[0]) && IS_DEVICE_SEP (newdir[1]))
        {
          drive = newdir[0];
-         newdirlen -= 2;
          newdir += 2;
        }
 
@@ -1377,36 +1363,31 @@
          if (IS_DIRECTORY_SEP (newdir[0]) && IS_DIRECTORY_SEP (newdir[1])
              && !IS_DIRECTORY_SEP (newdir[2]))
            {
-             char *adir = strcpy (alloca (newdirlen + 1), newdir);
+             char *adir = strcpy (alloca (newdirlim - newdir + 1), newdir);
              char *p = adir + 2;
              while (*p && !IS_DIRECTORY_SEP (*p)) p++;
              p++;
              while (*p && !IS_DIRECTORY_SEP (*p)) p++;
              *p = 0;
              newdir = adir;
-             newdirlen = strlen (adir);
+             newdirlim = newdir + strlen (adir);
            }
          else
 #endif
-           newdirlen = 0, newdir = "";
+           newdir = newdirlim = "";
        }
     }
 #endif /* DOS_NT */
 
-  if (newdir)
-    {
-      /* Ignore any slash at the end of newdir, unless newdir is
-        just "/" or "//".  */
-      length = CHECK_LENGTH (newdir, newdirlen);
-      while (length > 1 && IS_DIRECTORY_SEP (newdir[length - 1])
-            && ! (length == 2 && IS_DIRECTORY_SEP (newdir[0])))
-       length--;
-    }
-  else
-    length = 0;
+  /* Ignore any slash at the end of newdir, unless newdir is
+     just "/" or "//".  */
+  length = newdirlim - newdir;
+  while (length > 1 && IS_DIRECTORY_SEP (newdir[length - 1])
+        && ! (length == 2 && IS_DIRECTORY_SEP (newdir[0])))
+    length--;
 
   /* Now concatenate the directory and name to new space in the stack frame.  
*/
-  tlen = length + file_name_as_directory_slop + CHECK_LENGTH (nm, nmlen) + 1;
+  tlen = length + file_name_as_directory_slop + (nmlim - nm) + 1;
 #ifdef DOS_NT
   /* Reserve space for drive specifier and escape prefix, since either
      or both may need to be inserted.  (The Microsoft x86 compiler
@@ -1442,7 +1423,7 @@
        nbytes = file_name_as_directory (target, newdir, length, multibyte);
     }
 
-  memcpy (target + nbytes, nm, nmlen + 1);
+  memcpy (target + nbytes, nm, nmlim - nm + 1);
 
   /* Now canonicalize by removing `//', `/.' and `/foo/..' if they
      appear.  */

=== modified file 'src/lisp.h'
--- a/src/lisp.h        2014-09-02 06:49:40 +0000
+++ b/src/lisp.h        2014-09-02 18:05:00 +0000
@@ -4444,12 +4444,10 @@
 
 extern char *egetenv_internal (const char *, ptrdiff_t);
 
-/* VAR is usually a compile-time constant, so the
-   call to strlen is likely to be optimized away.  */
-
 INLINE char *
-egetenv(const char *var)
+egetenv (const char *var)
 {
+  /* When VAR is a string literal, strlen can be optimized away.  */
   return egetenv_internal (var, strlen (var));
 }
 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]