From b6236be77adaeb7034a0af8d877be8e2f4d4ed23 Mon Sep 17 00:00:00 2001 From: tg Date: Sat, 12 Jul 2008 17:23:00 +0000 Subject: [PATCH] further optimisation attempts in the str_save() and str_nsave() area --- misc.c | 44 +++++++++++++++++++++++++++----------------- sh.h | 25 +++++++++++++++++++++---- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/misc.c b/misc.c index 93c4aee..764920c 100644 --- a/misc.c +++ b/misc.c @@ -6,7 +6,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.80 2008/07/12 16:26:58 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.81 2008/07/12 17:23:00 tg Exp $"); #undef USE_CHVT #if defined(TIOCSCTTY) && !defined(MKSH_SMALL) @@ -63,25 +63,35 @@ initctypes(void) setctypes(" \n\t\"#$&'()*;<>?[]\\`|", C_QUOTE); } -/* Allocate a string of size n+1 and copy upto n characters from the possibly - * NUL terminated string s into it. Always returns a NUL terminated string - * (unless n < 0). - */ -char * -str_nsave(const char *s, int n, Area *ap) -{ - char *ns = NULL; - - if (n >= 0 && s) - strlcpy(ns = alloc(n + 1, ap), s, n + 1); - return (ns); -} - #ifdef MKSH_SMALL char * -str_save(const char *s, Area *ap) +str_save_s(const char *s, Area *ap) { - return (s ? str_nsave(s, strlen(s), ap) : NULL); + char *rv; + size_t sz; + + sz = strlen(s) + 1; + strlcpy(rv = alloc(sz, ap), s, sz); + return (rv); +} + +char * +str_nsave_s(const char *s, int n, Area *ap) +{ + char *rv = NULL; + + if (n >= 0) + strlcpy(rv = alloc(n + 1, ap), s, n + 1); + return (rv); +} +#elif !HAVE_EXPSTMT +char * +str_nsave_ns(const char *s, unsigned int sz, Area *ap) +{ + char *rv; + + strlcpy(rv = alloc(sz, ap), s, sz); + return (rv); } #endif diff --git a/sh.h b/sh.h index db0be15..5da4130 100644 --- a/sh.h +++ b/sh.h @@ -100,7 +100,7 @@ #define __SCCSID(x) __IDSTRING(sccsid,x) #ifdef EXTERN -__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.224 2008/07/10 21:55:08 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.225 2008/07/12 17:23:00 tg Exp $"); #endif #define MKSH_VERSION "R35 2008/07/10" @@ -1423,11 +1423,28 @@ struct tbl **ktsort(struct table *); void setctypes(const char *, int); void initctypes(void); #ifdef MKSH_SMALL -char *str_save(const char *, Area *); +#define str_save(s,ap) ((s) ? str_save_s((s), (ap)) : NULL) +#define str_nsave(s,n,ap) ((s) ? str_nsave_s((s), (n), (ap)) : NULL) +char *str_save_s(const char *, Area *); +char *str_nsave_s(const char *, int, Area *); #else -#define str_save(s,ap) (str_nsave((s), (s) ? strlen(s) : 0, (ap))) +#if HAVE_EXPSTMT +#define str_nsave_ns(s,sz,ap) ({ \ + unsigned int str_nsave_ns_sz = (sz); \ + char *str_nsave_ns_rv = alloc(str_nsave_ns_sz, (ap)); \ + strlcpy(str_nsave_ns_rv, (s), str_nsave_ns_sz); \ + (str_nsave_ns_rv); \ +}) +#else +char *str_nsave_ns(const char *, unsigned int, Area *); +#endif +#define str_save(s,ap) ((s) \ + ? str_nsave_ns((s), strlen(s) + 1, (ap)) \ + : NULL) +#define str_nsave(s,n,ap) (((s) && ((n) >= 0)) \ + ? str_nsave_ns((s), (n) + 1, (ap)) \ + : NULL) #endif -char *str_nsave(const char *, int, Area *); size_t option(const char *); char *getoptions(void); void change_flag(enum sh_flag, int, char);