-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 */
-}
-