Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
feat: implement ArrayBuffer realloc (#669)
Browse files Browse the repository at this point in the history
* feat: implement ArrayBuffer realloc
  • Loading branch information
codebytere committed Aug 31, 2018
1 parent 4b0a023 commit 1fff3a0
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
11 changes: 11 additions & 0 deletions patches/common/chromium/.patches.yaml
@@ -1,5 +1,16 @@
repo: src
patches:
-
author: Shelley Vohr <shelley.vohr@gmail.com>
file: add_realloc.patch
description: |
Blink overrides ArrayBuffer's allocator with its own one, while Node simply
uses malloc and free, so we need to use v8's allocator in Node. As part of the
10.6.0 upgrade, we needed to make SerializerDelegate accept an allocator
argument in its constructor, and override ReallocateBufferMemory and
FreeBufferMemory to use the allocator. We cannot simply allocate and then memcpy
when we override ReallocateBufferMemory, so we therefore need to implement
Realloc on the v8 side and correspondingly in gin.
-
author: Ales Pergl <alpergl@microsoft.com>
file: build_gn.patch
Expand Down
70 changes: 70 additions & 0 deletions patches/common/chromium/add_realloc.patch
@@ -0,0 +1,70 @@
diff --git a/gin/array_buffer.cc b/gin/array_buffer.cc
index f84934bfd712..63bce16a9d06 100644
--- a/gin/array_buffer.cc
+++ b/gin/array_buffer.cc
@@ -43,6 +43,10 @@ void* ArrayBufferAllocator::AllocateUninitialized(size_t length) {
return malloc(length);
}

+void* ArrayBufferAllocator::Realloc(void* data, size_t length) {
+ return realloc(data, length);
+}
+
void ArrayBufferAllocator::Free(void* data, size_t length) {
free(data);
}
diff --git a/gin/array_buffer.h b/gin/array_buffer.h
index 2aef366ac819..c037808a9bb3 100644
--- a/gin/array_buffer.h
+++ b/gin/array_buffer.h
@@ -21,6 +21,7 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
void* Allocate(size_t length) override;
void* AllocateUninitialized(size_t length) override;
+ void* Realloc(void* data, size_t length) override;
void Free(void* data, size_t length) override;

GIN_EXPORT static ArrayBufferAllocator* SharedInstance();
diff --git a/third_party/blink/renderer/platform/wtf/typed_arrays/array_buffer_contents.cc b/third_party/blink/renderer/platform/wtf/typed_arrays/array_buffer_contents.cc
index 053babce1051..e33d6d4ceb5a 100644
--- a/third_party/blink/renderer/platform/wtf/typed_arrays/array_buffer_contents.cc
+++ b/third_party/blink/renderer/platform/wtf/typed_arrays/array_buffer_contents.cc
@@ -121,6 +121,11 @@ void* ArrayBufferContents::AllocateMemoryOrNull(size_t size,
return AllocateMemoryWithFlags(size, policy, base::PartitionAllocReturnNull);
}

+void* ArrayBufferContents::Realloc(void* data, size_t size) {
+ return Partitions::ArrayBufferPartition()->Realloc(data, size,
+ WTF_HEAP_PROFILER_TYPE_NAME(ArrayBufferContents));
+}
+
void ArrayBufferContents::FreeMemory(void* data) {
Partitions::ArrayBufferPartition()->Free(data);
}
diff --git a/third_party/blink/renderer/platform/wtf/typed_arrays/array_buffer_contents.h b/third_party/blink/renderer/platform/wtf/typed_arrays/array_buffer_contents.h
index 809229caa872..6248ad32d6b0 100644
--- a/third_party/blink/renderer/platform/wtf/typed_arrays/array_buffer_contents.h
+++ b/third_party/blink/renderer/platform/wtf/typed_arrays/array_buffer_contents.h
@@ -178,6 +178,7 @@ class WTF_EXPORT ArrayBufferContents {
void CopyTo(ArrayBufferContents& other);

static void* AllocateMemoryOrNull(size_t, InitializationPolicy);
+ static void* Realloc(void* data, size_t);
static void FreeMemory(void*);
static DataHandle CreateDataHandle(size_t, InitializationPolicy);
static void Initialize(
diff --git a/third_party/blink/renderer/bindings/core/v8/v8_initializer.cc b/third_party/blink/renderer/bindings/core/v8/v8_initializer.cc
index cf2762ede559..f065b5ebafb8 100644
--- a/third_party/blink/renderer/bindings/core/v8/v8_initializer.cc
+++ b/third_party/blink/renderer/bindings/core/v8/v8_initializer.cc
@@ -555,6 +555,10 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
size, WTF::ArrayBufferContents::kDontInitialize);
}

+ void* Realloc(void* data, size_t size) override {
+ return WTF::ArrayBufferContents::Realloc(data, size);
+ }
+
void Free(void* data, size_t size) override {
WTF::ArrayBufferContents::FreeMemory(data);
}
11 changes: 11 additions & 0 deletions patches/common/v8/.patches.yaml
@@ -1,5 +1,16 @@
repo: src/v8
patches:
-
author: Shelley Vohr <shelley.vohr@gmail.com>
file: add_realloc.patch
description: |
Blink overrides ArrayBuffer's allocator with its own one, while Node simply
uses malloc and free, so we need to use v8's allocator in Node. As part of the
10.6.0 upgrade, we needed to make SerializerDelegate accept an allocator
argument in its constructor, and override ReallocateBufferMemory and
FreeBufferMemory to use the allocator. We cannot simply allocate and then memcpy
when we override ReallocateBufferMemory, so we therefore need to implement
Realloc on the v8 side.
-
author: Ales Pergl <alpergl@microsoft.com>
file: build_gn.patch
Expand Down
33 changes: 33 additions & 0 deletions patches/common/v8/add_realloc.patch
@@ -0,0 +1,33 @@
diff --git a/include/v8.h b/include/v8.h
index 573e80176d..5eefe26fe9 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -4318,6 +4318,13 @@ class V8_EXPORT ArrayBuffer : public Object {
*/
virtual void* AllocateUninitialized(size_t length) = 0;

+ /**
+ * Free the memory block of size |length|, pointed to by |data|.
+ * That memory must be previously allocated by |Allocate| and not yet freed
+ * with a call to |Free| or |Realloc|
+ */
+ virtual void* Realloc(void* data, size_t length);
+
/**
* Free the memory block of size |length|, pointed to by |data|.
* That memory is guaranteed to be previously allocated by |Allocate|.
diff --git a/src/api.cc b/src/api.cc
index 8b177d041d..e06ca2a207 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -460,6 +460,10 @@ void V8::SetSnapshotDataBlob(StartupData* snapshot_blob) {
i::V8::SetSnapshotBlob(snapshot_blob);
}

+void* v8::ArrayBuffer::Allocator::Realloc(void* data, size_t length) {
+ UNIMPLEMENTED();
+}
+
namespace {

class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {

0 comments on commit 1fff3a0

Please sign in to comment.