1 // Copyright 2021 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #include "fxjs/cfx_v8_array_buffer_allocator.h" 8 9 #include "core/fxcrt/fx_memory.h" 10 11 CFX_V8ArrayBufferAllocator::CFX_V8ArrayBufferAllocator() = default; 12 13 CFX_V8ArrayBufferAllocator::~CFX_V8ArrayBufferAllocator() = default; 14 15 // NOTE: Under V8 sandbox mode, defer NewDefaultAllocator() call until 16 // first use, since V8 must be initialized first for it to succeed, but 17 // we need the allocator in order to initialize V8. 18 Allocate(size_t length)19void* CFX_V8ArrayBufferAllocator::Allocate(size_t length) { 20 if (length > kMaxAllowedBytes) 21 return nullptr; 22 #ifdef V8_ENABLE_SANDBOX 23 if (!wrapped_) { 24 wrapped_.reset(v8::ArrayBuffer::Allocator::NewDefaultAllocator()); 25 } 26 return wrapped_->Allocate(length); 27 #else // V8_ENABLE_SANDBOX 28 return FX_ArrayBufferAllocate(length); 29 #endif // V8_ENABLE_SANDBOX 30 } 31 AllocateUninitialized(size_t length)32void* CFX_V8ArrayBufferAllocator::AllocateUninitialized(size_t length) { 33 if (length > kMaxAllowedBytes) 34 return nullptr; 35 #ifdef V8_ENABLE_SANDBOX 36 if (!wrapped_) { 37 wrapped_.reset(v8::ArrayBuffer::Allocator::NewDefaultAllocator()); 38 } 39 return wrapped_->AllocateUninitialized(length); 40 #else // V8_ENABLE_SANDBOX 41 return FX_ArrayBufferAllocateUninitialized(length); 42 #endif 43 } 44 Free(void * data,size_t length)45void CFX_V8ArrayBufferAllocator::Free(void* data, size_t length) { 46 #ifdef V8_ENABLE_SANDBOX 47 if (wrapped_) { 48 wrapped_->Free(data, length); 49 } 50 #else // V8_ENABLE_SANDBOX 51 FX_ArrayBufferFree(data); 52 #endif 53 } 54