diff options
author | Chris Lattner <sabre@nondot.org> | 2008-04-14 21:41:00 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2008-04-14 21:41:00 +0000 |
commit | 5618d88b93056bae76845b1503cce6ba0a6080f1 (patch) | |
tree | 774ac2bd9e1e78717f24e77025a83f3908e3a810 /lib/Rewrite/RewriteRope.cpp | |
parent | e93705f3400f1779579715791b021f9c4f6b35db (diff) |
Add a bunch of comments, move RewriteRope::MakeRopeString out of line.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@49689 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Rewrite/RewriteRope.cpp')
-rw-r--r-- | lib/Rewrite/RewriteRope.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/Rewrite/RewriteRope.cpp b/lib/Rewrite/RewriteRope.cpp index 8a082edba6..2adf5c4f4b 100644 --- a/lib/Rewrite/RewriteRope.cpp +++ b/lib/Rewrite/RewriteRope.cpp @@ -13,6 +13,7 @@ #include "clang/Rewrite/RewriteRope.h" #include "llvm/Support/Casting.h" +#include <algorithm> using namespace clang; using llvm::dyn_cast; using llvm::cast; @@ -670,3 +671,49 @@ void RopePieceBTree::erase(unsigned Offset, unsigned NumBytes) { // #2. Do the erasing. getRoot(Root)->erase(Offset, NumBytes); } + +//===----------------------------------------------------------------------===// +// RewriteRope Implementation +//===----------------------------------------------------------------------===// + +RopePiece RewriteRope::MakeRopeString(const char *Start, const char *End) { + unsigned Len = End-Start; + + // If we have space for this string in the current alloc buffer, use it. + if (AllocOffs+Len <= AllocChunkSize) { + memcpy(AllocBuffer->Data+AllocOffs, Start, Len); + AllocOffs += Len; + return RopePiece(AllocBuffer, AllocOffs-Len, AllocOffs); + } + + // If we don't have enough room because this specific allocation is huge, + // just allocate a new rope piece for it alone. + if (Len > AllocChunkSize) { + unsigned Size = End-Start+sizeof(RopeRefCountString)-1; + RopeRefCountString *Res = + reinterpret_cast<RopeRefCountString *>(new char[Size]); + Res->RefCount = 0; + memcpy(Res->Data, Start, End-Start); + return RopePiece(Res, 0, End-Start); + } + + // Otherwise, this was a small request but we just don't have space for it + // Make a new chunk and share it with later allocations. + + // If we had an old allocation, drop our reference to it. + if (AllocBuffer && --AllocBuffer->RefCount == 0) + delete [] (char*)AllocBuffer; + + unsigned AllocSize = sizeof(RopeRefCountString)-1+AllocChunkSize; + AllocBuffer = reinterpret_cast<RopeRefCountString *>(new char[AllocSize]); + AllocBuffer->RefCount = 0; + memcpy(AllocBuffer->Data, Start, Len); + AllocOffs = Len; + + // Start out the new allocation with a refcount of 1, since we have an + // internal reference to it. + AllocBuffer->addRef(); + return RopePiece(AllocBuffer, 0, Len); +} + + |