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 "v8config.h" // NOLINT(build/include_directory)
16
17namespace v8 {
18
19class SharedArrayBuffer;
20
21#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
22// Defined using gn arg `v8_array_buffer_internal_field_count`.
23#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
24#endif
25
28
43 public:
45
51 void* Data() const;
52
56 size_t ByteLength() const;
57
65 size_t MaxByteLength() const;
66
71 bool IsShared() const;
72
79
85 void operator delete(void* ptr) { ::operator delete(ptr); }
86
93 "Reallocate is unsafe, please do not use. Please allocate a new "
94 "BackingStore and copy instead.")
95 static std::unique_ptr<BackingStore> Reallocate(
96 v8::Isolate* isolate, std::unique_ptr<BackingStore> backing_store,
97 size_t byte_length);
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
188 "Reallocate is unsafe, please do not use. Please allocate new memory "
189 "and copy instead.")
190 virtual void* Reallocate(void* data, size_t old_length, size_t new_length);
191
197 enum class AllocationMode { kNormal, kReservation };
198
209 };
210
214 size_t ByteLength() const;
215
219 size_t MaxByteLength() const;
220
229 Isolate* isolate, size_t byte_length,
230 BackingStoreInitializationMode initialization_mode =
231 BackingStoreInitializationMode::kZeroInitialized);
232
240 Isolate* isolate, size_t byte_length,
241 BackingStoreInitializationMode initialization_mode =
242 BackingStoreInitializationMode::kZeroInitialized);
243
257 std::shared_ptr<BackingStore> backing_store);
258
269 static std::unique_ptr<BackingStore> NewBackingStore(
270 Isolate* isolate, size_t byte_length,
271 BackingStoreInitializationMode initialization_mode =
272 BackingStoreInitializationMode::kZeroInitialized);
281 static std::unique_ptr<BackingStore> NewBackingStore(
282 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
283 void* deleter_data);
284
297 static std::unique_ptr<BackingStore> NewResizableBackingStore(
298 size_t byte_length, size_t max_byte_length);
299
303 bool IsDetachable() const;
304
308 bool WasDetached() const;
309
317 "Use the version which takes a key parameter (passing a null handle is "
318 "ok).")
319 void Detach();
320
330
334 void SetDetachKey(v8::Local<v8::Value> key);
335
345 std::shared_ptr<BackingStore> GetBackingStore();
346
351 bool IsResizableByUserJavaScript() const;
352
357 void* Data() const;
358
359 V8_INLINE static ArrayBuffer* Cast(Value* value) {
360#ifdef V8_ENABLE_CHECKS
361 CheckCast(value);
362#endif
363 return static_cast<ArrayBuffer*>(value);
364 }
365
366 static constexpr int kInternalFieldCount =
368 static constexpr int kEmbedderFieldCount = kInternalFieldCount;
369
370 private:
371 ArrayBuffer();
372 static void CheckCast(Value* obj);
373};
374
375#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
376// Defined using gn arg `v8_array_buffer_view_internal_field_count`.
377#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
378#endif
379
385 public:
393 size_t ByteOffset();
397 size_t ByteLength();
398
408 size_t CopyContents(void* dest, size_t byte_length);
409
419
424 bool HasBuffer() const;
425
427#ifdef V8_ENABLE_CHECKS
428 CheckCast(value);
429#endif
430 return static_cast<ArrayBufferView*>(value);
431 }
432
433 static constexpr int kInternalFieldCount =
435 static const int kEmbedderFieldCount = kInternalFieldCount;
436
437 private:
439 static void CheckCast(Value* obj);
440};
441
446 public:
448 size_t byte_offset, size_t length);
449 static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
450 size_t byte_offset, size_t length);
451 V8_INLINE static DataView* Cast(Value* value) {
452#ifdef V8_ENABLE_CHECKS
453 CheckCast(value);
454#endif
455 return static_cast<DataView*>(value);
456 }
457
458 private:
459 DataView();
460 static void CheckCast(Value* obj);
461};
462
467 public:
471 size_t ByteLength() const;
472
476 size_t MaxByteLength() const;
477
485 Isolate* isolate, size_t byte_length,
486 BackingStoreInitializationMode initialization_mode =
487 BackingStoreInitializationMode::kZeroInitialized);
488
502 Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
503
514 static std::unique_ptr<BackingStore> NewBackingStore(
515 Isolate* isolate, size_t byte_length,
516 BackingStoreInitializationMode initialization_mode =
517 BackingStoreInitializationMode::kZeroInitialized);
526 static std::unique_ptr<BackingStore> NewBackingStore(
527 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
528 void* deleter_data);
529
536 std::shared_ptr<BackingStore> GetBackingStore();
537
542 void* Data() const;
543
545#ifdef V8_ENABLE_CHECKS
546 CheckCast(value);
547#endif
548 return static_cast<SharedArrayBuffer*>(value);
549 }
550
551 static constexpr int kInternalFieldCount =
553
554 private:
556 static void CheckCast(Value* obj);
557};
558
559} // namespace v8
560
561#endif // INCLUDE_V8_ARRAY_BUFFER_H_
Definition: v8-array-buffer.h:384
static ArrayBufferView * Cast(Value *value)
Definition: v8-array-buffer.h:426
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
AllocationMode
Definition: v8-array-buffer.h:197
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)
static std::unique_ptr< BackingStore > NewBackingStore(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
bool IsDetachable() const
bool WasDetached() const
static Local< ArrayBuffer > New(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
size_t ByteLength() const
static MaybeLocal< ArrayBuffer > MaybeNew(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
Definition: v8-array-buffer.h:42
size_t MaxByteLength() const
bool IsShared() const
size_t ByteLength() const
void(*)(void *data, size_t length, void *deleter_data) DeleterCallback
Definition: v8-array-buffer.h:105
bool IsResizableByUserJavaScript() const
void * Data() const
Definition: v8-array-buffer.h:445
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:451
static Local< DataView > New(Local< ArrayBuffer > array_buffer, size_t byte_offset, size_t length)
Definition: v8-data.h:18
Definition: v8-isolate.h:261
Definition: v8-local-handle.h:266
Definition: v8-local-handle.h:632
Definition: v8-maybe.h:32
Definition: v8-memory-span.h:64
Definition: v8-object.h:233
Definition: v8-array-buffer.h:466
void * Data() const
static Local< SharedArrayBuffer > New(Isolate *isolate, std::shared_ptr< BackingStore > backing_store)
std::shared_ptr< BackingStore > GetBackingStore()
static std::unique_ptr< BackingStore > NewBackingStore(void *data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, void *deleter_data)
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:544
static std::unique_ptr< BackingStore > NewBackingStore(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
Definition: v8-value.h:32
Definition: v8-internal.h:1297
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:27
ArrayBufferCreationMode
Definition: v8-array-buffer.h:26
#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
Definition: v8-array-buffer.h:23
#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
Definition: v8-array-buffer.h:377
#define V8_EXPORT
Definition: v8config.h:793
#define V8_INLINE
Definition: v8config.h:499
#define V8_DEPRECATED(message)
Definition: v8config.h:595
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:660