From 59e10fbd934323702a4586f50139d58db846bbf1 Mon Sep 17 00:00:00 2001 From: Dmitry Antipov Date: Tue, 23 Sep 2014 19:49:00 +0400 Subject: [PATCH] Use known length of a Lisp string to copy it faster. * lisp.h (lispstrcpy): New function. Add comment. * callproc.c (child_setup): * dbusbind.c (xd_append_arg): * doc.c (get_doc_string): * font.c (Ffont_xlfd_name): * frame.c (xrdb_get_resource): * process.c (Fmake_network_process, network_interface_info): * w32fns.c (Fx_open_connection): * w32proc.c (sys_spawnve): * xfns.c (select_visual): * xfont.c (xfont_list): * xsmfns.c (x_session_initialize): * xterm.c (x_term_init): Use it. --- src/ChangeLog | 19 ++++++++++++++++++- src/callproc.c | 2 +- src/dbusbind.c | 2 +- src/doc.c | 2 +- src/font.c | 2 +- src/frame.c | 4 ++-- src/lisp.h | 9 +++++++++ src/process.c | 4 ++-- src/w32fns.c | 2 +- src/w32proc.c | 2 +- src/xfns.c | 2 +- src/xfont.c | 2 +- src/xsmfns.c | 2 +- src/xterm.c | 2 +- 14 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 02d7871e884..63d732c8d2c 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,7 +1,24 @@ +2014-09-23 Dmitry Antipov + + Use known length of a Lisp string to copy it faster. + * lisp.h (lispstrcpy): New function. Add comment. + * callproc.c (child_setup): + * dbusbind.c (xd_append_arg): + * doc.c (get_doc_string): + * font.c (Ffont_xlfd_name): + * frame.c (xrdb_get_resource): + * process.c (Fmake_network_process, network_interface_info): + * w32fns.c (Fx_open_connection): + * w32proc.c (sys_spawnve): + * xfns.c (select_visual): + * xfont.c (xfont_list): + * xsmfns.c (x_session_initialize): + * xterm.c (x_term_init): Use it. + 2014-09-23 Paul Eggert Fix SAFE_ALLOCA to not exhaust the stack when in a loop. - Problem reported by Dmietry Antipov in thread leading to: + Problem reported by Dmitry Antipov in thread leading to: http://lists.gnu.org/archive/html/emacs-devel/2014-09/msg00713.html This patch fixes only SAFE_ALLOCA, SAFE_NALLOCA, and SAFE_ALLOCA_LISP; the experimental local_* macros enabled by USE_LOCAL_ALLOCATORS diff --git a/src/callproc.c b/src/callproc.c index 4bedf671e83..2fa475e72df 100644 --- a/src/callproc.c +++ b/src/callproc.c @@ -1235,7 +1235,7 @@ child_setup (int in, int out, int err, char **new_argv, bool set_pgrp, #endif temp = pwd_var + 4; memcpy (pwd_var, "PWD=", 4); - strcpy (temp, SSDATA (current_dir)); + lispstrcpy (temp, current_dir); #ifndef DOS_NT /* We can't signal an Elisp error here; we're in a vfork. Since diff --git a/src/dbusbind.c b/src/dbusbind.c index 8997e01b068..58302df4927 100644 --- a/src/dbusbind.c +++ b/src/dbusbind.c @@ -761,7 +761,7 @@ xd_append_arg (int dtype, Lisp_Object object, DBusMessageIter *iter) && STRINGP (CAR_SAFE (XD_NEXT_VALUE (object))) && NILP (CDR_SAFE (XD_NEXT_VALUE (object)))) { - strcpy (signature, SSDATA (CAR_SAFE (XD_NEXT_VALUE (object)))); + lispstrcpy (signature, CAR_SAFE (XD_NEXT_VALUE (object))); object = CDR_SAFE (XD_NEXT_VALUE (object)); } diff --git a/src/doc.c b/src/doc.c index 98f2f8563a1..da6a9deb977 100644 --- a/src/doc.c +++ b/src/doc.c @@ -121,7 +121,7 @@ get_doc_string (Lisp_Object filepos, bool unibyte, bool definition) if (minsize < 8) minsize = 8; name = SAFE_ALLOCA (minsize + SCHARS (file) + 8); - strcpy (name, SSDATA (docdir)); + lispstrcpy (name, docdir); strcat (name, SSDATA (file)); } else diff --git a/src/font.c b/src/font.c index 83860090820..e8a13b7eeda 100644 --- a/src/font.c +++ b/src/font.c @@ -4266,7 +4266,7 @@ the consecutive wildcards are folded into one. */) { if (NILP (fold_wildcards)) return font_name; - strcpy (name, SSDATA (font_name)); + lispstrcpy (name, font_name); namelen = SBYTES (font_name); goto done; } diff --git a/src/frame.c b/src/frame.c index d56b11d962c..51bd8fa7cfc 100644 --- a/src/frame.c +++ b/src/frame.c @@ -4036,8 +4036,8 @@ xrdb_get_resource (XrmDatabase rdb, Lisp_Object attribute, Lisp_Object class, Li /* Start with emacs.FRAMENAME for the name (the specific one) and with `Emacs' for the class key (the general one). */ - strcpy (name_key, SSDATA (Vx_resource_name)); - strcpy (class_key, SSDATA (Vx_resource_class)); + lispstrcpy (name_key, Vx_resource_name); + lispstrcpy (class_key, Vx_resource_class); strcat (class_key, "."); strcat (class_key, SSDATA (class)); diff --git a/src/lisp.h b/src/lisp.h index 21f652b81ac..6ece4810b0b 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -4462,6 +4462,15 @@ extern void *xpalloc (void *, ptrdiff_t *, ptrdiff_t, ptrdiff_t, ptrdiff_t); extern char *xstrdup (const char *) ATTRIBUTE_MALLOC; extern char *xlispstrdup (Lisp_Object) ATTRIBUTE_MALLOC; extern void dupstring (char **, char const *); + +/* Like strcpy but uses known length of a Lisp string. */ + +INLINE char * +lispstrcpy (const char *dest, Lisp_Object string) +{ + return memcpy ((void *) dest, SSDATA (string), SBYTES (string) + 1); +} + extern void xputenv (const char *); extern char *egetenv_internal (const char *, ptrdiff_t); diff --git a/src/process.c b/src/process.c index 0807939dd25..c6140083784 100644 --- a/src/process.c +++ b/src/process.c @@ -2989,7 +2989,7 @@ usage: (make-network-process &rest ARGS) */) address_un.sun_family = AF_LOCAL; if (sizeof address_un.sun_path <= SBYTES (service)) error ("Service name too long"); - strcpy (address_un.sun_path, SSDATA (service)); + lispstrcpy (address_un.sun_path, service); ai.ai_addr = (struct sockaddr *) &address_un; ai.ai_addrlen = sizeof address_un; goto open_socket; @@ -3680,7 +3680,7 @@ network_interface_info (Lisp_Object ifname) if (sizeof rq.ifr_name <= SBYTES (ifname)) error ("interface name too long"); - strcpy (rq.ifr_name, SSDATA (ifname)); + lispstrcpy (rq.ifr_name, ifname); s = socket (AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); if (s < 0) diff --git a/src/w32fns.c b/src/w32fns.c index fee80d24690..a58a9ced85c 100644 --- a/src/w32fns.c +++ b/src/w32fns.c @@ -5339,7 +5339,7 @@ terminate Emacs if we can't open the connection. { char basename[ MAX_PATH ], *str; - strcpy (basename, SDATA (Vinvocation_name)); + lispstrcpy (basename, Vinvocation_name); str = strrchr (basename, '.'); if (str) *str = 0; Vinvocation_name = build_string (basename); diff --git a/src/w32proc.c b/src/w32proc.c index 96f94a116af..795df31c858 100644 --- a/src/w32proc.c +++ b/src/w32proc.c @@ -1647,7 +1647,7 @@ sys_spawnve (int mode, char *cmdname, char **argv, char **envp) strcpy (cmdname, egetenv ("CMDPROXY")); else { - strcpy (cmdname, SDATA (Vinvocation_directory)); + lispstrcpy (cmdname, Vinvocation_directory); strcat (cmdname, "cmdproxy.exe"); } diff --git a/src/xfns.c b/src/xfns.c index b107f6e688c..7ecd15aea91 100644 --- a/src/xfns.c +++ b/src/xfns.c @@ -4289,7 +4289,7 @@ select_visual (struct x_display_info *dpyinfo) int i, class = -1; XVisualInfo vinfo; - strcpy (s, SSDATA (value)); + lispstrcpy (s, value); dash = strchr (s, '-'); if (dash) { diff --git a/src/xfont.c b/src/xfont.c index 90b69ad5187..db0449716ab 100644 --- a/src/xfont.c +++ b/src/xfont.c @@ -541,7 +541,7 @@ xfont_list (struct frame *f, Lisp_Object spec) if (STRINGP (XCAR (alter)) && ((r - name) + SBYTES (XCAR (alter))) < 256) { - strcpy (r, SSDATA (XCAR (alter))); + lispstrcpy (r, XCAR (alter)); list = xfont_list_pattern (display, name, registry, script); if (! NILP (list)) break; diff --git a/src/xsmfns.c b/src/xsmfns.c index 8721cc81b03..5efbfaafa8f 100644 --- a/src/xsmfns.c +++ b/src/xsmfns.c @@ -418,7 +418,7 @@ x_session_initialize (struct x_display_info *dpyinfo) emacs_program[0] = '\0'; if (! EQ (Vinvocation_directory, Qnil)) - strcpy (emacs_program, SSDATA (Vinvocation_directory)); + lispstrcpy (emacs_program, Vinvocation_directory); strcat (emacs_program, SSDATA (Vinvocation_name)); /* The SM protocol says all callbacks are mandatory, so set up all diff --git a/src/xterm.c b/src/xterm.c index 4b4349d2622..1b721b042be 100644 --- a/src/xterm.c +++ b/src/xterm.c @@ -10902,7 +10902,7 @@ x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name) dpyinfo->x_id = ++x_display_id; dpyinfo->x_id_name = xmalloc (SBYTES (Vinvocation_name) + SBYTES (Vsystem_name) + 2); - strcat (strcat (strcpy (dpyinfo->x_id_name, SSDATA (Vinvocation_name)), "@"), + strcat (strcat (lispstrcpy (dpyinfo->x_id_name, Vinvocation_name), "@"), SSDATA (Vsystem_name)); /* Figure out which modifier bits mean what. */ -- 2.39.5