buffer: reapply 07c0667

Original commit message:

    buffer: align chunks on 8-byte boundary

    When slicing global pool - ensure that the underlying buffer's data
    ptr is 8-byte alignment to do not ruin expectations of 3rd party C++
    addons.

    NOTE: 0.10 node.js always returned aligned pointers and io.js should
    do this too for compatibility.

PR-URL: https://github.com/nodejs/node/pull/2487
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
pull/2487/merge
Fedor Indutny 2015-08-21 12:48:10 -07:00
parent 68e53ddcba
commit 43660ad37b
1 changed files with 11 additions and 0 deletions

View File

@ -21,6 +21,15 @@ function createPool() {
} }
function alignPool() {
// Ensure aligned slices
if (poolOffset & 0x7) {
poolOffset |= 0x7;
poolOffset++;
}
}
function Buffer(arg) { function Buffer(arg) {
// Common case. // Common case.
if (typeof arg === 'number') { if (typeof arg === 'number') {
@ -66,6 +75,7 @@ function allocate(size) {
createPool(); createPool();
var b = binding.slice(allocPool, poolOffset, poolOffset + size); var b = binding.slice(allocPool, poolOffset, poolOffset + size);
poolOffset += size; poolOffset += size;
alignPool();
return b; return b;
} else { } else {
return binding.create(size); return binding.create(size);
@ -86,6 +96,7 @@ function fromString(string, encoding) {
var actual = allocPool.write(string, poolOffset, encoding); var actual = allocPool.write(string, poolOffset, encoding);
var b = binding.slice(allocPool, poolOffset, poolOffset + actual); var b = binding.slice(allocPool, poolOffset, poolOffset + actual);
poolOffset += actual; poolOffset += actual;
alignPool();
return b; return b;
} }