Expose the virtual memory limit to blink

V8 queries the limit before sandbox initialization via static
initializers. We should pass it in like the other memory related values
instead.

BUG=none
R=mark@chromium.org

Review URL: https://codereview.chromium.org/227113011

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@262544 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: 275a29deaf8a70834e4c5dc7d2f9458bedd80154
diff --git a/base/sys_info.h b/base/sys_info.h
index aa40cad..7ce4e65 100644
--- a/base/sys_info.h
+++ b/base/sys_info.h
@@ -28,11 +28,22 @@
   // machine.
   static int64 AmountOfAvailablePhysicalMemory();
 
+  // Return the number of bytes of virtual memory of this process. A return
+  // value of zero means that there is no limit on the available virtual
+  // memory.
+  static int64 AmountOfVirtualMemory();
+
   // Return the number of megabytes of physical memory on the current machine.
   static int AmountOfPhysicalMemoryMB() {
     return static_cast<int>(AmountOfPhysicalMemory() / 1024 / 1024);
   }
 
+  // Return the number of megabytes of available virtual memory, or zero if it
+  // is unlimited.
+  static int AmountOfVirtualMemoryMB() {
+    return static_cast<int>(AmountOfVirtualMemory() / 1024 / 1024);
+  }
+
   // Return the available disk space in bytes on the volume containing |path|,
   // or -1 on failure.
   static int64 AmountOfFreeDiskSpace(const FilePath& path);
diff --git a/base/sys_info_posix.cc b/base/sys_info_posix.cc
index 07d08b7..90baa69 100644
--- a/base/sys_info_posix.cc
+++ b/base/sys_info_posix.cc
@@ -7,6 +7,7 @@
 #include <errno.h>
 #include <string.h>
 #include <sys/param.h>
+#include <sys/resource.h>
 #include <sys/utsname.h>
 #include <unistd.h>
 
@@ -45,6 +46,20 @@
     g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER;
 #endif
 
+int64 AmountOfVirtualMemory() {
+  struct rlimit limit;
+  int result = getrlimit(RLIMIT_DATA, &limit);
+  if (result != 0) {
+    NOTREACHED();
+    return 0;
+  }
+  return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur;
+}
+
+base::LazyInstance<
+    base::internal::LazySysInfoValue<int64, AmountOfVirtualMemory> >::Leaky
+    g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER;
+
 }  // namespace
 
 namespace base {
@@ -56,6 +71,11 @@
 #endif
 
 // static
+int64 SysInfo::AmountOfVirtualMemory() {
+  return g_lazy_virtual_memory.Get().value();
+}
+
+// static
 int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
   base::ThreadRestrictions::AssertIOAllowed();
 
diff --git a/base/sys_info_unittest.cc b/base/sys_info_unittest.cc
index 93eff77..e771b2b 100644
--- a/base/sys_info_unittest.cc
+++ b/base/sys_info_unittest.cc
@@ -29,6 +29,8 @@
   // We aren't actually testing that it's correct, just that it's sane.
   EXPECT_GT(base::SysInfo::AmountOfPhysicalMemory(), 0);
   EXPECT_GT(base::SysInfo::AmountOfPhysicalMemoryMB(), 0);
+  // The maxmimal amount of virtual memory can be zero which means unlimited.
+  EXPECT_GE(base::SysInfo::AmountOfVirtualMemory(), 0);
 }
 
 TEST_F(SysInfoTest, AmountOfFreeDiskSpace) {