Skip to content

Commit 34eb0d1

Browse files
committed
Fix macos build break.
1 parent 3c5133c commit 34eb0d1

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

src/memory/map.cpp

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
#include <bitcoin/database/error.hpp>
3838
#include <bitcoin/database/file/file.hpp>
3939

40+
#if defined(HAVE_APPLE)
41+
int fallocate(int fd, int, off_t offset, off_t len) NOEXCEPT;
42+
#endif
43+
4044
namespace libbitcoin {
4145
namespace database {
4246

@@ -554,47 +558,6 @@ bool map::remap_(size_t size) NOEXCEPT
554558
return finalize_(size);
555559
}
556560

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-
598561
// disk_full: space is set but no code is set with false return.
599562
bool map::resize_(size_t size) NOEXCEPT
600563
{
@@ -681,3 +644,46 @@ BC_POP_WARNING()
681644

682645
} // namespace database
683646
} // 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

Comments
 (0)