[llvm-commits] [llvm] r171330 - in /llvm/trunk: include/llvm/Support/Process.h lib/Support/Process.cpp lib/Support/Unix/Process.inc lib/Support/Windows/Process.inc unittests/Support/ProcessTest.cpp

Chandler Carruth chandlerc at gmail.com
Mon Dec 31 15:23:35 PST 2012


Author: chandlerc
Date: Mon Dec 31 17:23:35 2012
New Revision: 171330

URL: http://llvm.org/viewvc/llvm-project?rev=171330&view=rev
Log:
Flesh out a page size accessor in the new API.

Implement the old API in terms of the new one. This simplifies the
implementation on Windows which can now re-use the self_process's once
initialization.

Modified:
    llvm/trunk/include/llvm/Support/Process.h
    llvm/trunk/lib/Support/Process.cpp
    llvm/trunk/lib/Support/Unix/Process.inc
    llvm/trunk/lib/Support/Windows/Process.inc
    llvm/trunk/unittests/Support/ProcessTest.cpp

Modified: llvm/trunk/include/llvm/Support/Process.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Process.h?rev=171330&r1=171329&r2=171330&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Process.h (original)
+++ llvm/trunk/include/llvm/Support/Process.h Mon Dec 31 17:23:35 2012
@@ -89,7 +89,27 @@
 public:
   virtual id_type get_id();
 
+  /// \name Process configuration (sysconf on POSIX)
+  /// @{
+
+  /// \brief Get the virtual memory page size.
+  ///
+  /// Query the operating system for this process's page size.
+  size_t page_size() const { return PageSize; };
+
+  /// @}
+
 private:
+  /// \name Cached process state.
+  /// @{
+
+  /// \brief Cached page size, this cannot vary during the life of the process.
+  size_t PageSize;
+
+  /// @}
+
+  /// \brief Constructor, used by \c process::get_self() only.
+  self_process();
 };
 
 

Modified: llvm/trunk/lib/Support/Process.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Process.cpp?rev=171330&r1=171329&r2=171330&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Process.cpp (original)
+++ llvm/trunk/lib/Support/Process.cpp Mon Dec 31 17:23:35 2012
@@ -55,6 +55,14 @@
 #endif
 
 
+//===----------------------------------------------------------------------===//
+// Implementations of legacy functions in terms of the new self_process object.
+
+unsigned Process::GetPageSize() {
+  return process::get_self()->page_size();
+}
+
+
 // Include the platform-specific parts of this class.
 #ifdef LLVM_ON_UNIX
 #include "Unix/Process.inc"

Modified: llvm/trunk/lib/Support/Unix/Process.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/Process.inc?rev=171330&r1=171329&r2=171330&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/Process.inc (original)
+++ llvm/trunk/lib/Support/Unix/Process.inc Mon Dec 31 17:23:35 2012
@@ -49,10 +49,7 @@
   return getpid();
 }
 
-
-unsigned
-Process::GetPageSize()
-{
+static unsigned getPageSize() {
 #if defined(__CYGWIN__)
   // On Cygwin, getpagesize() returns 64k but the page size for the purposes of
   // memory protection and mmap() is 4k.
@@ -68,6 +65,12 @@
   return static_cast<unsigned>(page_size);
 }
 
+// This constructor guaranteed to be run exactly once on a single thread, and
+// sets up various process invariants that can be queried cheaply from then on.
+self_process::self_process() : PageSize(getPageSize()) {
+}
+
+
 size_t Process::GetMallocUsage() {
 #if defined(HAVE_MALLINFO)
   struct mallinfo mi;

Modified: llvm/trunk/lib/Support/Windows/Process.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/Process.inc?rev=171330&r1=171329&r2=171330&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Windows/Process.inc (original)
+++ llvm/trunk/lib/Support/Windows/Process.inc Mon Dec 31 17:23:35 2012
@@ -43,11 +43,9 @@
   return GetCurrentProcess();
 }
 
-
 // This function retrieves the page size using GetSystemInfo and is present
-// solely so it can be called once in Process::GetPageSize to initialize the
-// static variable PageSize.
-inline unsigned GetPageSizeOnce() {
+// solely so it can be called once to initialize the self_process member below.
+static unsigned getPageSize() {
   // NOTE: A 32-bit application running under WOW64 is supposed to use
   // GetNativeSystemInfo.  However, this interface is not present prior
   // to Windows XP so to use it requires dynamic linking.  It is not clear
@@ -58,12 +56,12 @@
   return static_cast<unsigned>(info.dwPageSize);
 }
 
-unsigned
-Process::GetPageSize() {
-  static const unsigned PageSize = GetPageSizeOnce();
-  return PageSize;
+// This constructor guaranteed to be run exactly once on a single thread, and
+// sets up various process invariants that can be queried cheaply from then on.
+self_process::self_process() : PageSize(getPageSize()) {
 }
 
+
 size_t
 Process::GetMallocUsage()
 {

Modified: llvm/trunk/unittests/Support/ProcessTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ProcessTest.cpp?rev=171330&r1=171329&r2=171330&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ProcessTest.cpp (original)
+++ llvm/trunk/unittests/Support/ProcessTest.cpp Mon Dec 31 17:23:35 2012
@@ -28,6 +28,8 @@
 #elif defined(LLVM_ON_WIN32)
   EXPECT_EQ(GetCurrentProcess(), process::get_self()->get_id());
 #endif
+
+  EXPECT_LT(1u, process::get_self()->page_size());
 }
 
 } // end anonymous namespace





More information about the llvm-commits mailing list