[PATCH] D138555: Cache memory buffer's name length
serge via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 29 05:01:27 PST 2022
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG1824432174b3: Cache memory buffer's name length (authored by serge-sans-paille).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D138555/new/
https://reviews.llvm.org/D138555
Files:
llvm/lib/Support/MemoryBuffer.cpp
Index: llvm/lib/Support/MemoryBuffer.cpp
===================================================================
--- llvm/lib/Support/MemoryBuffer.cpp
+++ llvm/lib/Support/MemoryBuffer.cpp
@@ -58,12 +58,10 @@
// MemoryBufferMem implementation.
//===----------------------------------------------------------------------===//
-/// CopyStringRef - Copies contents of a StringRef into a block of memory and
-/// null-terminates it.
+/// CopyStringRef - Copies contents of a StringRef into a block of memory.
static void CopyStringRef(char *Memory, StringRef Data) {
if (!Data.empty())
memcpy(Memory, Data.data(), Data.size());
- Memory[Data.size()] = 0; // Null terminate string.
}
namespace {
@@ -77,8 +75,10 @@
SmallString<256> NameBuf;
StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
- char *Mem = static_cast<char *>(operator new(N + NameRef.size() + 1));
- CopyStringRef(Mem + N, NameRef);
+ char *Mem =
+ static_cast<char *>(operator new(N + sizeof(size_t) + NameRef.size()));
+ *reinterpret_cast<size_t *>(Mem + N) = NameRef.size();
+ CopyStringRef(Mem + N + sizeof(size_t), NameRef);
return Mem;
}
@@ -98,7 +98,8 @@
StringRef getBufferIdentifier() const override {
// The name is stored after the class itself.
- return StringRef(reinterpret_cast<const char *>(this + 1));
+ return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t),
+ *reinterpret_cast<const size_t *>(this + 1));
}
MemoryBuffer::BufferKind getBufferKind() const override {
@@ -221,7 +222,8 @@
StringRef getBufferIdentifier() const override {
// The name is stored after the class itself.
- return StringRef(reinterpret_cast<const char *>(this + 1));
+ return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t),
+ *reinterpret_cast<const size_t *>(this + 1));
}
MemoryBuffer::BufferKind getBufferKind() const override {
@@ -301,7 +303,8 @@
// that MemoryBuffer and data are aligned so PointerIntPair works with them.
SmallString<256> NameBuf;
StringRef NameRef = BufferName.toStringRef(NameBuf);
- size_t StringLen = sizeof(MemBuffer) + NameRef.size() + 1;
+
+ size_t StringLen = sizeof(MemBuffer) + sizeof(size_t) + NameRef.size();
size_t RealLen = StringLen + Size + 1 + BufAlign.value();
if (RealLen <= Size) // Check for rollover.
return nullptr;
@@ -310,13 +313,15 @@
return nullptr;
// The name is stored after the class itself.
- CopyStringRef(Mem + sizeof(MemBuffer), NameRef);
+ *reinterpret_cast<size_t *>(Mem + sizeof(MemBuffer)) =
+ NameRef.size(); // Null terminate buffer.
+ CopyStringRef(Mem + sizeof(MemBuffer) + sizeof(size_t), NameRef);
// The buffer begins after the name and must be aligned.
char *Buf = (char *)alignAddr(Mem + StringLen, BufAlign);
- Buf[Size] = 0; // Null terminate buffer.
auto *Ret = new (Mem) MemBuffer(StringRef(Buf, Size), true);
+ Buf[Size] = 0; // Null terminate buffer.
return std::unique_ptr<WritableMemoryBuffer>(Ret);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138555.478534.patch
Type: text/x-patch
Size: 3069 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221129/9fecbe20/attachment.bin>
More information about the llvm-commits
mailing list