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 2bbc6e52e8fc133ab66ad046b2f7ac5209392823..d8a45a89bae4b3d49163e4c1ec15ba2335422ce6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+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.
 2007-02-11  Bruno Haible  <bruno@clisp.org>
 
        * lib/mbsspn.c (mbsspn): Fix bug. Remove unnecessary strlen call.
index 6c372e003171f41516fe1b7fd13adbda78623148..9e0cd387f2ee86475d5565c8b35bbd05ca8797e0 100644 (file)
@@ -25,6 +25,7 @@
 #include <stddef.h>
 #include <string.h>
 
 #include <stddef.h>
 #include <string.h>
 
+#include "allocsa.h"
 #include "c-ctype.h"
 
 /* Knuth-Morris-Pratt algorithm.
 #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 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.
   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;
 }
 
   return true;
 }
 
index fe38f5321955150abbd597d85951179b3c1b9878..ce97ee353a9f3624413db8e2fb8e83df4e8ad523 100644 (file)
@@ -25,6 +25,8 @@
 #include <stdlib.h>
 #include <string.h>
 
 #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.  */
 /* 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 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.
   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;
 }
 
   return true;
 }
 
index 838120cbad0c7561d8bab1826a2590f6d46ee199..ce28342c4978651ab0dfd8f1ff623104916e0dab 100644 (file)
@@ -25,6 +25,7 @@
 #include <stdbool.h>
 #include <stddef.h>  /* for NULL, in case a nonstandard string.h lacks it */
 
 #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
 #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 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.
   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;
 }
 
   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.  */
   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;
   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
   return true;
 }
 #endif
index f28311815638a8335cea3e4bbd2992ca635355a2..1876e6e78429b4e3642536375842ea1c45c06712 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdbool.h>
 #include <stddef.h>  /* for NULL, in case a nonstandard string.h lacks it */
 
 #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
 #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 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.
   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;
 }
 
   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.  */
   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;
   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
   return true;
 }
 #endif
index 24610f7f3d5466e6c932ac80959282f6c30114cb..f70deeeb7ce93929a60e5c46c4e7ebf34ca8058d 100644 (file)
@@ -25,6 +25,8 @@
 #include <stdbool.h>
 #include <stddef.h>  /* for NULL, in case a nonstandard string.h lacks it */
 
 #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.
 #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 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.
   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;
 }
 
   return true;
 }
 
index 93b8e89c1aa15bc306e596b43da2a60976612b28..87d02dd55f96e6a472c86f130dbbd1bb0286b618 100644 (file)
@@ -8,6 +8,7 @@ lib/c-strcasestr.c
 Depends-on:
 c-ctype
 stdbool
 Depends-on:
 c-ctype
 stdbool
+allocsa
 strnlen
 
 configure.ac:
 strnlen
 
 configure.ac:
index 703d879a210ba33622311f0e4b9f2105182811c4..2803aac95fc579c87fc04c9c1d0193715fb633b6 100644 (file)
@@ -7,6 +7,7 @@ lib/c-strstr.c
 
 Depends-on:
 stdbool
 
 Depends-on:
 stdbool
+allocsa
 strnlen
 
 configure.ac:
 strnlen
 
 configure.ac:
index 5a6bda95bd125f0af3b447ac00283d846a5d60f3..fe9b019f14363e290e170ca9684e19866b42341a 100644 (file)
@@ -11,6 +11,7 @@ mbuiter
 stdbool
 string
 mbslen
 stdbool
 string
 mbslen
+allocsa
 strnlen
 
 configure.ac:
 strnlen
 
 configure.ac:
index dcc7b161c5250e86113592ab7e8f484ab5634c76..750f2042e488067e5298d9665d6518f41a3dfa7a 100644 (file)
@@ -11,6 +11,7 @@ mbuiter
 stdbool
 string
 mbslen
 stdbool
 string
 mbslen
+allocsa
 strnlen
 
 configure.ac:
 strnlen
 
 configure.ac:
index 9f95538209f54fb5c24eb6a7b5169e52a0b49e30..35c1017e220fc457210a58ee760057d250cc6a01 100644 (file)
@@ -8,6 +8,7 @@ m4/strcasestr.m4
 Depends-on:
 string
 stdbool
 Depends-on:
 string
 stdbool
+allocsa
 strnlen
 
 configure.ac:
 strnlen
 
 configure.ac: