lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject [lucy-commits] [12/16] git commit: refs/heads/master - Consolidate static vars, reduce mallocs.
Date Sat, 03 Nov 2012 18:21:54 GMT
Consolidate static vars, reduce mallocs.

*   Remove some static vars.
*   Reduce the scope of some static vars.
*   Dial down the paranoia on memory allocation a little: rather than
    malloc, use fixed size buffers (and throw errors if necessary) on
    the assumption that it is not necessary to support arbitrarily long
    names of standard library routines or header files.


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/0998a382
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/0998a382
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/0998a382

Branch: refs/heads/master
Commit: 0998a382abf6a9009a9482fd5cc4ea316a4ec5ae
Parents: 31cc8ed
Author: Marvin Humphrey <marvin@rectangular.com>
Authored: Sat Nov 3 11:19:18 2012 -0700
Committer: Marvin Humphrey <marvin@rectangular.com>
Committed: Sat Nov 3 11:19:18 2012 -0700

----------------------------------------------------------------------
 charmonizer/src/Charmonizer/Core/HeaderChecker.c |   14 ++--
 charmonizer/src/Charmonizer/Probe/AtomicOps.c    |   36 ++++----
 charmonizer/src/Charmonizer/Probe/DirManip.c     |   67 +++++++-------
 charmonizer/src/Charmonizer/Probe/FuncMacro.c    |   19 ++--
 charmonizer/src/Charmonizer/Probe/Headers.c      |   28 +++----
 charmonizer/src/Charmonizer/Probe/LargeFiles.c   |   81 ++++++-----------
 6 files changed, 106 insertions(+), 139 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/0998a382/charmonizer/src/Charmonizer/Core/HeaderChecker.c
----------------------------------------------------------------------
diff --git a/charmonizer/src/Charmonizer/Core/HeaderChecker.c b/charmonizer/src/Charmonizer/Core/HeaderChecker.c
index 832e454..148943b 100644
--- a/charmonizer/src/Charmonizer/Core/HeaderChecker.c
+++ b/charmonizer/src/Charmonizer/Core/HeaderChecker.c
@@ -26,9 +26,6 @@ typedef struct Header {
     int  exists;
 } Header;
 
-/* "hello_world.c" without the hello or the world. */
-static const char test_code[] = "int main() { return 0; }\n";
-
 /* Keep a sorted, dynamically-sized array of names of all headers we've
  * checked for so far.
  */
