Skip to content

Commit

Permalink
Add header with low level I/O helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
de-vri-es committed Jun 12, 2018
1 parent cf5667d commit e032e39
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 102 deletions.
166 changes: 166 additions & 0 deletions io/include/pcl/io/helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2018 Fizyr BV. - https://fizyr.com
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/**
* This file defines compatibility wrappers for low level I/O functions.
* Implemented as inlinable functions to prevent any performance overhead.
*/

#ifndef __PCL_IO_HELPERS__
#define __PCL_IO_HELPERS__

#ifdef _WIN32
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <io.h>
# include <windows.h>
# include <BaseTsd.h>
#else
# include <unistd.h>
# include <sys/mman.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <sys/fcntl.h>
#endif

namespace pcl
{
namespace io
{
#ifdef _WIN32
typedef SSIZE_T ssize_t;

inline int pcl_open(const char * pathname, int flags, int mode)
{
return ::_open(pathname, flags, mode);
}

inline int pcl_open(const char * pathname, int flags)
{
return ::_open(pathname, flags);
}

inline int pcl_close(int fd)
{
return ::_close(fd);
}

inline int pcl_lseek(int fd, long offset, int whence)
{
return ::_lseek(fd, offset, whence);
}

inline int pcl_read(int fd, void * buffer, size_t count)
{
return ::_read(fd, buffer, count);
}

inline int pcl_write(int fd, const void * buffer, size_t count)
{
return ::_write(fd, buffer, count);
}

inline int pcl_fallocate(int fd, off_t len)
{
return ::chsize(fd, len);
}
#else
inline int pcl_open(const char * pathname, int flags, int mode)
{
return ::open(pathname, flags, mode);
}

inline int pcl_open(const char * pathname, int flags)
{
return ::open(pathname, flags);
}

inline int pcl_close(int fd)
{
return ::close(fd);
}

inline off_t pcl_lseek(int fd, off_t offset, int whence)
{
return ::lseek(fd, offset, whence);
}

inline ssize_t pcl_read(int fd, void * buffer, size_t count)
{
return ::read(fd, buffer, count);
}

inline ssize_t pcl_write(int fd, const void * buffer, size_t count)
{
return ::write(fd, buffer, count);
}

# ifndef __APPLE__
inline int pcl_fallocate(int fd, off_t len)
{
return ::posix_fallocate(fd, 0, len);
}
# else
inline int pcl_fallocate(int fd, off_t len)
{
// Try to allocate contiguous space first.
::fstore_t store = {F_ALLOCATEALL | F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, len};
if (::fcntl(fd, F_PREALLOCATE, &store) < 0)
{
// Try fragmented if that failed.
store.fst_flags = F_ALLOCATEALL;
int ret = ::fcntl(fd, F_PREALLOCATE, &store);

// Bail if it still failed.
if (ret < 0) {
return ret;
}
}

// File could be larger than requested, so truncate.
return ::ftruncate(fd, len);
}
#endif

}
}

#endif
#endif // __PCL_IO_HELPERS__
78 changes: 25 additions & 53 deletions io/include/pcl/io/impl/pcd_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,41 +44,13 @@
#include <fcntl.h>
#include <string>
#include <stdlib.h>
#include <pcl/io/boost.h>
#include <pcl/console/print.h>
#include <pcl/io/boost.h>
#include <pcl/io/helpers.h>
#include <pcl/io/pcd_io.h>

#ifdef _WIN32
# include <io.h>
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif // WIN32_LEAN_AND_MEAN
# ifndef NOMINMAX
# define NOMINMAX
# endif // NOMINMAX
# include <windows.h>
# define pcl_open _open
# define pcl_close(fd) _close(fd)
# define pcl_lseek(fd,offset,origin) _lseek(fd,offset,origin)
# define pcl_read(fd,dest,size) _read(fd,dest,size)
/* ssize_t is also not available (copy/paste from MinGW) */
#ifdef _MSC_VER
#ifndef _SSIZE_T_DEFINED
#define _SSIZE_T_DEFINED
#undef ssize_t
#ifdef _WIN64
typedef __int64 ssize_t;
#else
typedef int ssize_t;
#endif /* _WIN64 */
#endif /* _SSIZE_T_DEFINED */
#endif /* _MSC_VER */
#else
#ifndef _WIN32
# include <sys/mman.h>
# define pcl_open open
# define pcl_close(fd) close(fd)
# define pcl_lseek(fd,offset,origin) lseek(fd,offset,origin)
# define pcl_read(fd,dest,size) ::read(fd,dest,size)
#endif

