OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 640 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 return isolate->heap()->undefined_value(); | 651 return isolate->heap()->undefined_value(); |
652 } | 652 } |
653 | 653 |
654 | 654 |
655 static void ArrayBufferWeakCallback(v8::Isolate* external_isolate, | 655 static void ArrayBufferWeakCallback(v8::Isolate* external_isolate, |
656 Persistent<Value>* object, | 656 Persistent<Value>* object, |
657 void* data) { | 657 void* data) { |
658 Isolate* isolate = reinterpret_cast<Isolate*>(external_isolate); | 658 Isolate* isolate = reinterpret_cast<Isolate*>(external_isolate); |
659 HandleScope scope(isolate); | 659 HandleScope scope(isolate); |
660 Handle<Object> internal_object = Utils::OpenHandle(**object); | 660 Handle<Object> internal_object = Utils::OpenHandle(**object); |
| 661 Handle<JSArrayBuffer> array_buffer(JSArrayBuffer::cast(*internal_object)); |
661 | 662 |
662 size_t allocated_length = NumberToSize( | 663 if (!array_buffer->is_external()) { |
663 isolate, JSArrayBuffer::cast(*internal_object)->byte_length()); | 664 size_t allocated_length = NumberToSize( |
664 isolate->heap()->AdjustAmountOfExternalAllocatedMemory( | 665 isolate, array_buffer->byte_length()); |
665 -static_cast<intptr_t>(allocated_length)); | 666 isolate->heap()->AdjustAmountOfExternalAllocatedMemory( |
666 if (data != NULL) | 667 -static_cast<intptr_t>(allocated_length)); |
667 free(data); | 668 free(data); |
| 669 } |
668 object->Dispose(external_isolate); | 670 object->Dispose(external_isolate); |
669 } | 671 } |
670 | 672 |
671 | 673 |
672 bool Runtime::SetupArrayBuffer(Isolate* isolate, | 674 void Runtime::SetupArrayBuffer(Isolate* isolate, |
673 Handle<JSArrayBuffer> array_buffer, | 675 Handle<JSArrayBuffer> array_buffer, |
| 676 bool is_external, |
674 void* data, | 677 void* data, |
675 size_t allocated_length) { | 678 size_t allocated_length) { |
| 679 ASSERT(array_buffer->GetInternalFieldCount() == |
| 680 v8::ArrayBuffer::kInternalFieldCount); |
| 681 for (int i = 0; i < v8::ArrayBuffer::kInternalFieldCount; i++) { |
| 682 array_buffer->SetInternalField(i, Smi::FromInt(0)); |
| 683 } |
676 array_buffer->set_backing_store(data); | 684 array_buffer->set_backing_store(data); |
| 685 array_buffer->set_flag(Smi::FromInt(0)); |
| 686 array_buffer->set_is_external(is_external); |
677 | 687 |
678 Handle<Object> byte_length = | 688 Handle<Object> byte_length = |
679 isolate->factory()->NewNumberFromSize(allocated_length); | 689 isolate->factory()->NewNumberFromSize(allocated_length); |
680 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); | 690 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); |
681 array_buffer->set_byte_length(*byte_length); | 691 array_buffer->set_byte_length(*byte_length); |
682 return true; | |
683 } | 692 } |
684 | 693 |
685 | 694 |
686 bool Runtime::SetupArrayBufferAllocatingData( | 695 bool Runtime::SetupArrayBufferAllocatingData( |
687 Isolate* isolate, | 696 Isolate* isolate, |
688 Handle<JSArrayBuffer> array_buffer, | 697 Handle<JSArrayBuffer> array_buffer, |
689 size_t allocated_length) { | 698 size_t allocated_length) { |
690 void* data; | 699 void* data; |
691 if (allocated_length != 0) { | 700 if (allocated_length != 0) { |
692 data = malloc(allocated_length); | 701 data = malloc(allocated_length); |
693 if (data == NULL) return false; | 702 if (data == NULL) return false; |
694 memset(data, 0, allocated_length); | 703 memset(data, 0, allocated_length); |
695 } else { | 704 } else { |
696 data = NULL; | 705 data = NULL; |
697 } | 706 } |
698 | 707 |
699 if (!SetupArrayBuffer(isolate, array_buffer, data, allocated_length)) | 708 SetupArrayBuffer(isolate, array_buffer, false, data, allocated_length); |
700 return false; | |
701 | 709 |
702 v8::Isolate* external_isolate = reinterpret_cast<v8::Isolate*>(isolate); | 710 v8::Isolate* external_isolate = reinterpret_cast<v8::Isolate*>(isolate); |
703 v8::Persistent<v8::Value> weak_handle = v8::Persistent<v8::Value>::New( | 711 v8::Persistent<v8::Value> weak_handle = v8::Persistent<v8::Value>::New( |
704 external_isolate, v8::Utils::ToLocal(Handle<Object>::cast(array_buffer))); | 712 external_isolate, v8::Utils::ToLocal(Handle<Object>::cast(array_buffer))); |
705 weak_handle.MakeWeak(external_isolate, data, ArrayBufferWeakCallback); | 713 weak_handle.MakeWeak(external_isolate, data, ArrayBufferWeakCallback); |
706 weak_handle.MarkIndependent(external_isolate); | 714 weak_handle.MarkIndependent(external_isolate); |
707 isolate->heap()->AdjustAmountOfExternalAllocatedMemory(allocated_length); | 715 isolate->heap()->AdjustAmountOfExternalAllocatedMemory(allocated_length); |
708 | 716 |
709 return true; | 717 return true; |
710 } | 718 } |
(...skipping 12793 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
13504 // Handle last resort GC and make sure to allow future allocations | 13512 // Handle last resort GC and make sure to allow future allocations |
13505 // to grow the heap without causing GCs (if possible). | 13513 // to grow the heap without causing GCs (if possible). |
13506 isolate->counters()->gc_last_resort_from_js()->Increment(); | 13514 isolate->counters()->gc_last_resort_from_js()->Increment(); |
13507 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, | 13515 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, |
13508 "Runtime::PerformGC"); | 13516 "Runtime::PerformGC"); |
13509 } | 13517 } |
13510 } | 13518 } |
13511 | 13519 |
13512 | 13520 |
13513 } } // namespace v8::internal | 13521 } } // namespace v8::internal |
OLD | NEW |