diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/IPread.C b/src/OpenFOAM/db/IOstreams/Pstreams/IPread.C index 385be9a081..72aa238ac9 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/IPread.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/IPread.C @@ -27,9 +27,8 @@ Description \*---------------------------------------------------------------------------*/ -#include "error.H" - #include "IPstream.H" +#include "error.H" #include "int.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -51,17 +50,24 @@ inline void IPstream::checkEof() template inline void IPstream::readFromBuffer(T& t) { + const size_t align = sizeof(T); + bufPosition_ = align + ((bufPosition_ - 1) & ~(align - 1)); + t = reinterpret_cast(buf_[bufPosition_]); bufPosition_ += sizeof(T); checkEof(); - - //readFromBuffer(&t, sizeof(T)); } -inline void IPstream::readFromBuffer(void* data, size_t count) +inline void IPstream::readFromBuffer(void* data, size_t count, size_t align) { + if (align > 1) + { + bufPosition_ = align + ((bufPosition_ - 1) & ~(align - 1)); + } + register const char* bufPtr = &buf_[bufPosition_]; + register char* dataPtr = reinterpret_cast(data); register size_t i = count; while (i--) *dataPtr++ = *bufPtr++; @@ -137,7 +143,7 @@ Istream& IPstream::read(char* data, std::streamsize count) << Foam::abort(FatalError); } - readFromBuffer(data, count); + readFromBuffer(data, count, 8); return *this; } diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/IPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/IPstream.H index 9b637bd0da..e609f97d9a 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/IPstream.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/IPstream.H @@ -71,7 +71,7 @@ class IPstream inline void readFromBuffer(T&); //- Read data from the transfer buffer - inline void readFromBuffer(void* data, size_t count); + inline void readFromBuffer(void* data, size_t count, size_t align); public: diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/OPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/OPstream.H index 386781c35b..d7bf086807 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/OPstream.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/OPstream.H @@ -70,7 +70,7 @@ class OPstream inline void writeToBuffer(const char&); //- Write data to the transfer buffer - inline void writeToBuffer(const void* data, size_t count); + inline void writeToBuffer(const void* data, size_t count, size_t align); public: diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/OPstreamI.H b/src/OpenFOAM/db/IOstreams/Pstreams/OPstreamI.H index 75d40f8071..bb4eba4eb8 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/OPstreamI.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/OPstreamI.H @@ -40,7 +40,7 @@ inline void OPstream::writeToBuffer(const T& t) //(T&)(buf_[bufPosition_]) = t; //bufPosition_ += sizeof(T); - writeToBuffer(&t, sizeof(T)); + writeToBuffer(&t, sizeof(T), sizeof(T)); } @@ -56,11 +56,24 @@ inline void OPstream::writeToBuffer(const char& c) } -inline void OPstream::writeToBuffer(const void* data, size_t count) +inline void OPstream::writeToBuffer +( + const void* data, + size_t count, + size_t align +) { + label oldPos = bufPosition_; + + if (align > 1) + { + // Align bufPosition. Pads bufPosition_-oldPos characters. + bufPosition_ = align + ((bufPosition_ - 1) & ~(align - 1)); + } + if (size_t(buf_.size()) < bufPosition_ + count) { - enlargeBuffer(count); + enlargeBuffer(bufPosition_ - oldPos + count); } register char* bufPtr = &buf_[bufPosition_]; diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/OPwrite.C b/src/OpenFOAM/db/IOstreams/Pstreams/OPwrite.C index 85324b5d04..41f239c9ad 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/OPwrite.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/OPwrite.C @@ -111,7 +111,7 @@ Ostream& OPstream::write(const word& w) size_t ws = w.size(); writeToBuffer(ws); - writeToBuffer(w.c_str(), ws + 1); + writeToBuffer(w.c_str(), ws + 1, 1); return *this; } @@ -123,7 +123,7 @@ Ostream& OPstream::write(const string& s) size_t ss = s.size(); writeToBuffer(ss); - writeToBuffer(s.c_str(), ss + 1); + writeToBuffer(s.c_str(), ss + 1, 1); return *this; } @@ -168,7 +168,7 @@ Ostream& OPstream::write(const char* data, std::streamsize count) << Foam::abort(FatalError); } - writeToBuffer(data, count); + writeToBuffer(data, count, 8); return *this; }