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
72 size_t MaxByteLength() const;
73
78 bool IsShared() const;
79
83 bool IsImmutable() const;
84
91
97 void operator delete(void* ptr) { ::operator delete(ptr); }
98
104 using DeleterCallback = void (*)(void* data, size_t length,
105 void* deleter_data);
106
116 static void EmptyDeleter(void* data, size_t length, void* deleter_data);
117
118 private:
123 BackingStore();
124};
125
126#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
127// Use v8::BackingStore::DeleterCallback instead.
128using BackingStoreDeleterCallback = void (*)(void* data, size_t length,
129 void* deleter_data);
130
131#endif
132
137 public:
154 public:
155 virtual ~Allocator() = default;
156
161 virtual void* Allocate(size_t length) = 0;
162
167 virtual void* AllocateUninitialized(size_t length) = 0;
168
173 virtual void Free(void* data, size_t length) = 0;
174
182 virtual size_t MaxAllocationSize() const { return kMaxByteLength; }
183
189 enum class AllocationMode { kNormal, kReservation };
190
198 virtual PageAllocator* GetPageAllocator() { return nullptr; }
199
200#if defined(V8_COMPRESS_POINTERS) && \
201 !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
212 static Allocator* NewDefaultAllocator(const IsolateGroup& group);
213#endif // defined(V8_COMPRESS_POINTERS) &&
214 // !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
215
227 };
228
232 size_t ByteLength() const;
233
237 size_t MaxByteLength() const;
238
247 Isolate* isolate, size_t byte_length,
248 BackingStoreInitializationMode initialization_mode =
249 BackingStoreInitializationMode::kZeroInitialized);
250
258 Isolate* isolate, size_t byte_length,
259 BackingStoreInitializationMode initialization_mode =
260 BackingStoreInitializationMode::kZeroInitialized);
261
275 std::shared_ptr<BackingStore> backing_store);
276
292 static std::unique_ptr<BackingStore> NewBackingStore(
293 Isolate* isolate, size_t byte_length,
294 BackingStoreInitializationMode initialization_mode =
295 BackingStoreInitializationMode::kZeroInitialized,
296 BackingStoreOnFailureMode on_failure =
297 BackingStoreOnFailureMode::kOutOfMemory);
298
307 static std::unique_ptr<BackingStore> NewBackingStore(
308 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
309 void* deleter_data);
310
323 static std::unique_ptr<BackingStore> NewResizableBackingStore(
324 size_t byte_length, size_t max_byte_length);
325
329 bool IsDetachable() const;
330
334 bool WasDetached() const;
335
339 bool IsImmutable() const;
340
348 size_t CopyArrayBufferBytes(size_t source_start, size_t bytes_to_copy,
349 Local<ArrayBuffer> target,
350 size_t target_start) const;
351
359 "Use the version which takes a key parameter (passing a null handle is "
360 "ok).")
361 void Detach();
362
372
376 void SetDetachKey(v8::Local<v8::Value> key);
377
387 std::shared_ptr<BackingStore> GetBackingStore();
388
393 bool IsResizableByUserJavaScript() const;
394
399 void* Data() const;
400
401 V8_INLINE static ArrayBuffer* Cast(Value* value) {
402#ifdef V8_ENABLE_CHECKS
403 CheckCast(value);
404#endif
405 return static_cast<ArrayBuffer*>(value);
406 }
407
408 static constexpr int kInternalFieldCount =
410 static constexpr int kEmbedderFieldCount = kInternalFieldCount;
411
412#if V8_ENABLE_SANDBOX
413 static constexpr size_t kMaxByteLength =
414 internal::kMaxSafeBufferSizeForSandbox;
415#elif V8_HOST_ARCH_32_BIT
416 static constexpr size_t kMaxByteLength = std::numeric_limits<int>::max();
417#else
418 // The maximum safe integer (2^53 - 1).
419 static constexpr size_t kMaxByteLength =
420 static_cast<size_t>((uint64_t{1} << 53) - 1);
421#endif
422
423 private:
424 ArrayBuffer();
425 static void CheckCast(Value* obj);
426 friend class TypedArray;
427};
428
429#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
430// Defined using gn arg `v8_array_buffer_view_internal_field_count`.
431#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
432#endif
433
439 public:
447 size_t ByteOffset();
451 size_t ByteLength();
452
462 size_t CopyContents(void* dest, size_t byte_length);
463
472 std::span<uint8_t> GetContents(std::span<uint8_t> storage);
473
478 bool HasBuffer() const;
479
481#ifdef V8_ENABLE_CHECKS
482 CheckCast(value);
483#endif
484 return static_cast<ArrayBufferView*>(value);
485 }
486
487 static constexpr int kInternalFieldCount =
489 static const int kEmbedderFieldCount = kInternalFieldCount;
490
491 private:
493 static void CheckCast(Value* obj);
494};
495
500 public:
502 size_t byte_offset, size_t length);
503 static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
504 size_t byte_offset, size_t length);
505 V8_INLINE static DataView* Cast(Value* value) {
506#ifdef V8_ENABLE_CHECKS
507 CheckCast(value);
508#endif
509 return static_cast<DataView*>(value);
510 }
511
512 private:
513 DataView();
514 static void CheckCast(Value* obj);
515};
516
521 public:
525 size_t ByteLength() const;
526
530 size_t MaxByteLength() const;
531
539 Isolate* isolate, size_t byte_length,
540 BackingStoreInitializationMode initialization_mode =
541 BackingStoreInitializationMode::kZeroInitialized);
542
551 Isolate* isolate, size_t byte_length,
552 BackingStoreInitializationMode initialization_mode =
553 BackingStoreInitializationMode::kZeroInitialized);
554
568 Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
569
586 static std::unique_ptr<BackingStore> NewBackingStore(
587 Isolate* isolate, size_t byte_length,
588 BackingStoreInitializationMode initialization_mode =
589 BackingStoreInitializationMode::kZeroInitialized,
590 BackingStoreOnFailureMode on_failure =
591 BackingStoreOnFailureMode::kOutOfMemory);
592
601 static std::unique_ptr<BackingStore> NewBackingStore(
602 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
603 void* deleter_data);
604
611 std::shared_ptr<BackingStore> GetBackingStore();
612
617 void* Data() const;
618
625 size_t CopyArrayBufferBytes(size_t source_start, size_t bytes_to_copy,
627 size_t target_start) const;
628
630#ifdef V8_ENABLE_CHECKS
631 CheckCast(value);
632#endif
633 return static_cast<SharedArrayBuffer*>(value);
634 }
635
636 static constexpr int kInternalFieldCount =
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:438
static ArrayBufferView * Cast(Value *value)
Definition: v8-array-buffer.h:480
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:153
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:182
virtual PageAllocator * GetPageAllocator()
Definition: v8-array-buffer.h:198
AllocationMode
Definition: v8-array-buffer.h:189
virtual void * Allocate(size_t length)=0
Definition: v8-array-buffer.h:136
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:105
static void EmptyDeleter(void *data, size_t length, void *deleter_data)
bool IsResizableByUserJavaScript() const
void * Data() const
Definition: v8-array-buffer.h:499
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:505
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:266
Definition: v8-platform.h:548
Definition: v8-array-buffer.h:520
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:629
Definition: v8-typed-array.h:20
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:129
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:431
#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