Don't rely on seek() to write alignment bytes

This commit is contained in:
Jack Andersen 2017-03-20 11:12:53 -10:00
parent 5df07a2039
commit f3226c6102
2 changed files with 14 additions and 10 deletions

View File

@ -1870,26 +1870,19 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor>
if (alignVal) if (alignVal)
{ {
fileOut << " /* " << fieldName << " */\n"; fileOut << " /* " << fieldName << " */\n";
if (alignVal == 32) if (align.isPowerOf2())
{
if (!p)
fileOut << " " ATHENA_DNA_READER ".seekAlign32();\n";
else
fileOut << " " ATHENA_DNA_WRITER ".seekAlign32();\n";
}
else if (align.isPowerOf2())
{ {
if (!p) if (!p)
fileOut << " " ATHENA_DNA_READER ".seek((" ATHENA_DNA_READER ".position() + " << alignVal-1 << ") & ~" << alignVal-1 << ", athena::Begin);\n"; fileOut << " " ATHENA_DNA_READER ".seek((" ATHENA_DNA_READER ".position() + " << alignVal-1 << ") & ~" << alignVal-1 << ", athena::Begin);\n";
else else
fileOut << " " ATHENA_DNA_WRITER ".seek((" ATHENA_DNA_WRITER ".position() + " << alignVal-1 << ") & ~" << alignVal-1 << ", athena::Begin);\n"; fileOut << " " ATHENA_DNA_WRITER ".writeZeroTo((" ATHENA_DNA_WRITER ".position() + " << alignVal-1 << ") & ~" << alignVal-1 << ");\n";
} }
else else
{ {
if (!p) if (!p)
fileOut << " " ATHENA_DNA_READER ".seek((" ATHENA_DNA_READER ".position() + " << alignVal-1 << ") / " << alignVal << " * " << alignVal << ", athena::Begin);\n"; fileOut << " " ATHENA_DNA_READER ".seek((" ATHENA_DNA_READER ".position() + " << alignVal-1 << ") / " << alignVal << " * " << alignVal << ", athena::Begin);\n";
else else
fileOut << " " ATHENA_DNA_WRITER ".seek((" ATHENA_DNA_WRITER ".position() + " << alignVal-1 << ") / " << alignVal << " * " << alignVal << ", athena::Begin);\n"; fileOut << " " ATHENA_DNA_WRITER ".writeZeroTo((" ATHENA_DNA_WRITER ".position() + " << alignVal-1 << ") / " << alignVal << " * " << alignVal << ");\n";
} }
} }
} }

View File

@ -24,6 +24,17 @@ public:
*/ */
inline void seekAlign32() {seek(ROUND_UP_32(position()), SeekOrigin::Begin);} inline void seekAlign32() {seek(ROUND_UP_32(position()), SeekOrigin::Begin);}
/** @brief Writes zero up to specified absolute offset.<br />
*/
inline void writeZeroTo(atInt64 pos)
{
atInt64 delta = pos - position();
if (delta <= 0)
return;
for (atInt64 i=0 ; i<delta ; ++i)
writeUByte(0);
}
/** @brief Returns whether or not the stream is at the end. /** @brief Returns whether or not the stream is at the end.
* *
* @return True if at end; False otherwise. * @return True if at end; False otherwise.