]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Introduce and use x_fmmap() and x_munmap()
authorRamiro Polla <ramiro.polla@gmail.com>
Thu, 15 Jul 2010 17:38:13 +0000 (14:38 -0300)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 16 Jul 2010 13:45:52 +0000 (15:45 +0200)
x_fmmap() opens, stats, and mmap()s a file. x_munmap() is currently just a
wrapper around munmap().

ccache.c
ccache.h
configure.ac
hashutil.c
unify.c
util.c

index a2660ad5bb408ddde3929abe82a075e565a70c1b..9073ce19b4a04917ba5bbba75f90fa0d638ad873 100644 (file)
--- a/ccache.c
+++ b/ccache.c
@@ -32,7 +32,6 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/mman.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdlib.h>
@@ -330,7 +329,6 @@ static void remember_include_file(char *path, size_t path_len)
        struct file_hash *h;
        struct mdfour fhash;
        struct stat st;
-       int fd = -1;
        char *data = (char *)-1;
        char *source;
        int result;
@@ -364,27 +362,20 @@ static void remember_include_file(char *path, size_t path_len)
        }
 
        /* Let's hash the include file. */
-       fd = open(path, O_RDONLY|O_BINARY);
-       if (fd == -1) {
-               cc_log("Failed to open include file %s", path);
-               goto failure;
-       }
        if (!(sloppiness & SLOPPY_INCLUDE_FILE_MTIME)
            && st.st_mtime >= time_of_compilation) {
                cc_log("Include file %s too new", path);
                goto failure;
        }
        if (st.st_size > 0) {
-               data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+               data = x_fmmap(path, &st.st_size, "include file");
                if (data == (char *)-1) {
-                       cc_log("Failed to mmap %s", path);
                        goto failure;
                }
                source = data;
        } else {
                source = "";
        }
-       close(fd);
 
        hash_start(&fhash);
        result = hash_source_code_string(&fhash, source, st.st_size, path);
@@ -397,7 +388,7 @@ static void remember_include_file(char *path, size_t path_len)
        hash_result_as_bytes(&fhash, h->hash);
        h->size = fhash.totalN;
        hashtable_insert(included_files, path, h);
-       munmap(data, st.st_size);
+       x_munmap(data, st.st_size);
        return;
 
 failure:
@@ -407,10 +398,7 @@ failure:
 ignore:
        free(path);
        if (data != (char *)-1) {
-               munmap(data, st.st_size);
-       }
-       if (fd != -1) {
-               close(fd);
+               x_munmap(data, st.st_size);
        }
 }
 
@@ -442,26 +430,12 @@ static char *make_relative_path(char *path)
  */
 static int process_preprocessed_file(struct mdfour *hash, const char *path)
 {
-       int fd;
        char *data;
        char *p, *q, *end;
        off_t size;
-       struct stat st;
 
-       fd = open(path, O_RDONLY);
-       if (fd == -1) {
-               cc_log("Failed to open %s", path);
-               return 0;
-       }
-       if (fstat(fd, &st) != 0) {
-               cc_log("Failed to fstat %s", path);
-               return 0;
-       }
-       size = st.st_size;
-       data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
-       close(fd);
+       data = x_fmmap(path, &size, "preprocessed file");
        if (data == (void *)-1) {
-               cc_log("Failed to mmap %s", path);
                return 0;
        }
 
@@ -507,7 +481,7 @@ static int process_preprocessed_file(struct mdfour *hash, const char *path)
                        q++;
                        if (q >= end) {
                                cc_log("Failed to parse included file path");
-                               munmap(data, size);
+                               x_munmap(data, size);
                                return 0;
                        }
                        /* q points to the beginning of an include file path */
@@ -532,7 +506,7 @@ static int process_preprocessed_file(struct mdfour *hash, const char *path)
        }
 
        hash_buffer(hash, p, (end - p));
-       munmap(data, size);
+       x_munmap(data, size);
        return 1;
 }
 
index 5c155f06aa9a68082eba774330c11fd80fa28f7b..5a99ebfb4a48d7f8a3ac90e2fb788d6f4dd64bf2 100644 (file)
--- a/ccache.h
+++ b/ccache.h
@@ -108,6 +108,8 @@ char *get_cwd();
 size_t common_dir_prefix_length(const char *s1, const char *s2);
 char *get_relative_path(const char *from, const char *to);
 void update_mtime(const char *path);
+void *x_fmmap(const char *fname, off_t *size, const char *errstr);
+int x_munmap(void *addr, size_t length);
 
 void stats_update(enum stats stat);
 void stats_flush(void);
index 08d0dad147a9efae1aa25e46907b57ff052f33cc..8753be9f9c93307e161a940b3910b9cd2879f8a1 100644 (file)
@@ -171,7 +171,7 @@ AC_HEADER_DIRENT
 AC_HEADER_TIME
 AC_HEADER_SYS_WAIT
 
