]> granicus.if.org Git - procps-ng/commitdiff
top: trade that 'strncpy' for more efficient 'memccpy'
authorJim Warner <james.warner@comcast.net>
Mon, 14 Feb 2022 06:00:00 +0000 (00:00 -0600)
committerCraig Small <csmall@dropbear.xyz>
Thu, 17 Feb 2022 07:23:30 +0000 (18:23 +1100)
This patch was prompted by the merge request for pgrep
referenced below. In top's case, any performance gains
will be minimal since the now defunct strncpy was only
employed for termcap rebuilds after interacting with a
user (+ 1 other non-termcap related user interaction).

[ golly, strncpy always calls at least two functions ]
[ but usually calls a total of 3. on the other hand, ]
[ memccpy will only call a maximum of two functions. ]

And thanks to Baruch Siach for these strncpy insights.

Reference(s):
https://gitlab.com/procps-ng/procps/-/merge_requests/148

Signed-off-by: Jim Warner <james.warner@comcast.net>
top/top.h

index 28ae231063024d8294a54f584fdcb216e46540b9..3e67e49347a2dea5e2553662cfb3d38b53bcd2b1 100644 (file)
--- a/top/top.h
+++ b/top/top.h
@@ -435,7 +435,7 @@ typedef struct WIN_t {
 
         /* A null-terminating strncpy, assuming strlcpy is not available.
            ( and assuming callers don't need the string length returned ) */
-#define STRLCPY(dst,src) { strncpy(dst, src, sizeof(dst)); dst[sizeof(dst) - 1] = '\0'; }
+#define STRLCPY(dst,src) { memccpy(dst, src, '\0', sizeof(dst)); dst[sizeof(dst) - 1] = '\0'; }
 
         /* Used to clear all or part of our Pseudo_screen */
 #define PSU_CLREOS(y) memset(&Pseudo_screen[ROWMAXSIZ*y], '\0', Pseudo_size-(ROWMAXSIZ*y))