Simplify the sys::Memory interface per Chris' request.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16318 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Spencer
2004-09-13 22:38:11 +00:00
parent 2565943289
commit 33189e787b
11 changed files with 114 additions and 94 deletions

View File

@ -11,38 +11,48 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#ifndef LLVM_SYSTEM_PATH_H #ifndef LLVM_SYSTEM_MEMORY_H
#define LLVM_SYSTEM_PATH_H #define LLVM_SYSTEM_MEMORY_H
#include <string>
namespace llvm { namespace llvm {
namespace sys { 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 /// @since 1.4
/// @brief An abstraction for operating system paths. /// @brief An abstraction for memory operations.
class Memory { class Memory {
/// @name Functions /// @name Functions
/// @{ /// @{
public: public:
Memory() { Address = 0; AllocSize = 0; } /// This method allocates a block of Read/Write/Execute memory that is
~Memory() { ReleaseRWX(*this); } /// 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);
/// @throws std::string if an error occurred /// This method releases a block of Read/Write/Execute memory that was
static void* AllocateRWX(Memory& block, unsigned NumBytes); /// 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 /// @throws std::string if an error occurred.
static void ReleaseRWX(Memory& block); /// @brief Release Read/Write/Execute memory.
static void ReleaseRWX(MemoryBlock& 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
/// @} /// @}
}; };
} }

View File

@ -38,7 +38,7 @@ namespace {
/// are emitting is. This never bothers to release the memory, because when /// are emitting is. This never bothers to release the memory, because when
/// we are ready to destroy the JIT, the program exits. /// we are ready to destroy the JIT, the program exits.
class JITMemoryManager { 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 *MemBase; // Base of block of memory, start of stub mem
unsigned char *FunctionBase; // Start of the function body area unsigned char *FunctionBase; // Start of the function body area
unsigned char *CurStubPtr, *CurFunctionPtr; unsigned char *CurStubPtr, *CurFunctionPtr;
@ -53,7 +53,7 @@ namespace {
JITMemoryManager::JITMemoryManager() { JITMemoryManager::JITMemoryManager() {
// Allocate a 16M block of memory... // 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()); MemBase = reinterpret_cast<unsigned char*>(MemBlock.base());
FunctionBase = MemBase + 512*1024; // Use 512k for stubs FunctionBase = MemBase + 512*1024; // Use 512k for stubs

View File

@ -26,8 +26,8 @@ using namespace sys;
//=== and must not be generic UNIX code (see ../Unix/Memory.cpp) //=== and must not be generic UNIX code (see ../Unix/Memory.cpp)
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) { MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
if (NumBytes == 0) return 0; if (NumBytes == 0) return MemoryBlock();
static const long pageSize = Process::GetPageSize(); static const long pageSize = Process::GetPageSize();
unsigned NumPages = (NumBytes+pageSize-1)/pageSize; unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@ -37,14 +37,15 @@ void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
if (pa == (void*)-1) { if (pa == (void*)-1) {
throw std::string("Can't allocate RWX Memory: ") + strerror(errno); throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
} }
M.Address = pa; MemoryBlock result;
M.AllocSize = NumPages*pageSize; result.Address = pa;
return pa; result.Size = NumPages*pageSize;
return result;
} }
void Memory::ReleaseRWX(Memory& M) { void Memory::ReleaseRWX(MemoryBlock& M) {
if (M.Address == 0 || M.AllocSize == 0) return; if (M.Address == 0 || M.Size == 0) return;
if (0 != munmap(M.Address, M.AllocSize)) { if (0 != munmap(M.Address, M.Size)) {
throw std::string("Can't release RWX Memory: ") + strerror(errno); throw std::string("Can't release RWX Memory: ") + strerror(errno);
} }
} }

View File

@ -26,8 +26,8 @@ using namespace sys;
//=== and must not be generic UNIX code (see ../Unix/Memory.cpp) //=== and must not be generic UNIX code (see ../Unix/Memory.cpp)
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) { MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
if (NumBytes == 0) return 0; if (NumBytes == 0) return MemoryBlock();
static const long pageSize = Process::GetPageSize(); static const long pageSize = Process::GetPageSize();
unsigned NumPages = (NumBytes+pageSize-1)/pageSize; unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@ -37,14 +37,15 @@ void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
if (pa == (void*)-1) { if (pa == (void*)-1) {
throw std::string("Can't allocate RWX Memory: ") + strerror(errno); throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
} }
M.Address = pa; MemoryBlock result;
M.AllocSize = NumPages*pageSize; result.Address = pa;
return pa; result.Size = NumPages*pageSize;
return result;
} }
void Memory::ReleaseRWX(Memory& M) { void Memory::ReleaseRWX(Memory& M) {
if (M.Address == 0 || M.AllocSize == 0) return; if (M.Address == 0 || M.Size == 0) return;
if (0 != munmap(M.Address, M.AllocSize)) { if (0 != munmap(M.Address, M.Size)) {
throw std::string("Can't release RWX Memory: ") + strerror(errno); throw std::string("Can't release RWX Memory: ") + strerror(errno);
} }
} }

View File

@ -30,8 +30,8 @@ using namespace sys;
/// to emit code to the memory then jump to it. Getting this type of memory /// to emit code to the memory then jump to it. Getting this type of memory
/// is very OS specific. /// is very OS specific.
/// ///
void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) { MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
if (NumBytes == 0) return 0; if (NumBytes == 0) return MemoryBlock();
static const long pageSize = Process::GetPageSize(); static const long pageSize = Process::GetPageSize();
unsigned NumPages = (NumBytes+pageSize-1)/pageSize; unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@ -43,14 +43,15 @@ void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
strerror_r(errno, msg, MAXPATHLEN-1); strerror_r(errno, msg, MAXPATHLEN-1);
throw std::string("Can't allocate RWX Memory: ") + msg; throw std::string("Can't allocate RWX Memory: ") + msg;
} }
M.Address = pa; MemoryBlock result;
M.AllocSize = NumPages*pageSize; result.Address = pa;
return pa; result.Size = NumPages*pageSize;
return result;
} }
void Memory::ReleaseRWX(Memory& M) { void Memory::ReleaseRWX(MemoryBlock& M) {
if (M.Address == 0 || M.AllocSize == 0) return; if (M.Address == 0 || M.Size == 0) return;
if (0 != munmap(M.Address, M.AllocSize)) { if (0 != munmap(M.Address, M.Size)) {
char msg[MAXPATHLEN]; char msg[MAXPATHLEN];
strerror_r(errno, msg, MAXPATHLEN-1); strerror_r(errno, msg, MAXPATHLEN-1);
throw std::string("Can't release RWX Memory: ") + msg; throw std::string("Can't release RWX Memory: ") + msg;

View File

@ -25,8 +25,8 @@ using namespace sys;
//=== and must not be generic UNIX code (see ../Unix/Memory.cpp) //=== and must not be generic UNIX code (see ../Unix/Memory.cpp)
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) { MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
if (NumBytes == 0) return 0; if (NumBytes == 0) return MemoryBlock();
static const long pageSize = Process::GetPageSize(); static const long pageSize = Process::GetPageSize();
unsigned NumPages = (NumBytes+pageSize-1)/pageSize; unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@ -36,14 +36,16 @@ void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
if (pa == (void*)-1) { if (pa == (void*)-1) {
throw std::string("Can't allocate RWX Memory: ") + strerror(errno); throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
} }
M.Address = pa;
M.AllocSize = NumPages*pageSize; MemoryBlock result;
return pa; result.Address = pa;
result.Size = NumPages*pageSize;
return result;
} }
void Memory::ReleaseRWX(Memory& M) { void Memory::ReleaseRWX(Memory& M) {
if (M.Address == 0 || M.AllocSize == 0) return; if (M.Address == 0 || M.Size == 0) return;
if (0 != munmap(M.Address, M.AllocSize)) { if (0 != munmap(M.Address, M.Size)) {
throw std::string("Can't release RWX Memory: ") + strerror(errno); throw std::string("Can't release RWX Memory: ") + strerror(errno);
} }
} }

View File

@ -25,8 +25,8 @@ using namespace sys;
//=== and must not be generic UNIX code (see ../Unix/Memory.cpp) //=== and must not be generic UNIX code (see ../Unix/Memory.cpp)
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) { MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
if (NumBytes == 0) return 0; if (NumBytes == 0) return MemoryBlock();
static const long pageSize = Process::GetPageSize(); static const long pageSize = Process::GetPageSize();
unsigned NumPages = (NumBytes+pageSize-1)/pageSize; unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@ -36,14 +36,15 @@ void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
if (pa == (void*)-1) { if (pa == (void*)-1) {
throw std::string("Can't allocate RWX Memory: ") + strerror(errno); throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
} }
M.Address = pa; MemoryBlock result;
M.AllocSize = NumPages*pageSize; result.Address = pa;
return pa; result.Size = NumPages*pageSize;
return result;
} }
void Memory::ReleaseRWX(Memory& M) { void Memory::ReleaseRWX(MemoryBlock& M) {
if (M.Address == 0 || M.AllocSize == 0) return; if (M.Address == 0 || M.Size == 0) return;
if (0 != munmap(M.Address, M.AllocSize)) { if (0 != munmap(M.Address, M.Size)) {
throw std::string("Can't release RWX Memory: ") + strerror(errno); throw std::string("Can't release RWX Memory: ") + strerror(errno);
} }
} }

