blob: cc8de1b11a18bfa3d435263e299a22fb90a2edf4 [file] [log] [blame]
Chris Lattner5fd3e262008-04-14 17:54:23 +00001//===--- RewriteRope.cpp - Rope specialized for rewriter --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the RewriteRope class, which is a powerful string.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Rewrite/RewriteRope.h"
Chris Lattner5f9e2722011-07-23 10:55:15 +000015#include "clang/Basic/LLVM.h"
Chris Lattner5618d882008-04-14 21:41:00 +000016#include <algorithm>
Chris Lattner5fd3e262008-04-14 17:54:23 +000017using namespace clang;
Chris Lattner5fd3e262008-04-14 17:54:23 +000018
Chris Lattnerb9b30942008-04-15 06:37:11 +000019/// RewriteRope is a "strong" string class, designed to make insertions and
20/// deletions in the middle of the string nearly constant time (really, they are
21/// O(log N), but with a very low constant factor).
22///
23/// The implementation of this datastructure is a conceptual linear sequence of
24/// RopePiece elements. Each RopePiece represents a view on a separately
25/// allocated and reference counted string. This means that splitting a very
26/// long string can be done in constant time by splitting a RopePiece that
27/// references the whole string into two rope pieces that reference each half.
28/// Once split, another string can be inserted in between the two halves by
29/// inserting a RopePiece in between the two others. All of this is very
30/// inexpensive: it takes time proportional to the number of RopePieces, not the
31/// length of the strings they represent.
32///
33/// While a linear sequences of RopePieces is the conceptual model, the actual
34/// implementation captures them in an adapted B+ Tree. Using a B+ tree (which
35/// is a tree that keeps the values in the leaves and has where each node
36/// contains a reasonable number of pointers to children/values) allows us to
37/// maintain efficient operation when the RewriteRope contains a *huge* number
38/// of RopePieces. The basic idea of the B+ Tree is that it allows us to find
39/// the RopePiece corresponding to some offset very efficiently, and it
40/// automatically balances itself on insertions of RopePieces (which can happen
41/// for both insertions and erases of string ranges).
42///
43/// The one wrinkle on the theory is that we don't attempt to keep the tree
44/// properly balanced when erases happen. Erases of string data can both insert
45/// new RopePieces (e.g. when the middle of some other rope piece is deleted,
46/// which results in two rope pieces, which is just like an insert) or it can
47/// reduce the number of RopePieces maintained by the B+Tree. In the case when
48/// the number of RopePieces is reduced, we don't attempt to maintain the
49/// standard 'invariant' that each node in the tree contains at least
50/// 'WidthFactor' children/values. For our use cases, this doesn't seem to
51/// matter.
52///
53/// The implementation below is primarily implemented in terms of three classes:
54/// RopePieceBTreeNode - Common base class for:
55///
56/// RopePieceBTreeLeaf - Directly manages up to '2*WidthFactor' RopePiece
57/// nodes. This directly represents a chunk of the string with those
58/// RopePieces contatenated.
59/// RopePieceBTreeInterior - An interior node in the B+ Tree, which manages
60/// up to '2*WidthFactor' other nodes in the tree.
Chris Lattner5fd3e262008-04-14 17:54:23 +000061
Chris Lattner5fd3e262008-04-14 17:54:23 +000062
Chris Lattner5fd3e262008-04-14 17:54:23 +000063//===----------------------------------------------------------------------===//
64// RopePieceBTreeNode Class
65//===----------------------------------------------------------------------===//
66
67namespace {
Chris Lattnerb9b30942008-04-15 06:37:11 +000068 /// RopePieceBTreeNode - Common base class of RopePieceBTreeLeaf and
69 /// RopePieceBTreeInterior. This provides some 'virtual' dispatching methods
70 /// and a flag that determines which subclass the instance is. Also
71 /// important, this node knows the full extend of the node, including any
72 /// children that it has. This allows efficient skipping over entire subtrees
73 /// when looking for an offset in the BTree.
Chris Lattner5fd3e262008-04-14 17:54:23 +000074 class RopePieceBTreeNode {
75 protected:
76 /// WidthFactor - This controls the number of K/V slots held in the BTree:
77 /// how wide it is. Each level of the BTree is guaranteed to have at least
78 /// 'WidthFactor' elements in it (either ropepieces or children), (except
79 /// the root, which may have less) and may have at most 2*WidthFactor
80 /// elements.
81 enum { WidthFactor = 8 };
Mike Stump1eb44332009-09-09 15:08:12 +000082
Chris Lattner5fd3e262008-04-14 17:54:23 +000083 /// Size - This is the number of bytes of file this node (including any
84 /// potential children) covers.
85 unsigned Size;
Mike Stump1eb44332009-09-09 15:08:12 +000086
Chris Lattner5fd3e262008-04-14 17:54:23 +000087 /// IsLeaf - True if this is an instance of RopePieceBTreeLeaf, false if it
88 /// is an instance of RopePieceBTreeInterior.
89 bool IsLeaf;
Mike Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattnerb442e212008-04-14 20:05:32 +000091 RopePieceBTreeNode(bool isLeaf) : Size(0), IsLeaf(isLeaf) {}
Chris Lattner5fd3e262008-04-14 17:54:23 +000092 ~RopePieceBTreeNode() {}
93 public:
Mike Stump1eb44332009-09-09 15:08:12 +000094
Chris Lattner5fd3e262008-04-14 17:54:23 +000095 bool isLeaf() const { return IsLeaf; }
96 unsigned size() const { return Size; }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chris Lattner5fd3e262008-04-14 17:54:23 +000098 void Destroy();
Mike Stump1eb44332009-09-09 15:08:12 +000099
Chris Lattner5fd3e262008-04-14 17:54:23 +0000100 /// split - Split the range containing the specified offset so that we are
101 /// guaranteed that there is a place to do an insertion at the specified
Chris Lattnerbf268562008-04-14 22:10:58 +0000102 /// offset. The offset is relative, so "0" is the start of the node.
103 ///
104 /// If there is no space in this subtree for the extra piece, the extra tree
105 /// node is returned and must be inserted into a parent.
106 RopePieceBTreeNode *split(unsigned Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Chris Lattner5fd3e262008-04-14 17:54:23 +0000108 /// insert - Insert the specified ropepiece into this tree node at the
109 /// specified offset. The offset is relative, so "0" is the start of the
Chris Lattnerbf268562008-04-14 22:10:58 +0000110 /// node.
111 ///
112 /// If there is no space in this subtree for the extra piece, the extra tree
113 /// node is returned and must be inserted into a parent.
114 RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R);
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Chris Lattner5fd3e262008-04-14 17:54:23 +0000116 /// erase - Remove NumBytes from this node at the specified offset. We are
117 /// guaranteed that there is a split at Offset.
118 void erase(unsigned Offset, unsigned NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Chris Lattner24dce6e2010-09-04 18:19:08 +0000120 //static inline bool classof(const RopePieceBTreeNode *) { return true; }
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Chris Lattner5fd3e262008-04-14 17:54:23 +0000122 };
123} // end anonymous namespace
124
125//===----------------------------------------------------------------------===//
126// RopePieceBTreeLeaf Class
127//===----------------------------------------------------------------------===//
128
129namespace {
Chris Lattnerb9b30942008-04-15 06:37:11 +0000130 /// RopePieceBTreeLeaf - Directly manages up to '2*WidthFactor' RopePiece
131 /// nodes. This directly represents a chunk of the string with those
132 /// RopePieces contatenated. Since this is a B+Tree, all values (in this case
133 /// instances of RopePiece) are stored in leaves like this. To make iteration
134 /// over the leaves efficient, they maintain a singly linked list through the
135 /// NextLeaf field. This allows the B+Tree forward iterator to be constant
136 /// time for all increments.
Chris Lattner5fd3e262008-04-14 17:54:23 +0000137 class RopePieceBTreeLeaf : public RopePieceBTreeNode {
138 /// NumPieces - This holds the number of rope pieces currently active in the
139 /// Pieces array.
140 unsigned char NumPieces;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Chris Lattner5fd3e262008-04-14 17:54:23 +0000142 /// Pieces - This tracks the file chunks currently in this leaf.
143 ///
144 RopePiece Pieces[2*WidthFactor];
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Chris Lattner5fd3e262008-04-14 17:54:23 +0000146 /// NextLeaf - This is a pointer to the next leaf in the tree, allowing
147 /// efficient in-order forward iteration of the tree without traversal.
Chris Lattner3d2e8c72008-05-28 18:45:56 +0000148 RopePieceBTreeLeaf **PrevLeaf, *NextLeaf;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000149 public:
Chris Lattner3d2e8c72008-05-28 18:45:56 +0000150 RopePieceBTreeLeaf() : RopePieceBTreeNode(true), NumPieces(0),
151 PrevLeaf(0), NextLeaf(0) {}
152 ~RopePieceBTreeLeaf() {
153 if (PrevLeaf || NextLeaf)
154 removeFromLeafInOrder();
Ted Kremenek90556e32009-10-20 06:31:34 +0000155 clear();
Chris Lattner3d2e8c72008-05-28 18:45:56 +0000156 }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Chris Lattner5fd3e262008-04-14 17:54:23 +0000158 bool isFull() const { return NumPieces == 2*WidthFactor; }
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Chris Lattner5fd3e262008-04-14 17:54:23 +0000160 /// clear - Remove all rope pieces from this leaf.
161 void clear() {
162 while (NumPieces)
163 Pieces[--NumPieces] = RopePiece();
164 Size = 0;
165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Chris Lattner5fd3e262008-04-14 17:54:23 +0000167 unsigned getNumPieces() const { return NumPieces; }
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Chris Lattner5fd3e262008-04-14 17:54:23 +0000169 const RopePiece &getPiece(unsigned i) const {
170 assert(i < getNumPieces() && "Invalid piece ID");
171 return Pieces[i];
172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Chris Lattner5fd3e262008-04-14 17:54:23 +0000174 const RopePieceBTreeLeaf *getNextLeafInOrder() const { return NextLeaf; }
Chris Lattner3d2e8c72008-05-28 18:45:56 +0000175 void insertAfterLeafInOrder(RopePieceBTreeLeaf *Node) {
176 assert(PrevLeaf == 0 && NextLeaf == 0 && "Already in ordering");
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Chris Lattner3d2e8c72008-05-28 18:45:56 +0000178 NextLeaf = Node->NextLeaf;
179 if (NextLeaf)
180 NextLeaf->PrevLeaf = &NextLeaf;
181 PrevLeaf = &Node->NextLeaf;
182 Node->NextLeaf = this;
183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Chris Lattner3d2e8c72008-05-28 18:45:56 +0000185 void removeFromLeafInOrder() {
186 if (PrevLeaf) {
187 *PrevLeaf = NextLeaf;
188 if (NextLeaf)
189 NextLeaf->PrevLeaf = PrevLeaf;
190 } else if (NextLeaf) {
191 NextLeaf->PrevLeaf = 0;
192 }
193 }
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Chris Lattnerb9b30942008-04-15 06:37:11 +0000195 /// FullRecomputeSizeLocally - This method recomputes the 'Size' field by
196 /// summing the size of all RopePieces.
Chris Lattner5fd3e262008-04-14 17:54:23 +0000197 void FullRecomputeSizeLocally() {
198 Size = 0;
199 for (unsigned i = 0, e = getNumPieces(); i != e; ++i)
200 Size += getPiece(i).size();
201 }
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Chris Lattner5fd3e262008-04-14 17:54:23 +0000203 /// split - Split the range containing the specified offset so that we are
204 /// guaranteed that there is a place to do an insertion at the specified
Chris Lattnerbf268562008-04-14 22:10:58 +0000205 /// offset. The offset is relative, so "0" is the start of the node.
206 ///
207 /// If there is no space in this subtree for the extra piece, the extra tree
208 /// node is returned and must be inserted into a parent.
209 RopePieceBTreeNode *split(unsigned Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Chris Lattner5fd3e262008-04-14 17:54:23 +0000211 /// insert - Insert the specified ropepiece into this tree node at the
212 /// specified offset. The offset is relative, so "0" is the start of the
Chris Lattnerbf268562008-04-14 22:10:58 +0000213 /// node.
214 ///
215 /// If there is no space in this subtree for the extra piece, the extra tree
216 /// node is returned and must be inserted into a parent.
217 RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R);
Mike Stump1eb44332009-09-09 15:08:12 +0000218
219
Chris Lattner5fd3e262008-04-14 17:54:23 +0000220 /// erase - Remove NumBytes from this node at the specified offset. We are
221 /// guaranteed that there is a split at Offset.
222 void erase(unsigned Offset, unsigned NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Chris Lattner24dce6e2010-09-04 18:19:08 +0000224 //static inline bool classof(const RopePieceBTreeLeaf *) { return true; }
Chris Lattner5fd3e262008-04-14 17:54:23 +0000225 static inline bool classof(const RopePieceBTreeNode *N) {
226 return N->isLeaf();
227 }
228 };
229} // end anonymous namespace
230
231/// split - Split the range containing the specified offset so that we are
232/// guaranteed that there is a place to do an insertion at the specified
Chris Lattnerbf268562008-04-14 22:10:58 +0000233/// offset. The offset is relative, so "0" is the start of the node.
234///
235/// If there is no space in this subtree for the extra piece, the extra tree
236/// node is returned and must be inserted into a parent.
237RopePieceBTreeNode *RopePieceBTreeLeaf::split(unsigned Offset) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000238 // Find the insertion point. We are guaranteed that there is a split at the
239 // specified offset so find it.
240 if (Offset == 0 || Offset == size()) {
241 // Fastpath for a common case. There is already a splitpoint at the end.
Chris Lattnerbf268562008-04-14 22:10:58 +0000242 return 0;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000243 }
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Chris Lattner5fd3e262008-04-14 17:54:23 +0000245 // Find the piece that this offset lands in.
246 unsigned PieceOffs = 0;
247 unsigned i = 0;
248 while (Offset >= PieceOffs+Pieces[i].size()) {
249 PieceOffs += Pieces[i].size();
250 ++i;
251 }
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Chris Lattner5fd3e262008-04-14 17:54:23 +0000253 // If there is already a split point at the specified offset, just return
254 // success.
255 if (PieceOffs == Offset)
Chris Lattnerbf268562008-04-14 22:10:58 +0000256 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Chris Lattner5fd3e262008-04-14 17:54:23 +0000258 // Otherwise, we need to split piece 'i' at Offset-PieceOffs. Convert Offset
259 // to being Piece relative.
260 unsigned IntraPieceOffset = Offset-PieceOffs;
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Chris Lattner5fd3e262008-04-14 17:54:23 +0000262 // We do this by shrinking the RopePiece and then doing an insert of the tail.
263 RopePiece Tail(Pieces[i].StrData, Pieces[i].StartOffs+IntraPieceOffset,
264 Pieces[i].EndOffs);
265 Size -= Pieces[i].size();
266 Pieces[i].EndOffs = Pieces[i].StartOffs+IntraPieceOffset;
267 Size += Pieces[i].size();
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Chris Lattnerbf268562008-04-14 22:10:58 +0000269 return insert(Offset, Tail);
Chris Lattner5fd3e262008-04-14 17:54:23 +0000270}
271
272
273/// insert - Insert the specified RopePiece into this tree node at the
Chris Lattnerbf268562008-04-14 22:10:58 +0000274/// specified offset. The offset is relative, so "0" is the start of the node.
275///
276/// If there is no space in this subtree for the extra piece, the extra tree
277/// node is returned and must be inserted into a parent.
278RopePieceBTreeNode *RopePieceBTreeLeaf::insert(unsigned Offset,
279 const RopePiece &R) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000280 // If this node is not full, insert the piece.
281 if (!isFull()) {
282 // Find the insertion point. We are guaranteed that there is a split at the
283 // specified offset so find it.
284 unsigned i = 0, e = getNumPieces();
285 if (Offset == size()) {
286 // Fastpath for a common case.
287 i = e;
288 } else {
289 unsigned SlotOffs = 0;
290 for (; Offset > SlotOffs; ++i)
291 SlotOffs += getPiece(i).size();
292 assert(SlotOffs == Offset && "Split didn't occur before insertion!");
293 }
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Chris Lattner5fd3e262008-04-14 17:54:23 +0000295 // For an insertion into a non-full leaf node, just insert the value in
296 // its sorted position. This requires moving later values over.
297 for (; i != e; --e)
298 Pieces[e] = Pieces[e-1];
299 Pieces[i] = R;
300 ++NumPieces;
301 Size += R.size();
Chris Lattnerbf268562008-04-14 22:10:58 +0000302 return 0;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000303 }
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Chris Lattner5fd3e262008-04-14 17:54:23 +0000305 // Otherwise, if this is leaf is full, split it in two halves. Since this
306 // node is full, it contains 2*WidthFactor values. We move the first
307 // 'WidthFactor' values to the LHS child (which we leave in this node) and
308 // move the last 'WidthFactor' values into the RHS child.
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Chris Lattner5fd3e262008-04-14 17:54:23 +0000310 // Create the new node.
311 RopePieceBTreeLeaf *NewNode = new RopePieceBTreeLeaf();
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Chris Lattner5fd3e262008-04-14 17:54:23 +0000313 // Move over the last 'WidthFactor' values from here to NewNode.
314 std::copy(&Pieces[WidthFactor], &Pieces[2*WidthFactor],
315 &NewNode->Pieces[0]);
316 // Replace old pieces with null RopePieces to drop refcounts.
317 std::fill(&Pieces[WidthFactor], &Pieces[2*WidthFactor], RopePiece());
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Chris Lattner5fd3e262008-04-14 17:54:23 +0000319 // Decrease the number of values in the two nodes.
320 NewNode->NumPieces = NumPieces = WidthFactor;
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Chris Lattner5fd3e262008-04-14 17:54:23 +0000322 // Recompute the two nodes' size.
323 NewNode->FullRecomputeSizeLocally();
324 FullRecomputeSizeLocally();
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Chris Lattner5fd3e262008-04-14 17:54:23 +0000326 // Update the list of leaves.
Chris Lattner3d2e8c72008-05-28 18:45:56 +0000327 NewNode->insertAfterLeafInOrder(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Chris Lattnerbf268562008-04-14 22:10:58 +0000329 // These insertions can't fail.
Chris Lattner5fd3e262008-04-14 17:54:23 +0000330 if (this->size() >= Offset)
Chris Lattnerbf268562008-04-14 22:10:58 +0000331 this->insert(Offset, R);
Chris Lattner5fd3e262008-04-14 17:54:23 +0000332 else
Chris Lattnerbf268562008-04-14 22:10:58 +0000333 NewNode->insert(Offset - this->size(), R);
334 return NewNode;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000335}
336
337/// erase - Remove NumBytes from this node at the specified offset. We are
338/// guaranteed that there is a split at Offset.
339void RopePieceBTreeLeaf::erase(unsigned Offset, unsigned NumBytes) {
340 // Since we are guaranteed that there is a split at Offset, we start by
341 // finding the Piece that starts there.
342 unsigned PieceOffs = 0;
343 unsigned i = 0;
344 for (; Offset > PieceOffs; ++i)
345 PieceOffs += getPiece(i).size();
346 assert(PieceOffs == Offset && "Split didn't occur before erase!");
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Chris Lattner5fd3e262008-04-14 17:54:23 +0000348 unsigned StartPiece = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Chris Lattner5fd3e262008-04-14 17:54:23 +0000350 // Figure out how many pieces completely cover 'NumBytes'. We want to remove
351 // all of them.
352 for (; Offset+NumBytes > PieceOffs+getPiece(i).size(); ++i)
353 PieceOffs += getPiece(i).size();
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Chris Lattner5fd3e262008-04-14 17:54:23 +0000355 // If we exactly include the last one, include it in the region to delete.
356 if (Offset+NumBytes == PieceOffs+getPiece(i).size())
357 PieceOffs += getPiece(i).size(), ++i;
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Chris Lattner5fd3e262008-04-14 17:54:23 +0000359 // If we completely cover some RopePieces, erase them now.
360 if (i != StartPiece) {
361 unsigned NumDeleted = i-StartPiece;
362 for (; i != getNumPieces(); ++i)
363 Pieces[i-NumDeleted] = Pieces[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Chris Lattner5fd3e262008-04-14 17:54:23 +0000365 // Drop references to dead rope pieces.
366 std::fill(&Pieces[getNumPieces()-NumDeleted], &Pieces[getNumPieces()],
367 RopePiece());
368 NumPieces -= NumDeleted;
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Chris Lattner5fd3e262008-04-14 17:54:23 +0000370 unsigned CoverBytes = PieceOffs-Offset;
371 NumBytes -= CoverBytes;
372 Size -= CoverBytes;
373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattner5fd3e262008-04-14 17:54:23 +0000375 // If we completely removed some stuff, we could be done.
376 if (NumBytes == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Chris Lattner5fd3e262008-04-14 17:54:23 +0000378 // Okay, now might be erasing part of some Piece. If this is the case, then
379 // move the start point of the piece.
380 assert(getPiece(StartPiece).size() > NumBytes);
381 Pieces[StartPiece].StartOffs += NumBytes;
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Chris Lattner5fd3e262008-04-14 17:54:23 +0000383 // The size of this node just shrunk by NumBytes.
384 Size -= NumBytes;
385}
386
387//===----------------------------------------------------------------------===//
388// RopePieceBTreeInterior Class
389//===----------------------------------------------------------------------===//
390
391namespace {
Chris Lattnerb9b30942008-04-15 06:37:11 +0000392 /// RopePieceBTreeInterior - This represents an interior node in the B+Tree,
393 /// which holds up to 2*WidthFactor pointers to child nodes.
Chris Lattner5fd3e262008-04-14 17:54:23 +0000394 class RopePieceBTreeInterior : public RopePieceBTreeNode {
395 /// NumChildren - This holds the number of children currently active in the
396 /// Children array.
397 unsigned char NumChildren;
398 RopePieceBTreeNode *Children[2*WidthFactor];
399 public:
Chris Lattner70778c82008-04-14 20:07:03 +0000400 RopePieceBTreeInterior() : RopePieceBTreeNode(false), NumChildren(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Chris Lattner5fd3e262008-04-14 17:54:23 +0000402 RopePieceBTreeInterior(RopePieceBTreeNode *LHS, RopePieceBTreeNode *RHS)
403 : RopePieceBTreeNode(false) {
404 Children[0] = LHS;
405 Children[1] = RHS;
406 NumChildren = 2;
407 Size = LHS->size() + RHS->size();
408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Benjamin Kramer398a4982012-04-15 11:09:40 +0000410 ~RopePieceBTreeInterior() {
Benjamin Kramera9ab2092012-04-15 11:35:18 +0000411 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
412 Children[i]->Destroy();
Benjamin Kramer398a4982012-04-15 11:09:40 +0000413 }
414
Chris Lattner5fd3e262008-04-14 17:54:23 +0000415 bool isFull() const { return NumChildren == 2*WidthFactor; }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner5fd3e262008-04-14 17:54:23 +0000417 unsigned getNumChildren() const { return NumChildren; }
418 const RopePieceBTreeNode *getChild(unsigned i) const {
419 assert(i < NumChildren && "invalid child #");
420 return Children[i];
421 }
422 RopePieceBTreeNode *getChild(unsigned i) {
423 assert(i < NumChildren && "invalid child #");
424 return Children[i];
425 }
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Chris Lattnerb9b30942008-04-15 06:37:11 +0000427 /// FullRecomputeSizeLocally - Recompute the Size field of this node by
428 /// summing up the sizes of the child nodes.
Chris Lattner5fd3e262008-04-14 17:54:23 +0000429 void FullRecomputeSizeLocally() {
430 Size = 0;
431 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
432 Size += getChild(i)->size();
433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
435
Chris Lattner5fd3e262008-04-14 17:54:23 +0000436 /// split - Split the range containing the specified offset so that we are
437 /// guaranteed that there is a place to do an insertion at the specified
Chris Lattnerbf268562008-04-14 22:10:58 +0000438 /// offset. The offset is relative, so "0" is the start of the node.
439 ///
440 /// If there is no space in this subtree for the extra piece, the extra tree
441 /// node is returned and must be inserted into a parent.
442 RopePieceBTreeNode *split(unsigned Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000443
444
Chris Lattner5fd3e262008-04-14 17:54:23 +0000445 /// insert - Insert the specified ropepiece into this tree node at the
446 /// specified offset. The offset is relative, so "0" is the start of the
Chris Lattnerbf268562008-04-14 22:10:58 +0000447 /// node.
448 ///
449 /// If there is no space in this subtree for the extra piece, the extra tree
450 /// node is returned and must be inserted into a parent.
451 RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R);
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Chris Lattner5fd3e262008-04-14 17:54:23 +0000453 /// HandleChildPiece - A child propagated an insertion result up to us.
454 /// Insert the new child, and/or propagate the result further up the tree.
Chris Lattnerbf268562008-04-14 22:10:58 +0000455 RopePieceBTreeNode *HandleChildPiece(unsigned i, RopePieceBTreeNode *RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Chris Lattner5fd3e262008-04-14 17:54:23 +0000457 /// erase - Remove NumBytes from this node at the specified offset. We are
458 /// guaranteed that there is a split at Offset.
459 void erase(unsigned Offset, unsigned NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Chris Lattner24dce6e2010-09-04 18:19:08 +0000461 //static inline bool classof(const RopePieceBTreeInterior *) { return true; }
Chris Lattner5fd3e262008-04-14 17:54:23 +0000462 static inline bool classof(const RopePieceBTreeNode *N) {
Mike Stump1eb44332009-09-09 15:08:12 +0000463 return !N->isLeaf();
Chris Lattner5fd3e262008-04-14 17:54:23 +0000464 }
465 };
466} // end anonymous namespace
467
468/// split - Split the range containing the specified offset so that we are
469/// guaranteed that there is a place to do an insertion at the specified
Chris Lattnerbf268562008-04-14 22:10:58 +0000470/// offset. The offset is relative, so "0" is the start of the node.
471///
472/// If there is no space in this subtree for the extra piece, the extra tree
473/// node is returned and must be inserted into a parent.
474RopePieceBTreeNode *RopePieceBTreeInterior::split(unsigned Offset) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000475 // Figure out which child to split.
476 if (Offset == 0 || Offset == size())
Chris Lattnerbf268562008-04-14 22:10:58 +0000477 return 0; // If we have an exact offset, we're already split.
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Chris Lattner5fd3e262008-04-14 17:54:23 +0000479 unsigned ChildOffset = 0;
480 unsigned i = 0;
481 for (; Offset >= ChildOffset+getChild(i)->size(); ++i)
482 ChildOffset += getChild(i)->size();
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattner5fd3e262008-04-14 17:54:23 +0000484 // If already split there, we're done.
485 if (ChildOffset == Offset)
Chris Lattnerbf268562008-04-14 22:10:58 +0000486 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Chris Lattner5fd3e262008-04-14 17:54:23 +0000488 // Otherwise, recursively split the child.
Mike Stump1eb44332009-09-09 15:08:12 +0000489 if (RopePieceBTreeNode *RHS = getChild(i)->split(Offset-ChildOffset))
Chris Lattnerbf268562008-04-14 22:10:58 +0000490 return HandleChildPiece(i, RHS);
491 return 0; // Done!
Chris Lattner5fd3e262008-04-14 17:54:23 +0000492}
493
494/// insert - Insert the specified ropepiece into this tree node at the
495/// specified offset. The offset is relative, so "0" is the start of the
Chris Lattnerbf268562008-04-14 22:10:58 +0000496/// node.
497///
498/// If there is no space in this subtree for the extra piece, the extra tree
499/// node is returned and must be inserted into a parent.
500RopePieceBTreeNode *RopePieceBTreeInterior::insert(unsigned Offset,
501 const RopePiece &R) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000502 // Find the insertion point. We are guaranteed that there is a split at the
503 // specified offset so find it.
504 unsigned i = 0, e = getNumChildren();
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Chris Lattner5fd3e262008-04-14 17:54:23 +0000506 unsigned ChildOffs = 0;
507 if (Offset == size()) {
508 // Fastpath for a common case. Insert at end of last child.
509 i = e-1;
510 ChildOffs = size()-getChild(i)->size();
511 } else {
512 for (; Offset > ChildOffs+getChild(i)->size(); ++i)
513 ChildOffs += getChild(i)->size();
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Chris Lattner5fd3e262008-04-14 17:54:23 +0000516 Size += R.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Chris Lattner5fd3e262008-04-14 17:54:23 +0000518 // Insert at the end of this child.
Chris Lattnerbf268562008-04-14 22:10:58 +0000519 if (RopePieceBTreeNode *RHS = getChild(i)->insert(Offset-ChildOffs, R))
520 return HandleChildPiece(i, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Chris Lattnerbf268562008-04-14 22:10:58 +0000522 return 0;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000523}
524
525/// HandleChildPiece - A child propagated an insertion result up to us.
526/// Insert the new child, and/or propagate the result further up the tree.
Chris Lattnerbf268562008-04-14 22:10:58 +0000527RopePieceBTreeNode *
528RopePieceBTreeInterior::HandleChildPiece(unsigned i, RopePieceBTreeNode *RHS) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000529 // Otherwise the child propagated a subtree up to us as a new child. See if
530 // we have space for it here.
531 if (!isFull()) {
Chris Lattnerbf268562008-04-14 22:10:58 +0000532 // Insert RHS after child 'i'.
Chris Lattner5fd3e262008-04-14 17:54:23 +0000533 if (i + 1 != getNumChildren())
534 memmove(&Children[i+2], &Children[i+1],
535 (getNumChildren()-i-1)*sizeof(Children[0]));
Chris Lattnerbf268562008-04-14 22:10:58 +0000536 Children[i+1] = RHS;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000537 ++NumChildren;
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +0000538 return 0;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000539 }
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Chris Lattner5fd3e262008-04-14 17:54:23 +0000541 // Okay, this node is full. Split it in half, moving WidthFactor children to
542 // a newly allocated interior node.
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Chris Lattner5fd3e262008-04-14 17:54:23 +0000544 // Create the new node.
545 RopePieceBTreeInterior *NewNode = new RopePieceBTreeInterior();
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Chris Lattner5fd3e262008-04-14 17:54:23 +0000547 // Move over the last 'WidthFactor' values from here to NewNode.
548 memcpy(&NewNode->Children[0], &Children[WidthFactor],
549 WidthFactor*sizeof(Children[0]));
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chris Lattner5fd3e262008-04-14 17:54:23 +0000551 // Decrease the number of values in the two nodes.
552 NewNode->NumChildren = NumChildren = WidthFactor;
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Chris Lattner5fd3e262008-04-14 17:54:23 +0000554 // Finally, insert the two new children in the side the can (now) hold them.
Chris Lattnerbf268562008-04-14 22:10:58 +0000555 // These insertions can't fail.
Chris Lattner5fd3e262008-04-14 17:54:23 +0000556 if (i < WidthFactor)
Chris Lattnerbf268562008-04-14 22:10:58 +0000557 this->HandleChildPiece(i, RHS);
Chris Lattner5fd3e262008-04-14 17:54:23 +0000558 else
Chris Lattnerbf268562008-04-14 22:10:58 +0000559 NewNode->HandleChildPiece(i-WidthFactor, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Chris Lattner5fd3e262008-04-14 17:54:23 +0000561 // Recompute the two nodes' size.
562 NewNode->FullRecomputeSizeLocally();
563 FullRecomputeSizeLocally();
Chris Lattnerbf268562008-04-14 22:10:58 +0000564 return NewNode;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000565}
566
567/// erase - Remove NumBytes from this node at the specified offset. We are
568/// guaranteed that there is a split at Offset.
569void RopePieceBTreeInterior::erase(unsigned Offset, unsigned NumBytes) {
570 // This will shrink this node by NumBytes.
571 Size -= NumBytes;
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Chris Lattner5fd3e262008-04-14 17:54:23 +0000573 // Find the first child that overlaps with Offset.
574 unsigned i = 0;
575 for (; Offset >= getChild(i)->size(); ++i)
576 Offset -= getChild(i)->size();
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Chris Lattner5fd3e262008-04-14 17:54:23 +0000578 // Propagate the delete request into overlapping children, or completely
579 // delete the children as appropriate.
580 while (NumBytes) {
581 RopePieceBTreeNode *CurChild = getChild(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Chris Lattner5fd3e262008-04-14 17:54:23 +0000583 // If we are deleting something contained entirely in the child, pass on the
584 // request.
585 if (Offset+NumBytes < CurChild->size()) {
586 CurChild->erase(Offset, NumBytes);
587 return;
588 }
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Chris Lattner5fd3e262008-04-14 17:54:23 +0000590 // If this deletion request starts somewhere in the middle of the child, it
591 // must be deleting to the end of the child.
592 if (Offset) {
593 unsigned BytesFromChild = CurChild->size()-Offset;
594 CurChild->erase(Offset, BytesFromChild);
595 NumBytes -= BytesFromChild;
Chris Lattnerb6403af2008-05-08 03:23:46 +0000596 // Start at the beginning of the next child.
597 Offset = 0;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000598 ++i;
599 continue;
600 }
Chris Lattnerb6403af2008-05-08 03:23:46 +0000601
Chris Lattner5fd3e262008-04-14 17:54:23 +0000602 // If the deletion request completely covers the child, delete it and move
603 // the rest down.
604 NumBytes -= CurChild->size();
605 CurChild->Destroy();
606 --NumChildren;
Chris Lattner514b24c2008-05-23 23:29:33 +0000607 if (i != getNumChildren())
Chris Lattner5fd3e262008-04-14 17:54:23 +0000608 memmove(&Children[i], &Children[i+1],
609 (getNumChildren()-i)*sizeof(Children[0]));
610 }
611}
612
613//===----------------------------------------------------------------------===//
614// RopePieceBTreeNode Implementation
615//===----------------------------------------------------------------------===//
616
617void RopePieceBTreeNode::Destroy() {
618 if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
619 delete Leaf;
620 else
621 delete cast<RopePieceBTreeInterior>(this);
622}
623
624/// split - Split the range containing the specified offset so that we are
625/// guaranteed that there is a place to do an insertion at the specified
Chris Lattnerbf268562008-04-14 22:10:58 +0000626/// offset. The offset is relative, so "0" is the start of the node.
627///
628/// If there is no space in this subtree for the extra piece, the extra tree
629/// node is returned and must be inserted into a parent.
630RopePieceBTreeNode *RopePieceBTreeNode::split(unsigned Offset) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000631 assert(Offset <= size() && "Invalid offset to split!");
632 if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
Chris Lattnerbf268562008-04-14 22:10:58 +0000633 return Leaf->split(Offset);
634 return cast<RopePieceBTreeInterior>(this)->split(Offset);
Chris Lattner5fd3e262008-04-14 17:54:23 +0000635}
636
637/// insert - Insert the specified ropepiece into this tree node at the
638/// specified offset. The offset is relative, so "0" is the start of the
639/// node.
Chris Lattnerbf268562008-04-14 22:10:58 +0000640///
641/// If there is no space in this subtree for the extra piece, the extra tree
642/// node is returned and must be inserted into a parent.
643RopePieceBTreeNode *RopePieceBTreeNode::insert(unsigned Offset,
644 const RopePiece &R) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000645 assert(Offset <= size() && "Invalid offset to insert!");
646 if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
Chris Lattnerbf268562008-04-14 22:10:58 +0000647 return Leaf->insert(Offset, R);
648 return cast<RopePieceBTreeInterior>(this)->insert(Offset, R);
Chris Lattner5fd3e262008-04-14 17:54:23 +0000649}
650
651/// erase - Remove NumBytes from this node at the specified offset. We are
652/// guaranteed that there is a split at Offset.
653void RopePieceBTreeNode::erase(unsigned Offset, unsigned NumBytes) {
654 assert(Offset+NumBytes <= size() && "Invalid offset to erase!");
655 if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
656 return Leaf->erase(Offset, NumBytes);
657 return cast<RopePieceBTreeInterior>(this)->erase(Offset, NumBytes);
658}
659
660
661//===----------------------------------------------------------------------===//
662// RopePieceBTreeIterator Implementation
663//===----------------------------------------------------------------------===//
664
665static const RopePieceBTreeLeaf *getCN(const void *P) {
666 return static_cast<const RopePieceBTreeLeaf*>(P);
667}
668
669// begin iterator.
670RopePieceBTreeIterator::RopePieceBTreeIterator(const void *n) {
671 const RopePieceBTreeNode *N = static_cast<const RopePieceBTreeNode*>(n);
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Chris Lattner5fd3e262008-04-14 17:54:23 +0000673 // Walk down the left side of the tree until we get to a leaf.
674 while (const RopePieceBTreeInterior *IN = dyn_cast<RopePieceBTreeInterior>(N))
675 N = IN->getChild(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Chris Lattner5fd3e262008-04-14 17:54:23 +0000677 // We must have at least one leaf.
678 CurNode = cast<RopePieceBTreeLeaf>(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Chris Lattner5fd3e262008-04-14 17:54:23 +0000680 // If we found a leaf that happens to be empty, skip over it until we get
681 // to something full.
682 while (CurNode && getCN(CurNode)->getNumPieces() == 0)
683 CurNode = getCN(CurNode)->getNextLeafInOrder();
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Chris Lattner5fd3e262008-04-14 17:54:23 +0000685 if (CurNode != 0)
686 CurPiece = &getCN(CurNode)->getPiece(0);
687 else // Empty tree, this is an end() iterator.
688 CurPiece = 0;
689 CurChar = 0;
690}
691
692void RopePieceBTreeIterator::MoveToNextPiece() {
693 if (CurPiece != &getCN(CurNode)->getPiece(getCN(CurNode)->getNumPieces()-1)) {
694 CurChar = 0;
695 ++CurPiece;
696 return;
697 }
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Chris Lattner5fd3e262008-04-14 17:54:23 +0000699 // Find the next non-empty leaf node.
700 do
701 CurNode = getCN(CurNode)->getNextLeafInOrder();
702 while (CurNode && getCN(CurNode)->getNumPieces() == 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Chris Lattner5fd3e262008-04-14 17:54:23 +0000704 if (CurNode != 0)
705 CurPiece = &getCN(CurNode)->getPiece(0);
706 else // Hit end().
707 CurPiece = 0;
708 CurChar = 0;
709}
710
711//===----------------------------------------------------------------------===//
712// RopePieceBTree Implementation
713//===----------------------------------------------------------------------===//
714
715static RopePieceBTreeNode *getRoot(void *P) {
716 return static_cast<RopePieceBTreeNode*>(P);
717}
718
719RopePieceBTree::RopePieceBTree() {
720 Root = new RopePieceBTreeLeaf();
721}
722RopePieceBTree::RopePieceBTree(const RopePieceBTree &RHS) {
723 assert(RHS.empty() && "Can't copy non-empty tree yet");
724 Root = new RopePieceBTreeLeaf();
725}
726RopePieceBTree::~RopePieceBTree() {
727 getRoot(Root)->Destroy();
728}
729
730unsigned RopePieceBTree::size() const {
731 return getRoot(Root)->size();
732}
733
734void RopePieceBTree::clear() {
735 if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(getRoot(Root)))
736 Leaf->clear();
737 else {
738 getRoot(Root)->Destroy();
739 Root = new RopePieceBTreeLeaf();
740 }
741}
742
743void RopePieceBTree::insert(unsigned Offset, const RopePiece &R) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000744 // #1. Split at Offset.
Chris Lattnerbf268562008-04-14 22:10:58 +0000745 if (RopePieceBTreeNode *RHS = getRoot(Root)->split(Offset))
746 Root = new RopePieceBTreeInterior(getRoot(Root), RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Chris Lattner5fd3e262008-04-14 17:54:23 +0000748 // #2. Do the insertion.
Chris Lattnerbf268562008-04-14 22:10:58 +0000749 if (RopePieceBTreeNode *RHS = getRoot(Root)->insert(Offset, R))
750 Root = new RopePieceBTreeInterior(getRoot(Root), RHS);
Chris Lattner5fd3e262008-04-14 17:54:23 +0000751}
752
753void RopePieceBTree::erase(unsigned Offset, unsigned NumBytes) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000754 // #1. Split at Offset.
Chris Lattnerbf268562008-04-14 22:10:58 +0000755 if (RopePieceBTreeNode *RHS = getRoot(Root)->split(Offset))
756 Root = new RopePieceBTreeInterior(getRoot(Root), RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Chris Lattner5fd3e262008-04-14 17:54:23 +0000758 // #2. Do the erasing.
759 getRoot(Root)->erase(Offset, NumBytes);
760}
Chris Lattner5618d882008-04-14 21:41:00 +0000761
762//===----------------------------------------------------------------------===//
763// RewriteRope Implementation
764//===----------------------------------------------------------------------===//
765
Chris Lattnerb9b30942008-04-15 06:37:11 +0000766/// MakeRopeString - This copies the specified byte range into some instance of
767/// RopeRefCountString, and return a RopePiece that represents it. This uses
768/// the AllocBuffer object to aggregate requests for small strings into one
769/// allocation instead of doing tons of tiny allocations.
Chris Lattner5618d882008-04-14 21:41:00 +0000770RopePiece RewriteRope::MakeRopeString(const char *Start, const char *End) {
771 unsigned Len = End-Start;
Chris Lattnerc66d0aa2008-04-23 03:21:50 +0000772 assert(Len && "Zero length RopePiece is invalid!");
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Chris Lattner5618d882008-04-14 21:41:00 +0000774 // If we have space for this string in the current alloc buffer, use it.
775 if (AllocOffs+Len <= AllocChunkSize) {
776 memcpy(AllocBuffer->Data+AllocOffs, Start, Len);
777 AllocOffs += Len;
778 return RopePiece(AllocBuffer, AllocOffs-Len, AllocOffs);
779 }
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Chris Lattner5618d882008-04-14 21:41:00 +0000781 // If we don't have enough room because this specific allocation is huge,
782 // just allocate a new rope piece for it alone.
783 if (Len > AllocChunkSize) {
784 unsigned Size = End-Start+sizeof(RopeRefCountString)-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000785 RopeRefCountString *Res =
Chris Lattnerbf268562008-04-14 22:10:58 +0000786 reinterpret_cast<RopeRefCountString *>(new char[Size]);
Chris Lattner5618d882008-04-14 21:41:00 +0000787 Res->RefCount = 0;
788 memcpy(Res->Data, Start, End-Start);
789 return RopePiece(Res, 0, End-Start);
790 }
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Chris Lattner5618d882008-04-14 21:41:00 +0000792 // Otherwise, this was a small request but we just don't have space for it
793 // Make a new chunk and share it with later allocations.
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Chris Lattner5618d882008-04-14 21:41:00 +0000795 // If we had an old allocation, drop our reference to it.
796 if (AllocBuffer && --AllocBuffer->RefCount == 0)
797 delete [] (char*)AllocBuffer;
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Zhongxing Xu3f61c182008-09-16 07:58:21 +0000799 unsigned AllocSize = offsetof(RopeRefCountString, Data) + AllocChunkSize;
Chris Lattner5618d882008-04-14 21:41:00 +0000800 AllocBuffer = reinterpret_cast<RopeRefCountString *>(new char[AllocSize]);
801 AllocBuffer->RefCount = 0;
802 memcpy(AllocBuffer->Data, Start, Len);
803 AllocOffs = Len;
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Ted Kremenek8f999932009-10-20 05:53:05 +0000805 // Start out the new allocation with a refcount of 1, since we have an
806 // internal reference to it.
807 AllocBuffer->addRef();
Chris Lattner5618d882008-04-14 21:41:00 +0000808 return RopePiece(AllocBuffer, 0, Len);
809}
810
811