From 4bc8b69ccf12c40ba0eec577b18e24bdd3cbc945 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 12 Feb 2007 02:58:17 +0000 Subject: [PATCH] Optimize memory allocation to use alloca when possible. --- ChangeLog | 20 ++++++++++++++++++++ lib/c-strcasestr.c | 5 +++-- lib/c-strstr.c | 6 ++++-- lib/mbscasestr.c | 9 +++++---- lib/mbsstr.c | 9 +++++---- lib/strcasestr.c | 6 ++++-- modules/c-strcasestr | 1 + modules/c-strstr | 1 + modules/mbscasestr | 1 + modules/mbsstr | 1 + modules/strcasestr | 1 + 11 files changed, 46 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2bbc6e52e..d8a45a89b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,25 @@ 2007-02-11 Bruno Haible + * lib/c-strstr.c: Include allocsa.h. + (knuth_morris_pratt): Use allocsa/freesa instead of malloc/free. + * lib/c-strcasestr.c: Include allocsa.h. + (knuth_morris_pratt): Use allocsa/freesa instead of malloc/free. + * lib/strcasestr.c: Include allocsa.h. + (knuth_morris_pratt): Use allocsa/freesa instead of malloc/free. + * lib/mbsstr.c: Include allocsa.h. + (knuth_morris_pratt_unibyte, knuth_morris_pratt_multibyte): Use + allocsa/freesa instead of malloc/free. + * lib/mbscasestr.c: Include allocsa.h. + (knuth_morris_pratt_unibyte, knuth_morris_pratt_multibyte): Use + allocsa/freesa instead of malloc/free. + * modules/c-strstr (Depends-on): Add allocsa. + * modules/c-strcasestr (Depends-on): Likewise. + * modules/strcasestr (Depends-on): Likewise. + * modules/mbsstr (Depends-on): Likewise. + * modules/mbscasestr (Depends-on): Likewise. + +2007-02-11 Bruno Haible + * lib/mbsspn.c (mbsspn): Fix bug. Remove unnecessary strlen call. * modules/mbsspn-tests: New file. diff --git a/lib/c-strcasestr.c b/lib/c-strcasestr.c index 6c372e003..9e0cd387f 100644 --- a/lib/c-strcasestr.c +++ b/lib/c-strcasestr.c @@ -25,6 +25,7 @@ #include #include +#include "allocsa.h" #include "c-ctype.h" /* Knuth-Morris-Pratt algorithm. @@ -37,7 +38,7 @@ knuth_morris_pratt (const char *haystack, const char *needle, size_t m = strlen (needle); /* Allocate the table. */ - size_t *table = (size_t *) malloc (m * sizeof (size_t)); + size_t *table = (size_t *) allocsa (m * sizeof (size_t)); if (table == NULL) return false; /* Fill the table. @@ -112,7 +113,7 @@ knuth_morris_pratt (const char *haystack, const char *needle, } } - free (table); + freesa (table); return true; } diff --git a/lib/c-strstr.c b/lib/c-strstr.c index fe38f5321..ce97ee353 100644 --- a/lib/c-strstr.c +++ b/lib/c-strstr.c @@ -25,6 +25,8 @@ #include #include +#include "allocsa.h" + /* Knuth-Morris-Pratt algorithm. See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm Return a boolean indicating success. */ @@ -35,7 +37,7 @@ knuth_morris_pratt (const char *haystack, const char *needle, size_t m = strlen (needle); /* Allocate the table. */ - size_t *table = (size_t *) malloc (m * sizeof (size_t)); + size_t *table = (size_t *) allocsa (m * sizeof (size_t)); if (table == NULL) return false; /* Fill the table. @@ -109,7 +111,7 @@ knuth_morris_pratt (const char *haystack, const char *needle, } } - free (table); + freesa (table); return true; } diff --git a/lib/mbscasestr.c b/lib/mbscasestr.c index 838120cba..ce28342c4 100644 --- a/lib/mbscasestr.c +++ b/lib/mbscasestr.c @@ -25,6 +25,7 @@ #include #include /* for NULL, in case a nonstandard string.h lacks it */ +#include "allocsa.h" #if HAVE_MBRTOWC # include "mbuiter.h" #endif @@ -42,7 +43,7 @@ knuth_morris_pratt_unibyte (const char *haystack, const char *needle, size_t m = strlen (needle); /* Allocate the table. */ - size_t *table = (size_t *) malloc (m * sizeof (size_t)); + size_t *table = (size_t *) allocsa (m * sizeof (size_t)); if (table == NULL) return false; /* Fill the table. @@ -117,7 +118,7 @@ knuth_morris_pratt_unibyte (const char *haystack, const char *needle, } } - free (table); + freesa (table); return true; } @@ -131,7 +132,7 @@ knuth_morris_pratt_multibyte (const char *haystack, const char *needle, size_t *table; /* Allocate room for needle_mbchars and the table. */ - char *memory = (char *) malloc (m * (sizeof (mbchar_t) + sizeof (size_t))); + char *memory = (char *) allocsa (m * (sizeof (mbchar_t) + sizeof (size_t))); if (memory == NULL) return false; needle_mbchars = (mbchar_t *) memory; @@ -237,7 +238,7 @@ knuth_morris_pratt_multibyte (const char *haystack, const char *needle, } } - free (memory); + freesa (memory); return true; } #endif diff --git a/lib/mbsstr.c b/lib/mbsstr.c index f28311815..1876e6e78 100644 --- a/lib/mbsstr.c +++ b/lib/mbsstr.c @@ -24,6 +24,7 @@ #include #include /* for NULL, in case a nonstandard string.h lacks it */ +#include "allocsa.h" #if HAVE_MBRTOWC # include "mbuiter.h" #endif @@ -39,7 +40,7 @@ knuth_morris_pratt_unibyte (const char *haystack, const char *needle, size_t m = strlen (needle); /* Allocate the table. */ - size_t *table = (size_t *) malloc (m * sizeof (size_t)); + size_t *table = (size_t *) allocsa (m * sizeof (size_t)); if (table == NULL) return false; /* Fill the table. @@ -113,7 +114,7 @@ knuth_morris_pratt_unibyte (const char *haystack, const char *needle, } } - free (table); + freesa (table); return true; } @@ -127,7 +128,7 @@ knuth_morris_pratt_multibyte (const char *haystack, const char *needle, size_t *table; /* Allocate room for needle_mbchars and the table. */ - char *memory = (char *) malloc (m * (sizeof (mbchar_t) + sizeof (size_t))); + char *memory = (char *) allocsa (m * (sizeof (mbchar_t) + sizeof (size_t))); if (memory == NULL) return false; needle_mbchars = (mbchar_t *) memory; @@ -222,7 +223,7 @@ knuth_morris_pratt_multibyte (const char *haystack, const char *needle, } } - free (memory); + freesa (memory); return true; } #endif diff --git a/lib/strcasestr.c b/lib/strcasestr.c index 24610f7f3..f70deeeb7 100644 --- a/lib/strcasestr.c +++ b/lib/strcasestr.c @@ -25,6 +25,8 @@ #include #include /* for NULL, in case a nonstandard string.h lacks it */ +#include "allocsa.h" + #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch)) /* Knuth-Morris-Pratt algorithm. @@ -37,7 +39,7 @@ knuth_morris_pratt (const char *haystack, const char *needle, size_t m = strlen (needle); /* Allocate the table. */ - size_t *table = (size_t *) malloc (m * sizeof (size_t)); + size_t *table = (size_t *) allocsa (m * sizeof (size_t)); if (table == NULL) return false; /* Fill the table. @@ -112,7 +114,7 @@ knuth_morris_pratt (const char *haystack, const char *needle, } } - free (table); + freesa (table); return true; } diff --git a/modules/c-strcasestr b/modules/c-strcasestr index 93b8e89c1..87d02dd55 100644 --- a/modules/c-strcasestr +++ b/modules/c-strcasestr @@ -8,6 +8,7 @@ lib/c-strcasestr.c Depends-on: c-ctype stdbool +allocsa strnlen configure.ac: diff --git a/modules/c-strstr b/modules/c-strstr index 703d879a2..2803aac95 100644 --- a/modules/c-strstr +++ b/modules/c-strstr @@ -7,6 +7,7 @@ lib/c-strstr.c Depends-on: stdbool +allocsa strnlen configure.ac: diff --git a/modules/mbscasestr b/modules/mbscasestr index 5a6bda95b..fe9b019f1 100644 --- a/modules/mbscasestr +++ b/modules/mbscasestr @@ -11,6 +11,7 @@ mbuiter stdbool string mbslen +allocsa strnlen configure.ac: diff --git a/modules/mbsstr b/modules/mbsstr index dcc7b161c..750f2042e 100644 --- a/modules/mbsstr +++ b/modules/mbsstr @@ -11,6 +11,7 @@ mbuiter stdbool string mbslen +allocsa strnlen configure.ac: diff --git a/modules/strcasestr b/modules/strcasestr index 9f9553820..35c1017e2 100644 --- a/modules/strcasestr +++ b/modules/strcasestr @@ -8,6 +8,7 @@ m4/strcasestr.m4 Depends-on: string stdbool +allocsa strnlen configure.ac: -- 2.11.0