[llvm-commits] CVS: llvm/lib/System/Win32/Program.inc Win32.h

Jeff Cohen jeffc at jolt-lang.org
Sun Mar 4 21:22:24 PST 2007



Changes in directory llvm/lib/System/Win32:

Program.inc updated: 1.19 -> 1.20
Win32.h updated: 1.10 -> 1.11
---
Log message:

Implement memoryLimit on Windows.

---
Diffs of the changes:  (+52 -6)

 Program.inc |   37 +++++++++++++++++++++++++++++++------
 Win32.h     |   21 +++++++++++++++++++++
 2 files changed, 52 insertions(+), 6 deletions(-)


Index: llvm/lib/System/Win32/Program.inc
diff -u llvm/lib/System/Win32/Program.inc:1.19 llvm/lib/System/Win32/Program.inc:1.20
--- llvm/lib/System/Win32/Program.inc:1.19	Fri Feb 16 13:11:06 2007
+++ llvm/lib/System/Win32/Program.inc	Sun Mar  4 23:22:07 2007
@@ -93,8 +93,8 @@
   if (h == INVALID_HANDLE_VALUE) {
     MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " +
         (fd ? "input: " : "output: "));
-    return h;
   }
+
   return h;
 }
 
@@ -179,7 +179,7 @@
                       0, TRUE, DUPLICATE_SAME_ACCESS);
     }
   }
-
+  
   PROCESS_INFORMATION pi;
   memset(&pi, 0, sizeof(pi));
 
@@ -204,6 +204,35 @@
     return -1;
   }
 
+  // Make sure these get closed no matter what.
+  AutoHandle hProcess(pi.hProcess);
+  AutoHandle hThread(pi.hThread);
+
+  // Assign the process to a job if a memory limit is defined.
+  AutoHandle hJob(0);
+  if (memoryLimit != 0) {
+    hJob = CreateJobObject(0, 0);
+    bool success = false;
+    if (hJob != 0) {
+      JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli;
+      memset(&jeli, 0, sizeof(jeli));
+      jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_MEMORY;
+      jeli.ProcessMemoryLimit = memoryLimit * 1048576;
+      if (SetInformationJobObject(hJob, JobObjectExtendedLimitInformation,
+                                  &jeli, sizeof(jeli))) {
+        if (AssignProcessToJobObject(hJob, pi.hProcess))
+          success = true;
+      }
+    }
+    if (!success) {
+      SetLastError(GetLastError());
+      MakeErrMsg(ErrMsg, std::string("Unable to set memory limit"));
+      TerminateProcess(pi.hProcess, 1);
+      WaitForSingleObject(pi.hProcess, INFINITE);
+      return -1;
+    }
+  }
+
   // Wait for it to terminate.
   DWORD millisecondsToWait = INFINITE;
   if (secondsToWait > 0)
@@ -223,10 +252,6 @@
   rc = GetExitCodeProcess(pi.hProcess, &status);
   err = GetLastError();
 
-  // Done with the handles; go close them.
-  CloseHandle(pi.hProcess);
-  CloseHandle(pi.hThread);
-
   if (!rc) {
     SetLastError(err);
     MakeErrMsg(ErrMsg, std::string("Failed getting status for program '") + 


Index: llvm/lib/System/Win32/Win32.h
diff -u llvm/lib/System/Win32/Win32.h:1.10 llvm/lib/System/Win32/Win32.h:1.11
--- llvm/lib/System/Win32/Win32.h:1.10	Fri Sep  1 15:35:17 2006
+++ llvm/lib/System/Win32/Win32.h	Sun Mar  4 23:22:08 2007
@@ -34,3 +34,24 @@
   LocalFree(buffer);
   return true;
 }
+
+class AutoHandle {
+  HANDLE handle;
+
+public:
+  AutoHandle(HANDLE h) : handle(h) {}
+
+  ~AutoHandle() {
+    if (handle)
+      CloseHandle(handle);
+  }
+
+  operator HANDLE() {
+    return handle;
+  }
+
+  AutoHandle &operator=(HANDLE h) {
+    handle = h;
+    return *this;
+  }
+};






More information about the llvm-commits mailing list