Reviewers: Jakob,

Message:
Jakob: I chatted with Daniel and we decided to go with the callback mechanism.
PTAL.

Description:
Provide a mechanism for the embedder to set a TotalPhysicalMemory callback
function.

This is necessary due to the fact that sysconf (used by OS::TotalPhysicalMemory)
is blocked by the Chromium renderer's sandbox.

BUG=None

Please review this at https://codereview.chromium.org/29053002/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files (+36, -2 lines):
  M include/v8.h
  M src/api.cc
  M src/defaults.cc
  M src/isolate.h
  M src/isolate.cc


Index: include/v8.h
diff --git a/include/v8.h b/include/v8.h
index 1ec04816871f8000ba7032654b71ae7bcb9f0802..2c7a9a8a7fc40fe0d635ef4f986f66d137cf48e9 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -3844,6 +3844,8 @@ typedef void (*MemoryAllocationCallback)(ObjectSpace space,
                                          AllocationAction action,
                                          int size);

+typedef uint64_t (*TotalPhysicalMemoryCallback)();
+
 // --- Leave Script Callback ---
 typedef void (*CallCompletedCallback)();

@@ -4467,10 +4469,16 @@ class V8_EXPORT V8 {
   static void SetCreateHistogramFunction(CreateHistogramCallback);
   static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);

-  /** Callback function for reporting failed access checks.*/
+  /** Callback function for reporting failed access checks. */
static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);

   /**
+ * Enables the host application to provide a function which returns the total
+   * amount of physical memory available on the platform.
+   */
+  static void SetTotalPhysicalMemoryFunction(TotalPhysicalMemoryCallback);
+
+  /**
    * Enables the host application to receive a notification before a
    * garbage collection.  Allocations are not allowed in the
    * callback function, you therefore cannot manipulate objects (set
Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index 469c7a1814df85e35e1e409684be5c22efa3437c..eb794281978ef7c4596829c323ce5e717539c86a 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -6436,6 +6436,12 @@ void V8::RemoveGCEpilogueCallback(GCEpilogueCallback callback) {
 }


+void V8::SetTotalPhysicalMemoryFunction(TotalPhysicalMemoryCallback callback) {
+  i::Isolate* isolate = i::Isolate::Current();
+  isolate->SetTotalPhysicalMemoryCallback(callback);
+}
+
+
 void V8::AddMemoryAllocationCallback(MemoryAllocationCallback callback,
                                      ObjectSpace space,
                                      AllocationAction action) {
Index: src/defaults.cc
diff --git a/src/defaults.cc b/src/defaults.cc
index cbbe53729ebd171772106b589ae8b8c6fb4488d1..913d094ad59a2a6c97aed80ffa6f2f3a8959930b 100644
--- a/src/defaults.cc
+++ b/src/defaults.cc
@@ -50,7 +50,15 @@ bool ConfigureResourceConstraintsForCurrentPlatform(
     return false;
   }

-  uint64_t physical_memory = i::OS::TotalPhysicalMemory();
+  uint64_t physical_memory;
+  i::Isolate* isolate = i::Isolate::UncheckedCurrent();
+  if (isolate != NULL &&
+ isolate->thread_local_top()->total_physical_memory_callback_ != NULL) {
+    physical_memory =
+        isolate->thread_local_top()->total_physical_memory_callback_();
+  } else {
+    physical_memory = i::OS::TotalPhysicalMemory();
+  }
   int lump_of_memory = (i::kPointerSize / 4) * i::MB;

// The young_space_size should be a power of 2 and old_generation_size should
Index: src/isolate.cc
diff --git a/src/isolate.cc b/src/isolate.cc
index 29ead298196a765e8ca856628bf301ef7d263a8a..47a795aa185d21c5aa48fdd98c19fcf9f6d27d22 100644
--- a/src/isolate.cc
+++ b/src/isolate.cc
@@ -102,6 +102,7 @@ void ThreadLocalTop::InitializeInternal() {
   thread_id_ = ThreadId::Invalid();
   external_caught_exception_ = false;
   failed_access_check_callback_ = NULL;
+  total_physical_memory_callback_ = NULL;
   save_context_ = NULL;
   catcher_ = NULL;
   top_lookup_result_ = NULL;
@@ -962,6 +963,12 @@ void Isolate::ReportFailedAccessCheck(JSObject* receiver, v8::AccessType type) {
 }


+void Isolate::SetTotalPhysicalMemoryCallback(
+      v8::TotalPhysicalMemoryCallback callback) {
+  thread_local_top()->total_physical_memory_callback_ = callback;
+}
+
+
 enum MayAccessDecision {
   YES, NO, UNKNOWN
 };
Index: src/isolate.h
diff --git a/src/isolate.h b/src/isolate.h
index 6693b578f5aaaffe5396d9550a3627a4d1853234..24d482950f62f080cb61acac45ad6903f9b29703 100644
--- a/src/isolate.h
+++ b/src/isolate.h
@@ -287,6 +287,9 @@ class ThreadLocalTop BASE_EMBEDDED {
   // Call back function to report unsafe JS accesses.
   v8::FailedAccessCheckCallback failed_access_check_callback_;

+  // Call back function to get total physical memory of the platform.
+  v8::TotalPhysicalMemoryCallback total_physical_memory_callback_;
+
   // Head of the list of live LookupResults.
   LookupResult* top_lookup_result_;

@@ -773,6 +776,8 @@ class Isolate {
void SetFailedAccessCheckCallback(v8::FailedAccessCheckCallback callback);
   void ReportFailedAccessCheck(JSObject* receiver, v8::AccessType type);

+ void SetTotalPhysicalMemoryCallback(v8::TotalPhysicalMemoryCallback callback);
+
   // Exception throwing support. The caller should use the result
   // of Throw() as its return value.
   Failure* Throw(Object* exception, MessageLocation* location = NULL);


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to