From a1633e09a761b9135a0a7084d2489d359a004e5a Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Wed, 9 Nov 2016 11:54:57 +0000 Subject: 39886 based on 39877: Optimise arrdup to arrdup_max. Only duplicate as much of the array as is needed. --- Src/utils.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'Src/utils.c') diff --git a/Src/utils.c b/Src/utils.c index 93e757ce8..3d535b85c 100644 --- a/Src/utils.c +++ b/Src/utils.c @@ -4229,6 +4229,31 @@ arrdup(char **s) return y; } +/* Duplicate at most max elements of the array s with heap memory */ + +/**/ +mod_export char ** +arrdup_max(char **s, unsigned max) +{ + char **x, **y, **send; + int len; + + len = arrlen(s); + + /* Limit has sense only if not equal to len */ + if (max > len) + max = len; + + y = x = (char **) zhalloc(sizeof(char *) * (max + 1)); + + send = s + max; + while (s < send) + *x++ = dupstring(*s++); + *x = NULL; + + return y; +} + /**/ mod_export char ** zarrdup(char **s) -- cgit 1.4.1