Simplify the sys::Memory interface per Chris' request.
authorReid Spencer <rspencer@reidspencer.com>
Mon, 13 Sep 2004 22:38:11 +0000 (22:38 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Mon, 13 Sep 2004 22:38:11 +0000 (22:38 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16318 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/System/Memory.h
lib/ExecutionEngine/JIT/JITEmitter.cpp
lib/System/AIX/Memory.cpp
lib/System/Cygwin/Memory.cpp
lib/System/Darwin/Memory.cpp
lib/System/FreeBSD/Memory.cpp
lib/System/Interix/Memory.cpp
lib/System/Linux/Memory.cpp
lib/System/SunOS/Memory.cpp
lib/System/Win32/Memory.cpp
lib/System/Win32/Memory.inc

index 5b3c884cd78e2c9a2c6df565147913803212dfe6..3b319cb3bafa28c91c003e0c2d2381bd55176142 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_SYSTEM_PATH_H
-#define LLVM_SYSTEM_PATH_H
-
-#include <string>
+#ifndef LLVM_SYSTEM_MEMORY_H
+#define LLVM_SYSTEM_MEMORY_H
 
 namespace llvm {
 namespace sys {
 
-  /// This class provides an abstraction for various memory handling functions
+  /// This class encapsulates the notion of a memory block which has an address
+  /// and a size. It is used by the Memory class (a friend) as the result of
+  /// various memory allocation operations.
+  /// @see Memory
+  /// @brief Memory block abstraction.
+  class MemoryBlock {
+  public:
+    void* base() const { return Address; }
+    unsigned size() const { return Size; }
+  private:
+    void * Address;   ///< Address of first byte of memory area
+    unsigned Size;    ///< Size, in bytes of the memory area
+    friend class Memory;
+  };
+
+  /// This class provides various memory handling functions that manipulate
+  /// MemoryBlock instances.
   /// @since 1.4
-  /// @brief An abstraction for operating system paths.
+  /// @brief An abstraction for memory operations.
   class Memory {
     /// @name Functions
     /// @{
     public:
-      Memory() { Address = 0; AllocSize = 0; }
-      ~Memory() { ReleaseRWX(*this); }
-
-      /// @throws std::string if an error occurred
-      static void* AllocateRWX(Memory& block, unsigned NumBytes);
-
-      /// @throws std::string if an error occurred
-      static void ReleaseRWX(Memory& block);
-
-      char* base() const { return reinterpret_cast<char*>(Address); }
-      unsigned size() const { return AllocSize; }
-    /// @}
-    /// @name Data
-    /// @{
-    private:
-      void * Address;        // Address of first byte of memory area
-      unsigned AllocSize;    // Size, in bytes of the memory area
+      /// This method allocates a block of Read/Write/Execute memory that is
+      /// suitable for executing dynamically generated code (e.g. JIT). An
+      /// attempt to allocate \p NumBytes bytes of virtual memory is made. 
+      /// @throws std::string if an error occurred.
+      /// @brief Allocate Read/Write/Execute memory.
+      static MemoryBlock AllocateRWX(unsigned NumBytes);
+
+      /// This method releases a block of Read/Write/Execute memory that was
+      /// allocated with the AllocateRWX method. It should not be used to release
+      /// any memory block allocated any other way.
+      /// @throws std::string if an error occurred.
+      /// @brief Release Read/Write/Execute memory.
+      static void ReleaseRWX(MemoryBlock& block);
     /// @}
   };
 }
index 3ce5765cf4952096b4cebd5f9d455f18ed456e51..a0e7a946092aa01d8288f41fa62f347ce30c474e 100644 (file)
@@ -38,7 +38,7 @@ namespace {
   /// are emitting is.  This never bothers to release the memory, because when
   /// we are ready to destroy the JIT, the program exits.
   class JITMemoryManager {
-    sys::Memory  MemBlock;       // Virtual memory block allocated RWX
+    sys::MemoryBlock  MemBlock;  // Virtual memory block allocated RWX
     unsigned char *MemBase;      // Base of block of memory, start of stub mem
     unsigned char *FunctionBase; // Start of the function body area
     unsigned char *CurStubPtr, *CurFunctionPtr;
@@ -53,7 +53,7 @@ namespace {
 
 JITMemoryManager::JITMemoryManager() {
   // Allocate a 16M block of memory...
-  sys::Memory::AllocateRWX(MemBlock,(16 << 20));
+  MemBlock = sys::Memory::AllocateRWX((16 << 20));
   MemBase = reinterpret_cast<unsigned char*>(MemBlock.base());
   FunctionBase = MemBase + 512*1024; // Use 512k for stubs
 
index 6ba57de40a6678babae9b132f99569e3dfb9c91e..dc8f2d1c47559c93c6b310959a4c250231dadd05 100644 (file)
@@ -26,8 +26,8 @@ using namespace sys;
 //===          and must not be generic UNIX code (see ../Unix/Memory.cpp)
 //===----------------------------------------------------------------------===//
 
-void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
-  if (NumBytes == 0) return 0;
+MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
+  if (NumBytes == 0) return MemoryBlock();
 
   static const long pageSize = Process::GetPageSize();
   unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@@ -37,14 +37,15 @@ void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
   if (pa == (void*)-1) {
     throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
   }
-  M.Address = pa;
-  M.AllocSize = NumPages*pageSize;
-  return pa;
+  MemoryBlock result;
+  result.Address = pa;
+  result.Size = NumPages*pageSize;
+  return result;
 }
 
-void Memory::ReleaseRWX(Memory& M) {
-  if (M.Address == 0 || M.AllocSize == 0) return;
-  if (0 != munmap(M.Address, M.AllocSize)) {
+void Memory::ReleaseRWX(MemoryBlock& M) {
+  if (M.Address == 0 || M.Size == 0) return;
+  if (0 != munmap(M.Address, M.Size)) {
     throw std::string("Can't release RWX Memory: ") + strerror(errno);
   }
 }
index 58e660c2083721b593e39e1ab234a2165a69a3bd..2392e71e0db4361f50134484dcc34f8f62ef2aec 100644 (file)
@@ -26,8 +26,8 @@ using namespace sys;
 //===          and must not be generic UNIX code (see ../Unix/Memory.cpp)
 //===----------------------------------------------------------------------===//
 
-void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
-  if (NumBytes == 0) return 0;
+MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
+  if (NumBytes == 0) return MemoryBlock();
 
   static const long pageSize = Process::GetPageSize();
   unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@@ -37,14 +37,15 @@ void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
   if (pa == (void*)-1) {
     throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
   }
-  M.Address = pa;
-  M.AllocSize = NumPages*pageSize;
-  return pa;
+  MemoryBlock result;
+  result.Address = pa;
+  result.Size = NumPages*pageSize;
+  return result;
 }
 
 void Memory::ReleaseRWX(Memory& M) {
-  if (M.Address == 0 || M.AllocSize == 0) return;
-  if (0 != munmap(M.Address, M.AllocSize)) {
+  if (M.Address == 0 || M.Size == 0) return;
+  if (0 != munmap(M.Address, M.Size)) {
     throw std::string("Can't release RWX Memory: ") + strerror(errno);
   }
 }
index a6a88835ad997e926063d6157b3a45a79a178c2a..974d1dbe104ed2a03d10c126d87ba2b5ca612068 100644 (file)
@@ -30,8 +30,8 @@ using namespace sys;
 /// to emit code to the memory then jump to it.  Getting this type of memory
 /// is very OS specific.
 ///
-void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
-  if (NumBytes == 0) return 0;
+MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
+  if (NumBytes == 0) return MemoryBlock();
 
   static const long pageSize = Process::GetPageSize();
   unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@@ -43,14 +43,15 @@ void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
     strerror_r(errno, msg, MAXPATHLEN-1);
     throw std::string("Can't allocate RWX Memory: ") + msg;
   }
-  M.Address = pa;
-  M.AllocSize = NumPages*pageSize;
-  return pa;
+  MemoryBlock result;
+  result.Address = pa;
+  result.Size = NumPages*pageSize;
+  return result;
 }
 
-void Memory::ReleaseRWX(Memory& M) {
-  if (M.Address == 0 || M.AllocSize == 0) return;
-  if (0 != munmap(M.Address, M.AllocSize)) {
+void Memory::ReleaseRWX(MemoryBlock& M) {
+  if (M.Address == 0 || M.Size == 0) return;
+  if (0 != munmap(M.Address, M.Size)) {
     char msg[MAXPATHLEN];
     strerror_r(errno, msg, MAXPATHLEN-1);
     throw std::string("Can't release RWX Memory: ") + msg;
index eeb22e910bfc8864fea0085757f67e88e0078172..4ab1a5dcc15523541c34c1139591e30ab37a46dc 100644 (file)
@@ -25,8 +25,8 @@ using namespace sys;
 //===          and must not be generic UNIX code (see ../Unix/Memory.cpp)
 //===----------------------------------------------------------------------===//
 
-void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
-  if (NumBytes == 0) return 0;
+MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
+  if (NumBytes == 0) return MemoryBlock();
 
   static const long pageSize = Process::GetPageSize();
   unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@@ -36,14 +36,16 @@ void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
   if (pa == (void*)-1) {
     throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
   }
-  M.Address = pa;
-  M.AllocSize = NumPages*pageSize;
-  return pa;
+
+  MemoryBlock result;
+  result.Address = pa;
+  result.Size = NumPages*pageSize;
+  return result;
 }
 
 void Memory::ReleaseRWX(Memory& M) {
-  if (M.Address == 0 || M.AllocSize == 0) return;
-  if (0 != munmap(M.Address, M.AllocSize)) {
+  if (M.Address == 0 || M.Size == 0) return;
+  if (0 != munmap(M.Address, M.Size)) {
     throw std::string("Can't release RWX Memory: ") + strerror(errno);
   }
 }
index b79f8b62680ced88ea32055dab13cf3e60548512..94e5893932893e4eb1f51f30b71d11c23b90788f 100644 (file)
@@ -25,8 +25,8 @@ using namespace sys;
 //===          and must not be generic UNIX code (see ../Unix/Memory.cpp)
 //===----------------------------------------------------------------------===//
 
-void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
-  if (NumBytes == 0) return 0;
+MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
+  if (NumBytes == 0) return MemoryBlock();
 
   static const long pageSize = Process::GetPageSize();
   unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@@ -36,14 +36,15 @@ void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
   if (pa == (void*)-1) {
     throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
   }
-  M.Address = pa;
-  M.AllocSize = NumPages*pageSize;
-  return pa;
+  MemoryBlock result;
+  result.Address = pa;
+  result.Size = NumPages*pageSize;
+  return result;
 }
 
-void Memory::ReleaseRWX(Memory& M) {
-  if (M.Address == 0 || M.AllocSize == 0) return;
-  if (0 != munmap(M.Address, M.AllocSize)) {
+void Memory::ReleaseRWX(MemoryBlock& M) {
+  if (M.Address == 0 || M.Size == 0) return;
+  if (0 != munmap(M.Address, M.Size)) {
     throw std::string("Can't release RWX Memory: ") + strerror(errno);
   }
 }
index 1bc6fd98bb452185d9281d4f456fa8de2da4bdd0..1a55ad8a19602ece755a6081afc957a9b0866388 100644 (file)
@@ -30,8 +30,8 @@ using namespace sys;
 /// to emit code to the memory then jump to it.  Getting this type of memory
 /// is very OS specific.
 ///
-void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
-  if (NumBytes == 0) return 0;
+MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
+  if (NumBytes == 0) return MemoryBlock();
 
   static const long pageSize = Process::GetPageSize();
   unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@@ -43,14 +43,15 @@ void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
     strerror_r(errno, msg, MAXPATHLEN-1);
     throw std::string("Can't allocate RWX Memory: ") + msg;
   }
-  M.Address = pa;
-  M.AllocSize = NumPages*pageSize;
-  return pa;
+  MemoryBlock result;
+  result.Address = pa;
+  result.Size = NumPages*pageSize;
+  return result;
 }
 
-void Memory::ReleaseRWX(Memory& M) {
-  if (M.Address == 0 || M.AllocSize == 0) return;
-  if (0 != munmap(M.Address, M.AllocSize)) {
+void Memory::ReleaseRWX(MemoryBlock& M) {
+  if (M.Address == 0 || M.Size == 0) return;
+  if (0 != munmap(M.Address, M.Size)) {
     char msg[MAXPATHLEN];
     strerror_r(errno, msg, MAXPATHLEN-1);
     throw std::string("Can't release RWX Memory: ") + msg;
index d6d871aa715fbfc0cd81af60473b01646f7af38b..8c6a44d27737537bb9ce2afcb3ae4bd795285fcf 100644 (file)
@@ -26,8 +26,8 @@ using namespace sys;
 //===          and must not be generic UNIX code (see ../Unix/Memory.cpp)
 //===----------------------------------------------------------------------===//
 
-void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
-  if (NumBytes == 0) return 0;
+MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
+  if (NumBytes == 0) return MemoryBlock();
 
   static const long pageSize = Process::GetPageSize();
   unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@@ -37,14 +37,15 @@ void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
   if (pa == (void*)-1) {
     throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
   }
-  M.Address = pa;
-  M.AllocSize = NumPages*pageSize;
-  return pa;
+  MemoryBlock result;
+  result.Address = pa;
+  result.AllocSize = NumPages*pageSize;
+  return result;
 }
 
-void Memory::ReleaseRWX(Memory& M) {
-  if (M.Address == 0 || M.AllocSize == 0) return;
-  if (0 != munmap(M.Address, M.AllocSize)) {
+void Memory::ReleaseRWX(MemoryBlock& M) {
+  if (M.Address == 0 || M.Size == 0) return;
+  if (0 != munmap(M.Address, M.Size)) {
     throw std::string("Can't release RWX Memory: ") + strerror(errno);
   }
 }
index 8a9ae05f37454ac5be4049b11b4a70fd97c8d8e0..946e50cd6fc2af0481b81d43819ca441a50e098f 100644 (file)
@@ -22,8 +22,8 @@ using namespace sys;
 //=== WARNING: Implementation here must contain only Win32 specific code.
 //===----------------------------------------------------------------------===//
 
-void* Memory::AllocateRWX(Memory&M, unsigned NumBytes) {
-  if (NumBytes == 0) return 0;
+MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
+  if (NumBytes == 0) return MemoryBlock();
 
   unsigned pageSize = Process::GetPageSize();
   unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@@ -33,12 +33,13 @@ void* Memory::AllocateRWX(Memory&M, unsigned NumBytes) {
     throw std::string("Couldn't allocate ") + utostr(NumBytes) + 
         " bytes of executable memory!";
   }
-  M.Address = P;
-  M.AllocSize = NumBytes;
-  return P;
+  MemoryBlock result;
+  result.Address = P;
+  result.Size = NumBytes;
+  return result;
 }
 
-void Memory::ReleaseRWX(Memory& M) {
-  if (M.Address == 0 ) return;
-  VirtualFree(M.Address, M.AllocSize, MEM_DECOMMIT, PAGE_NOACCESS);
+void Memory::ReleaseRWX(MemoryBlock& M) {
+  if (M.Address == 0 || M.Size == 0) return;
+  VirtualFree(M.Address, M.Size, MEM_DECOMMIT, PAGE_NOACCESS);
 }
index 8a9ae05f37454ac5be4049b11b4a70fd97c8d8e0..946e50cd6fc2af0481b81d43819ca441a50e098f 100644 (file)
@@ -22,8 +22,8 @@ using namespace sys;
 //=== WARNING: Implementation here must contain only Win32 specific code.
 //===----------------------------------------------------------------------===//
 
-void* Memory::AllocateRWX(Memory&M, unsigned NumBytes) {
-  if (NumBytes == 0) return 0;
+MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
+  if (NumBytes == 0) return MemoryBlock();
 
   unsigned pageSize = Process::GetPageSize();
   unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@@ -33,12 +33,13 @@ void* Memory::AllocateRWX(Memory&M, unsigned NumBytes) {
     throw std::string("Couldn't allocate ") + utostr(NumBytes) + 
         " bytes of executable memory!";
   }
-  M.Address = P;
-  M.AllocSize = NumBytes;
-  return P;
+  MemoryBlock result;
+  result.Address = P;
+  result.Size = NumBytes;
+  return result;
 }
 
-void Memory::ReleaseRWX(Memory& M) {
-  if (M.Address == 0 ) return;
-  VirtualFree(M.Address, M.AllocSize, MEM_DECOMMIT, PAGE_NOACCESS);
+void Memory::ReleaseRWX(MemoryBlock& M) {
+  if (M.Address == 0 || M.Size == 0) return;
+  VirtualFree(M.Address, M.Size, MEM_DECOMMIT, PAGE_NOACCESS);
 }