diff options
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r-- | llvm/lib/Support/FileOutputBuffer.cpp | 18 | ||||
-rw-r--r-- | llvm/lib/Support/Memory.cpp | 4 | ||||
-rw-r--r-- | llvm/lib/Support/Unix/Memory.inc | 22 | ||||
-rw-r--r-- | llvm/lib/Support/Windows/Memory.inc | 14 |
4 files changed, 32 insertions, 26 deletions
diff --git a/llvm/lib/Support/FileOutputBuffer.cpp b/llvm/lib/Support/FileOutputBuffer.cpp index b0b817b4bdd..19ab2e9917a 100644 --- a/llvm/lib/Support/FileOutputBuffer.cpp +++ b/llvm/lib/Support/FileOutputBuffer.cpp @@ -75,20 +75,22 @@ private: // output file on commit(). This is used only when we cannot use OnDiskBuffer. class InMemoryBuffer : public FileOutputBuffer { public: - InMemoryBuffer(StringRef Path, MemoryBlock Buf, unsigned Mode) - : FileOutputBuffer(Path), Buffer(Buf), Mode(Mode) {} + InMemoryBuffer(StringRef Path, MemoryBlock Buf, std::size_t BufSize, + unsigned Mode) + : FileOutputBuffer(Path), Buffer(Buf), BufferSize(BufSize), + Mode(Mode) {} uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); } uint8_t *getBufferEnd() const override { - return (uint8_t *)Buffer.base() + Buffer.size(); + return (uint8_t *)Buffer.base() + BufferSize; } - size_t getBufferSize() const override { return Buffer.size(); } + size_t getBufferSize() const override { return BufferSize; } Error commit() override { if (FinalPath == "-") { - llvm::outs() << StringRef((const char *)Buffer.base(), Buffer.size()); + llvm::outs() << StringRef((const char *)Buffer.base(), BufferSize); llvm::outs().flush(); return Error::success(); } @@ -100,12 +102,14 @@ public: openFileForWrite(FinalPath, FD, CD_CreateAlways, OF_None, Mode)) return errorCodeToError(EC); raw_fd_ostream OS(FD, /*shouldClose=*/true, /*unbuffered=*/true); - OS << StringRef((const char *)Buffer.base(), Buffer.size()); + OS << StringRef((const char *)Buffer.base(), BufferSize); return Error::success(); } private: + // Buffer may actually contain a larger memory block than BufferSize OwningMemoryBlock Buffer; + size_t BufferSize; unsigned Mode; }; } // namespace @@ -117,7 +121,7 @@ createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) { Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC); if (EC) return errorCodeToError(EC); - return llvm::make_unique<InMemoryBuffer>(Path, MB, Mode); + return llvm::make_unique<InMemoryBuffer>(Path, MB, Size, Mode); } static Expected<std::unique_ptr<FileOutputBuffer>> diff --git a/llvm/lib/Support/Memory.cpp b/llvm/lib/Support/Memory.cpp index 4a84247abf4..581484268cd 100644 --- a/llvm/lib/Support/Memory.cpp +++ b/llvm/lib/Support/Memory.cpp @@ -43,8 +43,8 @@ raw_ostream &operator<<(raw_ostream &OS, const Memory::ProtectionFlags &PF) { raw_ostream &operator<<(raw_ostream &OS, const MemoryBlock &MB) { return OS << "[ " << MB.base() << " .. " - << (void *)((char *)MB.base() + MB.size()) << " ] (" << MB.size() - << " bytes)"; + << (void *)((char *)MB.base() + MB.allocatedSize()) << " ] (" + << MB.allocatedSize() << " bytes)"; } } // end namespace sys diff --git a/llvm/lib/Support/Unix/Memory.inc b/llvm/lib/Support/Unix/Memory.inc index 05c5ccbdb3d..3b003362959 100644 --- a/llvm/lib/Support/Unix/Memory.inc +++ b/llvm/lib/Support/Unix/Memory.inc @@ -117,13 +117,15 @@ Memory::allocateMappedMemory(size_t NumBytes, // Use any near hint and the page size to set a page-aligned starting address uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) + - NearBlock->size() : 0; + NearBlock->allocatedSize() : 0; static const size_t PageSize = Process::getPageSizeEstimate(); + const size_t NumPages = (NumBytes+PageSize-1)/PageSize; + if (Start && Start % PageSize) Start += PageSize - Start % PageSize; // FIXME: Handle huge page requests (MF_HUGE_HINT). - void *Addr = ::mmap(reinterpret_cast<void *>(Start), NumBytes, Protect, + void *Addr = ::mmap(reinterpret_cast<void *>(Start), PageSize*NumPages, Protect, MMFlags, fd, 0); if (Addr == MAP_FAILED) { if (NearBlock) { //Try again without a near hint @@ -146,7 +148,7 @@ Memory::allocateMappedMemory(size_t NumBytes, MemoryBlock Result; Result.Address = Addr; - Result.Size = NumBytes; + Result.AllocatedSize = PageSize*NumPages; Result.Flags = PFlags; // Rely on protectMappedMemory to invalidate instruction cache. @@ -161,14 +163,14 @@ Memory::allocateMappedMemory(size_t NumBytes, std::error_code Memory::releaseMappedMemory(MemoryBlock &M) { - if (M.Address == nullptr || M.Size == 0) + if (M.Address == nullptr || M.AllocatedSize == 0) return std::error_code(); - if (0 != ::munmap(M.Address, M.Size)) + if (0 != ::munmap(M.Address, M.AllocatedSize)) return std::error_code(errno, std::generic_category()); M.Address = nullptr; - M.Size = 0; + M.AllocatedSize = 0; return std::error_code(); } @@ -176,7 +178,7 @@ Memory::releaseMappedMemory(MemoryBlock &M) { std::error_code Memory::protectMappedMemory(const MemoryBlock &M, unsigned Flags) { static const size_t PageSize = Process::getPageSizeEstimate(); - if (M.Address == nullptr || M.Size == 0) + if (M.Address == nullptr || M.AllocatedSize == 0) return std::error_code(); if (!Flags) @@ -184,7 +186,7 @@ Memory::protectMappedMemory(const MemoryBlock &M, unsigned Flags) { int Protect = getPosixProtectionFlags(Flags); uintptr_t Start = alignAddr((uint8_t *)M.Address - PageSize + 1, PageSize); - uintptr_t End = alignAddr((uint8_t *)M.Address + M.Size, PageSize); + uintptr_t End = alignAddr((uint8_t *)M.Address + M.AllocatedSize, PageSize); bool InvalidateCache = (Flags & MF_EXEC); @@ -197,7 +199,7 @@ Memory::protectMappedMemory(const MemoryBlock &M, unsigned Flags) { if (Result != 0) return std::error_code(errno, std::generic_category()); - Memory::InvalidateInstructionCache(M.Address, M.Size); + Memory::InvalidateInstructionCache(M.Address, M.AllocatedSize); InvalidateCache = false; } #endif @@ -208,7 +210,7 @@ Memory::protectMappedMemory(const MemoryBlock &M, unsigned Flags) { return std::error_code(errno, std::generic_category()); if (InvalidateCache) - Memory::InvalidateInstructionCache(M.Address, M.Size); + Memory::InvalidateInstructionCache(M.Address, M.AllocatedSize); return std::error_code(); } diff --git a/llvm/lib/Support/Windows/Memory.inc b/llvm/lib/Support/Windows/Memory.inc index b1d68596f5c..a67f9c7d0f3 100644 --- a/llvm/lib/Support/Windows/Memory.inc +++ b/llvm/lib/Support/Windows/Memory.inc @@ -125,7 +125,7 @@ MemoryBlock Memory::allocateMappedMemory(size_t NumBytes, size_t NumBlocks = (NumBytes + Granularity - 1) / Granularity; uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) + - NearBlock->size() + NearBlock->allocatedSize() : 0; // If the requested address is not aligned to the allocation granularity, @@ -149,7 +149,7 @@ MemoryBlock Memory::allocateMappedMemory(size_t NumBytes, MemoryBlock Result; Result.Address = PA; - Result.Size = NumBytes; + Result.AllocatedSize = AllocSize; Result.Flags = (Flags & ~MF_HUGE_HINT) | (HugePages ? MF_HUGE_HINT : 0); if (Flags & MF_EXEC) @@ -159,31 +159,31 @@ MemoryBlock Memory::allocateMappedMemory(size_t NumBytes, } std::error_code Memory::releaseMappedMemory(MemoryBlock &M) { - if (M.Address == 0 || M.Size == 0) + if (M.Address == 0 || M.AllocatedSize == 0) return std::error_code(); if (!VirtualFree(M.Address, 0, MEM_RELEASE)) return mapWindowsError(::GetLastError()); M.Address = 0; - M.Size = 0; + M.AllocatedSize = 0; return std::error_code(); } std::error_code Memory::protectMappedMemory(const MemoryBlock &M, unsigned Flags) { - if (M.Address == 0 || M.Size == 0) + if (M.Address == 0 || M.AllocatedSize == 0) return std::error_code(); DWORD Protect = getWindowsProtectionFlags(Flags); DWORD OldFlags; - if (!VirtualProtect(M.Address, M.Size, Protect, &OldFlags)) + if (!VirtualProtect(M.Address, M.AllocatedSize, Protect, &OldFlags)) return mapWindowsError(::GetLastError()); if (Flags & MF_EXEC) - Memory::InvalidateInstructionCache(M.Address, M.Size); + Memory::InvalidateInstructionCache(M.Address, M.AllocatedSize); return std::error_code(); } |