-AC_CHECK_HEADERS(ctype.h pwd.h stdlib.h string.h strings.h sys/time.h)
+AC_CHECK_HEADERS(ctype.h pwd.h stdlib.h string.h strings.h sys/time.h sys/mman.h)
 
 AC_CHECK_FUNCS(asprintf)
 AC_CHECK_FUNCS(gethostname)
index 2b745b362b00feca60a2c3726bf9e6283a63a485..d8f00bd72c69c6c0c3db937fd4c27dc2292dafc7 100644 (file)
@@ -21,7 +21,6 @@
 #include "murmurhashneutral2.h"
 
 #include <string.h>
-#include <sys/mman.h>
 #include <fcntl.h>
 #include <time.h>
 
@@ -199,7 +198,6 @@ end:
 int
 hash_source_code_file(struct mdfour *hash, const char *path)
 {
-       int fd;
        struct stat st;
        char *data;
        int result;
@@ -211,19 +209,13 @@ hash_source_code_file(struct mdfour *hash, const char *path)
        if (st.st_size == 0) {
                return HASH_SOURCE_CODE_OK;
        }
-       fd = open(path, O_RDONLY|O_BINARY);
-       if (fd == -1) {
-               cc_log("Failed to open %s", path);
-               return HASH_SOURCE_CODE_ERROR;
-       }
-       data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-       close(fd);
+
+       data = x_fmmap(path, &st.st_size, "source code file");
        if (data == (void *)-1) {
-               cc_log("Failed to mmap %s", path);
                return HASH_SOURCE_CODE_ERROR;
        }
 
        result = hash_source_code_string(hash, data, st.st_size, path);
-       munmap(data, st.st_size);
+       x_munmap(data, st.st_size);
        return result;
 }
diff --git a/unify.c b/unify.c
index 18c47ec440e6ff5cb2a44b34aae9c9498209931b..66d31847e3beb4a106722a4cfca4ebd4487c391e 100644 (file)
--- a/unify.c
+++ b/unify.c
@@ -33,8 +33,6 @@
 #include "ccache.h"
 
 #include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
 #include <ctype.h>
 #include <fcntl.h>
 #include <string.h>
@@ -246,31 +244,19 @@ static void unify(struct mdfour *hash, unsigned char *p, size_t size)
 */
 int unify_hash(struct mdfour *hash, const char *fname)
 {
-       int fd;
-       struct stat st;
+       off_t size;
        char *map;
 
-       fd = open(fname, O_RDONLY|O_BINARY);
-       if (fd == -1 || fstat(fd, &st) != 0) {
-               cc_log("Failed to open preprocessor output %s", fname);
+       map = x_fmmap(fname, &size, "preprocessor output");
+       if (map == (void *) -1) {
                stats_update(STATS_PREPROCESSOR);
                return -1;
        }
 
-       /* we use mmap() to make it easy to handle arbitrarily long
-           lines in preprocessor output. I have seen lines of over
-           100k in length, so this is well worth it */
-       map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-       close(fd);
-       if (map == (char *)-1) {
-               cc_log("Failed to mmap %s", fname);
-               return -1;
-       }
-
        /* pass it through the unifier */
-       unify(hash, (unsigned char *)map, st.st_size);
+       unify(hash, (unsigned char *)map, size);
 
-       munmap(map, st.st_size);
+       x_munmap(map, size);
 
        return 0;
 }
diff --git a/util.c b/util.c
index d02f16bbf6a486023978c350c3ae399e36dac11a..cb58c0a6dc4a100414e3a5a9e904a9dad8525f0c 100644 (file)
--- a/util.c
+++ b/util.c
@@ -21,6 +21,9 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
+#ifdef HAVE_SYS_MMAN_H
+#include <sys/mman.h>
+#endif
 #include <fcntl.h>
 #include <ctype.h>
 #include <unistd.h>
@@ -946,3 +949,40 @@ update_mtime(const char *path)
        utime(path, NULL);
 #endif
 }
+
+/*
+ * Map file into memory. Return a pointer to the mapped area if successful or
+ * -1 if any error occurred. The file size is also returned,
+ */
+void *x_fmmap(const char *fname, off_t *size, const char *errstr)
+{
+       struct stat st;
+       void *data = (void *) -1;
+       int fd = -1;
+       fd = open(fname, O_RDONLY | O_BINARY);
+       if (fd == -1) {
+               cc_log("Failed to open %s %s", errstr, fname);
+               goto error;
+       }
+       if (fstat(fd, &st) == -1) {
+               cc_log("Failed to fstat %s %s", errstr, fname);
+               close(fd);
+               goto error;
+       }
+       data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+       close(fd);
+       if (data == (void *) -1) {
+               cc_log("Failed to mmap %s %s", errstr, fname);
+       }
+       *size = st.st_size;
+error:
+       return data;
+}
+
+/*
+ * Unmap file from memory.
+ */
+int x_munmap(void *addr, size_t length)
+{
+       return munmap(addr, length);
+}