OSDN Git Service

DO NOT MERGE: fec: remove unused mmap code
authorSami Tolvanen <samitolvanen@google.com>
Thu, 1 Sep 2016 20:35:17 +0000 (13:35 -0700)
committerZheng Zhang <zhzh@google.com>
Thu, 10 Nov 2016 18:36:12 +0000 (18:36 +0000)
Bug: 32789520

Change-Id: If4b32546c6678afaaebffbcf1fbeee17a1879561

(cherrypicked from commit 8bad827700bae35005872f3e6d674c5144fda8ff)

verity/fec/image.cpp
verity/fec/image.h
verity/fec/main.cpp

index a378c93..780d981 100644 (file)
@@ -51,24 +51,7 @@ void image_init(image *ctx)
     memset(ctx, 0, sizeof(*ctx));
 }
 
-static void mmap_image_free(image *ctx)
-{
-    if (ctx->input) {
-        munmap(ctx->input, (size_t)ctx->inp_size);
-        close(ctx->inp_fd);
-    }
-
-    if (ctx->fec_mmap_addr) {
-        munmap(ctx->fec_mmap_addr, FEC_BLOCKSIZE + ctx->fec_size);
-        close(ctx->fec_fd);
-    }
-
-    if (!ctx->inplace && ctx->output) {
-        delete[] ctx->output;
-    }
-}
-
-static void file_image_free(image *ctx)
+void image_free(image *ctx)
 {
     assert(ctx->input == ctx->output);
 
@@ -79,19 +62,11 @@ static void file_image_free(image *ctx)
     if (ctx->fec) {
         delete[] ctx->fec;
     }
-}
-
-void image_free(image *ctx)
-{
-    if (ctx->mmap) {
-        mmap_image_free(ctx);
-    } else {
-        file_image_free(ctx);
-    }
 
     image_init(ctx);
 }
 
+#ifdef IMAGE_NO_SPARSE
 static uint64_t get_size(int fd)
 {
     struct stat st;
@@ -114,6 +89,7 @@ static uint64_t get_size(int fd)
 
     return size;
 }
+#endif
 
 static void calculate_rounds(uint64_t size, image *ctx)
 {
@@ -129,63 +105,6 @@ static void calculate_rounds(uint64_t size, image *ctx)
     ctx->rounds = fec_div_round_up(ctx->blocks, ctx->rs_n);
 }
 
-static void mmap_image_load(const std::vector<int>& fds, image *ctx,
-        bool output_needed)
-{
-    if (fds.size() != 1) {
-        FATAL("multiple input files not supported with mmap\n");
-    }
-
-    int fd = fds.front();
-
-    calculate_rounds(get_size(fd), ctx);
-
-    /* check that we can memory map the file; on 32-bit platforms we are
-       limited to encoding at most 4 GiB files */
-    if (ctx->inp_size > SIZE_MAX) {
-        FATAL("cannot mmap %" PRIu64 " bytes\n", ctx->inp_size);
-    }
-
-    if (ctx->verbose) {
-        INFO("memory mapping '%s' (size %" PRIu64 ")\n", ctx->fec_filename,
-            ctx->inp_size);
-    }
-
-    int flags = PROT_READ;
-
-    if (ctx->inplace) {
-        flags |= PROT_WRITE;
-    }
-
-    void *p = mmap(NULL, (size_t)ctx->inp_size, flags, MAP_SHARED, fd, 0);
-
-    if (p == MAP_FAILED) {
-        FATAL("failed to mmap '%s' (size %" PRIu64 "): %s\n",
-            ctx->fec_filename, ctx->inp_size, strerror(errno));
-    }
-
-    ctx->inp_fd = fd;
-    ctx->input = (uint8_t *)p;
-
-    if (ctx->inplace) {
-        ctx->output = ctx->input;
-    } else if (output_needed) {
-        if (ctx->verbose) {
-            INFO("allocating %" PRIu64 " bytes of memory\n", ctx->inp_size);
-        }
-
-        ctx->output = new uint8_t[ctx->inp_size];
-
-        if (!ctx->output) {
-                FATAL("failed to allocate memory\n");
-        }
-
-        memcpy(ctx->output, ctx->input, ctx->inp_size);
-    }
-
-    /* fd is closed in mmap_image_free */
-}
-
 #ifndef IMAGE_NO_SPARSE
 static int process_chunk(void *priv, const void *data, int len)
 {
@@ -276,8 +195,7 @@ static void file_image_load(const std::vector<int>& fds, image *ctx)
 #endif
 }
 
