aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2012-12-31 23:23:35 +0000
committerChandler Carruth <chandlerc@gmail.com>2012-12-31 23:23:35 +0000
commit814afe91ccad0e5e1f767303d780fa0318fa5212 (patch)
tree9905492e5626775b81a12370475b4325f59b8e3e
parent02d75477cd9bed5acf46979869f041e7f9726200 (diff)
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171330 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/Support/Process.h20
-rw-r--r--lib/Support/Process.cpp8
-rw-r--r--lib/Support/Unix/Process.inc11
-rw-r--r--lib/Support/Windows/Process.inc14
-rw-r--r--unittests/Support/ProcessTest.cpp2
5 files changed, 43 insertions, 12 deletions
diff --git a/include/llvm/Support/Process.h b/include/llvm/Support/Process.h
index 6b29f57fc1..15fa3763c9 100644
--- a/include/llvm/Support/Process.h
+++ b/include/llvm/Support/Process.h
@@ -89,7 +89,27 @@ class self_process : public process {
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();
};
diff --git a/lib/Support/Process.cpp b/lib/Support/Process.cpp
index 1e21d64e60..9d87b7744b 100644
--- a/lib/Support/Process.cpp
+++ b/lib/Support/Process.cpp
@@ -55,6 +55,14 @@ self_process::~self_process() {
#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"
diff --git a/lib/Support/Unix/Process.inc b/lib/Support/Unix/Process.inc
index e96d37ce56..9ad6272bab 100644
--- a/lib/Support/Unix/Process.inc
+++ b/lib/Support/Unix/Process.inc
@@ -49,10 +49,7 @@ process::id_type self_process::get_id() {
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 @@ Process::GetPageSize()
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;
diff --git a/lib/Support/Windows/Process.inc b/lib/Support/Windows/Process.inc
index d3a0c94e1c..9eb611be87 100644
--- a/lib/Support/Windows/Process.inc
+++ b/lib/Support/Windows/Process.inc
@@ -43,11 +43,9 @@ process::id_type self_process::get_id() {
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 @@ inline unsigned GetPageSizeOnce() {
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()
{
diff --git a/unittests/Support/ProcessTest.cpp b/unittests/Support/ProcessTest.cpp
index d4c4b54ccc..58a7c4acaa 100644
--- a/unittests/Support/ProcessTest.cpp
+++ b/unittests/Support/ProcessTest.cpp
@@ -28,6 +28,8 @@ TEST(ProcessTest, SelfProcess) {
#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