OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <errno.h> | 5 #include <errno.h> |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 #include <string.h> | 7 #include <string.h> |
8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 #ifndef CHECK | 59 #ifndef CHECK |
60 #define CHECK(condition) assert(condition) | 60 #define CHECK(condition) assert(condition) |
61 #endif | 61 #endif |
62 | 62 |
63 namespace v8 { | 63 namespace v8 { |
64 | 64 |
65 namespace { | 65 namespace { |
66 | 66 |
67 const int MB = 1024 * 1024; | 67 const int MB = 1024 * 1024; |
68 const int kMaxWorkers = 50; | 68 const int kMaxWorkers = 50; |
| 69 const int kMaxSerializerMemoryUsage = 1 * MB; // Arbitrary maximum for testing. |
69 | 70 |
70 #define USE_VM 1 | 71 #define USE_VM 1 |
71 #define VM_THRESHOLD 65536 | 72 #define VM_THRESHOLD 65536 |
72 // TODO(titzer): allocations should fail if >= 2gb because of | 73 // TODO(titzer): allocations should fail if >= 2gb because of |
73 // array buffers storing the lengths as a SMI internally. | 74 // array buffers storing the lengths as a SMI internally. |
74 #define TWO_GB (2u * 1024u * 1024u * 1024u) | 75 #define TWO_GB (2u * 1024u * 1024u * 1024u) |
75 | 76 |
76 class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | 77 class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator { |
77 public: | 78 public: |
78 virtual void* Allocate(size_t length) { | 79 virtual void* Allocate(size_t length) { |
(...skipping 2481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2560 if (!i::FLAG_verify_predictable) { | 2561 if (!i::FLAG_verify_predictable) { |
2561 while (v8::platform::PumpMessageLoop(g_platform, isolate)) continue; | 2562 while (v8::platform::PumpMessageLoop(g_platform, isolate)) continue; |
2562 v8::platform::RunIdleTasks(g_platform, isolate, | 2563 v8::platform::RunIdleTasks(g_platform, isolate, |
2563 50.0 / base::Time::kMillisecondsPerSecond); | 2564 50.0 / base::Time::kMillisecondsPerSecond); |
2564 } | 2565 } |
2565 } | 2566 } |
2566 | 2567 |
2567 class Serializer : public ValueSerializer::Delegate { | 2568 class Serializer : public ValueSerializer::Delegate { |
2568 public: | 2569 public: |
2569 explicit Serializer(Isolate* isolate) | 2570 explicit Serializer(Isolate* isolate) |
2570 : isolate_(isolate), serializer_(isolate, this) {} | 2571 : isolate_(isolate), |
| 2572 serializer_(isolate, this), |
| 2573 current_memory_usage_(0) {} |
2571 | 2574 |
2572 Maybe<bool> WriteValue(Local<Context> context, Local<Value> value, | 2575 Maybe<bool> WriteValue(Local<Context> context, Local<Value> value, |
2573 Local<Value> transfer) { | 2576 Local<Value> transfer) { |
2574 bool ok; | 2577 bool ok; |
2575 DCHECK(!data_); | 2578 DCHECK(!data_); |
2576 data_.reset(new SerializationData); | 2579 data_.reset(new SerializationData); |
2577 if (!PrepareTransfer(context, transfer).To(&ok)) { | 2580 if (!PrepareTransfer(context, transfer).To(&ok)) { |
2578 return Nothing<bool>(); | 2581 return Nothing<bool>(); |
2579 } | 2582 } |
2580 serializer_.WriteHeader(); | 2583 serializer_.WriteHeader(); |
(...skipping 30 matching lines...) Expand all Loading... |
2611 } | 2614 } |
2612 } | 2615 } |
2613 | 2616 |
2614 size_t index = shared_array_buffers_.size(); | 2617 size_t index = shared_array_buffers_.size(); |
2615 shared_array_buffers_.emplace_back(isolate_, shared_array_buffer); | 2618 shared_array_buffers_.emplace_back(isolate_, shared_array_buffer); |
2616 return Just<uint32_t>(static_cast<uint32_t>(index)); | 2619 return Just<uint32_t>(static_cast<uint32_t>(index)); |
2617 } | 2620 } |
2618 | 2621 |
2619 void* ReallocateBufferMemory(void* old_buffer, size_t size, | 2622 void* ReallocateBufferMemory(void* old_buffer, size_t size, |
2620 size_t* actual_size) override { | 2623 size_t* actual_size) override { |
| 2624 // Not accurate, because we don't take into account reallocated buffers, |
| 2625 // but this is fine for testing. |
| 2626 current_memory_usage_ += size; |
| 2627 if (current_memory_usage_ > kMaxSerializerMemoryUsage) return nullptr; |
| 2628 |
2621 void* result = realloc(old_buffer, size); | 2629 void* result = realloc(old_buffer, size); |
2622 *actual_size = result ? size : 0; | 2630 *actual_size = result ? size : 0; |
2623 return result; | 2631 return result; |
2624 } | 2632 } |
2625 | 2633 |
2626 void FreeBufferMemory(void* buffer) override { free(buffer); } | 2634 void FreeBufferMemory(void* buffer) override { free(buffer); } |
2627 | 2635 |
2628 private: | 2636 private: |
2629 Maybe<bool> PrepareTransfer(Local<Context> context, Local<Value> transfer) { | 2637 Maybe<bool> PrepareTransfer(Local<Context> context, Local<Value> transfer) { |
2630 if (transfer->IsArray()) { | 2638 if (transfer->IsArray()) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2688 } | 2696 } |
2689 | 2697 |
2690 return Just(true); | 2698 return Just(true); |
2691 } | 2699 } |
2692 | 2700 |
2693 Isolate* isolate_; | 2701 Isolate* isolate_; |
2694 ValueSerializer serializer_; | 2702 ValueSerializer serializer_; |
2695 std::unique_ptr<SerializationData> data_; | 2703 std::unique_ptr<SerializationData> data_; |
2696 std::vector<Global<ArrayBuffer>> array_buffers_; | 2704 std::vector<Global<ArrayBuffer>> array_buffers_; |
2697 std::vector<Global<SharedArrayBuffer>> shared_array_buffers_; | 2705 std::vector<Global<SharedArrayBuffer>> shared_array_buffers_; |
| 2706 size_t current_memory_usage_; |
2698 | 2707 |
2699 DISALLOW_COPY_AND_ASSIGN(Serializer); | 2708 DISALLOW_COPY_AND_ASSIGN(Serializer); |
2700 }; | 2709 }; |
2701 | 2710 |
2702 class Deserializer : public ValueDeserializer::Delegate { | 2711 class Deserializer : public ValueDeserializer::Delegate { |
2703 public: | 2712 public: |
2704 Deserializer(Isolate* isolate, std::unique_ptr<SerializationData> data) | 2713 Deserializer(Isolate* isolate, std::unique_ptr<SerializationData> data) |
2705 : isolate_(isolate), | 2714 : isolate_(isolate), |
2706 deserializer_(isolate, data->data(), data->size(), this), | 2715 deserializer_(isolate, data->data(), data->size(), this), |
2707 data_(std::move(data)) { | 2716 data_(std::move(data)) { |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2997 } | 3006 } |
2998 | 3007 |
2999 } // namespace v8 | 3008 } // namespace v8 |
3000 | 3009 |
3001 | 3010 |
3002 #ifndef GOOGLE3 | 3011 #ifndef GOOGLE3 |
3003 int main(int argc, char* argv[]) { | 3012 int main(int argc, char* argv[]) { |
3004 return v8::Shell::Main(argc, argv); | 3013 return v8::Shell::Main(argc, argv); |
3005 } | 3014 } |
3006 #endif | 3015 #endif |
OLD | NEW |