#include <pcl/io/lzf.h>
Expand Down Expand Up @@ -162,7 +134,7 @@ pcl::PCDWriter::writeBinary (const std::string &file_name,
return (-1);
}
#else
int fd = pcl_open (file_name.c_str (), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
int fd = io::pcl_open (file_name.c_str (), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0)
{
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during open!");
Expand Down Expand Up @@ -209,18 +181,18 @@ pcl::PCDWriter::writeBinary (const std::string &file_name,
// Allocate disk space for the entire file to prevent bus errors.
if (::posix_fallocate (fd, 0, data_idx + data_size) != 0)
{
pcl_close (fd);
io::pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
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);
}

char *map = static_cast<char*> (mmap (0, data_idx + data_size, PROT_WRITE, MAP_SHARED, fd, 0));
char *map = static_cast<char*> (::mmap (0, data_idx + data_size, PROT_WRITE, MAP_SHARED, fd, 0));
if (map == reinterpret_cast<char*> (-1)) //MAP_FAILED)
{
pcl_close (fd);
io::pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during mmap ()!");
return (-1);
Expand Down Expand Up @@ -252,9 +224,9 @@ pcl::PCDWriter::writeBinary (const std::string &file_name,
#if _WIN32
UnmapViewOfFile (map);
#else
if (munmap (map, (data_idx + data_size)) == -1)
if (::munmap (map, (data_idx + data_size)) == -1)
{
pcl_close (fd);
io::pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during munmap ()!");
return (-1);
Expand All @@ -264,7 +236,7 @@ pcl::PCDWriter::writeBinary (const std::string &file_name,
#if _WIN32
CloseHandle (h_native_file);
#else
pcl_close (fd);
io::pcl_close (fd);
#endif
resetLockingPermissions (file_name, file_lock);
return (0);
Expand Down Expand Up @@ -294,7 +266,7 @@ pcl::PCDWriter::writeBinaryCompressed (const std::string &file_name,
return (-1);
}
#else
int fd = pcl_open (file_name.c_str (), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
int fd = io::pcl_open (file_name.c_str (), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0)
{
throw pcl::IOException ("[pcl::PCDWriter::writeBinaryCompressed] Error during open!");
Expand Down Expand Up @@ -391,7 +363,7 @@ pcl::PCDWriter::writeBinaryCompressed (const std::string &file_name,
else
{
#if !_WIN32
pcl_close (fd);
io::pcl_close (fd);
#endif
resetLockingPermissions (file_name, file_lock);
throw pcl::IOException ("[pcl::PCDWriter::writeBinaryCompressed] Error during compression!");
Expand All @@ -408,18 +380,18 @@ pcl::PCDWriter::writeBinaryCompressed (const std::string &file_name,
// Allocate disk space for the entire file to prevent bus errors.
if (::posix_fallocate (fd, 0, compressed_final_size) != 0)
{
pcl_close (fd);
io::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));
char *map = static_cast<char*> (::mmap (0, compressed_final_size, PROT_WRITE, MAP_SHARED, fd, 0));
if (map == reinterpret_cast<char*> (-1)) //MAP_FAILED)
{
pcl_close (fd);
io::pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
throw pcl::IOException ("[pcl::PCDWriter::writeBinaryCompressed] Error during mmap ()!");
return (-1);
Expand All @@ -441,9 +413,9 @@ pcl::PCDWriter::writeBinaryCompressed (const std::string &file_name,
#if _WIN32
UnmapViewOfFile (map);
#else
if (munmap (map, (compressed_final_size)) == -1)
if (::munmap (map, (compressed_final_size)) == -1)
{
pcl_close (fd);
io::pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
throw pcl::IOException ("[pcl::PCDWriter::writeBinaryCompressed] Error during munmap ()!");
return (-1);
Expand All @@ -454,7 +426,7 @@ pcl::PCDWriter::writeBinaryCompressed (const std::string &file_name,
#if _WIN32
CloseHandle (h_native_file);
#else
pcl_close (fd);
io::pcl_close (fd);
#endif
resetLockingPermissions (file_name, file_lock);

Expand Down Expand Up @@ -666,7 +638,7 @@ pcl::PCDWriter::writeBinary (const std::string &file_name,
return (-1);
}
#else
int fd = pcl_open (file_name.c_str (), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
int fd = io::pcl_open (file_name.c_str (), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0)
{
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during open!");
Expand Down Expand Up @@ -708,18 +680,18 @@ pcl::PCDWriter::writeBinary (const std::string &file_name,
// Allocate disk space for the entire file to prevent bus errors.
if (::posix_fallocate (fd, 0, data_idx + data_size) != 0)
{
pcl_close (fd);
io::pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
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);
}

char *map = static_cast<char*> (mmap (0, data_idx + data_size, PROT_WRITE, MAP_SHARED, fd, 0));
char *map = static_cast<char*> (::mmap (0, data_idx + data_size, PROT_WRITE, MAP_SHARED, fd, 0));
if (map == reinterpret_cast<char*> (-1)) //MAP_FAILED)
{
pcl_close (fd);
io::pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during mmap ()!");
return (-1);
Expand Down Expand Up @@ -751,9 +723,9 @@ pcl::PCDWriter::writeBinary (const std::string &file_name,
#if _WIN32
UnmapViewOfFile (map);
#else
if (munmap (map, (data_idx + data_size)) == -1)
if (::munmap (map, (data_idx + data_size)) == -1)
{
pcl_close (fd);
io::pcl_close (fd);
resetLockingPermissions (file_name, file_lock);
throw pcl::IOException ("[pcl::PCDWriter::writeBinary] Error during munmap ()!");
return (-1);
Expand All @@ -763,7 +735,7 @@ pcl::PCDWriter::writeBinary (const std::string &file_name,
#if _WIN32
CloseHandle(h_native_file);
#else
pcl_close (fd);
io::pcl_close (fd);
#endif

resetLockingPermissions (file_name, file_lock);
Expand Down
Loading

0 comments on commit e032e39

Please sign in to comment.