- off_t pages = (stats.st_size - 1) / page_size + 1;
- long map_size = pages * page_size;
- char *m = mmap(NULL, map_size, PROT_READ, MAP_SHARED, fd, 0);
- if (m == MAP_FAILED) err(EX_NOINPUT, "Could not mmap input");
+ off_t num_chunks = ceil_div(stats.st_size, mmap_chunk_size);
+ for (off_t chunk = num_chunks - 1; chunk >= 0; chunk--) {
+ off_t start_offset = chunk * mmap_chunk_size;
+ off_t end_offset = (chunk + 1) * mmap_chunk_size;
+ if (end_offset > stats.st_size) {
+ end_offset = stats.st_size;
+ }
+ off_t pages = ceil_div(end_offset - start_offset, page_size);
+ long map_size = pages * page_size;
+ char *m = mmap(NULL, map_size, PROT_READ, MAP_SHARED, fd, start_offset);
+ if (m == MAP_FAILED) err(EX_NOINPUT, "Could not mmap chunk %lld of %lld", chunk, num_chunks);