Skip to content

Commit

Permalink
Allocate disk space with posix_fallocate before mmapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
de-vri-es committed May 29, 2018
1 parent 5f0cf69 commit 2b0ed8a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 63 deletions.
73 changes: 20 additions & 53 deletions io/include/pcl/io/impl/pcd_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,25 +206,14 @@ pcl::PCDWriter::writeBinary (const std::string &file_name,
CloseHandle (fm);

#else
// Stretch the file size to the size of the data
off_t result = pcl_lseek (fd, getpagesize () + data_size - 1, SEEK_SET);

if (result < 0)
// Allocate disk space for the entire file to prevent bus errors.
if (::posix_fallocate (fd, 0, data_idx + data_size) != 0)
{
pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
PCL_ERROR ("[pcl::PCDWriter::writeBinary] lseek errno: %d strerror: %s\n", errno, strerror (errno));
PCL_ERROR ("[pcl::PCDWriter::writeBinary] posix_fallocate errno: %d strerror: %s\n", errno, strerror (errno));

throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during lseek ()!");
return (-1);
}
// Write a bogus entry so that the new file size comes in effect
result = static_cast<int> (::write (fd, "", 1));
if (result != 1)
{
pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during write ()!");
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during posix_fallocate ()!");
return (-1);
}

Expand Down Expand Up @@ -400,36 +389,24 @@ pcl::PCDWriter::writeBinaryCompressed (const std::string &file_name,
return (-1);
}

#if !_WIN32
// Stretch the file size to the size of the data
off_t result = pcl_lseek (fd, getpagesize () + data_size - 1, SEEK_SET);
if (result < 0)
{
pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
PCL_ERROR ("[pcl::PCDWriter::writeBinary] lseek errno: %d strerror: %s\n", errno, strerror (errno));

throw pcl::IOException ("[pcl::PCDWriter::writeBinaryCompressed] Error during lseek ()!");
return (-1);
}
// Write a bogus entry so that the new file size comes in effect
result = static_cast<int> (::write (fd, "", 1));
if (result != 1)
{
pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
throw pcl::IOException ("[pcl::PCDWriter::writeBinaryCompressed] Error during write ()!");
return (-1);
}
#endif

// Prepare the map
#if _WIN32
HANDLE fm = CreateFileMapping (h_native_file, NULL, PAGE_READWRITE, 0, compressed_final_size, NULL);
char *map = static_cast<char*>(MapViewOfFile (fm, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, compressed_final_size));
CloseHandle (fm);

#else
// Allocate disk space for the entire file to prevent bus errors.
if (::posix_fallocate (fd, 0, compressed_final_size) != 0)
{
pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
PCL_ERROR ("[pcl::PCDWriter::writeBinaryCompressed] posix_fallocate errno: %d strerror: %s\n", errno, strerror (errno));

throw pcl::IOException ("[pcl::PCDWriter::writeBinaryCompressed] Error during posix_fallocate ()!");
return (-1);
}

char *map = static_cast<char*> (mmap (0, compressed_final_size, PROT_WRITE, MAP_SHARED, fd, 0));
if (map == reinterpret_cast<char*> (-1)) //MAP_FAILED)
{
Expand Down Expand Up @@ -719,24 +696,14 @@ pcl::PCDWriter::writeBinary (const std::string &file_name,
CloseHandle (fm);

#else
// Stretch the file size to the size of the data
off_t result = pcl_lseek (fd, getpagesize () + data_size - 1, SEEK_SET);
if (result < 0)
{
pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
PCL_ERROR ("[pcl::PCDWriter::writeBinary] lseek errno: %d strerror: %s\n", errno, strerror (errno));

throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during lseek ()!");
return (-1);
}
// Write a bogus entry so that the new file size comes in effect
result = static_cast<int> (::write (fd, "", 1));
if (result != 1)
// Allocate disk space for the entire file to prevent bus errors.
if (::posix_fallocate (fd, 0, data_idx + data_size) != 0)
{
pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during write ()!");
PCL_ERROR ("[pcl::PCDWriter::writeBinary] posix_fallocate errno: %d strerror: %s\n", errno, strerror (errno));

throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during posix_fallocate ()!");
return (-1);
}

Expand Down
14 changes: 4 additions & 10 deletions io/src/lzf_image_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,12 @@ pcl::io::LZFImageWriter::saveImageBlob (const char* data,
int fd = pcl_open (filename.c_str (), O_RDWR | O_CREAT | O_TRUNC, static_cast<mode_t> (0600));
if (fd < 0)
return (false);
// Stretch the file size to the size of the data
off_t result = pcl_lseek (fd, data_size - 1, SEEK_SET);
if (result < 0)
{
pcl_close (fd);
return (false);
}
// Write a bogus entry so that the new file size comes in effect
result = static_cast<int> (::write (fd, "", 1));
if (result != 1)

// Allocate disk space for the entire file to prevent bus errors.
if (::posix_fallocate (fd, 0, data_size) != 0)
{
pcl_close (fd);
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during posix_fallocate ()!");
return (false);
}

Expand Down

2 comments on commit 2b0ed8a

@rowanborder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks compilation on Mac OSX as posix_fallocate() does not exist.

@taketwo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a fix for it in #2341, but awaiting for someone with MacOS to try it. Please do try and report your results.

Please sign in to comment.