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

Unified Diff: src/d8.cc

Issue 9835055: Properly AdjustAmountOfExternalAllocatedMemory() in d8 (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: addressed comment Created 8 years, 9 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
« no previous file with comments | « no previous file | no next file » | 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 45781cf0d4a4d0c30961505381e3acc2fed7d8cf..75a6c7e37c8c99be4809e24db857a167055b71c5 100644
--- a/src/d8.cc
+++ b/src/d8.cc
@@ -426,14 +426,20 @@ Handle<Value> Shell::CreateExternalArray(const Arguments& args,
}
Persistent<Object> persistent_array = Persistent<Object>::New(array);
- persistent_array.MakeWeak(data, ExternalArrayWeakCallback);
- persistent_array.MarkIndependent();
if (data == NULL && length != 0) {
- data = calloc(length, element_size);
+ // Prepend the size of the allocated chunk to the data itself.
+ int total_size = length * element_size + sizeof(size_t);
+ data = malloc(total_size);
if (data == NULL) {
return ThrowException(String::New("Memory allocation failed."));
}
+ *reinterpret_cast<size_t*>(data) = total_size;
+ data = reinterpret_cast<size_t*>(data) + 1;
+ memset(data, 0, length * element_size);
+ V8::AdjustAmountOfExternalAllocatedMemory(total_size);
}
+ persistent_array.MakeWeak(data, ExternalArrayWeakCallback);
+ persistent_array.MarkIndependent();
array->SetIndexedPropertiesToExternalArrayData(
reinterpret_cast<uint8_t*>(data) + offset, type,
@@ -452,6 +458,9 @@ void Shell::ExternalArrayWeakCallback(Persistent<Value> object, void* data) {
Handle<Object> converted_object = object->ToObject();
Local<Value> prop_value = converted_object->Get(prop_name);
if (data != NULL && !prop_value->IsObject()) {
+ data = reinterpret_cast<size_t*>(data) - 1;
+ V8::AdjustAmountOfExternalAllocatedMemory(
+ -(*reinterpret_cast<size_t*>(data)));
free(data);
}
object.Dispose();
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698