Loading...
Searching...
No Matches
v8-array-buffer.h
Go to the documentation of this file.
1// Copyright 2021 the V8 project authors. All rights reserved.
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 INCLUDE_V8_ARRAY_BUFFER_H_
6#define INCLUDE_V8_ARRAY_BUFFER_H_
7
8#include <stddef.h>
9
10#include <memory>
11
12#include "v8-local-handle.h" // NOLINT(build/include_directory)
13#include "v8-memory-span.h" // NOLINT(build/include_directory)
14#include "v8-object.h" // NOLINT(build/include_directory)
15#include "v8-platform.h" // NOLINT(build/include_directory)
16#include "v8config.h" // NOLINT(build/include_directory)
17
18namespace v8 {
19
20class SharedArrayBuffer;
21
22#if defined(V8_COMPRESS_POINTERS) && \
23 !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
24class IsolateGroup;
25#endif
26
27#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
28// Defined using gn arg `v8_array_buffer_internal_field_count`.
29#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
30#endif
31
35
50 public:
52
58 void* Data() const;
59
63 size_t ByteLength() const;
64
72 size_t MaxByteLength() const;
73
78 bool IsShared() const;
79
86
92 void operator delete(void* ptr) { ::operator delete(ptr); }
93
99 using DeleterCallback = void (*)(void* data, size_t length,
100 void* deleter_data);
101
111 static void EmptyDeleter(void* data, size_t length, void* deleter_data);
112
113 private:
118 BackingStore();
119};
120
121#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
122// Use v8::BackingStore::DeleterCallback instead.
123using BackingStoreDeleterCallback = void (*)(void* data, size_t length,
124 void* deleter_data);
125
126#endif
127
132 public:
149 public:
150 virtual ~Allocator() = default;
151
156 virtual void* Allocate(size_t length) = 0;
157
162 virtual void* AllocateUninitialized(size_t length) = 0;
163
168 virtual void Free(void* data, size_t length) = 0;
169
177 virtual size_t MaxAllocationSize() const { return kMaxByteLength; }
178
184 enum class AllocationMode { kNormal, kReservation };
185
193 virtual PageAllocator* GetPageAllocator() { return nullptr; }
194
195#if defined(V8_COMPRESS_POINTERS) && \
196 !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
207 static Allocator* NewDefaultAllocator(const IsolateGroup& group);
208#endif // defined(V8_COMPRESS_POINTERS) &&
209 // !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
210
222 };
223
227 size_t ByteLength() const;
228
232 size_t MaxByteLength() const;
233
242 Isolate* isolate, size_t byte_length,
243 BackingStoreInitializationMode initialization_mode =
244 BackingStoreInitializationMode::kZeroInitialized);
245
253 Isolate* isolate, size_t byte_length,
254 BackingStoreInitializationMode initialization_mode =
255 BackingStoreInitializationMode::kZeroInitialized);
256
270 std::shared_ptr<BackingStore> backing_store);
271
287 static std::unique_ptr<BackingStore> NewBackingStore(
288 Isolate* isolate, size_t byte_length,
289 BackingStoreInitializationMode initialization_mode =
290 BackingStoreInitializationMode::kZeroInitialized,
291 BackingStoreOnFailureMode on_failure =
292 BackingStoreOnFailureMode::kOutOfMemory);
293
302 static std::unique_ptr<BackingStore> NewBackingStore(
303 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
304 void* deleter_data);
305
318 static std::unique_ptr<BackingStore> NewResizableBackingStore(
319 size_t byte_length, size_t max_byte_length);
320
324 bool IsDetachable() const;
325
329 bool WasDetached() const;
330
338 "Use the version which takes a key parameter (passing a null handle is "
339 "ok).")
340 void Detach();
341
351
355 void SetDetachKey(v8::Local<v8::Value> key);
356
366 std::shared_ptr<BackingStore> GetBackingStore();
367
372 bool IsResizableByUserJavaScript() const;
373
378 void* Data() const;
379
380 V8_INLINE static ArrayBuffer* Cast(Value* value) {
381#ifdef V8_ENABLE_CHECKS
382 CheckCast(value);
383#endif
384 return static_cast<ArrayBuffer*>(value);
385 }
386
387 static constexpr int kInternalFieldCount =
389 static constexpr int kEmbedderFieldCount = kInternalFieldCount;
390
391#if V8_ENABLE_SANDBOX
392 static constexpr size_t kMaxByteLength =
393 internal::kMaxSafeBufferSizeForSandbox;
394#elif V8_HOST_ARCH_32_BIT
395 static constexpr size_t kMaxByteLength = std::numeric_limits<int>::max();
396#else
397 // The maximum safe integer (2^53 - 1).
398 static constexpr size_t kMaxByteLength =
399 static_cast<size_t>((uint64_t{1} << 53) - 1);
400#endif
401
402 private:
403 ArrayBuffer();
404 static void CheckCast(Value* obj);
405 friend class TypedArray;
406};
407
408#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
409// Defined using gn arg `v8_array_buffer_view_internal_field_count`.
410#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
411#endif
412
418 public:
426 size_t ByteOffset();
430 size_t ByteLength();
431
441 size_t CopyContents(void* dest, size_t byte_length);
442
452
457 bool HasBuffer() const;
458
460#ifdef V8_ENABLE_CHECKS
461 CheckCast(value);
462#endif
463 return static_cast<ArrayBufferView*>(value);
464 }
465
466 static constexpr int kInternalFieldCount =
468 static const int kEmbedderFieldCount = kInternalFieldCount;
469
470 private:
472 static void CheckCast(Value* obj);
473};
474
479 public:
481 size_t byte_offset, size_t length);
482 static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
483 size_t byte_offset, size_t length);
484 V8_INLINE static DataView* Cast(Value* value) {
485#ifdef V8_ENABLE_CHECKS
486 CheckCast(value);
487#endif
488 return static_cast<DataView*>(value);
489 }
490
491 private:
492 DataView();
493 static void CheckCast(Value* obj);
494};
495
500 public:
504 size_t ByteLength() const;
505
509 size_t MaxByteLength() const;
510
518 Isolate* isolate, size_t byte_length,
519 BackingStoreInitializationMode initialization_mode =
520 BackingStoreInitializationMode::kZeroInitialized);
521
530 Isolate* isolate, size_t byte_length,
531 BackingStoreInitializationMode initialization_mode =
532 BackingStoreInitializationMode::kZeroInitialized);
533
547 Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
548
565 static std::unique_ptr<BackingStore> NewBackingStore(
566 Isolate* isolate, size_t byte_length,
567 BackingStoreInitializationMode initialization_mode =
568 BackingStoreInitializationMode::kZeroInitialized,
569 BackingStoreOnFailureMode on_failure =
570 BackingStoreOnFailureMode::kOutOfMemory);
571
580 static std::unique_ptr<BackingStore> NewBackingStore(
581 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
582 void* deleter_data);
583
590 std::shared_ptr<BackingStore> GetBackingStore();
591
596 void* Data() const;
597
599#ifdef V8_ENABLE_CHECKS
600 CheckCast(value);
601#endif
602 return static_cast<SharedArrayBuffer*>(value);
603 }
604
605 static constexpr int kInternalFieldCount =
607
608 private:
610 static void CheckCast(Value* obj);
611};
612
613} // namespace v8
614
615#endif // INCLUDE_V8_ARRAY_BUFFER_H_
Definition: v8-array-buffer.h:417
static ArrayBufferView * Cast(Value *value)
Definition: v8-array-buffer.h:459
Local< ArrayBuffer > Buffer()
v8::MemorySpan< uint8_t > GetContents(v8::MemorySpan< uint8_t > storage)
size_t CopyContents(void *dest, size_t byte_length)
bool HasBuffer() const
Definition: v8-array-buffer.h:148
virtual ~Allocator()=default
virtual void Free(void *data, size_t length)=0
static Allocator * NewDefaultAllocator()
virtual void * AllocateUninitialized(size_t length)=0
virtual size_t MaxAllocationSize() const
Definition: v8-array-buffer.h:177
virtual PageAllocator * GetPageAllocator()
Definition: v8-array-buffer.h:193
AllocationMode
Definition: v8-array-buffer.h:184
virtual void * Allocate(size_t length)=0
Definition: v8-array-buffer.h:131
static std::unique_ptr< BackingStore > NewBackingStore(void *data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, void *deleter_data)
size_t MaxByteLength() const
static std::unique_ptr< BackingStore > NewResizableBackingStore(size_t byte_length, size_t max_byte_length)
static Local< ArrayBuffer > New(Isolate *isolate, std::shared_ptr< BackingStore > backing_store)
bool IsDetachable() const
bool WasDetached() const
static Local< ArrayBuffer > New(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
static std::unique_ptr< BackingStore > NewBackingStore(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized, BackingStoreOnFailureMode on_failure=BackingStoreOnFailureMode::kOutOfMemory)
size_t ByteLength() const
static MaybeLocal< ArrayBuffer > MaybeNew(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
Definition: v8-array-buffer.h:49
size_t MaxByteLength() const
bool IsShared() const
size_t ByteLength() const
void(*)(void *data, size_t length, void *deleter_data) DeleterCallback
Definition: v8-array-buffer.h:100
static void EmptyDeleter(void *data, size_t length, void *deleter_data)
bool IsResizableByUserJavaScript() const
void * Data() const
Definition: v8-array-buffer.h:478
static Local< DataView > New(Local< SharedArrayBuffer > shared_array_buffer, size_t byte_offset, size_t length)
static DataView * Cast(Value *value)
Definition: v8-array-buffer.h:484
static Local< DataView > New(Local< ArrayBuffer > array_buffer, size_t byte_offset, size_t length)
Definition: v8-data.h:18
Definition: v8-isolate.h:218
Definition: v8-isolate.h:274
Definition: v8-local-handle.h:266
Definition: v8-local-handle.h:632
Definition: v8-maybe.h:32
Definition: v8-memory-span.h:64
Definition: v8-object.h:233
Definition: v8-platform.h:455
Definition: v8-array-buffer.h:499
void * Data() const
static Local< SharedArrayBuffer > New(Isolate *isolate, std::shared_ptr< BackingStore > backing_store)
static MaybeLocal< SharedArrayBuffer > MaybeNew(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
std::shared_ptr< BackingStore > GetBackingStore()
static std::unique_ptr< BackingStore > NewBackingStore(void *data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, void *deleter_data)
static std::unique_ptr< BackingStore > NewBackingStore(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized, BackingStoreOnFailureMode on_failure=BackingStoreOnFailureMode::kOutOfMemory)
size_t ByteLength() const
size_t MaxByteLength() const
static Local< SharedArrayBuffer > New(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
static SharedArrayBuffer * Cast(Value *value)
Definition: v8-array-buffer.h:598
Definition: v8-typed-array.h:20
Definition: v8-value.h:32
Definition: v8-internal.h:1344
Definition: libplatform.h:15
void(*)(void *data, size_t length, void *deleter_data) BackingStoreDeleterCallback
Definition: v8-array-buffer.h:124
BackingStoreInitializationMode
Definition: v8-array-buffer.h:33
ArrayBufferCreationMode
Definition: v8-array-buffer.h:32
BackingStoreOnFailureMode
Definition: v8-array-buffer.h:34
#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
Definition: v8-array-buffer.h:29
#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
Definition: v8-array-buffer.h:410
#define V8_EXPORT
Definition: v8config.h:803
#define V8_INLINE
Definition: v8config.h:499
#define V8_DEPRECATED(message)
Definition: v8config.h:605
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:670