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

Side by Side Diff: src/d8.cc

Issue 2520963002: [d8] Use virtual memory to allocate large array buffers. (Closed)
Patch Set: Fail for >= 2gb Created 4 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 12 matching lines...) Expand all
23 #include "include/libplatform/libplatform.h" 23 #include "include/libplatform/libplatform.h"
24 #include "include/libplatform/v8-tracing.h" 24 #include "include/libplatform/v8-tracing.h"
25 #include "src/api.h" 25 #include "src/api.h"
26 #include "src/base/cpu.h" 26 #include "src/base/cpu.h"
27 #include "src/base/debug/stack_trace.h" 27 #include "src/base/debug/stack_trace.h"
28 #include "src/base/logging.h" 28 #include "src/base/logging.h"
29 #include "src/base/platform/platform.h" 29 #include "src/base/platform/platform.h"
30 #include "src/base/sys-info.h" 30 #include "src/base/sys-info.h"
31 #include "src/basic-block-profiler.h" 31 #include "src/basic-block-profiler.h"
32 #include "src/interpreter/interpreter.h" 32 #include "src/interpreter/interpreter.h"
33 #include "src/msan.h"
33 #include "src/snapshot/natives.h" 34 #include "src/snapshot/natives.h"
34 #include "src/utils.h" 35 #include "src/utils.h"
35 #include "src/v8.h" 36 #include "src/v8.h"
36 37
37 #ifdef V8_INSPECTOR_ENABLED 38 #ifdef V8_INSPECTOR_ENABLED
38 #include "include/v8-inspector.h" 39 #include "include/v8-inspector.h"
39 #endif // V8_INSPECTOR_ENABLED 40 #endif // V8_INSPECTOR_ENABLED
40 41
41 #if !defined(_WIN32) && !defined(_WIN64) 42 #if !defined(_WIN32) && !defined(_WIN64)
42 #include <unistd.h> // NOLINT 43 #include <unistd.h> // NOLINT
(...skipping 12 matching lines...) Expand all
55 #define CHECK(condition) assert(condition) 56 #define CHECK(condition) assert(condition)
56 #endif 57 #endif
57 58
58 namespace v8 { 59 namespace v8 {
59 60
60 namespace { 61 namespace {
61 62
62 const int MB = 1024 * 1024; 63 const int MB = 1024 * 1024;
63 const int kMaxWorkers = 50; 64 const int kMaxWorkers = 50;
64 65
66 #define USE_VM 1
67 #define VM_THRESHOLD 65536
68 // TODO(titzer): allocations should fail if >= 2gb because of
69 // array buffers storing the lengths as a SMI internally.
70 #define TWO_GB (2u * 1024u * 1024u * 1024u)
65 71
66 class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator { 72 class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
67 public: 73 public:
68 virtual void* Allocate(size_t length) { 74 virtual void* Allocate(size_t length) {
75 #if USE_VM
76 if (RoundToPageSize(&length)) {
77 void* data = VirtualMemoryAllocate(length);
78 #if DEBUG
79 // In debug mode, check the memory is zero-initialized.
80 uint8_t* ptr = reinterpret_cast<uint8_t*>(data);
81 for (size_t i = 0; i < length; i++) {
82 DCHECK_EQ(0, ptr[i]);
83 }
84 #endif
85 return data;
86 }
87 #endif
69 void* data = AllocateUninitialized(length); 88 void* data = AllocateUninitialized(length);
70 return data == NULL ? data : memset(data, 0, length); 89 return data == NULL ? data : memset(data, 0, length);
71 } 90 }
72 virtual void* AllocateUninitialized(size_t length) { return malloc(length); } 91 virtual void* AllocateUninitialized(size_t length) {
73 virtual void Free(void* data, size_t) { free(data); } 92 #if USE_VM
93 if (RoundToPageSize(&length)) return VirtualMemoryAllocate(length);
94 #endif
95 return malloc(length);
96 }
97 virtual void Free(void* data, size_t length) {
98 #if USE_VM
99 if (RoundToPageSize(&length)) {
100 base::VirtualMemory::ReleaseRegion(data, length);
101 return;
102 }
103 #endif
104 free(data);
105 }
106 // If {length} is at least {VM_THRESHOLD}, round up to next page size
107 // and return {true}. Otherwise return {false}.
108 bool RoundToPageSize(size_t* length) {
109 const size_t kPageSize = base::OS::CommitPageSize();
110 if (*length >= VM_THRESHOLD && *length < TWO_GB) {
111 *length = ((*length + kPageSize - 1) / kPageSize) * kPageSize;
112 return true;
113 }
114 return false;
115 }
116 #if USE_VM
117 void* VirtualMemoryAllocate(size_t length) {
118 void* data = base::VirtualMemory::ReserveRegion(length);
119 if (data && !base::VirtualMemory::CommitRegion(data, length, false)) {
120 base::VirtualMemory::ReleaseRegion(data, length);
121 return nullptr;
122 }
123 MSAN_MEMORY_IS_INITIALIZED(data, length);
124 return data;
125 }
126 #endif
74 }; 127 };
75 128
76 129
77 class MockArrayBufferAllocator : public v8::ArrayBuffer::Allocator { 130 class MockArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
78 public: 131 public:
79 void* Allocate(size_t length) override { 132 void* Allocate(size_t length) override {
80 size_t actual_length = length > 10 * MB ? 1 : length; 133 size_t actual_length = length > 10 * MB ? 1 : length;
81 void* data = AllocateUninitialized(actual_length); 134 void* data = AllocateUninitialized(actual_length);
82 return data == NULL ? data : memset(data, 0, actual_length); 135 return data == NULL ? data : memset(data, 0, actual_length);
83 } 136 }
(...skipping 2880 matching lines...) Expand 10 before | Expand all | Expand 10 after
2964 } 3017 }
2965 3018
2966 } // namespace v8 3019 } // namespace v8
2967 3020
2968 3021
2969 #ifndef GOOGLE3 3022 #ifndef GOOGLE3
2970 int main(int argc, char* argv[]) { 3023 int main(int argc, char* argv[]) {
2971 return v8::Shell::Main(argc, argv); 3024 return v8::Shell::Main(argc, argv);
2972 } 3025 }
2973 #endif 3026 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698