summaryrefslogtreecommitdiffstats
path: root/clang/lib
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-04-14 21:41:00 +0000
committerChris Lattner <sabre@nondot.org>2008-04-14 21:41:00 +0000
commite58408d8db240ff01c690f49812169d594f7e4ee (patch)
tree554a927ba87d049d1bb653f8f217faca7fa4443f /clang/lib
parent4fff2a08aa53b8dccef6ce2b403e1a5f48cc267d (diff)
downloadbcm5719-llvm-e58408d8db240ff01c690f49812169d594f7e4ee.tar.gz
bcm5719-llvm-e58408d8db240ff01c690f49812169d594f7e4ee.zip
Add a bunch of comments, move RewriteRope::MakeRopeString out of line.
llvm-svn: 49689
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/Rewrite/RewriteRope.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/clang/lib/Rewrite/RewriteRope.cpp b/clang/lib/Rewrite/RewriteRope.cpp
index 8a082edba63..2adf5c4f4bb 100644
--- a/clang/lib/Rewrite/RewriteRope.cpp
+++ b/clang/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);
+}
+
+
OpenPOWER on IntegriCloud