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#include <span>
12
13#include "v8-local-handle.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
68 std::span<uint8_t> ByteSpan() const;
69
77 size_t MaxByteLength() const;
78
83 bool IsShared() const;
84
88 bool IsImmutable() const;
89
96
102 void operator delete(void* ptr) { ::operator delete(ptr); }
103
109 using DeleterCallback = void (*)(void* data, size_t length,
110 void* deleter_data);
111
121 static void EmptyDeleter(void* data, size_t length, void* deleter_data);
122
123 private:
128 BackingStore();
129};
130
131#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
132// Use v8::BackingStore::DeleterCallback instead.
133using BackingStoreDeleterCallback = void (*)(void* data, size_t length,
134 void* deleter_data);
135
136#endif
137
142 public:
159 public:
160 virtual ~Allocator() = default;
161
166 virtual void* Allocate(size_t length) = 0;
167
172 virtual void* AllocateUninitialized(size_t length) = 0;
173
178 virtual void Free(void* data, size_t length) = 0;
179
187 virtual size_t MaxAllocationSize() const { return kMaxByteLength; }
188
194 enum class AllocationMode { kNormal, kReservation };
195
203 virtual PageAllocator* GetPageAllocator() { return nullptr; }
204
205#if defined(V8_COMPRESS_POINTERS) && \
206 !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
217 static Allocator* NewDefaultAllocator(const IsolateGroup& group);
218#endif // defined(V8_COMPRESS_POINTERS) &&
219 // !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
220
232 };
233
237 size_t ByteLength() const;
238
242 size_t MaxByteLength() const;
243
252 Isolate* isolate, size_t byte_length,
253 BackingStoreInitializationMode initialization_mode =
254 BackingStoreInitializationMode::kZeroInitialized);
255
263 Isolate* isolate, size_t byte_length,
264 BackingStoreInitializationMode initialization_mode =
265 BackingStoreInitializationMode::kZeroInitialized);
266
280 std::shared_ptr<BackingStore> backing_store);
281
297 static std::unique_ptr<BackingStore> NewBackingStore(
298 Isolate* isolate, size_t byte_length,
299 BackingStoreInitializationMode initialization_mode =
300 BackingStoreInitializationMode::kZeroInitialized,
301 BackingStoreOnFailureMode on_failure =
302 BackingStoreOnFailureMode::kOutOfMemory);
303
312 static std::unique_ptr<BackingStore> NewBackingStore(
313 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
314 void* deleter_data);
315
328 static std::unique_ptr<BackingStore> NewResizableBackingStore(
329 size_t byte_length, size_t max_byte_length);
330
334 bool IsDetachable() const;
335
339 bool WasDetached() const;
340
344 bool IsImmutable() const;
345
353 size_t CopyArrayBufferBytes(size_t source_start, size_t bytes_to_copy,
354 Local<ArrayBuffer> target,
355 size_t target_start) const;
356
364 "Use the version which takes a key parameter (passing a null handle is "
365 "ok).")
366 void Detach();
367
377
381 void SetDetachKey(v8::Local<v8::Value> key);
382
392 std::shared_ptr<BackingStore> GetBackingStore();
393
398 bool IsResizableByUserJavaScript() const;
399
404 void* Data() const;
405
406 V8_INLINE static ArrayBuffer* Cast(Value* value) {
407#ifdef V8_ENABLE_CHECKS
408 CheckCast(value);
409#endif
410 return static_cast<ArrayBuffer*>(value);
411 }
412
413 static constexpr int kInternalFieldCount =
415 static constexpr int kEmbedderFieldCount = kInternalFieldCount;
416
417#if V8_ENABLE_SANDBOX
418 static constexpr size_t kMaxByteLength =
419 internal::kMaxSafeBufferSizeForSandbox;
420#elif V8_HOST_ARCH_32_BIT
421 static constexpr size_t kMaxByteLength = std::numeric_limits<int>::max();
422#else
423 // The maximum safe integer (2^53 - 1).
424 static constexpr size_t kMaxByteLength =
425 static_cast<size_t>((uint64_t{1} << 53) - 1);
426#endif
427
428 private:
429 ArrayBuffer();
430 static void CheckCast(Value* obj);
431 friend class TypedArray;
432};
433
434#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
435// Defined using gn arg `v8_array_buffer_view_internal_field_count`.
436#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
437#endif
438
444 public:
452 size_t ByteOffset();
456 size_t ByteLength();
457
467 size_t CopyContents(void* dest, size_t byte_length);
468
477 std::span<uint8_t> GetContents(std::span<uint8_t> storage);
478
483 bool HasBuffer() const;
484
486#ifdef V8_ENABLE_CHECKS
487 CheckCast(value);
488#endif
489 return static_cast<ArrayBufferView*>(value);
490 }
491
492 static constexpr int kInternalFieldCount =
494 static const int kEmbedderFieldCount = kInternalFieldCount;
495
496 private:
498 static void CheckCast(Value* obj);
499};
500
505 public:
507 size_t byte_offset, size_t length);
508 static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
509 size_t byte_offset, size_t length);
510 V8_INLINE static DataView* Cast(Value* value) {
511#ifdef V8_ENABLE_CHECKS
512 CheckCast(value);
513#endif
514 return static_cast<DataView*>(value);
515 }
516
517 private:
518 DataView();
519 static void CheckCast(Value* obj);
520};
521
526 public:
530 size_t ByteLength() const;
531
535 size_t MaxByteLength() const;
536
544 Isolate* isolate, size_t byte_length,
545 BackingStoreInitializationMode initialization_mode =
546 BackingStoreInitializationMode::kZeroInitialized);
547
556 Isolate* isolate, size_t byte_length,
557 BackingStoreInitializationMode initialization_mode =
558 BackingStoreInitializationMode::kZeroInitialized);
559
573 Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
574
591 static std::unique_ptr<BackingStore> NewBackingStore(
592 Isolate* isolate, size_t byte_length,
593 BackingStoreInitializationMode initialization_mode =
594 BackingStoreInitializationMode::kZeroInitialized,
595 BackingStoreOnFailureMode on_failure =
596 BackingStoreOnFailureMode::kOutOfMemory);
597
606 static std::unique_ptr<BackingStore> NewBackingStore(
607 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
608 void* deleter_data);
609
616 std::shared_ptr<BackingStore> GetBackingStore();
617
622 void* Data() const;
623
630 size_t CopyArrayBufferBytes(size_t source_start, size_t bytes_to_copy,
632 size_t target_start) const;
633
635#ifdef V8_ENABLE_CHECKS
636 CheckCast(value);
637#endif
638 return static_cast<SharedArrayBuffer*>(value);
639 }
640
641 static constexpr int kInternalFieldCount =
643
644 private:
646 static void CheckCast(Value* obj);
647};
648
649} // namespace v8
650
651#endif // INCLUDE_V8_ARRAY_BUFFER_H_
Definition: v8-array-buffer.h:443
static ArrayBufferView * Cast(Value *value)
Definition: v8-array-buffer.h:485
Local< ArrayBuffer > Buffer()
std::span< uint8_t > GetContents(std::span< uint8_t > storage)
size_t CopyContents(void *dest, size_t byte_length)
bool HasBuffer() const
Definition: v8-array-buffer.h:158
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:187
virtual PageAllocator * GetPageAllocator()
Definition: v8-array-buffer.h:203
AllocationMode
Definition: v8-array-buffer.h:194
virtual void * Allocate(size_t length)=0
Definition: v8-array-buffer.h:141
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)
size_t CopyArrayBufferBytes(size_t source_start, size_t bytes_to_copy, Local< ArrayBuffer > target, size_t target_start) const
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)
bool IsImmutable() const
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 IsImmutable() const
bool IsShared() const
size_t ByteLength() const
void(*)(void *data, size_t length, void *deleter_data) DeleterCallback
Definition: v8-array-buffer.h:110
static void EmptyDeleter(void *data, size_t length, void *deleter_data)
bool IsResizableByUserJavaScript() const
std::span< uint8_t > ByteSpan() const
void * Data() const
Definition: v8-array-buffer.h:504
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:510
static Local< DataView > New(Local< ArrayBuffer > array_buffer, size_t byte_offset, size_t length)
Definition: v8-data.h:18
Definition: v8-isolate.h:225
Definition: v8-isolate.h:292
Definition: v8-local-handle.h:366
Definition: v8-local-handle.h:734
Definition: v8-maybe.h:39
Definition: v8-object.h:267
Definition: v8-platform.h:548
Definition: v8-array-buffer.h:525
size_t CopyArrayBufferBytes(size_t source_start, size_t bytes_to_copy, Local< SharedArrayBuffer > target, size_t target_start) const
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:634
Definition: v8-typed-array.h:20
Definition: v8-value.h:32
Definition: v8-internal.h:1568
Definition: libplatform.h:15
void(*)(void *data, size_t length, void *deleter_data) BackingStoreDeleterCallback
Definition: v8-array-buffer.h:134
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:436
#define V8_EXPORT
Definition: v8config.h:867
#define V8_INLINE
Definition: v8config.h:511
#define V8_DEPRECATED(message)
Definition: v8config.h:616
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:681