99#include < openvdb/Exceptions.h>
1010#include < openvdb/util/logging.h>
1111#include < cstdint>
12+
13+ #ifdef OPENVDB_USE_DELAYED_LOADING
1214#include < boost/iostreams/copy.hpp>
1315#ifndef _WIN32
1416#include < sys/types.h>
15- #include < sys/stat.h>
1617#include < unistd.h>
1718#endif
19+ #endif // OPENVDB_USE_DELAYED_LOADING
20+
21+ #include < sys/stat.h> // stat()
22+
1823#include < cassert>
1924#include < cstdlib> // for getenv(), strtoul()
2025#include < cstring> // for strerror_r()
@@ -81,25 +86,27 @@ struct File::Impl
8186 std::string mFilename ;
8287 // The file-level metadata
8388 MetaMap::Ptr mMeta ;
84- // The memory-mapped file
85- MappedFile::Ptr mFileMapping ;
86- // The buffer for the input stream, if it is a memory-mapped file
87- SharedPtr<std::streambuf> mStreamBuf ;
8889 // The file stream that is open for reading
8990 std::unique_ptr<std::istream> mInStream ;
9091 // File-level stream metadata (file format, compression, etc.)
9192 StreamMetadata::Ptr mStreamMetadata ;
9293 // Flag indicating if we have read in the global information (header,
9394 // metadata, and grid descriptors) for this VDB file
9495 bool mIsOpen ;
95- // File size limit for copying during delayed loading
96- Index64 mCopyMaxBytes ;
9796 // Grid descriptors for all grids stored in the file, indexed by grid name
9897 NameMap mGridDescriptors ;
9998 // All grids, indexed by unique name (used only when mHasGridOffsets is false)
10099 Archive::NamedGridMap mNamedGrids ;
101100 // All grids stored in the file (used only when mHasGridOffsets is false)
102101 GridPtrVecPtr mGrids ;
102+ #ifdef OPENVDB_USE_DELAYED_LOADING
103+ // The memory-mapped file
104+ MappedFile::Ptr mFileMapping ;
105+ // The buffer for the input stream, if it is a memory-mapped file
106+ SharedPtr<std::streambuf> mStreamBuf ;
107+ // File size limit for copying during delayed loading
108+ Index64 mCopyMaxBytes ;
109+ #endif
103110}; // class File::Impl
104111
105112
@@ -110,7 +117,9 @@ File::File(const std::string& filename): mImpl(new Impl)
110117{
111118 mImpl ->mFilename = filename;
112119 mImpl ->mIsOpen = false ;
120+ #ifdef OPENVDB_USE_DELAYED_LOADING
113121 mImpl ->mCopyMaxBytes = Impl::getDefaultCopyMaxBytes ();
122+ #endif
114123 setInputHasGridOffsets (true );
115124}
116125
@@ -137,7 +146,9 @@ File::operator=(const File& other)
137146 mImpl ->mFilename = otherImpl.mFilename ;
138147 mImpl ->mMeta = otherImpl.mMeta ;
139148 mImpl ->mIsOpen = false ; // don't want two file objects reading from the same stream
149+ #ifdef OPENVDB_USE_DELAYED_LOADING
140150 mImpl ->mCopyMaxBytes = otherImpl.mCopyMaxBytes ;
151+ #endif
141152 mImpl ->mGridDescriptors = otherImpl.mGridDescriptors ;
142153 mImpl ->mNamedGrids = otherImpl.mNamedGrids ;
143154 mImpl ->mGrids = otherImpl.mGrids ;
@@ -241,6 +252,7 @@ File::getSize() const
241252}
242253
243254
255+ #ifdef OPENVDB_USE_DELAYED_LOADING
244256Index64
245257File::copyMaxBytes () const
246258{
@@ -253,6 +265,7 @@ File::setCopyMaxBytes(Index64 bytes)
253265{
254266 mImpl ->mCopyMaxBytes = bytes;
255267}
268+ #endif
256269
257270
258271// //////////////////////////////////////
@@ -266,7 +279,11 @@ File::isOpen() const
266279
267280
268281bool
282+ #ifdef OPENVDB_USE_DELAYED_LOADING
269283File::open (bool delayLoad, const MappedFile::Notifier& notifier)
284+ #else
285+ File::open (bool /* delayLoad = true*/ )
286+ #endif // OPENVDB_USE_DELAYED_LOADING
270287{
271288 if (isOpen ()) {
272289 OPENVDB_THROW (IoError, filename () << " is already open" );
@@ -276,10 +293,13 @@ File::open(bool delayLoad, const MappedFile::Notifier& notifier)
276293 // Open the file.
277294 std::unique_ptr<std::istream> newStream;
278295 SharedPtr<std::streambuf> newStreamBuf;
296+ #ifdef OPENVDB_USE_DELAYED_LOADING
279297 MappedFile::Ptr newFileMapping;
280298 if (!delayLoad || !Archive::isDelayedLoadingEnabled ()) {
299+ #endif
281300 newStream.reset (new std::ifstream (
282301 filename ().c_str (), std::ios_base::in | std::ios_base::binary));
302+ #ifdef OPENVDB_USE_DELAYED_LOADING
283303 } else {
284304 bool isTempFile = false ;
285305 std::string fname = filename ();
@@ -318,6 +338,7 @@ File::open(bool delayLoad, const MappedFile::Notifier& notifier)
318338 OPENVDB_THROW (IoError, ostr.str ());
319339 }
320340 }
341+ #endif // OPENVDB_USE_DELAYED_LOADING
321342
322343 if (newStream->fail ()) {
323344 OPENVDB_THROW (IoError, " could not open file " << filename ());
@@ -335,9 +356,11 @@ File::open(bool delayLoad, const MappedFile::Notifier& notifier)
335356 throw ;
336357 }
337358
359+ #ifdef OPENVDB_USE_DELAYED_LOADING
338360 mImpl ->mFileMapping = newFileMapping;
339361 if (mImpl ->mFileMapping ) mImpl ->mFileMapping ->setNotifier (notifier);
340362 mImpl ->mStreamBuf = newStreamBuf;
363+ #endif
341364 mImpl ->mInStream .swap (newStream);
342365
343366 // Tag the input stream with the file format and library version numbers
@@ -348,7 +371,9 @@ File::open(bool delayLoad, const MappedFile::Notifier& notifier)
348371 Archive::setFormatVersion (inputStream ());
349372 Archive::setLibraryVersion (inputStream ());
350373 Archive::setDataCompression (inputStream ());
374+ #ifdef OPENVDB_USE_DELAYED_LOADING
351375 io::setMappedFilePtr (inputStream (), mImpl ->mFileMapping );
376+ #endif
352377
353378 // Read in the VDB metadata.
354379 mImpl ->mMeta = MetaMap::Ptr (new MetaMap);
@@ -396,9 +421,11 @@ File::close()
396421 mImpl ->mGrids .reset ();
397422 mImpl ->mNamedGrids .clear ();
398423 mImpl ->mInStream .reset ();
399- mImpl ->mStreamBuf .reset ();
400424 mImpl ->mStreamMetadata .reset ();
425+ #ifdef OPENVDB_USE_DELAYED_LOADING
426+ mImpl ->mStreamBuf .reset ();
401427 mImpl ->mFileMapping .reset ();
428+ #endif
402429
403430 mImpl ->mIsOpen = false ;
404431 setInputHasGridOffsets (true );
0 commit comments