Optimize memory allocation to use alloca when possible.
authorBruno Haible <bruno@clisp.org>
Mon, 12 Feb 2007 02:58:17 +0000 (02:58 +0000)
committerBruno Haible <bruno@clisp.org>
Mon, 12 Feb 2007 02:58:17 +0000 (02:58 +0000)
ChangeLog
lib/c-strcasestr.c
lib/c-strstr.c
lib/mbscasestr.c
lib/mbsstr.c
lib/strcasestr.c
modules/c-strcasestr
modules/c-strstr
modules/mbscasestr
modules/mbsstr
modules/strcasestr

index 2bbc6e5..d8a45a8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,25 @@
 2007-02-11  Bruno Haible  <bruno@clisp.org>
 
+       * 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  <bruno@clisp.org>
+
        * lib/mbsspn.c (mbsspn): Fix bug. Remove unnecessary strlen call.
 
        * modules/mbsspn-tests: New file.
index 6c372e0..9e0cd38 100644 (file)
@@ -25,6 +25,7 @@
 #include <stddef.h>
 #include <string.h>
 
+#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;
 }
 
index fe38f53..ce97ee3 100644 (file)
@@ -25,6 +25,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#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;
 }
 
index 838120c..ce28342 100644 (file)
@@ -25,6 +25,7 @@
 #include <stdbool.h>
 #include <stddef.h>  /* 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
index f283118..1876e6e 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdbool.h>
 #include <stddef.h>  /* 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
index 24610f7..f70deee 100644 (file)
@@ -25,6 +25,8 @@
 #include <stdbool.h>
 #include <stddef.h>  /* 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;
 }
 
index 93b8e89..87d02dd 100644 (file)
@@ -8,6 +8,7 @@ lib/c-strcasestr.c
 Depends-on:
 c-ctype
 stdbool
+allocsa
 strnlen
 
 configure.ac:
index 703d879..2803aac 100644 (file)
@@ -7,6 +7,7 @@ lib/c-strstr.c
 
 Depends-on:
 stdbool
+allocsa
 strnlen
 
 configure.ac:
index 5a6bda9..fe9b019 100644 (file)
@@ -11,6 +11,7 @@ mbuiter
 stdbool
 string
 mbslen
+allocsa
 strnlen
 
 configure.ac:
index dcc7b16..750f204 100644 (file)
@@ -11,6 +11,7 @@ mbuiter
 stdbool
 string
 mbslen
+allocsa
 strnlen
 
 configure.ac:
index 9f95538..35c1017 100644 (file)
@@ -8,6 +8,7 @@ m4/strcasestr.m4
 Depends-on:
 string
 stdbool
+allocsa
 strnlen
 
 configure.ac: