Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: align chunks on 8-byte boundary #1126

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ function palloc(that, length) {
var buf = sliceOnto(allocPool, that, start, end);
poolOffset = end;

// Ensure aligned slices
if (poolOffset & 0x7) {
poolOffset |= 0x7;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious, why is this line needed if the condition already succeeded?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. poolOffset = 1
  2. poolOffset & 0x7 === 1
  3. poolOffset |= 0x7 => 0x7
  4. poolOffset++ => 0x8
  5. Voila, poolOffset is aligned!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mscdex in other words, I'm just setting all bits that are lower than alignment value and increment the number to align it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A less convoluted way of writing that is poolOffset = (poolOffset & ~7) + 8 :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used something like this: poolOffset += 8 - poolOffset % 8; :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I do this to for variable-size alignment or just non-power-of-two.

poolOffset++;
}

return buf;
}

Expand Down