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
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 "Use the version which takes a key parameter (passing a null handle is "
349 "ok).")
350 void Detach();
351
361
365 void SetDetachKey(v8::Local<v8::Value> key);
366
376 std::shared_ptr<BackingStore> GetBackingStore();
377
382 bool IsResizableByUserJavaScript() const;
383
388 void* Data() const;
389
390 V8_INLINE static ArrayBuffer* Cast(Value* value) {
391#ifdef V8_ENABLE_CHECKS
392 CheckCast(value);
393#endif
394 return static_cast<ArrayBuffer*>(value);
395 }
396
397 static constexpr int kInternalFieldCount =
399 static constexpr int kEmbedderFieldCount = kInternalFieldCount;
400
401#if V8_ENABLE_SANDBOX
402 static constexpr size_t kMaxByteLength =
403 internal::kMaxSafeBufferSizeForSandbox;
404#elif V8_HOST_ARCH_32_BIT
405 static constexpr size_t kMaxByteLength = std::numeric_limits<int>::max();
406#else
407 // The maximum safe integer (2^53 - 1).
408 static constexpr size_t kMaxByteLength =
409 static_cast<size_t>((uint64_t{1} << 53) - 1);
410#endif
411
412 private:
413 ArrayBuffer();
414 static void CheckCast(Value* obj);
415 friend class TypedArray;
416};
417
418#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
419// Defined using gn arg `v8_array_buffer_view_internal_field_count`.
420#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
421#endif
422
428 public:
436 size_t ByteOffset();
440 size_t ByteLength();
441
451 size_t CopyContents(void* dest, size_t byte_length);
452
462
467 bool HasBuffer() const;
468
470#ifdef V8_ENABLE_CHECKS
471 CheckCast(value);
472#endif
473 return static_cast<ArrayBufferView*>(value);
474 }
475
476 static constexpr int kInternalFieldCount =
478 static const int kEmbedderFieldCount = kInternalFieldCount;
479
480 private:
482 static void CheckCast(Value* obj);
483};
484
489 public:
491 size_t byte_offset, size_t length);
492 static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
493 size_t byte_offset, size_t length);
494 V8_INLINE static DataView* Cast(Value* value) {
495#ifdef V8_ENABLE_CHECKS
496 CheckCast(value);
497#endif
498 return static_cast<DataView*>(value);
499 }
500
501 private:
502 DataView();
503 static void CheckCast(Value* obj);
504};
505
510 public:
514 size_t ByteLength() const;
515
519 size_t MaxByteLength() const;
520
528 Isolate* isolate, size_t byte_length,
529 BackingStoreInitializationMode initialization_mode =
530 BackingStoreInitializationMode::kZeroInitialized);
531
540 Isolate* isolate, size_t byte_length,
541 BackingStoreInitializationMode initialization_mode =
542 BackingStoreInitializationMode::kZeroInitialized);
543
557 Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
558
575 static std::unique_ptr<BackingStore> NewBackingStore(
576 Isolate* isolate, size_t byte_length,
577 BackingStoreInitializationMode initialization_mode =
578 BackingStoreInitializationMode::kZeroInitialized,
579 BackingStoreOnFailureMode on_failure =
580 BackingStoreOnFailureMode::kOutOfMemory);
581
590 static std::unique_ptr<BackingStore> NewBackingStore(
591 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
592 void* deleter_data);
593
600 std::shared_ptr<BackingStore> GetBackingStore();
601
606 void* Data() const;
607
609#ifdef V8_ENABLE_CHECKS
610 CheckCast(value);
611#endif
612 return static_cast<SharedArrayBuffer*>(value);
613 }
614
615 static constexpr int kInternalFieldCount =
617
618 private:
620 static void CheckCast(Value* obj);
621};
622
623} // namespace v8
624
625#endif // INCLUDE_V8_ARRAY_BUFFER_H_
Definition: v8-array-buffer.h:427
static ArrayBufferView * Cast(Value *value)
Definition: v8-array-buffer.h:469
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: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)
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:488
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:494
static Local< DataView > New(Local< ArrayBuffer > array_buffer, size_t byte_offset, size_t length)
Definition: v8-data.h:18
Definition: v8-isolate.h:224
Definition: v8-isolate.h:291
Definition: v8-local-handle.h:366
Definition: v8-local-handle.h:734
Definition: v8-maybe.h:39
Definition: v8-memory-span.h:48
Definition: v8-object.h:262
Definition: v8-platform.h:548
Definition: v8-array-buffer.h:509
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:608
Definition: v8-typed-array.h:20
Definition: v8-value.h:32
Definition: v8-internal.h:1510
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:420
#define V8_EXPORT
Definition: v8config.h:854
#define V8_INLINE
Definition: v8config.h:508
#define V8_DEPRECATED(message)
Definition: v8config.h:613
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:678