array_buffer.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "gin/array_buffer.h"
  5. #include <stddef.h>
  6. #include <stdlib.h>
  7. #include "base/allocator/partition_allocator/page_allocator.h"
  8. #include "base/check_op.h"
  9. #include "build/build_config.h"
  10. #include "gin/per_isolate_data.h"
  11. #if BUILDFLAG(IS_POSIX)
  12. #include <sys/mman.h>
  13. #ifndef MAP_ANONYMOUS
  14. #define MAP_ANONYMOUS MAP_ANON
  15. #endif
  16. #endif // BUILDFLAG(IS_POSIX)
  17. namespace gin {
  18. static_assert(V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT == 2,
  19. "array buffers must have two internal fields");
  20. // ArrayBufferAllocator -------------------------------------------------------
  21. void* ArrayBufferAllocator::Allocate(size_t length) {
  22. // TODO(bbudge) Use partition allocator for malloc/calloc allocations.
  23. return calloc(1, length);
  24. }
  25. void* ArrayBufferAllocator::AllocateUninitialized(size_t length) {
  26. return malloc(length);
  27. }
  28. void ArrayBufferAllocator::Free(void* data, size_t length) {
  29. free(data);
  30. }
  31. ArrayBufferAllocator* ArrayBufferAllocator::SharedInstance() {
  32. static ArrayBufferAllocator* instance = new ArrayBufferAllocator();
  33. return instance;
  34. }
  35. // ArrayBuffer ----------------------------------------------------------------
  36. ArrayBuffer::ArrayBuffer() = default;
  37. ArrayBuffer::ArrayBuffer(v8::Isolate* isolate, v8::Local<v8::ArrayBuffer> array)
  38. : backing_store_(array->GetBackingStore()) {}
  39. ArrayBuffer::~ArrayBuffer() = default;
  40. ArrayBuffer& ArrayBuffer::operator=(const ArrayBuffer& other) = default;
  41. // Converter<ArrayBuffer> -----------------------------------------------------
  42. bool Converter<ArrayBuffer>::FromV8(v8::Isolate* isolate,
  43. v8::Local<v8::Value> val,
  44. ArrayBuffer* out) {
  45. if (!val->IsArrayBuffer())
  46. return false;
  47. *out = ArrayBuffer(isolate, v8::Local<v8::ArrayBuffer>::Cast(val));
  48. return true;
  49. }
  50. // ArrayBufferView ------------------------------------------------------------
  51. ArrayBufferView::ArrayBufferView()
  52. : offset_(0),
  53. num_bytes_(0) {
  54. }
  55. ArrayBufferView::ArrayBufferView(v8::Isolate* isolate,
  56. v8::Local<v8::ArrayBufferView> view)
  57. : array_buffer_(isolate, view->Buffer()),
  58. offset_(view->ByteOffset()),
  59. num_bytes_(view->ByteLength()) {
  60. }
  61. ArrayBufferView::~ArrayBufferView() = default;
  62. ArrayBufferView& ArrayBufferView::operator=(const ArrayBufferView& other) =
  63. default;
  64. // Converter<ArrayBufferView> -------------------------------------------------
  65. bool Converter<ArrayBufferView>::FromV8(v8::Isolate* isolate,
  66. v8::Local<v8::Value> val,
  67. ArrayBufferView* out) {
  68. if (!val->IsArrayBufferView())
  69. return false;
  70. *out = ArrayBufferView(isolate, v8::Local<v8::ArrayBufferView>::Cast(val));
  71. return true;
  72. }
  73. } // namespace gin