 Chromium Code Reviews
 Chromium Code Reviews Issue 2520963002:
  [d8] Use virtual memory to allocate large array buffers.  (Closed)
    
  
    Issue 2520963002:
  [d8] Use virtual memory to allocate large array buffers.  (Closed) 
  | 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 #define CHECK(condition) assert(condition) | 55 #define CHECK(condition) assert(condition) | 
| 56 #endif | 56 #endif | 
| 57 | 57 | 
| 58 namespace v8 { | 58 namespace v8 { | 
| 59 | 59 | 
| 60 namespace { | 60 namespace { | 
| 61 | 61 | 
| 62 const int MB = 1024 * 1024; | 62 const int MB = 1024 * 1024; | 
| 63 const int kMaxWorkers = 50; | 63 const int kMaxWorkers = 50; | 
| 64 | 64 | 
| 65 #define USE_VM 1 | |
| 66 #define VM_THRESHOLD 65536 | |
| 65 | 67 | 
| 66 class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | 68 class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | 
| 67 public: | 69 public: | 
| 68 virtual void* Allocate(size_t length) { | 70 virtual void* Allocate(size_t length) { | 
| 71 #if USE_VM | |
| 72 if (RoundToPageSize(&length)) { | |
| 73 void* data = base::VirtualMemory::ReserveRegion(length); | |
| 74 if (data && !base::VirtualMemory::CommitRegion(data, length, false)) { | |
| 
Michael Starzinger
2016/11/21 13:58:09
This relies on the mamory being provided by the un
 
Michael Starzinger
2016/11/21 13:59:33
Same for the other *SANs, not sure which one it is
 | |
| 75 base::VirtualMemory::ReleaseRegion(data, length); | |
| 76 data = nullptr; | |
| 77 } | |
| 78 return data; | |
| 79 } | |
| 80 #endif | |
| 
Clemens Hammacher
2016/11/21 14:00:28
The code duplication bothers me. We call AllocateU
 | |
| 69 void* data = AllocateUninitialized(length); | 81 void* data = AllocateUninitialized(length); | 
| 70 return data == NULL ? data : memset(data, 0, length); | 82 return data == NULL ? data : memset(data, 0, length); | 
| 71 } | 83 } | 
| 72 virtual void* AllocateUninitialized(size_t length) { return malloc(length); } | 84 virtual void* AllocateUninitialized(size_t length) { | 
| 73 virtual void Free(void* data, size_t) { free(data); } | 85 #if USE_VM | 
| 86 if (RoundToPageSize(&length)) { | |
| 87 void* data = base::VirtualMemory::ReserveRegion(length); | |
| 88 if (data && !base::VirtualMemory::CommitRegion(data, length, false)) { | |
| 89 base::VirtualMemory::ReleaseRegion(data, length); | |
| 90 data = nullptr; | |
| 91 } | |
| 92 return data; | |
| 93 } | |
| 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 a page, round up to next page size and return | |
| 
Clemens Hammacher
2016/11/21 14:00:28
... at least VM_THRESHOLD, ...
 | |
| 107 // {true}. | |
| 108 // Otherwise return {false}. | |
| 109 bool RoundToPageSize(size_t* length) { | |
| 110 const size_t kPageSize = base::OS::CommitPageSize(); | |
| 111 if (*length >= VM_THRESHOLD) { | |
| 112 *length = ((*length + kPageSize - 1) / kPageSize) * kPageSize; | |
| 113 return true; | |
| 114 } | |
| 115 return false; | |
| 116 } | |
| 74 }; | 117 }; | 
| 75 | 118 | 
| 76 | 119 | 
| 77 class MockArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | 120 class MockArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | 
| 78 public: | 121 public: | 
| 79 void* Allocate(size_t length) override { | 122 void* Allocate(size_t length) override { | 
| 80 size_t actual_length = length > 10 * MB ? 1 : length; | 123 size_t actual_length = length > 10 * MB ? 1 : length; | 
| 81 void* data = AllocateUninitialized(actual_length); | 124 void* data = AllocateUninitialized(actual_length); | 
| 82 return data == NULL ? data : memset(data, 0, actual_length); | 125 return data == NULL ? data : memset(data, 0, actual_length); | 
| 83 } | 126 } | 
| (...skipping 2880 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2964 } | 3007 } | 
| 2965 | 3008 | 
| 2966 } // namespace v8 | 3009 } // namespace v8 | 
| 2967 | 3010 | 
| 2968 | 3011 | 
| 2969 #ifndef GOOGLE3 | 3012 #ifndef GOOGLE3 | 
| 2970 int main(int argc, char* argv[]) { | 3013 int main(int argc, char* argv[]) { | 
| 2971 return v8::Shell::Main(argc, argv); | 3014 return v8::Shell::Main(argc, argv); | 
| 2972 } | 3015 } | 
| 2973 #endif | 3016 #endif | 
| OLD | NEW |