Revision: 11144
Author:   [email protected]
Date:     Mon Mar 26 06:24:20 2012
Log:      Properly AdjustAmountOfExternalAllocatedMemory() in d8

This is related to v8 issue 2022 but doesn't fix it as this patch only affects d8, while there is a related bug in the WebKit V8 bindings too.

Review URL: https://chromiumcodereview.appspot.com/9835055
http://code.google.com/p/v8/source/detail?r=11144

Modified:
 /branches/bleeding_edge/src/d8.cc

=======================================
--- /branches/bleeding_edge/src/d8.cc   Fri Mar 23 06:52:15 2012
+++ /branches/bleeding_edge/src/d8.cc   Mon Mar 26 06:24:20 2012
@@ -426,14 +426,20 @@
   }

   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 @@
   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();

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to