Revert "[M120 Migration]Fix for crash during chrome exit"
[platform/framework/web/chromium-efl.git] / gin / array_buffer.h
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.
4
5 #ifndef GIN_ARRAY_BUFFER_H_
6 #define GIN_ARRAY_BUFFER_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
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"
18
19 namespace gin {
20
21 class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
22  public:
23   void* Allocate(size_t length) override;
24   void* AllocateUninitialized(size_t length) override;
25   void Free(void* data, size_t length) override;
26
27   GIN_EXPORT static ArrayBufferAllocator* SharedInstance();
28
29  private:
30   friend class V8Initializer;
31
32   template <partition_alloc::AllocFlags flags>
33   void* AllocateInternal(size_t length);
34
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();
39
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_;
46 };
47
48 class GIN_EXPORT ArrayBuffer {
49  public:
50   ArrayBuffer();
51   ArrayBuffer(v8::Isolate* isolate, v8::Local<v8::ArrayBuffer> buffer);
52   ArrayBuffer(const ArrayBuffer&) = delete;
53   ~ArrayBuffer();
54   ArrayBuffer& operator=(const ArrayBuffer& other);
55
56   void* bytes() const {
57     return backing_store_ ? backing_store_->Data() : nullptr;
58   }
59   size_t num_bytes() const {
60     return backing_store_ ? backing_store_->ByteLength() : 0;
61   }
62
63  private:
64   std::shared_ptr<v8::BackingStore> backing_store_;
65 };
66
67 template<>
68 struct GIN_EXPORT Converter<ArrayBuffer> {
69   static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
70                      ArrayBuffer* out);
71 };
72
73 class GIN_EXPORT ArrayBufferView {
74  public:
75   ArrayBufferView();
76   ArrayBufferView(v8::Isolate* isolate, v8::Local<v8::ArrayBufferView> view);
77   ArrayBufferView(const ArrayBufferView&) = delete;
78   ~ArrayBufferView();
79   ArrayBufferView& operator=(const ArrayBufferView& other);
80
81   void* bytes() const {
82     return static_cast<uint8_t*>(array_buffer_.bytes()) + offset_;
83   }
84   size_t num_bytes() const { return num_bytes_; }
85
86  private:
87   ArrayBuffer array_buffer_;
88   size_t offset_;
89   size_t num_bytes_;
90 };
91
92 template<>
93 struct GIN_EXPORT Converter<ArrayBufferView> {
94   static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
95                      ArrayBufferView* out);
96 };
97
98 GIN_EXPORT base::SharedMemoryMapper* GetSharedMemoryMapperForArrayBuffers();
99
100 }  // namespace gin
101
102 #endif  // GIN_ARRAY_BUFFER_H_