@@ -96,6 +93,7 @@ chaz_HeadCheck_check_header(const char *header_name) {
 
 int
 chaz_HeadCheck_check_many_headers(const char **header_names) {
+    static const char test_code[] = "int main() { return 0; }\n";
     int success;
     int i;
     char *code_buf = chaz_Util_strdup("");
@@ -127,14 +125,13 @@ chaz_HeadCheck_check_many_headers(const char **header_names) {
     return success;
 }
 
-static const char contains_code[] =
-    CHAZ_QUOTE(  #include <stddef.h>                           )
-    CHAZ_QUOTE(  %s                                            )
-    CHAZ_QUOTE(  int main() { return offsetof(%s, %s); }       );
-
 int
 chaz_HeadCheck_contains_member(const char *struct_name, const char *member,
                                const char *includes) {
+    static const char contains_code[] =
+        CHAZ_QUOTE(  #include <stddef.h>                           )
+        CHAZ_QUOTE(  %s                                            )
+        CHAZ_QUOTE(  int main() { return offsetof(%s, %s); }       );
     long needed = sizeof(contains_code)
                   + strlen(struct_name)
                   + strlen(member)
@@ -161,6 +158,7 @@ S_compare_headers(const void *vptr_a, const void *vptr_b) {
 
 static Header*
 S_discover_header(const char *header_name) {
+    static const char test_code[] = "int main() { return 0; }\n";
     Header* header = (Header*)malloc(sizeof(Header));
     size_t  needed = strlen(header_name) + sizeof(test_code) + 50;
     char *include_test = (char*)malloc(needed);

http://git-wip-us.apache.org/repos/asf/lucy/blob/0998a382/charmonizer/src/Charmonizer/Probe/AtomicOps.c
----------------------------------------------------------------------
diff --git a/charmonizer/src/Charmonizer/Probe/AtomicOps.c b/charmonizer/src/Charmonizer/Probe/AtomicOps.c
index 79d776e..49c974a 100644
--- a/charmonizer/src/Charmonizer/Probe/AtomicOps.c
+++ b/charmonizer/src/Charmonizer/Probe/AtomicOps.c
@@ -23,45 +23,41 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-static const char osatomic_casptr_code[] =
-    CHAZ_QUOTE(  #include <libkern/OSAtomic.h>                                  )
-    CHAZ_QUOTE(  #include <libkern/OSAtomic.h>                                  )
-    CHAZ_QUOTE(  int main() {                                                   )
-    CHAZ_QUOTE(      int  foo = 1;                                              )
-    CHAZ_QUOTE(      int *foo_ptr = &foo;                                       )
-    CHAZ_QUOTE(      int *target = NULL;                                        )
-    CHAZ_QUOTE(      OSAtomicCompareAndSwapPtr(NULL, foo_ptr, (void**)&target); )
-    CHAZ_QUOTE(      return 0;                                                  )
-    CHAZ_QUOTE(  }                                                              );
+
+static int
+chaz_AtomicOps_osatomic_cas_ptr(void) {
+    static const char osatomic_casptr_code[] =
+        CHAZ_QUOTE(  #include <libkern/OSAtomic.h>                                
 )
+        CHAZ_QUOTE(  #include <libkern/OSAtomic.h>                                
 )
+        CHAZ_QUOTE(  int main() {                                                   )
+        CHAZ_QUOTE(      int  foo = 1;                                              )
+        CHAZ_QUOTE(      int *foo_ptr = &foo;                                       )
+        CHAZ_QUOTE(      int *target = NULL;                                        )
+        CHAZ_QUOTE(      OSAtomicCompareAndSwapPtr(NULL, foo_ptr, (void**)&target); )
+        CHAZ_QUOTE(      return 0;                                                  )
+        CHAZ_QUOTE(  }                                                              );
+     return chaz_CC_test_compile(osatomic_casptr_code);
+}
 
 void
 chaz_AtomicOps_run(void) {
-    int has_libkern_osatomic_h = false;
-    int has_osatomic_cas_ptr   = false;
-    int has_sys_atomic_h       = false;
-    int has_intrin_h           = false;
-
     chaz_ConfWriter_start_module("AtomicOps");
 
     if (chaz_HeadCheck_check_header("libkern/OSAtomic.h")) {
-        has_libkern_osatomic_h = true;
         chaz_ConfWriter_add_def("HAS_LIBKERN_OSATOMIC_H", NULL);
 
         /* Check for OSAtomicCompareAndSwapPtr, introduced in later versions
          * of OSAtomic.h. */
-        has_osatomic_cas_ptr = chaz_CC_test_compile(osatomic_casptr_code);
-        if (has_osatomic_cas_ptr) {
+        if (chaz_AtomicOps_osatomic_cas_ptr()) {
             chaz_ConfWriter_add_def("HAS_OSATOMIC_CAS_PTR", NULL);
         }
     }
     if (chaz_HeadCheck_check_header("sys/atomic.h")) {
-        has_sys_atomic_h = true;
         chaz_ConfWriter_add_def("HAS_SYS_ATOMIC_H", NULL);
     }
     if (chaz_HeadCheck_check_header("windows.h")
         && chaz_HeadCheck_check_header("intrin.h")
        ) {
-        has_intrin_h = true;
         chaz_ConfWriter_add_def("HAS_INTRIN_H", NULL);
     }
 

http://git-wip-us.apache.org/repos/asf/lucy/blob/0998a382/charmonizer/src/Charmonizer/Probe/DirManip.c
----------------------------------------------------------------------
diff --git a/charmonizer/src/Charmonizer/Probe/DirManip.c b/charmonizer/src/Charmonizer/Probe/DirManip.c
index c416e2d..abc3fa7 100644
--- a/charmonizer/src/Charmonizer/Probe/DirManip.c
+++ b/charmonizer/src/Charmonizer/Probe/DirManip.c
@@ -25,42 +25,24 @@
 #include <stdlib.h>
 
 static int   mkdir_num_args  = 0;
-static int   mkdir_available = 0;
 static char  mkdir_command_buf[7];
 static char *mkdir_command = mkdir_command_buf;
-static int   rmdir_available = 0;
-
-/* Source code for standard POSIX mkdir */
-static const char posix_mkdir_code[] =
-    CHAZ_QUOTE(  #include <%s>                                          )
-    CHAZ_QUOTE(  int main(int argc, char **argv) {                      )
-    CHAZ_QUOTE(      if (argc != 2) { return 1; }                       )
-    CHAZ_QUOTE(      if (mkdir(argv[1], 0777) != 0) { return 2; }       )
-    CHAZ_QUOTE(      return 0;                                          )
-    CHAZ_QUOTE(  }                                                      );
-
-/* Source code for Windows _mkdir. */
-static const char win_mkdir_code[] =
-    CHAZ_QUOTE(  #include <direct.h>                                    )
-    CHAZ_QUOTE(  int main(int argc, char **argv) {                      )
-    CHAZ_QUOTE(      if (argc != 2) { return 1; }                       )
-    CHAZ_QUOTE(      if (_mkdir(argv[1]) != 0) { return 2; }            )
-    CHAZ_QUOTE(      return 0;                                          )
-    CHAZ_QUOTE(  }                                                      );
 
 /* Source code for rmdir. */
-static const char rmdir_code[] =
-    CHAZ_QUOTE(  #include <%s>                                          )
-    CHAZ_QUOTE(  int main(int argc, char **argv) {                      )
-    CHAZ_QUOTE(      if (argc != 2) { return 1; }                       )
-    CHAZ_QUOTE(      if (rmdir(argv[1]) != 0) { return 2; }             )
-    CHAZ_QUOTE(      return 0;                                          )
-    CHAZ_QUOTE(  }                                                      );
-
 static int
 S_compile_posix_mkdir(const char *header) {
-    size_t needed = sizeof(posix_mkdir_code) + 30;
-    char *code_buf = (char*)malloc(needed);
+    static const char posix_mkdir_code[] =
+        CHAZ_QUOTE(  #include <%s>                                      )
+        CHAZ_QUOTE(  int main(int argc, char **argv) {                  )
+        CHAZ_QUOTE(      if (argc != 2) { return 1; }                   )
+        CHAZ_QUOTE(      if (mkdir(argv[1], 0777) != 0) { return 2; }   )
+        CHAZ_QUOTE(      return 0;                                      )
+        CHAZ_QUOTE(  }                                                  );
+    char code_buf[sizeof(posix_mkdir_code) + 30];
+    int mkdir_available;
+    if (strlen(header) > 25) {
+        chaz_Util_die("Header name too long: '%s'", header);
+    }
 
     /* Attempt compilation. */
     sprintf(code_buf, posix_mkdir_code, header);
@@ -77,12 +59,20 @@ S_compile_posix_mkdir(const char *header) {
         }
     }
 
-    free(code_buf);
     return mkdir_available;
 }
 
 static int
 S_compile_win_mkdir(void) {
+    static const char win_mkdir_code[] =
+        CHAZ_QUOTE(  #include <direct.h>                                )
+        CHAZ_QUOTE(  int main(int argc, char **argv) {                  )
+        CHAZ_QUOTE(      if (argc != 2) { return 1; }                   )
+        CHAZ_QUOTE(      if (_mkdir(argv[1]) != 0) { return 2; }        )
+        CHAZ_QUOTE(      return 0;                                      )
+        CHAZ_QUOTE(  }                                                  );
+    int mkdir_available;
+
     mkdir_available = chaz_CC_test_compile(win_mkdir_code);
     if (mkdir_available) {
         strcpy(mkdir_command, "_mkdir");
@@ -102,11 +92,20 @@ S_try_mkdir(void) {
 
 static int
 S_compile_rmdir(const char *header) {
-    size_t needed = sizeof(posix_mkdir_code) + 30;
-    char *code_buf = (char*)malloc(needed);
+    static const char rmdir_code[] =
+        CHAZ_QUOTE(  #include <%s>                                      )
+        CHAZ_QUOTE(  int main(int argc, char **argv) {                  )
+        CHAZ_QUOTE(      if (argc != 2) { return 1; }                   )
+        CHAZ_QUOTE(      if (rmdir(argv[1]) != 0) { return 2; }         )
+        CHAZ_QUOTE(      return 0;                                      )
+        CHAZ_QUOTE(  }                                                  );
+    char code_buf[sizeof(rmdir_code) + 30];
+    int rmdir_available;
+    if (strlen(header) > 25) {
+        chaz_Util_die("Header name too long: '%s'", header);
+    }
     sprintf(code_buf, rmdir_code, header);
     rmdir_available = chaz_CC_test_compile(code_buf);
-    free(code_buf);
     return rmdir_available;
 }
 

http://git-wip-us.apache.org/repos/asf/lucy/blob/0998a382/charmonizer/src/Charmonizer/Probe/FuncMacro.c
----------------------------------------------------------------------
diff --git a/charmonizer/src/Charmonizer/Probe/FuncMacro.c b/charmonizer/src/Charmonizer/Probe/FuncMacro.c
index 9606fd0..ac1d16c 100644
--- a/charmonizer/src/Charmonizer/Probe/FuncMacro.c
+++ b/charmonizer/src/Charmonizer/Probe/FuncMacro.c
@@ -40,18 +40,17 @@ static const char gnuc_func_code[] =
     CHAZ_QUOTE(      return 0;                     )
     CHAZ_QUOTE(  }                                 );
 
-/* Code for verifying inline keyword. */
-static const char inline_code[] =
-    CHAZ_QUOTE(  #include "_charm.h"               )
-    CHAZ_QUOTE(  static %s int foo() { return 1; } )
-    CHAZ_QUOTE(  int main() {                      )
-    CHAZ_QUOTE(      Charm_Setup;                  )
-    CHAZ_QUOTE(      printf("%%d", foo());         )
-    CHAZ_QUOTE(      return 0;                     )
-    CHAZ_QUOTE(  }                                 );
-
+/* Attempt to verify inline keyword. */
 static char*
 S_try_inline(const char *keyword, size_t *output_len) {
+    static const char inline_code[] =
+        CHAZ_QUOTE(  #include "_charm.h"               )
+        CHAZ_QUOTE(  static %s int foo() { return 1; } )
+        CHAZ_QUOTE(  int main() {                      )
+        CHAZ_QUOTE(      Charm_Setup;                  )
+        CHAZ_QUOTE(      printf("%%d", foo());         )
+        CHAZ_QUOTE(      return 0;                     )
+        CHAZ_QUOTE(  }                                 );
     char code[sizeof(inline_code) + 30];
     sprintf(code, inline_code, keyword);
     return chaz_CC_capture_output(code, output_len);

http://git-wip-us.apache.org/repos/asf/lucy/blob/0998a382/charmonizer/src/Charmonizer/Probe/Headers.c
----------------------------------------------------------------------
diff --git a/charmonizer/src/Charmonizer/Probe/Headers.c b/charmonizer/src/Charmonizer/Probe/Headers.c
index 2a422df..307956c 100644
--- a/charmonizer/src/Charmonizer/Probe/Headers.c
+++ b/charmonizer/src/Charmonizer/Probe/Headers.c
@@ -34,14 +34,11 @@ static const char *keepers[MAX_KEEPER_COUNT + 1] = { NULL };
 static void
 S_keep(const char *header_name);
 
-static size_t aff_buf_size = 0;
-static char *aff_buf = NULL;
-
-/* Transform "header.h" into "CHY_HAS_HEADER_H, storing the result in
- * [aff_buf].
+/* Transform "header.h" into "CHY_HAS_HEADER_H, storing the result into
+ * `buffer`.
  */
 static void
-S_encode_affirmation(const char *header_name);
+S_encode_affirmation(const char *header_name, char *buffer, size_t buf_size);
 
 #define NUM_C89_HEADERS 15
 const char *c89_headers[] = {
@@ -162,7 +159,8 @@ chaz_Headers_run(void) {
 
     /* Append the config with every header detected so far. */
     for (i = 0; keepers[i] != NULL; i++) {
-        S_encode_affirmation(keepers[i]);
+        char aff_buf[200];
+        S_encode_affirmation(keepers[i], aff_buf, 200);
         chaz_ConfWriter_add_def(aff_buf, NULL);
     }
 
@@ -179,20 +177,18 @@ S_keep(const char *header_name) {
 }
 
 static void
-S_encode_affirmation(const char *header_name) {
+S_encode_affirmation(const char *header_name, char *buffer, size_t buf_size) {
     char *buf, *buf_end;
     size_t len = strlen(header_name) + sizeof("HAS_");
-
-    /* Grow buffer and start off with "HAS_". */
-    if (aff_buf_size < len + 1) {
-        free(aff_buf);
-        aff_buf_size = len + 1;
-        aff_buf = (char*)malloc(aff_buf_size);
+    if (len + 1 > buf_size) {
+        chaz_Util_die("Buffer too small: %lu", (unsigned long)buf_size);
     }
-    strcpy(aff_buf, "HAS_");
+
+    /* Start off with "HAS_". */
+    strcpy(buffer, "HAS_");
 
     /* Transform one char at a time. */
-    for (buf = aff_buf + sizeof("HAS_") - 1, buf_end = aff_buf + len;
+    for (buf = buffer + sizeof("HAS_") - 1, buf_end = buffer + len;
          buf < buf_end;
          header_name++, buf++
         ) {

http://git-wip-us.apache.org/repos/asf/lucy/blob/0998a382/charmonizer/src/Charmonizer/Probe/LargeFiles.c
----------------------------------------------------------------------
diff --git a/charmonizer/src/Charmonizer/Probe/LargeFiles.c b/charmonizer/src/Charmonizer/Probe/LargeFiles.c
index 941e458..03559d1 100644
--- a/charmonizer/src/Charmonizer/Probe/LargeFiles.c
+++ b/charmonizer/src/Charmonizer/Probe/LargeFiles.c
@@ -182,8 +182,7 @@ static const char off64_code[] =
 
 static const int
 S_probe_off64(void) {
-    size_t needed = sizeof(off64_code) + 100;
-    char *code_buf = (char*)malloc(needed);
+    char code_buf[sizeof(off64_code) + 100];
     int i;
     int success = false;
     for (i = 0; i < NUM_OFF64_OPTIONS; i++) {
@@ -208,7 +207,6 @@ S_probe_off64(void) {
             }
         }
     }
-    free(code_buf);
     return success;
 }
 
@@ -233,13 +231,7 @@ static int
 S_probe_stdio64(stdio64_combo *combo) {
     char *output = NULL;
     size_t output_len;
-    size_t needed = sizeof(stdio64_code)
-                    + (2 * strlen(off64_type))
-                    + strlen(combo->fopen_command)
-                    + strlen(combo->ftell_command)
-                    + strlen(combo->fseek_command)
-                    + 20;
-    char *code_buf = (char*)malloc(needed);
+    char code_buf[sizeof(stdio64_code) + 200];
     int success = false;
 
     /* Prepare the source code. */
@@ -264,30 +256,24 @@ S_probe_stdio64(stdio64_combo *combo) {
     return success;
 }
 
-/* Code for checking 64-bit lseek. */
-static const char lseek_code[] =
-    CHAZ_QUOTE(  %s                                                        )
-    CHAZ_QUOTE(  #include "_charm.h"                                       )
-    CHAZ_QUOTE(  int main() {                                              )
-    CHAZ_QUOTE(      int fd;                                               )
-    CHAZ_QUOTE(      Charm_Setup;                                          )
-    CHAZ_QUOTE(      fd = open("_charm_lseek", O_WRONLY | O_CREAT, 0666);  )
-    CHAZ_QUOTE(      if (fd == -1) { return -1; }                          )
-    CHAZ_QUOTE(      %s(fd, 0, SEEK_SET);                                  )
-    CHAZ_QUOTE(      printf("%%d", 1);                                     )
-    CHAZ_QUOTE(      if (close(fd)) { return -1; }                         )
-    CHAZ_QUOTE(      return 0;                                             )
-    CHAZ_QUOTE(  }                                                         );
-
 static int
 S_probe_lseek(unbuff_combo *combo) {
+    static const char lseek_code[] =
+        CHAZ_QUOTE( %s                                                       )
+        CHAZ_QUOTE( #include "_charm.h"                                      )
+        CHAZ_QUOTE( int main() {                                             )
+        CHAZ_QUOTE(     int fd;                                              )
+        CHAZ_QUOTE(     Charm_Setup;                                         )
+        CHAZ_QUOTE(     fd = open("_charm_lseek", O_WRONLY | O_CREAT, 0666); )
+        CHAZ_QUOTE(     if (fd == -1) { return -1; }                         )
+        CHAZ_QUOTE(     %s(fd, 0, SEEK_SET);                                 )
+        CHAZ_QUOTE(     printf("%%d", 1);                                    )
+        CHAZ_QUOTE(     if (close(fd)) { return -1; }                        )
+        CHAZ_QUOTE(     return 0;                                            )
+        CHAZ_QUOTE( }                                                        );
+    char code_buf[sizeof(lseek_code) + 100];
     char *output = NULL;
     size_t output_len;
-    size_t needed = sizeof(lseek_code)
-                    + strlen(combo->includes)
-                    + strlen(combo->lseek_command)
-                    + 20;
-    char *code_buf = (char*)malloc(needed);
     int success = false;
 
     /* Verify compilation. */
@@ -302,33 +288,27 @@ S_probe_lseek(unbuff_combo *combo) {
         chaz_Util_die("Failed to remove '_charm_lseek'");
     }
 
-    free(code_buf);
     return success;
 }
 
-/* Code for checking 64-bit pread.  The pread call will fail, but that's fine
- * as long as it compiles. */
-static const char pread64_code[] =
-    CHAZ_QUOTE(  %s                                     )
-    CHAZ_QUOTE(  #include "_charm.h"                    )
-    CHAZ_QUOTE(  int main() {                           )
-    CHAZ_QUOTE(      int fd = 20;                       )
-    CHAZ_QUOTE(      char buf[1];                       )
-    CHAZ_QUOTE(      Charm_Setup;                       )
-    CHAZ_QUOTE(      printf("1");                       )
-    CHAZ_QUOTE(      %s(fd, buf, 1, 1);                 )
-    CHAZ_QUOTE(      return 0;                          )
-    CHAZ_QUOTE(  }                                      );
-
 static int
 S_probe_pread64(unbuff_combo *combo) {
+    /* Code for checking 64-bit pread.  The pread call will fail, but that's
+     * fine as long as it compiles. */
+    static const char pread64_code[] =
+        CHAZ_QUOTE(  %s                                     )
+        CHAZ_QUOTE(  #include "_charm.h"                    )
+        CHAZ_QUOTE(  int main() {                           )
+        CHAZ_QUOTE(      int fd = 20;                       )
+        CHAZ_QUOTE(      char buf[1];                       )
+        CHAZ_QUOTE(      Charm_Setup;                       )
+        CHAZ_QUOTE(      printf("1");                       )
+        CHAZ_QUOTE(      %s(fd, buf, 1, 1);                 )
+        CHAZ_QUOTE(      return 0;                          )
+        CHAZ_QUOTE(  }                                      );
+    char code_buf[sizeof(pread64_code) + 100];
     char *output = NULL;
     size_t output_len;
-    size_t needed = sizeof(pread64_code)
-                    + strlen(combo->includes)
-                    + strlen(combo->pread64_command)
-                    + 20;
-    char *code_buf = (char*)malloc(needed);
     int success = false;
 
     /* Verify compilation. */
@@ -339,7 +319,6 @@ S_probe_pread64(unbuff_combo *combo) {
         free(output);
     }
 
-    free(code_buf);
     return success;
 }
 


Mime
View raw message