-bool image_load(const std::vector<std::string>& filenames, image *ctx,
-        bool output_needed)
+bool image_load(const std::vector<std::string>& filenames, image *ctx)
 {
     assert(ctx->roots > 0 && ctx->roots < FEC_RSM);
     ctx->rs_n = FEC_RSM - ctx->roots;
@@ -300,21 +218,13 @@ bool image_load(const std::vector<std::string>& filenames, image *ctx,
         fds.push_back(fd);
     }
 
-    if (ctx->mmap) {
-        mmap_image_load(fds, ctx, output_needed);
-    } else {
-        file_image_load(fds, ctx);
-    }
+    file_image_load(fds, ctx);
 
     return true;
 }
 
 bool image_save(const std::string& filename, image *ctx)
 {
-    if (ctx->inplace && ctx->mmap) {
-        return true; /* nothing to do */
-    }
-
     /* TODO: support saving as a sparse file */
     int fd = TEMP_FAILURE_RETRY(open(filename.c_str(),
                 O_WRONLY | O_CREAT | O_TRUNC, 0666));
@@ -332,46 +242,13 @@ bool image_save(const std::string& filename, image *ctx)
     return true;
 }
 
-static void mmap_image_ecc_new(image *ctx)
+bool image_ecc_new(const std::string& filename, image *ctx)
 {
-    if (ctx->verbose) {
-        INFO("mmaping '%s' (size %u)\n", ctx->fec_filename, ctx->fec_size);
-    }
-
-    int fd = TEMP_FAILURE_RETRY(open(ctx->fec_filename,
-                O_RDWR | O_CREAT, 0666));
-
-    if (fd < 0) {
-        FATAL("failed to open file '%s': %s\n", ctx->fec_filename,
-            strerror(errno));
-    }
-
-    assert(sizeof(fec_header) <= FEC_BLOCKSIZE);
-    size_t fec_size = FEC_BLOCKSIZE + ctx->fec_size;
-
-    if (ftruncate(fd, fec_size) == -1) {
-        FATAL("failed to ftruncate file '%s': %s\n", ctx->fec_filename,
-            strerror(errno));
-    }
-
-    if (ctx->verbose) {
-        INFO("memory mapping '%s' (size %zu)\n", ctx->fec_filename, fec_size);
-    }
-
-    void *p = mmap(NULL, fec_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
-
-    if (p == MAP_FAILED) {
-        FATAL("failed to mmap '%s' (size %zu): %s\n", ctx->fec_filename,
-            fec_size, strerror(errno));
-    }
+    assert(ctx->rounds > 0); /* image_load should be called first */
 
-    ctx->fec_fd = fd;
-    ctx->fec_mmap_addr = (uint8_t *)p;
-    ctx->fec = ctx->fec_mmap_addr;
-}
+    ctx->fec_filename = filename.c_str();
+    ctx->fec_size = ctx->rounds * ctx->roots * FEC_BLOCKSIZE;
 
-static void file_image_ecc_new(image *ctx)
-{
     if (ctx->verbose) {
         INFO("allocating %u bytes of memory\n", ctx->fec_size);
     }
@@ -381,20 +258,6 @@ static void file_image_ecc_new(image *ctx)
     if (!ctx->fec) {
         FATAL("failed to allocate %u bytes\n", ctx->fec_size);
     }
-}
-
-bool image_ecc_new(const std::string& filename, image *ctx)
-{
-    assert(ctx->rounds > 0); /* image_load should be called first */
-
-    ctx->fec_filename = filename.c_str();
-    ctx->fec_size = ctx->rounds * ctx->roots * FEC_BLOCKSIZE;
-
-    if (ctx->mmap) {
-        mmap_image_ecc_new(ctx);
-    } else {
-        file_image_ecc_new(ctx);
-    }
 
     return true;
 }
@@ -462,7 +325,7 @@ bool image_ecc_load(const std::string& filename, image *ctx)
         FATAL("failed to rewind '%s': %s", filename.c_str(), strerror(errno));
     }
 
