|
37 | 37 | #include <bitcoin/database/error.hpp> |
38 | 38 | #include <bitcoin/database/file/file.hpp> |
39 | 39 |
|
| 40 | +#if defined(HAVE_APPLE) |
| 41 | +int fallocate(int fd, int, off_t offset, off_t len) NOEXCEPT; |
| 42 | +#endif |
| 43 | + |
40 | 44 | namespace libbitcoin { |
41 | 45 | namespace database { |
42 | 46 |
|
@@ -554,47 +558,6 @@ bool map::remap_(size_t size) NOEXCEPT |
554 | 558 | return finalize_(size); |
555 | 559 | } |
556 | 560 |
|
557 | | -#if defined(HAVE_APPLE) |
558 | | -// ::fallocate is not defined on macOS, so implement. Ignores mode (linux). |
559 | | -int fallocate(int fd, int, size_t offset, size_t len) NOEXCEPT |
560 | | -{ |
561 | | - fstore_t store |
562 | | - { |
563 | | - // Prefer contiguous allocation |
564 | | - .fst_flags = F_ALLOCATECONTIG, |
565 | | - |
566 | | - // Allocate from EOF |
567 | | - .fst_posmode = F_PEOFPOSMODE, |
568 | | - |
569 | | - // Start from current capacity |
570 | | - .fst_offset = offset, |
571 | | - |
572 | | - // Delta size |
573 | | - .fst_length = len, |
574 | | - |
575 | | - // Output: actual bytes allocated |
576 | | - .fst_bytesalloc = 0 |
577 | | - }; |
578 | | - |
579 | | - // Try contiguous allocation. |
580 | | - auto result = ::fcntl(fd, F_PREALLOCATE, &store); |
581 | | - |
582 | | - // Fallback to non-contiguous. |
583 | | - if ((result == fail) && (errno != ENOSPC)) |
584 | | - { |
585 | | - store.fst_flags = F_ALLOCATEALL; |
586 | | - result = ::fcntl(fd, F_PREALLOCATE, &store); |
587 | | - } |
588 | | - |
589 | | - if (result == fail) |
590 | | - return fail; |
591 | | - |
592 | | - // Extend file to new size (required for mmap). This is not required on |
593 | | - // Linux because fallocate(2) automatically extends file's logical size. |
594 | | - return ::ftruncate(fd, offset + len); |
595 | | -} |
596 | | -#endif // HAVE_APPLE |
597 | | - |
598 | 561 | // disk_full: space is set but no code is set with false return. |
599 | 562 | bool map::resize_(size_t size) NOEXCEPT |
600 | 563 | { |
@@ -681,3 +644,46 @@ BC_POP_WARNING() |
681 | 644 |
|
682 | 645 | } // namespace database |
683 | 646 | } // namespace libbitcoin |
| 647 | + |
| 648 | +#if defined(HAVE_APPLE) |
| 649 | +// ::fallocate is not defined on macOS, so implement. Ignores mode (linux). |
| 650 | +int fallocate(int fd, int, off_t offset, off_t len) NOEXCEPT |
| 651 | +{ |
| 652 | + constexpr auto fail = -1; |
| 653 | + |
| 654 | + fstore_t store |
| 655 | + { |
| 656 | + // Prefer contiguous allocation |
| 657 | + .fst_flags = F_ALLOCATECONTIG, |
| 658 | + |
| 659 | + // Allocate from EOF |
| 660 | + .fst_posmode = F_PEOFPOSMODE, |
| 661 | + |
| 662 | + // Start from current capacity |
| 663 | + .fst_offset = offset, |
| 664 | + |
| 665 | + // Delta size |
| 666 | + .fst_length = len, |
| 667 | + |
| 668 | + // Output: actual bytes allocated |
| 669 | + .fst_bytesalloc = 0 |
| 670 | + }; |
| 671 | + |
| 672 | + // Try contiguous allocation. |
| 673 | + auto result = ::fcntl(fd, F_PREALLOCATE, &store); |
| 674 | + |
| 675 | + // Fallback to non-contiguous. |
| 676 | + if ((result == fail) && (errno != ENOSPC)) |
| 677 | + { |
| 678 | + store.fst_flags = F_ALLOCATEALL; |
| 679 | + result = ::fcntl(fd, F_PREALLOCATE, &store); |
| 680 | + } |
| 681 | + |
| 682 | + if (result == fail) |
| 683 | + return fail; |
| 684 | + |
| 685 | + // Extend file to new size (required for mmap). This is not required on |
| 686 | + // Linux because fallocate(2) automatically extends file's logical size. |
| 687 | + return ::ftruncate(fd, offset + len); |
| 688 | +} |
| 689 | +#endif // HAVE_APPLE |
0 commit comments