Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Unified Diff: src/d8.cc

Issue 15001041: Externalization API for ArrayBuffer (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: CR feedback Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« include/v8.h ('K') | « src/bootstrapper.cc ('k') | src/objects.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/d8.cc
diff --git a/src/d8.cc b/src/d8.cc
index b95432e269b5d767c809b22004e82270c39d7852..3a4ba3b2e9cc8745fd3739ca1a25b297a8e9d6c6 100644
--- a/src/d8.cc
+++ b/src/d8.cc
@@ -1048,6 +1048,16 @@ static char* ReadChars(Isolate* isolate, const char* name, int* size_out) {
return chars;
}
+static void ReadBufferWeakCallback(v8::Isolate* isolate,
+ Persistent<Value>* object,
+ uint8_t* data) {
+ size_t byte_length = ArrayBuffer::Cast(**object)->ByteLength();
+ isolate->AdjustAmountOfExternalAllocatedMemory(
+ -static_cast<intptr_t>(byte_length));
+
+ delete[] data;
+ object->Dispose(isolate);
+}
Handle<Value> Shell::ReadBuffer(const Arguments& args) {
ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT
@@ -1057,14 +1067,19 @@ Handle<Value> Shell::ReadBuffer(const Arguments& args) {
return Throw("Error loading file");
}
+ Isolate* isolate = args.GetIsolate();
uint8_t* data = reinterpret_cast<uint8_t*>(
ReadChars(args.GetIsolate(), *filename, &length));
if (data == NULL) {
return Throw("Error reading file");
}
- Handle<v8::ArrayBuffer> buffer = ArrayBuffer::New(length);
- memcpy(buffer->Data(), data, length);
- delete[] data;
+ Handle<v8::ArrayBuffer> buffer = ArrayBuffer::New(data, length);
+ v8::Persistent<v8::Value> weak_handle =
+ v8::Persistent<v8::Value>::New(isolate, buffer);
+ weak_handle.MakeWeak(isolate, data, ReadBufferWeakCallback);
+ weak_handle.MarkIndependent();
+ isolate->AdjustAmountOfExternalAllocatedMemory(length);
+
return buffer;
}
« include/v8.h ('K') | « src/bootstrapper.cc ('k') | src/objects.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698