-    if (!ctx->mmap && !android::base::ReadFully(fd, ctx->fec, ctx->fec_size)) {
+    if (!android::base::ReadFully(fd, ctx->fec, ctx->fec_size)) {
         FATAL("failed to read %u bytes from '%s': %s\n", ctx->fec_size,
             filename.c_str(), strerror(errno));
     }
@@ -486,10 +349,6 @@ bool image_ecc_save(image *ctx)
     uint8_t header[FEC_BLOCKSIZE];
     uint8_t *p = header;
 
-    if (ctx->mmap) {
-        p = (uint8_t *)&ctx->fec_mmap_addr[ctx->fec_size];
-    }
-
     memset(p, 0, FEC_BLOCKSIZE);
 
     fec_header *f = (fec_header *)p;
@@ -506,25 +365,23 @@ bool image_ecc_save(image *ctx)
     /* store a copy of the fec_header at the end of the header block */
     memcpy(&p[sizeof(header) - sizeof(fec_header)], p, sizeof(fec_header));
 
-    if (!ctx->mmap) {
-        assert(ctx->fec_filename);
-
-        int fd = TEMP_FAILURE_RETRY(open(ctx->fec_filename,
-                    O_WRONLY | O_CREAT | O_TRUNC, 0666));
+    assert(ctx->fec_filename);
 
-        if (fd < 0) {
-            FATAL("failed to open file '%s': %s\n", ctx->fec_filename,
-                strerror(errno));
-        }
+    int fd = TEMP_FAILURE_RETRY(open(ctx->fec_filename,
+                O_WRONLY | O_CREAT | O_TRUNC, 0666));
 
-        if (!android::base::WriteFully(fd, ctx->fec, ctx->fec_size) ||
-            !android::base::WriteFully(fd, header, sizeof(header))) {
-            FATAL("failed to write to output: %s\n", strerror(errno));
-        }
+    if (fd < 0) {
+        FATAL("failed to open file '%s': %s\n", ctx->fec_filename,
+            strerror(errno));
+    }
 
-        close(fd);
+    if (!android::base::WriteFully(fd, ctx->fec, ctx->fec_size) ||
+        !android::base::WriteFully(fd, header, sizeof(header))) {
+        FATAL("failed to write to output: %s\n", strerror(errno));
     }
 
+    close(fd);
+
     return true;
 }
 
index f0211fd..72048c6 100644 (file)
@@ -38,8 +38,6 @@
 struct image {
     /* if true, decode file in place instead of creating a new output file */
     bool inplace;
-    /* if true, use memory mapping instead of copying all input into memory */
-    bool mmap;
     /* if true, assume input is a sparse file */
     bool sparse;
     /* if true, print more verbose information to stderr */
@@ -60,7 +58,6 @@ struct image {
     uint64_t rounds;
     uint64_t rv;
     uint8_t *fec;
-    uint8_t *fec_mmap_addr;
     uint8_t *input;
     uint8_t *output;
 };
@@ -79,8 +76,7 @@ struct image_proc_ctx {
     void *rs;
 };
 
-extern bool image_load(const std::vector<std::string>& filename, image *ctx,
-        bool output_needed);
+extern bool image_load(const std::vector<std::string>& filename, image *ctx);
 extern bool image_save(const std::string& filename, image *ctx);
 
 extern bool image_ecc_new(const std::string& filename, image *ctx);
index 50d807e..8dbb95d 100644 (file)
@@ -105,7 +105,6 @@ static int usage()
            "  -h                                show this help\n"
            "  -v                                enable verbose logging\n"
            "  -r, --roots=<bytes>               number of parity bytes\n"
-           "  -m, --mmap                        use memory mapping\n"
            "  -j, --threads=<threads>           number of threads to use\n"
            "  -S                                treat data as a sparse file\n"
            "decoding options:\n"
@@ -176,7 +175,7 @@ static int encode(image& ctx, const std::vector<std::string>& inp_filenames,
         FATAL("invalid parameters: inplace can only used when decoding\n");
     }
 
-    if (!image_load(inp_filenames, &ctx, false)) {
+    if (!image_load(inp_filenames, &ctx)) {
         FATAL("failed to read input\n");
     }
 
@@ -222,7 +221,7 @@ static int decode(image& ctx, const std::vector<std::string>& inp_filenames,
     }
 
     if (!image_ecc_load(fec_filename, &ctx) ||
-            !image_load(inp_filenames, &ctx, !out_filename.empty())) {
+            !image_load(inp_filenames, &ctx)) {
         FATAL("failed to read input\n");
     }
 
@@ -281,7 +280,6 @@ int main(int argc, char **argv)
             {"sparse", no_argument, 0, 'S'},
             {"roots", required_argument, 0, 'r'},
             {"inplace", no_argument, 0, 'i'},
-            {"mmap", no_argument, 0, 'm'},
             {"threads", required_argument, 0, 'j'},
             {"print-fec-size", required_argument, 0, 's'},
             {"get-ecc-start", required_argument, 0, 'E'},
@@ -317,9 +315,6 @@ int main(int argc, char **argv)
         case 'i':
             ctx.inplace = true;
             break;
-        case 'm':
-            ctx.mmap = true;
-            break;
         case 'j':
             ctx.threads = (int)parse_arg(optarg, "threads", IMAGE_MAX_THREADS);
             break;