View File

@ -30,8 +30,8 @@ using namespace sys;
/// to emit code to the memory then jump to it. Getting this type of memory /// to emit code to the memory then jump to it. Getting this type of memory
/// is very OS specific. /// is very OS specific.
/// ///
void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) { MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
if (NumBytes == 0) return 0; if (NumBytes == 0) return MemoryBlock();
static const long pageSize = Process::GetPageSize(); static const long pageSize = Process::GetPageSize();
unsigned NumPages = (NumBytes+pageSize-1)/pageSize; unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@ -43,14 +43,15 @@ void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
strerror_r(errno, msg, MAXPATHLEN-1); strerror_r(errno, msg, MAXPATHLEN-1);
throw std::string("Can't allocate RWX Memory: ") + msg; throw std::string("Can't allocate RWX Memory: ") + msg;
} }
M.Address = pa; MemoryBlock result;
M.AllocSize = NumPages*pageSize; result.Address = pa;
return pa; result.Size = NumPages*pageSize;
return result;
} }
void Memory::ReleaseRWX(Memory& M) { void Memory::ReleaseRWX(MemoryBlock& M) {
if (M.Address == 0 || M.AllocSize == 0) return; if (M.Address == 0 || M.Size == 0) return;
if (0 != munmap(M.Address, M.AllocSize)) { if (0 != munmap(M.Address, M.Size)) {
char msg[MAXPATHLEN]; char msg[MAXPATHLEN];
strerror_r(errno, msg, MAXPATHLEN-1); strerror_r(errno, msg, MAXPATHLEN-1);
throw std::string("Can't release RWX Memory: ") + msg; throw std::string("Can't release RWX Memory: ") + msg;

View File

@ -26,8 +26,8 @@ using namespace sys;
//=== and must not be generic UNIX code (see ../Unix/Memory.cpp) //=== and must not be generic UNIX code (see ../Unix/Memory.cpp)
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) { MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
if (NumBytes == 0) return 0; if (NumBytes == 0) return MemoryBlock();
static const long pageSize = Process::GetPageSize(); static const long pageSize = Process::GetPageSize();
unsigned NumPages = (NumBytes+pageSize-1)/pageSize; unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
@ -37,14 +37,15 @@ void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
if (pa == (void*)-1) { if (pa == (void*)-1) {
throw std::string("Can't allocate RWX Memory: ") + strerror(errno); throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
} }
M.Address = pa; MemoryBlock result;
M.AllocSize = NumPages*pageSize; result.Address = pa;
return pa; result.AllocSize = NumPages*pageSize;
return result;
} }
void Memory::ReleaseRWX(Memory& M) { void Memory::ReleaseRWX(MemoryBlock& M) {
if (M.Address == 0 || M.AllocSize == 0) return; if (M.Address == 0 || M.Size == 0) return;
if (0 != munmap(M.Address, M.AllocSize)) { if (0 != munmap(M.Address, M.Size)) {
throw std::string("Can't release RWX Memory: ") + strerror(errno); throw std::string("Can't release RWX Memory: ") + strerror(errno);
} }
} }

View File

@ -22,8 +22,8 @@ using namespace sys;
//=== WARNING: Implementation here must contain only Win32 specific code. //=== WARNING: Implementation here must contain only Win32 specific code.
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
void* Memory::AllocateRWX(Memory&M, unsigned NumBytes) { MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
if (NumBytes == 0) return 0; if (NumBytes == 0) return MemoryBlock();
unsigned pageSize = Process::GetPageSize(); unsigned pageSize = Process::GetPageSize();
unsigned NumPages = (NumBytes+pageSize-1)/pageSize; 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) + throw std::string("Couldn't allocate ") + utostr(NumBytes) +
" bytes of executable memory!"; " bytes of executable memory!";
} }
M.Address = P; MemoryBlock result;
M.AllocSize = NumBytes; result.Address = P;
return P; result.Size = NumBytes;
return result;
} }
void Memory::ReleaseRWX(Memory& M) { void Memory::ReleaseRWX(MemoryBlock& M) {
if (M.Address == 0 ) return; if (M.Address == 0 || M.Size == 0) return;
VirtualFree(M.Address, M.AllocSize, MEM_DECOMMIT, PAGE_NOACCESS); VirtualFree(M.Address, M.Size, MEM_DECOMMIT, PAGE_NOACCESS);
} }

View File

@ -22,8 +22,8 @@ using namespace sys;
//=== WARNING: Implementation here must contain only Win32 specific code. //=== WARNING: Implementation here must contain only Win32 specific code.
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
void* Memory::AllocateRWX(Memory&M, unsigned NumBytes) { MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
if (NumBytes == 0) return 0; if (NumBytes == 0) return MemoryBlock();
unsigned pageSize = Process::GetPageSize(); unsigned pageSize = Process::GetPageSize();
unsigned NumPages = (NumBytes+pageSize-1)/pageSize; 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) + throw std::string("Couldn't allocate ") + utostr(NumBytes) +
" bytes of executable memory!"; " bytes of executable memory!";
} }
M.Address = P; MemoryBlock result;
M.AllocSize = NumBytes; result.Address = P;
return P; result.Size = NumBytes;
return result;
} }
void Memory::ReleaseRWX(Memory& M) { void Memory::ReleaseRWX(MemoryBlock& M) {
if (M.Address == 0 ) return; if (M.Address == 0 || M.Size == 0) return;
VirtualFree(M.Address, M.AllocSize, MEM_DECOMMIT, PAGE_NOACCESS); VirtualFree(M.Address, M.Size, MEM_DECOMMIT, PAGE_NOACCESS);
} }