1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GIN_ARRAY_BUFFER_H_
6 #define GIN_ARRAY_BUFFER_H_
11 #include "base/allocator/partition_allocator/src/partition_alloc/partition_alloc.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/shared_memory_mapper.h"
14 #include "gin/converter.h"
15 #include "gin/gin_export.h"
16 #include "v8/include/v8-array-buffer.h"
17 #include "v8/include/v8-forward.h"
21 class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
23 void* Allocate(size_t length) override;
24 void* AllocateUninitialized(size_t length) override;
25 void Free(void* data, size_t length) override;
27 GIN_EXPORT static ArrayBufferAllocator* SharedInstance();
30 friend class V8Initializer;
32 template <partition_alloc::AllocFlags flags>
33 void* AllocateInternal(size_t length);
35 // Initialize the PartitionAlloc partition from which instances of this class
36 // allocate memory. This is called after initializing V8 since, when enabled,
37 // the V8 sandbox must be initialized first.
38 static void InitializePartition();
40 // The PartitionAlloc partition that instances of this class allocate memory
41 // chunks from. When the V8 sandbox is enabled, this partition must be placed
42 // inside of it. For that, PA's ConfigurablePool is created inside the V8
43 // sandbox during initialization of V8, and this partition is then placed
44 // inside the configurable pool during InitializePartition().
45 static partition_alloc::PartitionRoot* partition_;
48 class GIN_EXPORT ArrayBuffer {
51 ArrayBuffer(v8::Isolate* isolate, v8::Local<v8::ArrayBuffer> buffer);
52 ArrayBuffer(const ArrayBuffer&) = delete;
54 ArrayBuffer& operator=(const ArrayBuffer& other);
57 return backing_store_ ? backing_store_->Data() : nullptr;
59 size_t num_bytes() const {
60 return backing_store_ ? backing_store_->ByteLength() : 0;
64 std::shared_ptr<v8::BackingStore> backing_store_;
68 struct GIN_EXPORT Converter<ArrayBuffer> {
69 static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
73 class GIN_EXPORT ArrayBufferView {
76 ArrayBufferView(v8::Isolate* isolate, v8::Local<v8::ArrayBufferView> view);
77 ArrayBufferView(const ArrayBufferView&) = delete;
79 ArrayBufferView& operator=(const ArrayBufferView& other);
82 return static_cast<uint8_t*>(array_buffer_.bytes()) + offset_;
84 size_t num_bytes() const { return num_bytes_; }
87 ArrayBuffer array_buffer_;
93 struct GIN_EXPORT Converter<ArrayBufferView> {
94 static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
95 ArrayBufferView* out);
98 GIN_EXPORT base::SharedMemoryMapper* GetSharedMemoryMapperForArrayBuffers();
102 #endif // GIN_ARRAY_BUFFER_H_