Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Side by Side Diff: runtime/lib/byte_array.cc

Issue 11414211: Expose transferable constructor to Int8List (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/lib/byte_array.dart » ('j') | runtime/lib/byte_array.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/bootstrap_natives.h" 5 #include "vm/bootstrap_natives.h"
6 6
7 #include "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/exceptions.h" 8 #include "vm/exceptions.h"
9 #include "vm/native_entry.h" 9 #include "vm/native_entry.h"
10 #include "vm/object.h" 10 #include "vm/object.h"
(...skipping 24 matching lines...) Expand all
35 static void LengthCheck(intptr_t len, intptr_t max) { 35 static void LengthCheck(intptr_t len, intptr_t max) {
36 if (len < 0 || len > max) { 36 if (len < 0 || len > max) {
37 const String& error = String::Handle(String::NewFormatted( 37 const String& error = String::Handle(String::NewFormatted(
38 "length (%"Pd") must be in the range [0..%"Pd"]", len, max)); 38 "length (%"Pd") must be in the range [0..%"Pd"]", len, max));
39 GrowableArray<const Object*> args; 39 GrowableArray<const Object*> args;
40 args.Add(&error); 40 args.Add(&error);
41 Exceptions::ThrowByType(Exceptions::kArgument, args); 41 Exceptions::ThrowByType(Exceptions::kArgument, args);
42 } 42 }
43 } 43 }
44 44
45 // Allocates an aligned region of len bytes from the C heap.
46 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
47 intptr_t pointersize = sizeof(len);
48 intptr_t slop = alignment+pointersize;
srdjan 2012/11/28 23:30:26 please add spaces around +, - and everywhere else
49 void* bytes = malloc(len+slop);
srdjan 2012/11/28 23:30:26 why not intptr_t* ptr = reinterpret_cast<intptr_t*
50 intptr_t ptr = reinterpret_cast<intptr_t>(bytes);
51 // Move forward and then round down to the alignment boundary.
52 ptr += slop;
53 ptr &= (alignment-1);
54 // Store the original address immediately before the address we return.
55 intptr_t* address_slot = reinterpret_cast<intptr_t*>(ptr-pointersize);
56 *address_slot = reinterpret_cast<intptr_t>(bytes);
57 return reinterpret_cast<void*>(ptr);
58 }
59
60 // Free an aligned region of memory
61 // that was allocated by AllocateExternalBytes.
62 // Called as a Dart_PeerFinalizer callback.
63 static void FinalizeExternalBytes(void* bytes) {
64 intptr_t pointersize = sizeof(bytes);
65 intptr_t ptr = reinterpret_cast<intptr_t>(bytes);
srdjan 2012/11/28 23:30:26 ditto
66 intptr_t* address_slot = reinterpret_cast<intptr_t*>(ptr-pointersize);
67 void* bytes = reinterpret_cast<void*>(*address_slot);
68 free(bytes);
69 }
70
45 71
46 #define GETTER_ARGUMENTS(ArrayT, ValueT) \ 72 #define GETTER_ARGUMENTS(ArrayT, ValueT) \
47 GET_NATIVE_ARGUMENT(ArrayT, array, arguments->NativeArgAt(0)); \ 73 GET_NATIVE_ARGUMENT(ArrayT, array, arguments->NativeArgAt(0)); \
48 GET_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); 74 GET_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1));
49 75
50 76
51 #define SETTER_ARGUMENTS(ArrayT, ObjectT, ValueT) \ 77 #define SETTER_ARGUMENTS(ArrayT, ObjectT, ValueT) \
52 GET_NATIVE_ARGUMENT(ArrayT, array, arguments->NativeArgAt(0)); \ 78 GET_NATIVE_ARGUMENT(ArrayT, array, arguments->NativeArgAt(0)); \
53 GET_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); \ 79 GET_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); \
54 GET_NATIVE_ARGUMENT(ObjectT, value_object, arguments->NativeArgAt(2)); 80 GET_NATIVE_ARGUMENT(ObjectT, value_object, arguments->NativeArgAt(2));
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 // Int8Array 304 // Int8Array
279 305
280 DEFINE_NATIVE_ENTRY(Int8Array_new, 1) { 306 DEFINE_NATIVE_ENTRY(Int8Array_new, 1) {
281 GET_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(0)); 307 GET_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(0));
282 intptr_t len = length.Value(); 308 intptr_t len = length.Value();
283 LengthCheck(len, Int8Array::kMaxElements); 309 LengthCheck(len, Int8Array::kMaxElements);
284 return Int8Array::New(len); 310 return Int8Array::New(len);
285 } 311 }
286 312
287 313
314 DEFINE_NATIVE_ENTRY(Int8Array_newTransferrable, 1) {
315 GET_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(0));
316 intptr_t len = length.Value();
317 LengthCheck(len, Int8Array::kMaxElements);
318 void* bytes = AllocateExternalBytes(len);
319 return ExternalInt8Array::New(reinterpret_cast<int8_t*>(bytes),
320 len,
321 bytes,
322 FinalizeExternalBytes);
323 }
324
325
288 DEFINE_NATIVE_ENTRY(Int8Array_getIndexed, 2) { 326 DEFINE_NATIVE_ENTRY(Int8Array_getIndexed, 2) {
289 GETTER(Int8Array, Smi, int8_t); 327 GETTER(Int8Array, Smi, int8_t);
290 } 328 }
291 329
292 330
293 DEFINE_NATIVE_ENTRY(Int8Array_setIndexed, 3) { 331 DEFINE_NATIVE_ENTRY(Int8Array_setIndexed, 3) {
294 SETTER(Int8Array, Smi, Value, int8_t); 332 SETTER(Int8Array, Smi, Value, int8_t);
295 } 333 }
296 334
297 335
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 DEFINE_NATIVE_ENTRY(ExternalFloat64Array_getIndexed, 2) { 626 DEFINE_NATIVE_ENTRY(ExternalFloat64Array_getIndexed, 2) {
589 GETTER(ExternalFloat64Array, Double, double); 627 GETTER(ExternalFloat64Array, Double, double);
590 } 628 }
591 629
592 630
593 DEFINE_NATIVE_ENTRY(ExternalFloat64Array_setIndexed, 3) { 631 DEFINE_NATIVE_ENTRY(ExternalFloat64Array_setIndexed, 3) {
594 SETTER(ExternalFloat64Array, Double, value, double); 632 SETTER(ExternalFloat64Array, Double, value, double);
595 } 633 }
596 634
597 } // namespace dart 635 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/byte_array.dart » ('j') | runtime/lib/byte_array.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698