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 ArrayBuffer;
21class SharedArrayBuffer;
22
23#if defined(V8_COMPRESS_POINTERS) && \
24 !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
25class IsolateGroup;
26#endif
27
28#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
29// Defined using gn arg `v8_array_buffer_internal_field_count`.
30#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
31#endif
32
36
51 public:
53
59 void* Data() const;
60
64 size_t ByteLength() const;
65
69 std::span<uint8_t> ByteSpan() const;
70
78 size_t MaxByteLength() const;
79
84 bool IsShared() const;
85
89 bool IsImmutable() const;
90
97
103 void operator delete(void* ptr) { ::operator delete(ptr); }
104
110 using DeleterCallback = void (*)(void* data, size_t length,
111 void* deleter_data);
112
122 static void EmptyDeleter(void* data, size_t length, void* deleter_data);
123
124 private:
129 BackingStore();
130};
131
132#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
133// Use v8::BackingStore::DeleterCallback instead.
134using BackingStoreDeleterCallback = void (*)(void* data, size_t length,
135 void* deleter_data);
136
137#endif
138
144 public:
148 size_t ByteLength() const;
149
153 size_t MaxByteLength() const;
154
158 bool IsShared() const;
159
164 bool IsDetachable() const;
165
170 bool WasDetached() const;
171
176 bool IsImmutable() const;
177
185 size_t CopyArrayBufferBytes(size_t source_start, size_t bytes_to_copy,
187 size_t target_start) const;
188
198 std::shared_ptr<BackingStore> GetBackingStore();
199
205
210 void* Data() const;
211
213#ifdef V8_ENABLE_CHECKS
214 CheckCast(value);
215#endif
216 return static_cast<ArrayBufferBase*>(value);
217 }
218
219 static constexpr int kInternalFieldCount =
221 static constexpr int kEmbedderFieldCount = kInternalFieldCount;
222
223#if V8_ENABLE_SANDBOX
224 static constexpr size_t kMaxByteLength =
225 internal::kMaxSafeBufferSizeForSandbox;
226#elif V8_HOST_ARCH_32_BIT
227 static constexpr size_t kMaxByteLength = std::numeric_limits<int>::max();
228#else
229 // The maximum safe integer (2^53 - 1).
230 static constexpr size_t kMaxByteLength =
231 static_cast<size_t>((uint64_t{1} << 53) - 1);
232#endif
233
234 private:
236 static void CheckCast(Value* obj);
237 friend class ArrayBuffer;
238 friend class SharedArrayBuffer;
239};
240
245 public:
262 public:
263 virtual ~Allocator() = default;
264
269 virtual void* Allocate(size_t length) = 0;
270
275 virtual void* AllocateUninitialized(size_t length) = 0;
276
281 virtual void Free(void* data, size_t length) = 0;
282
290 virtual size_t MaxAllocationSize() const { return kMaxByteLength; }
291
297 enum class AllocationMode { kNormal, kReservation };
298
306 virtual PageAllocator* GetPageAllocator() { return nullptr; }
307
308#if defined(V8_COMPRESS_POINTERS) && \
309 !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
320 static Allocator* NewDefaultAllocator(const IsolateGroup& group);
321#endif // defined(V8_COMPRESS_POINTERS) &&
322 // !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
323
335 };
336
345 Isolate* isolate, size_t byte_length,
346 BackingStoreInitializationMode initialization_mode =
347 BackingStoreInitializationMode::kZeroInitialized);
348
356 Isolate* isolate, size_t byte_length,
357 BackingStoreInitializationMode initialization_mode =
358 BackingStoreInitializationMode::kZeroInitialized);
359
373 std::shared_ptr<BackingStore> backing_store);
374
390 static std::unique_ptr<BackingStore> NewBackingStore(
391 Isolate* isolate, size_t byte_length,
392 BackingStoreInitializationMode initialization_mode =
393 BackingStoreInitializationMode::kZeroInitialized,
394 BackingStoreOnFailureMode on_failure =
395 BackingStoreOnFailureMode::kOutOfMemory);
396
405 static std::unique_ptr<BackingStore> NewBackingStore(
406 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
407 void* deleter_data);
408
421 static std::unique_ptr<BackingStore> NewResizableBackingStore(
422 size_t byte_length, size_t max_byte_length);
423
431 "Use the version which takes a key parameter (passing a null handle is "
432 "ok).")
433 void Detach();
434
444
448 void SetDetachKey(v8::Local<v8::Value> key);
449
450 V8_INLINE static ArrayBuffer* Cast(Value* value) {
451#ifdef V8_ENABLE_CHECKS
452 CheckCast(value);
453#endif
454 return static_cast<ArrayBuffer*>(value);
455 }
456
457 private:
458 ArrayBuffer();
459 static void CheckCast(Value* obj);
460 friend class TypedArray;
461};
462
463#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
464// Defined using gn arg `v8_array_buffer_view_internal_field_count`.
465#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
466#endif
467
473 public:
481 size_t ByteOffset();
485 size_t ByteLength();
486
496 size_t CopyContents(void* dest, size_t byte_length);
497
506 std::span<uint8_t> GetContents(std::span<uint8_t> storage);
507
512 bool HasBuffer() const;
513
515#ifdef V8_ENABLE_CHECKS
516 CheckCast(value);
517#endif
518 return static_cast<ArrayBufferView*>(value);
519 }
520
521 static constexpr int kInternalFieldCount =
523 static const int kEmbedderFieldCount = kInternalFieldCount;
524
525 private:
527 static void CheckCast(Value* obj);
528};
529
534 public:
536 size_t byte_offset, size_t length);
537 V8_INLINE static DataView* Cast(Value* value) {
538#ifdef V8_ENABLE_CHECKS
539 CheckCast(value);
540#endif
541 return static_cast<DataView*>(value);
542 }
543
544 private:
545 DataView();
546 static void CheckCast(Value* obj);
547};
548
553 public:
561 Isolate* isolate, size_t byte_length,
562 BackingStoreInitializationMode initialization_mode =
563 BackingStoreInitializationMode::kZeroInitialized);
564
573 Isolate* isolate, size_t byte_length,
574 BackingStoreInitializationMode initialization_mode =
575 BackingStoreInitializationMode::kZeroInitialized);
576
590 Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
591
608 static std::unique_ptr<BackingStore> NewBackingStore(
609 Isolate* isolate, size_t byte_length,
610 BackingStoreInitializationMode initialization_mode =
611 BackingStoreInitializationMode::kZeroInitialized,
612 BackingStoreOnFailureMode on_failure =
613 BackingStoreOnFailureMode::kOutOfMemory);
614
623 static std::unique_ptr<BackingStore> NewBackingStore(
624 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
625 void* deleter_data);
626
628#ifdef V8_ENABLE_CHECKS
629 CheckCast(value);
630#endif
631 return static_cast<SharedArrayBuffer*>(value);
632 }
633
634 private:
636 static void CheckCast(Value* obj);
637};
638
639} // namespace v8
640
641#endif // INCLUDE_V8_ARRAY_BUFFER_H_
Definition: v8-array-buffer.h:143
bool IsResizableByUserJavaScript() const
bool IsImmutable() const
size_t CopyArrayBufferBytes(size_t source_start, size_t bytes_to_copy, Local< ArrayBufferBase > target, size_t target_start) const
static ArrayBufferBase * Cast(Value *value)
Definition: v8-array-buffer.h:212
size_t ByteLength() const
bool IsDetachable() const
size_t MaxByteLength() const
void * Data() const
bool WasDetached() const
bool IsShared() const
std::shared_ptr< BackingStore > GetBackingStore()
Definition: v8-array-buffer.h:472
static ArrayBufferView * Cast(Value *value)
Definition: v8-array-buffer.h:514
Local< ArrayBufferBase > GetBuffer()
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:261
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:290
virtual PageAllocator * GetPageAllocator()
Definition: v8-array-buffer.h:306
AllocationMode
Definition: v8-array-buffer.h:297
virtual void * Allocate(size_t length)=0
Definition: v8-array-buffer.h:244
static std::unique_ptr< BackingStore > NewBackingStore(void *data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, void *deleter_data)
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)
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)
static MaybeLocal< ArrayBuffer > MaybeNew(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
Definition: v8-array-buffer.h:50
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:111
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:533
static Local< DataView > New(Local< ArrayBufferBase > array_buffer, size_t byte_offset, size_t length)
static DataView * Cast(Value *value)
Definition: v8-array-buffer.h:537
Definition: v8-isolate.h:232
Definition: v8-isolate.h:305
Definition: v8-local-handle.h:356
Definition: v8-local-handle.h:724
Definition: v8-maybe.h:39
Definition: v8-object.h:269
Definition: v8-platform.h:557
Definition: v8-array-buffer.h:552
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)
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)
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:627
Definition: v8-typed-array.h:21
Definition: v8-value.h:32
Definition: v8-internal.h:1562
Definition: libplatform.h:15
void(*)(void *data, size_t length, void *deleter_data) BackingStoreDeleterCallback
Definition: v8-array-buffer.h:135
BackingStoreInitializationMode
Definition: v8-array-buffer.h:34
ArrayBufferCreationMode
Definition: v8-array-buffer.h:33
BackingStoreOnFailureMode
Definition: v8-array-buffer.h:35
#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
Definition: v8-array-buffer.h:30
#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
Definition: v8-array-buffer.h:465
#define V8_EXPORT
Definition: v8config.h:875
#define V8_INLINE
Definition: v8config.h:519
#define V8_DEPRECATED(message)
Definition: v8config.h:624
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:689