[lld] r194787 - [PECOFF] Avoid using statically initialized std::vector.
Rui Ueyama
ruiu at google.com
Thu Nov 14 21:45:20 PST 2013
Author: ruiu
Date: Thu Nov 14 23:45:20 2013
New Revision: 194787
URL: http://llvm.org/viewvc/llvm-project?rev=194787&view=rev
Log:
[PECOFF] Avoid using statically initialized std::vector.
Also slightly reduces PECOFFLinkingContext's memory footprint (~128B).
Modified:
lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
lld/trunk/lib/Driver/WinLinkDriver.cpp
lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp
lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
Modified: lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h?rev=194787&r1=194786&r2=194787&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h (original)
+++ lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h Thu Nov 14 23:45:20 2013
@@ -18,6 +18,7 @@
#include "lld/ReaderWriter/Reader.h"
#include "lld/ReaderWriter/Writer.h"
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/COFF.h"
@@ -27,6 +28,8 @@
using llvm::COFF::MachineTypes;
using llvm::COFF::WindowsSubsystem;
+static const uint8_t DEFAULT_DOS_STUB[128] = {'M', 'Z'};
+
namespace lld {
class PECOFFLinkingContext : public LinkingContext {
@@ -43,7 +46,8 @@ public:
_terminalServerAware(true), _dynamicBaseEnabled(true),
_createManifest(true), _embedManifest(false), _manifestId(1),
_manifestLevel("'asInvoker'"), _manifestUiAccess("'false'"),
- _imageType(ImageType::IMAGE_EXE), _dosStub(_defaultDosStub) {
+ _imageType(ImageType::IMAGE_EXE),
+ _dosStub(llvm::makeArrayRef(DEFAULT_DOS_STUB)) {
setDeadStripping(true);
}
@@ -230,8 +234,8 @@ public:
return it == _sectionAttributeMask.end() ? 0 : it->second;
}
- void setDosStub(std::vector<uint8_t> &data) { _dosStub = std::move(data); }
- const std::vector<uint8_t> &getDosStub() const { return _dosStub; }
+ void setDosStub(ArrayRef<uint8_t> data) { _dosStub = data; }
+ ArrayRef<uint8_t> getDosStub() const { return _dosStub; }
StringRef allocateString(StringRef ref) const {
char *x = _allocator.Allocate<char>(ref.size() + 1);
@@ -240,6 +244,13 @@ public:
return x;
}
+ ArrayRef<uint8_t> allocate(ArrayRef<uint8_t> array) const {
+ size_t size = array.size();
+ uint8_t *p = _allocator.Allocate<uint8_t>(size);
+ memcpy(p, array.data(), size);
+ return ArrayRef<uint8_t>(p, p + array.size());
+ }
+
virtual bool hasInputGraph() {
if (_inputGraph)
return true;
@@ -319,8 +330,7 @@ private:
// Windows loader do not really care about DOS stub contents, but it's usually
// a small DOS program that prints out a message "This program requires
// Microsoft Windows." This feature was somewhat useful before Windows 95.
- std::vector<uint8_t> _dosStub;
- static const std::vector<uint8_t> _defaultDosStub;
+ ArrayRef<uint8_t> _dosStub;
};
} // end namespace lld
Modified: lld/trunk/lib/Driver/WinLinkDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/WinLinkDriver.cpp?rev=194787&r1=194786&r2=194787&view=diff
==============================================================================
--- lld/trunk/lib/Driver/WinLinkDriver.cpp (original)
+++ lld/trunk/lib/Driver/WinLinkDriver.cpp Thu Nov 14 23:45:20 2013
@@ -21,6 +21,7 @@
#include "lld/Driver/WinLinkInputGraph.h"
#include "lld/ReaderWriter/PECOFFLinkingContext.h"
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringSwitch.h"
@@ -203,11 +204,14 @@ bool parseSection(StringRef option, std:
return true;
}
-bool readFile(StringRef path, std::vector<uint8_t> &result) {
+bool readFile(PECOFFLinkingContext &ctx, StringRef path,
+ ArrayRef<uint8_t> &result) {
OwningPtr<MemoryBuffer> buf;
if (MemoryBuffer::getFile(path, buf))
return false;
- result = std::vector<uint8_t>(buf->getBufferStart(), buf->getBufferEnd());
+ result = ctx.allocate(ArrayRef<uint8_t>(
+ reinterpret_cast<const uint8_t *>(buf->getBufferStart()),
+ buf->getBufferSize()));
return true;
}
@@ -837,8 +841,8 @@ WinLinkDriver::parse(int argc, const cha
break;
case OPT_stub: {
- std::vector<uint8_t> contents;
- if (!readFile(inputArg->getValue(), contents)) {
+ ArrayRef<uint8_t> contents;
+ if (!readFile(ctx, inputArg->getValue(), contents)) {
diagnostics << "Failed to read DOS stub file "
<< inputArg->getValue() << "\n";
return false;
Modified: lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp?rev=194787&r1=194786&r2=194787&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp Thu Nov 14 23:45:20 2013
@@ -29,13 +29,6 @@
namespace lld {
-namespace {
-uint8_t DefaultDosStub[128] = {'M', 'Z'};
-};
-
-const std::vector<uint8_t> PECOFFLinkingContext::_defaultDosStub(
- DefaultDosStub, DefaultDosStub + sizeof(DefaultDosStub));
-
bool PECOFFLinkingContext::validateImpl(raw_ostream &diagnostics) {
if (_stackReserve < _stackCommit) {
diagnostics << "Invalid stack size: reserve size must be equal to or "
Modified: lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=194787&r1=194786&r2=194787&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Thu Nov 14 23:45:20 2013
@@ -107,18 +107,19 @@ public:
class DOSStubChunk : public HeaderChunk {
public:
DOSStubChunk(const PECOFFLinkingContext &ctx)
- : HeaderChunk(), _dosStub(ctx.getDosStub()) {
- auto *header = reinterpret_cast<llvm::object::dos_header *>(&_dosStub[0]);
- header->AddressOfNewExeHeader = _dosStub.size();
- _size = _dosStub.size();
+ : HeaderChunk(), _context(ctx) {
+ _size = _context.getDosStub().size();
}
virtual void write(uint8_t *fileBuffer) {
- std::memcpy(fileBuffer, &_dosStub[0], _dosStub.size());
+ ArrayRef<uint8_t> array = _context.getDosStub();
+ std::memcpy(fileBuffer, array.data(), array.size());
+ auto *header = reinterpret_cast<llvm::object::dos_header *>(fileBuffer);
+ header->AddressOfNewExeHeader = array.size();
}
private:
- std::vector<uint8_t> _dosStub;
+ const PECOFFLinkingContext &_context;
};
/// A PEHeaderChunk represents PE header including COFF header.
More information about the llvm-commits
mailing list