array_buffer.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2013 The Chromium 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. #ifndef GIN_ARRAY_BUFFER_H_
  5. #define GIN_ARRAY_BUFFER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "base/allocator/partition_allocator/partition_alloc.h"
  9. #include "base/compiler_specific.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/shared_memory_mapper.h"
  12. #include "gin/converter.h"
  13. #include "gin/gin_export.h"
  14. #include "v8/include/v8-array-buffer.h"
  15. #include "v8/include/v8-forward.h"
  16. namespace gin {
  17. class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
  18. public:
  19. void* Allocate(size_t length) override;
  20. void* AllocateUninitialized(size_t length) override;
  21. void Free(void* data, size_t length) override;
  22. GIN_EXPORT static ArrayBufferAllocator* SharedInstance();
  23. private:
  24. friend class V8Initializer;
  25. void* AllocateInternal(size_t length, unsigned int flags);
  26. // Initialize the PartitionAlloc partition from which instances of this class
  27. // allocate memory. This is called after initializing V8 since, when enabled,
  28. // the V8 sandbox must be initialized first.
  29. static void InitializePartition();
  30. // The PartitionAlloc partition that instances of this class allocate memory
  31. // chunks from. When the V8 sandbox is enabled, this partition must be placed
  32. // inside of it. For that, PA's ConfigurablePool is created inside the V8
  33. // sandbox during initialization of V8, and this partition is then placed
  34. // inside the configurable pool during InitializePartition().
  35. static partition_alloc::ThreadSafePartitionRoot* partition_;
  36. };
  37. class GIN_EXPORT ArrayBuffer {
  38. public:
  39. ArrayBuffer();
  40. ArrayBuffer(v8::Isolate* isolate, v8::Local<v8::ArrayBuffer> buffer);
  41. ArrayBuffer(const ArrayBuffer&) = delete;
  42. ~ArrayBuffer();
  43. ArrayBuffer& operator=(const ArrayBuffer& other);
  44. void* bytes() const {
  45. return backing_store_ ? backing_store_->Data() : nullptr;
  46. }
  47. size_t num_bytes() const {
  48. return backing_store_ ? backing_store_->ByteLength() : 0;
  49. }
  50. private:
  51. std::shared_ptr<v8::BackingStore> backing_store_;
  52. };
  53. template<>
  54. struct GIN_EXPORT Converter<ArrayBuffer> {
  55. static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
  56. ArrayBuffer* out);
  57. };
  58. class GIN_EXPORT ArrayBufferView {
  59. public:
  60. ArrayBufferView();
  61. ArrayBufferView(v8::Isolate* isolate, v8::Local<v8::ArrayBufferView> view);
  62. ArrayBufferView(const ArrayBufferView&) = delete;
  63. ~ArrayBufferView();
  64. ArrayBufferView& operator=(const ArrayBufferView& other);
  65. void* bytes() const {
  66. return static_cast<uint8_t*>(array_buffer_.bytes()) + offset_;
  67. }
  68. size_t num_bytes() const { return num_bytes_; }
  69. private:
  70. ArrayBuffer array_buffer_;
  71. size_t offset_;
  72. size_t num_bytes_;
  73. };
  74. template<>
  75. struct GIN_EXPORT Converter<ArrayBufferView> {
  76. static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
  77. ArrayBufferView* out);
  78. };
  79. GIN_EXPORT base::SharedMemoryMapper* GetSharedMemoryMapperForArrayBuffers();
  80. } // namespace gin
  81. #endif // GIN_ARRAY_BUFFER_H_