| 523 | #define SMALL_FILE_SIZE (32*1024) |
| 524 | |
| 525 | static int allocate_snapshot_buffer(struct snapshot *snapshot, int fd, struct stat *st) |
| 526 | { |
| 527 | ssize_t bytes_read; |
| 528 | size_t size; |
| 529 | |
| 530 | size = xsize_t(st->st_size); |
| 531 | if (!size) |
| 532 | return 0; |
| 533 | |
| 534 | if (mmap_strategy == MMAP_NONE || size <= SMALL_FILE_SIZE) { |
| 535 | snapshot->buf = xmalloc(size); |
| 536 | bytes_read = read_in_full(fd, snapshot->buf, size); |
| 537 | if (bytes_read < 0 || bytes_read != size) |
| 538 | die_errno("couldn't read %s", snapshot->refs->path); |
| 539 | snapshot->mmapped = 0; |
| 540 | } else { |
| 541 | snapshot->buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 542 | snapshot->mmapped = 1; |
| 543 | } |
| 544 | |
| 545 | snapshot->start = snapshot->buf; |
| 546 | snapshot->eof = snapshot->buf + size; |
| 547 | |
| 548 | return 1; |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * Depending on `mmap_strategy`, either mmap or read the contents of |
no test coverage detected