blob: e29092184789132803127a2bad1190c3729e4b5d [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"
15#include "llvm/Support/Casting.h"
Chris Lattner5618d882008-04-14 21:41:00 +000016#include <algorithm>
Chris Lattner5fd3e262008-04-14 17:54:23 +000017using namespace clang;
18using llvm::dyn_cast;
19using llvm::cast;
20
Chris Lattnerb9b30942008-04-15 06:37:11 +000021/// RewriteRope is a "strong" string class, designed to make insertions and
22/// deletions in the middle of the string nearly constant time (really, they are
23/// O(log N), but with a very low constant factor).
24///
25/// The implementation of this datastructure is a conceptual linear sequence of
26/// RopePiece elements. Each RopePiece represents a view on a separately
27/// allocated and reference counted string. This means that splitting a very
28/// long string can be done in constant time by splitting a RopePiece that
29/// references the whole string into two rope pieces that reference each half.
30/// Once split, another string can be inserted in between the two halves by
31/// inserting a RopePiece in between the two others. All of this is very
32/// inexpensive: it takes time proportional to the number of RopePieces, not the
33/// length of the strings they represent.
34///
35/// While a linear sequences of RopePieces is the conceptual model, the actual
36/// implementation captures them in an adapted B+ Tree. Using a B+ tree (which
37/// is a tree that keeps the values in the leaves and has where each node
38/// contains a reasonable number of pointers to children/values) allows us to
39/// maintain efficient operation when the RewriteRope contains a *huge* number
40/// of RopePieces. The basic idea of the B+ Tree is that it allows us to find
41/// the RopePiece corresponding to some offset very efficiently, and it
42/// automatically balances itself on insertions of RopePieces (which can happen
43/// for both insertions and erases of string ranges).
44///
45/// The one wrinkle on the theory is that we don't attempt to keep the tree
46/// properly balanced when erases happen. Erases of string data can both insert
47/// new RopePieces (e.g. when the middle of some other rope piece is deleted,
48/// which results in two rope pieces, which is just like an insert) or it can
49/// reduce the number of RopePieces maintained by the B+Tree. In the case when
50/// the number of RopePieces is reduced, we don't attempt to maintain the
51/// standard 'invariant' that each node in the tree contains at least
52/// 'WidthFactor' children/values. For our use cases, this doesn't seem to
53/// matter.
54///
55/// The implementation below is primarily implemented in terms of three classes:
56/// RopePieceBTreeNode - Common base class for:
57///
58/// RopePieceBTreeLeaf - Directly manages up to '2*WidthFactor' RopePiece
59/// nodes. This directly represents a chunk of the string with those
60/// RopePieces contatenated.
61/// RopePieceBTreeInterior - An interior node in the B+ Tree, which manages
62/// up to '2*WidthFactor' other nodes in the tree.
Chris Lattner5fd3e262008-04-14 17:54:23 +000063
Chris Lattner5fd3e262008-04-14 17:54:23 +000064
Chris Lattner5fd3e262008-04-14 17:54:23 +000065//===----------------------------------------------------------------------===//
66// RopePieceBTreeNode Class
67//===----------------------------------------------------------------------===//
68
69namespace {
Chris Lattnerb9b30942008-04-15 06:37:11 +000070 /// RopePieceBTreeNode - Common base class of RopePieceBTreeLeaf and
71 /// RopePieceBTreeInterior. This provides some 'virtual' dispatching methods
72 /// and a flag that determines which subclass the instance is. Also
73 /// important, this node knows the full extend of the node, including any
74 /// children that it has. This allows efficient skipping over entire subtrees
75 /// when looking for an offset in the BTree.
Chris Lattner5fd3e262008-04-14 17:54:23 +000076 class RopePieceBTreeNode {
77 protected:
78 /// WidthFactor - This controls the number of K/V slots held in the BTree:
79 /// how wide it is. Each level of the BTree is guaranteed to have at least
80 /// 'WidthFactor' elements in it (either ropepieces or children), (except
81 /// the root, which may have less) and may have at most 2*WidthFactor
82 /// elements.
83 enum { WidthFactor = 8 };
Mike Stump1eb44332009-09-09 15:08:12 +000084
Chris Lattner5fd3e262008-04-14 17:54:23 +000085 /// Size - This is the number of bytes of file this node (including any
86 /// potential children) covers.
87 unsigned Size;
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner5fd3e262008-04-14 17:54:23 +000089 /// IsLeaf - True if this is an instance of RopePieceBTreeLeaf, false if it
90 /// is an instance of RopePieceBTreeInterior.
91 bool IsLeaf;
Mike Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattnerb442e212008-04-14 20:05:32 +000093 RopePieceBTreeNode(bool isLeaf) : Size(0), IsLeaf(isLeaf) {}
Chris Lattner5fd3e262008-04-14 17:54:23 +000094 ~RopePieceBTreeNode() {}
95 public:
Mike Stump1eb44332009-09-09 15:08:12 +000096
Chris Lattner5fd3e262008-04-14 17:54:23 +000097 bool isLeaf() const { return IsLeaf; }
98 unsigned size() const { return Size; }
Mike Stump1eb44332009-09-09 15:08:12 +000099
Chris Lattner5fd3e262008-04-14 17:54:23 +0000100 void Destroy();
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Chris Lattner5fd3e262008-04-14 17:54:23 +0000102 /// split - Split the range containing the specified offset so that we are
103 /// guaranteed that there is a place to do an insertion at the specified
Chris Lattnerbf268562008-04-14 22:10:58 +0000104 /// offset. The offset is relative, so "0" is the start of the node.
105 ///
106 /// If there is no space in this subtree for the extra piece, the extra tree
107 /// node is returned and must be inserted into a parent.
108 RopePieceBTreeNode *split(unsigned Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Chris Lattner5fd3e262008-04-14 17:54:23 +0000110 /// insert - Insert the specified ropepiece into this tree node at the
111 /// specified offset. The offset is relative, so "0" is the start of the
Chris Lattnerbf268562008-04-14 22:10:58 +0000112 /// node.
113 ///
114 /// If there is no space in this subtree for the extra piece, the extra tree
115 /// node is returned and must be inserted into a parent.
116 RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R);
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chris Lattner5fd3e262008-04-14 17:54:23 +0000118 /// erase - Remove NumBytes from this node at the specified offset. We are
119 /// guaranteed that there is a split at Offset.
120 void erase(unsigned Offset, unsigned NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Chris Lattner5fd3e262008-04-14 17:54:23 +0000122 static inline bool classof(const RopePieceBTreeNode *) { return true; }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Chris Lattner5fd3e262008-04-14 17:54:23 +0000124 };
125} // end anonymous namespace
126
127//===----------------------------------------------------------------------===//
128// RopePieceBTreeLeaf Class
129//===----------------------------------------------------------------------===//
130
131namespace {
Chris Lattnerb9b30942008-04-15 06:37:11 +0000132 /// RopePieceBTreeLeaf - Directly manages up to '2*WidthFactor' RopePiece
133 /// nodes. This directly represents a chunk of the string with those
134 /// RopePieces contatenated. Since this is a B+Tree, all values (in this case
135 /// instances of RopePiece) are stored in leaves like this. To make iteration
136 /// over the leaves efficient, they maintain a singly linked list through the
137 /// NextLeaf field. This allows the B+Tree forward iterator to be constant
138 /// time for all increments.
Chris Lattner5fd3e262008-04-14 17:54:23 +0000139 class RopePieceBTreeLeaf : public RopePieceBTreeNode {
140 /// NumPieces - This holds the number of rope pieces currently active in the
141 /// Pieces array.
142 unsigned char NumPieces;
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Chris Lattner5fd3e262008-04-14 17:54:23 +0000144 /// Pieces - This tracks the file chunks currently in this leaf.
145 ///
146 RopePiece Pieces[2*WidthFactor];
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Chris Lattner5fd3e262008-04-14 17:54:23 +0000148 /// NextLeaf - This is a pointer to the next leaf in the tree, allowing
149 /// efficient in-order forward iteration of the tree without traversal.
Chris Lattner3d2e8c72008-05-28 18:45:56 +0000150 RopePieceBTreeLeaf **PrevLeaf, *NextLeaf;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000151 public:
Chris Lattner3d2e8c72008-05-28 18:45:56 +0000152 RopePieceBTreeLeaf() : RopePieceBTreeNode(true), NumPieces(0),
153 PrevLeaf(0), NextLeaf(0) {}
154 ~RopePieceBTreeLeaf() {
155 if (PrevLeaf || NextLeaf)
156 removeFromLeafInOrder();
Ted Kremenek90556e32009-10-20 06:31:34 +0000157 clear();
Chris Lattner3d2e8c72008-05-28 18:45:56 +0000158 }
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Chris Lattner5fd3e262008-04-14 17:54:23 +0000160 bool isFull() const { return NumPieces == 2*WidthFactor; }
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Chris Lattner5fd3e262008-04-14 17:54:23 +0000162 /// clear - Remove all rope pieces from this leaf.
163 void clear() {
164 while (NumPieces)
165 Pieces[--NumPieces] = RopePiece();
166 Size = 0;
167 }
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Chris Lattner5fd3e262008-04-14 17:54:23 +0000169 unsigned getNumPieces() const { return NumPieces; }
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Chris Lattner5fd3e262008-04-14 17:54:23 +0000171 const RopePiece &getPiece(unsigned i) const {
172 assert(i < getNumPieces() && "Invalid piece ID");
173 return Pieces[i];
174 }
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Chris Lattner5fd3e262008-04-14 17:54:23 +0000176 const RopePieceBTreeLeaf *getNextLeafInOrder() const { return NextLeaf; }
Chris Lattner3d2e8c72008-05-28 18:45:56 +0000177 void insertAfterLeafInOrder(RopePieceBTreeLeaf *Node) {
178 assert(PrevLeaf == 0 && NextLeaf == 0 && "Already in ordering");
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattner3d2e8c72008-05-28 18:45:56 +0000180 NextLeaf = Node->NextLeaf;
181 if (NextLeaf)
182 NextLeaf->PrevLeaf = &NextLeaf;
183 PrevLeaf = &Node->NextLeaf;
184 Node->NextLeaf = this;
185 }
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Chris Lattner3d2e8c72008-05-28 18:45:56 +0000187 void removeFromLeafInOrder() {
188 if (PrevLeaf) {
189 *PrevLeaf = NextLeaf;
190 if (NextLeaf)
191 NextLeaf->PrevLeaf = PrevLeaf;
192 } else if (NextLeaf) {
193 NextLeaf->PrevLeaf = 0;
194 }
195 }
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Chris Lattnerb9b30942008-04-15 06:37:11 +0000197 /// FullRecomputeSizeLocally - This method recomputes the 'Size' field by
198 /// summing the size of all RopePieces.
Chris Lattner5fd3e262008-04-14 17:54:23 +0000199 void FullRecomputeSizeLocally() {
200 Size = 0;
201 for (unsigned i = 0, e = getNumPieces(); i != e; ++i)
202 Size += getPiece(i).size();
203 }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Chris Lattner5fd3e262008-04-14 17:54:23 +0000205 /// split - Split the range containing the specified offset so that we are
206 /// guaranteed that there is a place to do an insertion at the specified
Chris Lattnerbf268562008-04-14 22:10:58 +0000207 /// offset. The offset is relative, so "0" is the start of the node.
208 ///
209 /// If there is no space in this subtree for the extra piece, the extra tree
210 /// node is returned and must be inserted into a parent.
211 RopePieceBTreeNode *split(unsigned Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Chris Lattner5fd3e262008-04-14 17:54:23 +0000213 /// insert - Insert the specified ropepiece into this tree node at the
214 /// specified offset. The offset is relative, so "0" is the start of the
Chris Lattnerbf268562008-04-14 22:10:58 +0000215 /// node.
216 ///
217 /// If there is no space in this subtree for the extra piece, the extra tree
218 /// node is returned and must be inserted into a parent.
219 RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R);
Mike Stump1eb44332009-09-09 15:08:12 +0000220
221
Chris Lattner5fd3e262008-04-14 17:54:23 +0000222 /// erase - Remove NumBytes from this node at the specified offset. We are
223 /// guaranteed that there is a split at Offset.
224 void erase(unsigned Offset, unsigned NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Chris Lattner5fd3e262008-04-14 17:54:23 +0000226 static inline bool classof(const RopePieceBTreeLeaf *) { return true; }
227 static inline bool classof(const RopePieceBTreeNode *N) {
228 return N->isLeaf();
229 }
230 };
231} // end anonymous namespace
232
233/// split - Split the range containing the specified offset so that we are
234/// guaranteed that there is a place to do an insertion at the specified
Chris Lattnerbf268562008-04-14 22:10:58 +0000235/// offset. The offset is relative, so "0" is the start of the node.
236///
237/// If there is no space in this subtree for the extra piece, the extra tree
238/// node is returned and must be inserted into a parent.
239RopePieceBTreeNode *RopePieceBTreeLeaf::split(unsigned Offset) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000240 // Find the insertion point. We are guaranteed that there is a split at the
241 // specified offset so find it.
242 if (Offset == 0 || Offset == size()) {
243 // Fastpath for a common case. There is already a splitpoint at the end.
Chris Lattnerbf268562008-04-14 22:10:58 +0000244 return 0;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000245 }
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Chris Lattner5fd3e262008-04-14 17:54:23 +0000247 // Find the piece that this offset lands in.
248 unsigned PieceOffs = 0;
249 unsigned i = 0;
250 while (Offset >= PieceOffs+Pieces[i].size()) {
251 PieceOffs += Pieces[i].size();
252 ++i;
253 }
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Chris Lattner5fd3e262008-04-14 17:54:23 +0000255 // If there is already a split point at the specified offset, just return
256 // success.
257 if (PieceOffs == Offset)
Chris Lattnerbf268562008-04-14 22:10:58 +0000258 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Chris Lattner5fd3e262008-04-14 17:54:23 +0000260 // Otherwise, we need to split piece 'i' at Offset-PieceOffs. Convert Offset
261 // to being Piece relative.
262 unsigned IntraPieceOffset = Offset-PieceOffs;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Chris Lattner5fd3e262008-04-14 17:54:23 +0000264 // We do this by shrinking the RopePiece and then doing an insert of the tail.
265 RopePiece Tail(Pieces[i].StrData, Pieces[i].StartOffs+IntraPieceOffset,
266 Pieces[i].EndOffs);
267 Size -= Pieces[i].size();
268 Pieces[i].EndOffs = Pieces[i].StartOffs+IntraPieceOffset;
269 Size += Pieces[i].size();
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattnerbf268562008-04-14 22:10:58 +0000271 return insert(Offset, Tail);
Chris Lattner5fd3e262008-04-14 17:54:23 +0000272}
273
274
275/// insert - Insert the specified RopePiece into this tree node at the
Chris Lattnerbf268562008-04-14 22:10:58 +0000276/// specified offset. The offset is relative, so "0" is the start of the node.
277///
278/// If there is no space in this subtree for the extra piece, the extra tree
279/// node is returned and must be inserted into a parent.
280RopePieceBTreeNode *RopePieceBTreeLeaf::insert(unsigned Offset,
281 const RopePiece &R) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000282 // If this node is not full, insert the piece.
283 if (!isFull()) {
284 // Find the insertion point. We are guaranteed that there is a split at the
285 // specified offset so find it.
286 unsigned i = 0, e = getNumPieces();
287 if (Offset == size()) {
288 // Fastpath for a common case.
289 i = e;
290 } else {
291 unsigned SlotOffs = 0;
292 for (; Offset > SlotOffs; ++i)
293 SlotOffs += getPiece(i).size();
294 assert(SlotOffs == Offset && "Split didn't occur before insertion!");
295 }
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Chris Lattner5fd3e262008-04-14 17:54:23 +0000297 // For an insertion into a non-full leaf node, just insert the value in
298 // its sorted position. This requires moving later values over.
299 for (; i != e; --e)
300 Pieces[e] = Pieces[e-1];
301 Pieces[i] = R;
302 ++NumPieces;
303 Size += R.size();
Chris Lattnerbf268562008-04-14 22:10:58 +0000304 return 0;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000305 }
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Chris Lattner5fd3e262008-04-14 17:54:23 +0000307 // Otherwise, if this is leaf is full, split it in two halves. Since this
308 // node is full, it contains 2*WidthFactor values. We move the first
309 // 'WidthFactor' values to the LHS child (which we leave in this node) and
310 // move the last 'WidthFactor' values into the RHS child.
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Chris Lattner5fd3e262008-04-14 17:54:23 +0000312 // Create the new node.
313 RopePieceBTreeLeaf *NewNode = new RopePieceBTreeLeaf();
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Chris Lattner5fd3e262008-04-14 17:54:23 +0000315 // Move over the last 'WidthFactor' values from here to NewNode.
316 std::copy(&Pieces[WidthFactor], &Pieces[2*WidthFactor],
317 &NewNode->Pieces[0]);
318 // Replace old pieces with null RopePieces to drop refcounts.
319 std::fill(&Pieces[WidthFactor], &Pieces[2*WidthFactor], RopePiece());
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Chris Lattner5fd3e262008-04-14 17:54:23 +0000321 // Decrease the number of values in the two nodes.
322 NewNode->NumPieces = NumPieces = WidthFactor;
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattner5fd3e262008-04-14 17:54:23 +0000324 // Recompute the two nodes' size.
325 NewNode->FullRecomputeSizeLocally();
326 FullRecomputeSizeLocally();
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Chris Lattner5fd3e262008-04-14 17:54:23 +0000328 // Update the list of leaves.
Chris Lattner3d2e8c72008-05-28 18:45:56 +0000329 NewNode->insertAfterLeafInOrder(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Chris Lattnerbf268562008-04-14 22:10:58 +0000331 // These insertions can't fail.
Chris Lattner5fd3e262008-04-14 17:54:23 +0000332 if (this->size() >= Offset)
Chris Lattnerbf268562008-04-14 22:10:58 +0000333 this->insert(Offset, R);
Chris Lattner5fd3e262008-04-14 17:54:23 +0000334 else
Chris Lattnerbf268562008-04-14 22:10:58 +0000335 NewNode->insert(Offset - this->size(), R);
336 return NewNode;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000337}
338
339/// erase - Remove NumBytes from this node at the specified offset. We are
340/// guaranteed that there is a split at Offset.
341void RopePieceBTreeLeaf::erase(unsigned Offset, unsigned NumBytes) {
342 // Since we are guaranteed that there is a split at Offset, we start by
343 // finding the Piece that starts there.
344 unsigned PieceOffs = 0;
345 unsigned i = 0;
346 for (; Offset > PieceOffs; ++i)
347 PieceOffs += getPiece(i).size();
348 assert(PieceOffs == Offset && "Split didn't occur before erase!");
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Chris Lattner5fd3e262008-04-14 17:54:23 +0000350 unsigned StartPiece = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattner5fd3e262008-04-14 17:54:23 +0000352 // Figure out how many pieces completely cover 'NumBytes'. We want to remove
353 // all of them.
354 for (; Offset+NumBytes > PieceOffs+getPiece(i).size(); ++i)
355 PieceOffs += getPiece(i).size();
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Chris Lattner5fd3e262008-04-14 17:54:23 +0000357 // If we exactly include the last one, include it in the region to delete.
358 if (Offset+NumBytes == PieceOffs+getPiece(i).size())
359 PieceOffs += getPiece(i).size(), ++i;
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Chris Lattner5fd3e262008-04-14 17:54:23 +0000361 // If we completely cover some RopePieces, erase them now.
362 if (i != StartPiece) {
363 unsigned NumDeleted = i-StartPiece;
364 for (; i != getNumPieces(); ++i)
365 Pieces[i-NumDeleted] = Pieces[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Chris Lattner5fd3e262008-04-14 17:54:23 +0000367 // Drop references to dead rope pieces.
368 std::fill(&Pieces[getNumPieces()-NumDeleted], &Pieces[getNumPieces()],
369 RopePiece());
370 NumPieces -= NumDeleted;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattner5fd3e262008-04-14 17:54:23 +0000372 unsigned CoverBytes = PieceOffs-Offset;
373 NumBytes -= CoverBytes;
374 Size -= CoverBytes;
375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Chris Lattner5fd3e262008-04-14 17:54:23 +0000377 // If we completely removed some stuff, we could be done.
378 if (NumBytes == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattner5fd3e262008-04-14 17:54:23 +0000380 // Okay, now might be erasing part of some Piece. If this is the case, then
381 // move the start point of the piece.
382 assert(getPiece(StartPiece).size() > NumBytes);
383 Pieces[StartPiece].StartOffs += NumBytes;
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Chris Lattner5fd3e262008-04-14 17:54:23 +0000385 // The size of this node just shrunk by NumBytes.
386 Size -= NumBytes;
387}
388
389//===----------------------------------------------------------------------===//
390// RopePieceBTreeInterior Class
391//===----------------------------------------------------------------------===//
392
393namespace {
Chris Lattnerb9b30942008-04-15 06:37:11 +0000394 /// RopePieceBTreeInterior - This represents an interior node in the B+Tree,
395 /// which holds up to 2*WidthFactor pointers to child nodes.
Chris Lattner5fd3e262008-04-14 17:54:23 +0000396 class RopePieceBTreeInterior : public RopePieceBTreeNode {
397 /// NumChildren - This holds the number of children currently active in the
398 /// Children array.
399 unsigned char NumChildren;
400 RopePieceBTreeNode *Children[2*WidthFactor];
401 public:
Chris Lattner70778c82008-04-14 20:07:03 +0000402 RopePieceBTreeInterior() : RopePieceBTreeNode(false), NumChildren(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattner5fd3e262008-04-14 17:54:23 +0000404 RopePieceBTreeInterior(RopePieceBTreeNode *LHS, RopePieceBTreeNode *RHS)
405 : RopePieceBTreeNode(false) {
406 Children[0] = LHS;
407 Children[1] = RHS;
408 NumChildren = 2;
409 Size = LHS->size() + RHS->size();
410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Chris Lattner5fd3e262008-04-14 17:54:23 +0000412 bool isFull() const { return NumChildren == 2*WidthFactor; }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Chris Lattner5fd3e262008-04-14 17:54:23 +0000414 unsigned getNumChildren() const { return NumChildren; }
415 const RopePieceBTreeNode *getChild(unsigned i) const {
416 assert(i < NumChildren && "invalid child #");
417 return Children[i];
418 }
419 RopePieceBTreeNode *getChild(unsigned i) {
420 assert(i < NumChildren && "invalid child #");
421 return Children[i];
422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattnerb9b30942008-04-15 06:37:11 +0000424 /// FullRecomputeSizeLocally - Recompute the Size field of this node by
425 /// summing up the sizes of the child nodes.
Chris Lattner5fd3e262008-04-14 17:54:23 +0000426 void FullRecomputeSizeLocally() {
427 Size = 0;
428 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
429 Size += getChild(i)->size();
430 }
Mike Stump1eb44332009-09-09 15:08:12 +0000431
432
Chris Lattner5fd3e262008-04-14 17:54:23 +0000433 /// split - Split the range containing the specified offset so that we are
434 /// guaranteed that there is a place to do an insertion at the specified
Chris Lattnerbf268562008-04-14 22:10:58 +0000435 /// offset. The offset is relative, so "0" is the start of the node.
436 ///
437 /// If there is no space in this subtree for the extra piece, the extra tree
438 /// node is returned and must be inserted into a parent.
439 RopePieceBTreeNode *split(unsigned Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000440
441
Chris Lattner5fd3e262008-04-14 17:54:23 +0000442 /// insert - Insert the specified ropepiece into this tree node at the
443 /// specified offset. The offset is relative, so "0" is the start of the
Chris Lattnerbf268562008-04-14 22:10:58 +0000444 /// node.
445 ///
446 /// If there is no space in this subtree for the extra piece, the extra tree
447 /// node is returned and must be inserted into a parent.
448 RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R);
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Chris Lattner5fd3e262008-04-14 17:54:23 +0000450 /// HandleChildPiece - A child propagated an insertion result up to us.
451 /// Insert the new child, and/or propagate the result further up the tree.
Chris Lattnerbf268562008-04-14 22:10:58 +0000452 RopePieceBTreeNode *HandleChildPiece(unsigned i, RopePieceBTreeNode *RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Chris Lattner5fd3e262008-04-14 17:54:23 +0000454 /// erase - Remove NumBytes from this node at the specified offset. We are
455 /// guaranteed that there is a split at Offset.
456 void erase(unsigned Offset, unsigned NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Chris Lattner5fd3e262008-04-14 17:54:23 +0000458 static inline bool classof(const RopePieceBTreeInterior *) { return true; }
459 static inline bool classof(const RopePieceBTreeNode *N) {
Mike Stump1eb44332009-09-09 15:08:12 +0000460 return !N->isLeaf();
Chris Lattner5fd3e262008-04-14 17:54:23 +0000461 }
462 };
463} // end anonymous namespace
464
465/// split - Split the range containing the specified offset so that we are
466/// guaranteed that there is a place to do an insertion at the specified
Chris Lattnerbf268562008-04-14 22:10:58 +0000467/// offset. The offset is relative, so "0" is the start of the node.
468///
469/// If there is no space in this subtree for the extra piece, the extra tree
470/// node is returned and must be inserted into a parent.
471RopePieceBTreeNode *RopePieceBTreeInterior::split(unsigned Offset) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000472 // Figure out which child to split.
473 if (Offset == 0 || Offset == size())
Chris Lattnerbf268562008-04-14 22:10:58 +0000474 return 0; // If we have an exact offset, we're already split.
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Chris Lattner5fd3e262008-04-14 17:54:23 +0000476 unsigned ChildOffset = 0;
477 unsigned i = 0;
478 for (; Offset >= ChildOffset+getChild(i)->size(); ++i)
479 ChildOffset += getChild(i)->size();
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattner5fd3e262008-04-14 17:54:23 +0000481 // If already split there, we're done.
482 if (ChildOffset == Offset)
Chris Lattnerbf268562008-04-14 22:10:58 +0000483 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Chris Lattner5fd3e262008-04-14 17:54:23 +0000485 // Otherwise, recursively split the child.
Mike Stump1eb44332009-09-09 15:08:12 +0000486 if (RopePieceBTreeNode *RHS = getChild(i)->split(Offset-ChildOffset))
Chris Lattnerbf268562008-04-14 22:10:58 +0000487 return HandleChildPiece(i, RHS);
488 return 0; // Done!
Chris Lattner5fd3e262008-04-14 17:54:23 +0000489}
490
491/// insert - Insert the specified ropepiece into this tree node at the
492/// specified offset. The offset is relative, so "0" is the start of the
Chris Lattnerbf268562008-04-14 22:10:58 +0000493/// node.
494///
495/// If there is no space in this subtree for the extra piece, the extra tree
496/// node is returned and must be inserted into a parent.
497RopePieceBTreeNode *RopePieceBTreeInterior::insert(unsigned Offset,
498 const RopePiece &R) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000499 // Find the insertion point. We are guaranteed that there is a split at the
500 // specified offset so find it.
501 unsigned i = 0, e = getNumChildren();
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Chris Lattner5fd3e262008-04-14 17:54:23 +0000503 unsigned ChildOffs = 0;
504 if (Offset == size()) {
505 // Fastpath for a common case. Insert at end of last child.
506 i = e-1;
507 ChildOffs = size()-getChild(i)->size();
508 } else {
509 for (; Offset > ChildOffs+getChild(i)->size(); ++i)
510 ChildOffs += getChild(i)->size();
511 }
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Chris Lattner5fd3e262008-04-14 17:54:23 +0000513 Size += R.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Chris Lattner5fd3e262008-04-14 17:54:23 +0000515 // Insert at the end of this child.
Chris Lattnerbf268562008-04-14 22:10:58 +0000516 if (RopePieceBTreeNode *RHS = getChild(i)->insert(Offset-ChildOffs, R))
517 return HandleChildPiece(i, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Chris Lattnerbf268562008-04-14 22:10:58 +0000519 return 0;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000520}
521
522/// HandleChildPiece - A child propagated an insertion result up to us.
523/// Insert the new child, and/or propagate the result further up the tree.
Chris Lattnerbf268562008-04-14 22:10:58 +0000524RopePieceBTreeNode *
525RopePieceBTreeInterior::HandleChildPiece(unsigned i, RopePieceBTreeNode *RHS) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000526 // Otherwise the child propagated a subtree up to us as a new child. See if
527 // we have space for it here.
528 if (!isFull()) {
Chris Lattnerbf268562008-04-14 22:10:58 +0000529 // Insert RHS after child 'i'.
Chris Lattner5fd3e262008-04-14 17:54:23 +0000530 if (i + 1 != getNumChildren())
531 memmove(&Children[i+2], &Children[i+1],
532 (getNumChildren()-i-1)*sizeof(Children[0]));
Chris Lattnerbf268562008-04-14 22:10:58 +0000533 Children[i+1] = RHS;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000534 ++NumChildren;
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +0000535 return 0;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000536 }
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Chris Lattner5fd3e262008-04-14 17:54:23 +0000538 // Okay, this node is full. Split it in half, moving WidthFactor children to
539 // a newly allocated interior node.
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Chris Lattner5fd3e262008-04-14 17:54:23 +0000541 // Create the new node.
542 RopePieceBTreeInterior *NewNode = new RopePieceBTreeInterior();
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Chris Lattner5fd3e262008-04-14 17:54:23 +0000544 // Move over the last 'WidthFactor' values from here to NewNode.
545 memcpy(&NewNode->Children[0], &Children[WidthFactor],
546 WidthFactor*sizeof(Children[0]));
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattner5fd3e262008-04-14 17:54:23 +0000548 // Decrease the number of values in the two nodes.
549 NewNode->NumChildren = NumChildren = WidthFactor;
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chris Lattner5fd3e262008-04-14 17:54:23 +0000551 // Finally, insert the two new children in the side the can (now) hold them.
Chris Lattnerbf268562008-04-14 22:10:58 +0000552 // These insertions can't fail.
Chris Lattner5fd3e262008-04-14 17:54:23 +0000553 if (i < WidthFactor)
Chris Lattnerbf268562008-04-14 22:10:58 +0000554 this->HandleChildPiece(i, RHS);
Chris Lattner5fd3e262008-04-14 17:54:23 +0000555 else
Chris Lattnerbf268562008-04-14 22:10:58 +0000556 NewNode->HandleChildPiece(i-WidthFactor, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattner5fd3e262008-04-14 17:54:23 +0000558 // Recompute the two nodes' size.
559 NewNode->FullRecomputeSizeLocally();
560 FullRecomputeSizeLocally();
Chris Lattnerbf268562008-04-14 22:10:58 +0000561 return NewNode;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000562}
563
564/// erase - Remove NumBytes from this node at the specified offset. We are
565/// guaranteed that there is a split at Offset.
566void RopePieceBTreeInterior::erase(unsigned Offset, unsigned NumBytes) {
567 // This will shrink this node by NumBytes.
568 Size -= NumBytes;
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Chris Lattner5fd3e262008-04-14 17:54:23 +0000570 // Find the first child that overlaps with Offset.
571 unsigned i = 0;
572 for (; Offset >= getChild(i)->size(); ++i)
573 Offset -= getChild(i)->size();
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Chris Lattner5fd3e262008-04-14 17:54:23 +0000575 // Propagate the delete request into overlapping children, or completely
576 // delete the children as appropriate.
577 while (NumBytes) {
578 RopePieceBTreeNode *CurChild = getChild(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Chris Lattner5fd3e262008-04-14 17:54:23 +0000580 // If we are deleting something contained entirely in the child, pass on the
581 // request.
582 if (Offset+NumBytes < CurChild->size()) {
583 CurChild->erase(Offset, NumBytes);
584 return;
585 }
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Chris Lattner5fd3e262008-04-14 17:54:23 +0000587 // If this deletion request starts somewhere in the middle of the child, it
588 // must be deleting to the end of the child.
589 if (Offset) {
590 unsigned BytesFromChild = CurChild->size()-Offset;
591 CurChild->erase(Offset, BytesFromChild);
592 NumBytes -= BytesFromChild;
Chris Lattnerb6403af2008-05-08 03:23:46 +0000593 // Start at the beginning of the next child.
594 Offset = 0;
Chris Lattner5fd3e262008-04-14 17:54:23 +0000595 ++i;
596 continue;
597 }
Chris Lattnerb6403af2008-05-08 03:23:46 +0000598
Chris Lattner5fd3e262008-04-14 17:54:23 +0000599 // If the deletion request completely covers the child, delete it and move
600 // the rest down.
601 NumBytes -= CurChild->size();
602 CurChild->Destroy();
603 --NumChildren;
Chris Lattner514b24c2008-05-23 23:29:33 +0000604 if (i != getNumChildren())
Chris Lattner5fd3e262008-04-14 17:54:23 +0000605 memmove(&Children[i], &Children[i+1],
606 (getNumChildren()-i)*sizeof(Children[0]));
607 }
608}
609
610//===----------------------------------------------------------------------===//
611// RopePieceBTreeNode Implementation
612//===----------------------------------------------------------------------===//
613
614void RopePieceBTreeNode::Destroy() {
615 if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
616 delete Leaf;
617 else
618 delete cast<RopePieceBTreeInterior>(this);
619}
620
621/// split - Split the range containing the specified offset so that we are
622/// guaranteed that there is a place to do an insertion at the specified
Chris Lattnerbf268562008-04-14 22:10:58 +0000623/// offset. The offset is relative, so "0" is the start of the node.
624///
625/// If there is no space in this subtree for the extra piece, the extra tree
626/// node is returned and must be inserted into a parent.
627RopePieceBTreeNode *RopePieceBTreeNode::split(unsigned Offset) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000628 assert(Offset <= size() && "Invalid offset to split!");
629 if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
Chris Lattnerbf268562008-04-14 22:10:58 +0000630 return Leaf->split(Offset);
631 return cast<RopePieceBTreeInterior>(this)->split(Offset);
Chris Lattner5fd3e262008-04-14 17:54:23 +0000632}
633
634/// insert - Insert the specified ropepiece into this tree node at the
635/// specified offset. The offset is relative, so "0" is the start of the
636/// node.
Chris Lattnerbf268562008-04-14 22:10:58 +0000637///
638/// If there is no space in this subtree for the extra piece, the extra tree
639/// node is returned and must be inserted into a parent.
640RopePieceBTreeNode *RopePieceBTreeNode::insert(unsigned Offset,
641 const RopePiece &R) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000642 assert(Offset <= size() && "Invalid offset to insert!");
643 if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
Chris Lattnerbf268562008-04-14 22:10:58 +0000644 return Leaf->insert(Offset, R);
645 return cast<RopePieceBTreeInterior>(this)->insert(Offset, R);
Chris Lattner5fd3e262008-04-14 17:54:23 +0000646}
647
648/// erase - Remove NumBytes from this node at the specified offset. We are
649/// guaranteed that there is a split at Offset.
650void RopePieceBTreeNode::erase(unsigned Offset, unsigned NumBytes) {
651 assert(Offset+NumBytes <= size() && "Invalid offset to erase!");
652 if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
653 return Leaf->erase(Offset, NumBytes);
654 return cast<RopePieceBTreeInterior>(this)->erase(Offset, NumBytes);
655}
656
657
658//===----------------------------------------------------------------------===//
659// RopePieceBTreeIterator Implementation
660//===----------------------------------------------------------------------===//
661
662static const RopePieceBTreeLeaf *getCN(const void *P) {
663 return static_cast<const RopePieceBTreeLeaf*>(P);
664}
665
666// begin iterator.
667RopePieceBTreeIterator::RopePieceBTreeIterator(const void *n) {
668 const RopePieceBTreeNode *N = static_cast<const RopePieceBTreeNode*>(n);
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Chris Lattner5fd3e262008-04-14 17:54:23 +0000670 // Walk down the left side of the tree until we get to a leaf.
671 while (const RopePieceBTreeInterior *IN = dyn_cast<RopePieceBTreeInterior>(N))
672 N = IN->getChild(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Chris Lattner5fd3e262008-04-14 17:54:23 +0000674 // We must have at least one leaf.
675 CurNode = cast<RopePieceBTreeLeaf>(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Chris Lattner5fd3e262008-04-14 17:54:23 +0000677 // If we found a leaf that happens to be empty, skip over it until we get
678 // to something full.
679 while (CurNode && getCN(CurNode)->getNumPieces() == 0)
680 CurNode = getCN(CurNode)->getNextLeafInOrder();
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Chris Lattner5fd3e262008-04-14 17:54:23 +0000682 if (CurNode != 0)
683 CurPiece = &getCN(CurNode)->getPiece(0);
684 else // Empty tree, this is an end() iterator.
685 CurPiece = 0;
686 CurChar = 0;
687}
688
689void RopePieceBTreeIterator::MoveToNextPiece() {
690 if (CurPiece != &getCN(CurNode)->getPiece(getCN(CurNode)->getNumPieces()-1)) {
691 CurChar = 0;
692 ++CurPiece;
693 return;
694 }
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Chris Lattner5fd3e262008-04-14 17:54:23 +0000696 // Find the next non-empty leaf node.
697 do
698 CurNode = getCN(CurNode)->getNextLeafInOrder();
699 while (CurNode && getCN(CurNode)->getNumPieces() == 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Chris Lattner5fd3e262008-04-14 17:54:23 +0000701 if (CurNode != 0)
702 CurPiece = &getCN(CurNode)->getPiece(0);
703 else // Hit end().
704 CurPiece = 0;
705 CurChar = 0;
706}
707
708//===----------------------------------------------------------------------===//
709// RopePieceBTree Implementation
710//===----------------------------------------------------------------------===//
711
712static RopePieceBTreeNode *getRoot(void *P) {
713 return static_cast<RopePieceBTreeNode*>(P);
714}
715
716RopePieceBTree::RopePieceBTree() {
717 Root = new RopePieceBTreeLeaf();
718}
719RopePieceBTree::RopePieceBTree(const RopePieceBTree &RHS) {
720 assert(RHS.empty() && "Can't copy non-empty tree yet");
721 Root = new RopePieceBTreeLeaf();
722}
723RopePieceBTree::~RopePieceBTree() {
724 getRoot(Root)->Destroy();
725}
726
727unsigned RopePieceBTree::size() const {
728 return getRoot(Root)->size();
729}
730
731void RopePieceBTree::clear() {
732 if (RopePieceBTreeLeaf *Leaf = dyn_cast<RopePieceBTreeLeaf>(getRoot(Root)))
733 Leaf->clear();
734 else {
735 getRoot(Root)->Destroy();
736 Root = new RopePieceBTreeLeaf();
737 }
738}
739
740void RopePieceBTree::insert(unsigned Offset, const RopePiece &R) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000741 // #1. Split at Offset.
Chris Lattnerbf268562008-04-14 22:10:58 +0000742 if (RopePieceBTreeNode *RHS = getRoot(Root)->split(Offset))
743 Root = new RopePieceBTreeInterior(getRoot(Root), RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Chris Lattner5fd3e262008-04-14 17:54:23 +0000745 // #2. Do the insertion.
Chris Lattnerbf268562008-04-14 22:10:58 +0000746 if (RopePieceBTreeNode *RHS = getRoot(Root)->insert(Offset, R))
747 Root = new RopePieceBTreeInterior(getRoot(Root), RHS);
Chris Lattner5fd3e262008-04-14 17:54:23 +0000748}
749
750void RopePieceBTree::erase(unsigned Offset, unsigned NumBytes) {
Chris Lattner5fd3e262008-04-14 17:54:23 +0000751 // #1. Split at Offset.
Chris Lattnerbf268562008-04-14 22:10:58 +0000752 if (RopePieceBTreeNode *RHS = getRoot(Root)->split(Offset))
753 Root = new RopePieceBTreeInterior(getRoot(Root), RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Chris Lattner5fd3e262008-04-14 17:54:23 +0000755 // #2. Do the erasing.
756 getRoot(Root)->erase(Offset, NumBytes);
757}
Chris Lattner5618d882008-04-14 21:41:00 +0000758
759//===----------------------------------------------------------------------===//
760// RewriteRope Implementation
761//===----------------------------------------------------------------------===//
762
Chris Lattnerb9b30942008-04-15 06:37:11 +0000763/// MakeRopeString - This copies the specified byte range into some instance of
764/// RopeRefCountString, and return a RopePiece that represents it. This uses
765/// the AllocBuffer object to aggregate requests for small strings into one
766/// allocation instead of doing tons of tiny allocations.
Chris Lattner5618d882008-04-14 21:41:00 +0000767RopePiece RewriteRope::MakeRopeString(const char *Start, const char *End) {
768 unsigned Len = End-Start;
Chris Lattnerc66d0aa2008-04-23 03:21:50 +0000769 assert(Len && "Zero length RopePiece is invalid!");
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Chris Lattner5618d882008-04-14 21:41:00 +0000771 // If we have space for this string in the current alloc buffer, use it.
772 if (AllocOffs+Len <= AllocChunkSize) {
773 memcpy(AllocBuffer->Data+AllocOffs, Start, Len);
774 AllocOffs += Len;
775 return RopePiece(AllocBuffer, AllocOffs-Len, AllocOffs);
776 }
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Chris Lattner5618d882008-04-14 21:41:00 +0000778 // If we don't have enough room because this specific allocation is huge,
779 // just allocate a new rope piece for it alone.
780 if (Len > AllocChunkSize) {
781 unsigned Size = End-Start+sizeof(RopeRefCountString)-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000782 RopeRefCountString *Res =
Chris Lattnerbf268562008-04-14 22:10:58 +0000783 reinterpret_cast<RopeRefCountString *>(new char[Size]);
Chris Lattner5618d882008-04-14 21:41:00 +0000784 Res->RefCount = 0;
785 memcpy(Res->Data, Start, End-Start);
786 return RopePiece(Res, 0, End-Start);
787 }
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Chris Lattner5618d882008-04-14 21:41:00 +0000789 // Otherwise, this was a small request but we just don't have space for it
790 // Make a new chunk and share it with later allocations.
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Chris Lattner5618d882008-04-14 21:41:00 +0000792 // If we had an old allocation, drop our reference to it.
793 if (AllocBuffer && --AllocBuffer->RefCount == 0)
794 delete [] (char*)AllocBuffer;
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Zhongxing Xu3f61c182008-09-16 07:58:21 +0000796 unsigned AllocSize = offsetof(RopeRefCountString, Data) + AllocChunkSize;
Chris Lattner5618d882008-04-14 21:41:00 +0000797 AllocBuffer = reinterpret_cast<RopeRefCountString *>(new char[AllocSize]);
798 AllocBuffer->RefCount = 0;
799 memcpy(AllocBuffer->Data, Start, Len);
800 AllocOffs = Len;
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Ted Kremenek8f999932009-10-20 05:53:05 +0000802 // Start out the new allocation with a refcount of 1, since we have an
803 // internal reference to it.
804 AllocBuffer->addRef();
Chris Lattner5618d882008-04-14 21:41:00 +0000805 return RopePiece(AllocBuffer, 0, Len);
806}
807
808