Chromium Code Reviews| Index: runtime/lib/byte_array.cc |
| diff --git a/runtime/lib/byte_array.cc b/runtime/lib/byte_array.cc |
| index 2f61030e7a0b75145873cab828df04bebaf4c3d1..19b99954844d30882ece7015aac26ae8ea1ae1a7 100644 |
| --- a/runtime/lib/byte_array.cc |
| +++ b/runtime/lib/byte_array.cc |
| @@ -42,6 +42,32 @@ static void LengthCheck(intptr_t len, intptr_t max) { |
| } |
| } |
| +// Allocates an aligned region of len bytes from the C heap. |
| +static void* AllocateExternalBytes(intptr_t len, intptr_t alignment = 16) { |
|
cshapiro
2012/11/28 22:28:01
Maybe stick this constant into the base class of t
|
| + intptr_t pointersize = sizeof(len); |
| + intptr_t slop = alignment+pointersize; |
|
srdjan
2012/11/28 23:30:26
please add spaces around +, - and everywhere else
|
| + void* bytes = malloc(len+slop); |
|
srdjan
2012/11/28 23:30:26
why not intptr_t* ptr = reinterpret_cast<intptr_t*
|
| + intptr_t ptr = reinterpret_cast<intptr_t>(bytes); |
| + // Move forward and then round down to the alignment boundary. |
| + ptr += slop; |
| + ptr &= (alignment-1); |
| + // Store the original address immediately before the address we return. |
| + intptr_t* address_slot = reinterpret_cast<intptr_t*>(ptr-pointersize); |
| + *address_slot = reinterpret_cast<intptr_t>(bytes); |
| + return reinterpret_cast<void*>(ptr); |
| +} |
| + |
| +// Free an aligned region of memory |
| +// that was allocated by AllocateExternalBytes. |
| +// Called as a Dart_PeerFinalizer callback. |
| +static void FinalizeExternalBytes(void* bytes) { |
| + intptr_t pointersize = sizeof(bytes); |
| + intptr_t ptr = reinterpret_cast<intptr_t>(bytes); |
|
srdjan
2012/11/28 23:30:26
ditto
|
| + intptr_t* address_slot = reinterpret_cast<intptr_t*>(ptr-pointersize); |
| + void* bytes = reinterpret_cast<void*>(*address_slot); |
| + free(bytes); |
| +} |
| + |
| #define GETTER_ARGUMENTS(ArrayT, ValueT) \ |
| GET_NATIVE_ARGUMENT(ArrayT, array, arguments->NativeArgAt(0)); \ |
| @@ -285,6 +311,18 @@ DEFINE_NATIVE_ENTRY(Int8Array_new, 1) { |
| } |
| +DEFINE_NATIVE_ENTRY(Int8Array_newTransferrable, 1) { |
| + GET_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(0)); |
| + intptr_t len = length.Value(); |
| + LengthCheck(len, Int8Array::kMaxElements); |
| + void* bytes = AllocateExternalBytes(len); |
| + return ExternalInt8Array::New(reinterpret_cast<int8_t*>(bytes), |
| + len, |
| + bytes, |
| + FinalizeExternalBytes); |
| +} |
| + |
| + |
| DEFINE_NATIVE_ENTRY(Int8Array_getIndexed, 2) { |
| GETTER(Int8Array, Smi, int8_t); |
| } |