Skip to content

Commit 855142b

Browse files
committed
Define ::fallocate for macos (fix disk full handling).
1 parent 869dee5 commit 855142b

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/memory/map.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,47 @@ bool map::remap_(size_t size) NOEXCEPT
554554
return finalize_(size);
555555
}
556556

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+
557598
// disk_full: space is set but no code is set with false return.
558599
bool map::resize_(size_t size) NOEXCEPT
559600
{

0 commit comments

Comments
 (0)