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:
477 V8_DEPRECATE_SOON("Use GetBuffer instead")
482 Local<ArrayBufferBase> GetBuffer();
486 size_t ByteOffset();
490 size_t ByteLength();
491
501 size_t CopyContents(void* dest, size_t byte_length);
502
511 std::span<uint8_t> GetContents(std::span<uint8_t> storage);
512
517 bool HasBuffer() const;
518
519 V8_INLINE static ArrayBufferView* Cast(Value* value) {
520#ifdef V8_ENABLE_CHECKS
521 CheckCast(value);
522#endif
523 return static_cast<ArrayBufferView*>(value);
524 }
525
526 static constexpr int kInternalFieldCount =
528 static const int kEmbedderFieldCount = kInternalFieldCount;
529
530 private:
532 static void CheckCast(Value* obj);
533};
534
539 public:
541 size_t byte_offset, size_t length);
542 V8_INLINE static DataView* Cast(Value* value) {
543#ifdef V8_ENABLE_CHECKS
544 CheckCast(value);
545#endif
546 return static_cast<DataView*>(value);
547 }
548
549 private:
550 DataView();
551 static void CheckCast(Value* obj);
552};
553
558 public:
566 Isolate* isolate, size_t byte_length,
567 BackingStoreInitializationMode initialization_mode =
568 BackingStoreInitializationMode::kZeroInitialized);
569
578 Isolate* isolate, size_t byte_length,
579 BackingStoreInitializationMode initialization_mode =
580 BackingStoreInitializationMode::kZeroInitialized);
581
595 Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
596
613 static std::unique_ptr<BackingStore> NewBackingStore(
614 Isolate* isolate, size_t byte_length,
615 BackingStoreInitializationMode initialization_mode =
616 BackingStoreInitializationMode::kZeroInitialized,
617 BackingStoreOnFailureMode on_failure =
618 BackingStoreOnFailureMode::kOutOfMemory);
619
628 static std::unique_ptr<BackingStore> NewBackingStore(
629 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
630 void* deleter_data);
631
633#ifdef V8_ENABLE_CHECKS
634 CheckCast(value);
635#endif
636 return static_cast<SharedArrayBuffer*>(value);
637 }
638
639 private:
641 static void CheckCast(Value* obj);
642};
643
644} // namespace v8
645
646#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
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:538
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:542
Definition: v8-isolate.h:232
Definition: v8-isolate.h:305
Definition: v8-local-handle.h:359
Definition: v8-local-handle.h:730
Definition: v8-maybe.h:39
Definition: v8-object.h:262
Definition: v8-platform.h:557
Definition: v8-array-buffer.h:557
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:632
Definition: v8-typed-array.h:20
Definition: v8-value.h:32
Definition: v8-internal.h:1559
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_DEPRECATE_SOON(message)
Definition: v8config.h:632
#define V8_DEPRECATED(message)
Definition: v8config.h:624
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:689