@@ -51,6 +51,10 @@ function checkPosition(pos, name) {
51
51
}
52
52
}
53
53
54
+ function roundUpToMultipleOf8 ( n ) {
55
+ return ( n + 7 ) & ~ 7 ; // Align to 8 byte boundary.
56
+ }
57
+
54
58
function ReadStream ( path , options ) {
55
59
if ( ! ( this instanceof ReadStream ) )
56
60
return new ReadStream ( path , options ) ;
@@ -172,10 +176,18 @@ ReadStream.prototype._read = function(n) {
172
176
// Now that we know how much data we have actually read, re-wind the
173
177
// 'used' field if we can, and otherwise allow the remainder of our
174
178
// reservation to be used as a new pool later.
175
- if ( start + toRead === thisPool . used && thisPool === pool )
176
- thisPool . used += bytesRead - toRead ;
177
- else if ( toRead - bytesRead > kMinPoolSpace )
178
- poolFragments . push ( thisPool . slice ( start + bytesRead , start + toRead ) ) ;
179
+ if ( start + toRead === thisPool . used && thisPool === pool ) {
180
+ const newUsed = thisPool . used + bytesRead - toRead ;
181
+ thisPool . used = roundUpToMultipleOf8 ( newUsed ) ;
182
+ } else {
183
+ // Round down to the next lowest multiple of 8 to ensure the new pool
184
+ // fragment start and end positions are aligned to an 8 byte boundary.
185
+ const alignedEnd = ( start + toRead ) & ~ 7 ;
186
+ const alignedStart = roundUpToMultipleOf8 ( start + bytesRead ) ;
187
+ if ( alignedEnd - alignedStart >= kMinPoolSpace ) {
188
+ poolFragments . push ( thisPool . slice ( alignedStart , alignedEnd ) ) ;
189
+ }
190
+ }
179
191
180
192
if ( bytesRead > 0 ) {
181
193
this . bytesRead += bytesRead ;
@@ -189,7 +201,8 @@ ReadStream.prototype._read = function(n) {
189
201
// Move the pool positions, and internal position for reading.
190
202
if ( this . pos !== undefined )
191
203
this . pos += toRead ;
192
- pool . used += toRead ;
204
+
205
+ pool . used = roundUpToMultipleOf8 ( pool . used + toRead ) ;
193
206
} ;
194
207
195
208
ReadStream . prototype . _destroy = function ( err , cb ) {
0 commit comments