1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sw=4 et tw=99 ft=cpp:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
18 : * June 12, 2009.
19 : *
20 : * The Initial Developer of the Original Code is
21 : * the Mozilla Corporation.
22 : *
23 : * Contributor(s):
24 : * Luke Wagner <lw@mozilla.com>
25 : * Nicholas Nethercote <nnethercote@mozilla.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either of the GNU General Public License Version 2 or later (the "GPL"),
29 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #ifndef jsvector_h_
42 : #define jsvector_h_
43 :
44 : #include "mozilla/Attributes.h"
45 :
46 : #include "TemplateLib.h"
47 : #include "Utility.h"
48 :
49 : /* Silence dire "bugs in previous versions of MSVC have been fixed" warnings */
50 : #ifdef _MSC_VER
51 : #pragma warning(push)
52 : #pragma warning(disable:4345)
53 : #endif
54 :
55 : namespace js {
56 :
57 : class TempAllocPolicy;
58 :
59 : template <class T,
60 : size_t MinInlineCapacity = 0,
61 : class AllocPolicy = TempAllocPolicy>
62 : class Vector;
63 :
64 : /*
65 : * This template class provides a default implementation for vector operations
66 : * when the element type is not known to be a POD, as judged by IsPodType.
67 : */
68 : template <class T, size_t N, class AP, bool IsPod>
69 : struct VectorImpl
70 : {
71 : /* Destroys constructed objects in the range [begin, end). */
72 47243346 : static inline void destroy(T *begin, T *end) {
73 418999073 : for (T *p = begin; p != end; ++p)
74 371755727 : p->~T();
75 47243346 : }
76 :
77 : /* Constructs objects in the uninitialized range [begin, end). */
78 5786855 : static inline void initialize(T *begin, T *end) {
79 11583479 : for (T *p = begin; p != end; ++p)
80 5796624 : new(p) T();
81 5786855 : }
82 :
83 : /*
84 : * Copy-constructs objects in the uninitialized range
85 : * [dst, dst+(srcend-srcbeg)) from the range [srcbeg, srcend).
86 : */
87 : template <class U>
88 870158 : static inline void copyConstruct(T *dst, const U *srcbeg, const U *srcend) {
89 969996 : for (const U *p = srcbeg; p != srcend; ++p, ++dst)
90 99838 : new(dst) T(*p);
91 870158 : }
92 :
93 : /*
94 : * Move-constructs objects in the uninitialized range
95 : * [dst, dst+(srcend-srcbeg)) from the range [srcbeg, srcend).
96 : */
97 : template <class U>
98 7859620 : static inline void moveConstruct(T *dst, const U *srcbeg, const U *srcend) {
99 13994746 : for (const U *p = srcbeg; p != srcend; ++p, ++dst)
100 6135126 : new(dst) T(Move(*p));
101 7859620 : }
102 :
103 : /*
104 : * Copy-constructs objects in the uninitialized range [dst, dst+n) from the
105 : * same object u.
106 : */
107 : template <class U>
108 0 : static inline void copyConstructN(T *dst, size_t n, const U &u) {
109 0 : for (T *end = dst + n; dst != end; ++dst)
110 0 : new(dst) T(u);
111 0 : }
112 :
113 : /*
114 : * Grows the given buffer to have capacity newcap, preserving the objects
115 : * constructed in the range [begin, end) and updating v. Assumes that (1)
116 : * newcap has not overflowed, and (2) multiplying newcap by sizeof(T) will
117 : * not overflow.
118 : */
119 1673131 : static inline bool growTo(Vector<T,N,AP> &v, size_t newcap) {
120 1673131 : JS_ASSERT(!v.usingInlineStorage());
121 1673131 : T *newbuf = reinterpret_cast<T *>(v.malloc_(newcap * sizeof(T)));
122 1673131 : if (!newbuf)
123 0 : return false;
124 223280516 : for (T *dst = newbuf, *src = v.beginNoCheck(); src != v.endNoCheck(); ++dst, ++src)
125 221607385 : new(dst) T(Move(*src));
126 1673131 : VectorImpl::destroy(v.beginNoCheck(), v.endNoCheck());
127 1673131 : v.free_(v.mBegin);
128 1673131 : v.mBegin = newbuf;
129 : /* v.mLength is unchanged. */
130 1673131 : v.mCapacity = newcap;
131 1673131 : return true;
132 : }
133 : };
134 :
135 : /*
136 : * This partial template specialization provides a default implementation for
137 : * vector operations when the element type is known to be a POD, as judged by
138 : * IsPodType.
139 : */
140 : template <class T, size_t N, class AP>
141 : struct VectorImpl<T, N, AP, true>
142 : {
143 17767501 : static inline void destroy(T *, T *) {}
144 :
145 13653 : static inline void initialize(T *begin, T *end) {
146 : /*
147 : * You would think that memset would be a big win (or even break even)
148 : * when we know T is a POD. But currently it's not. This is probably
149 : * because |append| tends to be given small ranges and memset requires
150 : * a function call that doesn't get inlined.
151 : *
152 : * memset(begin, 0, sizeof(T) * (end-begin));
153 : */
154 8942517 : for (T *p = begin; p != end; ++p)
155 8928864 : new(p) T();
156 13653 : }
157 :
158 : template <class U>
159 6145815 : static inline void copyConstruct(T *dst, const U *srcbeg, const U *srcend) {
160 : /*
161 : * See above memset comment. Also, notice that copyConstruct is
162 : * currently templated (T != U), so memcpy won't work without
163 : * requiring T == U.
164 : *
165 : * memcpy(dst, srcbeg, sizeof(T) * (srcend - srcbeg));
166 : */
167 870140798 : for (const U *p = srcbeg; p != srcend; ++p, ++dst)
168 863994983 : *dst = *p;
169 6145815 : }
170 :
171 : template <class U>
172 977392 : static inline void moveConstruct(T *dst, const U *srcbeg, const U *srcend) {
173 977392 : copyConstruct(dst, srcbeg, srcend);
174 977392 : }
175 :
176 216 : static inline void copyConstructN(T *dst, size_t n, const T &t) {
177 414 : for (T *p = dst, *end = dst + n; p != end; ++p)
178 198 : *p = t;
179 216 : }
180 :
181 1071388 : static inline bool growTo(Vector<T,N,AP> &v, size_t newcap) {
182 1071388 : JS_ASSERT(!v.usingInlineStorage());
183 1071388 : size_t bytes = sizeof(T) * newcap;
184 1071388 : size_t oldBytes = sizeof(T) * v.mCapacity;
185 1071388 : T *newbuf = reinterpret_cast<T *>(v.realloc_(v.mBegin, oldBytes, bytes));
186 1071388 : if (!newbuf)
187 0 : return false;
188 1071388 : v.mBegin = newbuf;
189 : /* v.mLength is unchanged. */
190 1071388 : v.mCapacity = newcap;
191 1071388 : return true;
192 : }
193 : };
194 :
195 : /*
196 : * JS-friendly, STL-like container providing a short-lived, dynamic buffer.
197 : * Vector calls the constructors/destructors of all elements stored in
198 : * its internal buffer, so non-PODs may be safely used. Additionally,
199 : * Vector will store the first N elements in-place before resorting to
200 : * dynamic allocation.
201 : *
202 : * T requirements:
203 : * - default and copy constructible, assignable, destructible
204 : * - operations do not throw
205 : * N requirements:
206 : * - any value, however, N is clamped to min/max values
207 : * AllocPolicy:
208 : * - see "Allocation policies" in jsalloc.h (default js::TempAllocPolicy)
209 : *
210 : * N.B: Vector is not reentrant: T member functions called during Vector member
211 : * functions must not call back into the same object.
212 : */
213 : template <class T, size_t N, class AllocPolicy>
214 : class Vector : private AllocPolicy
215 : {
216 : /* utilities */
217 :
218 : static const bool sElemIsPod = tl::IsPodType<T>::result;
219 : typedef VectorImpl<T, N, AllocPolicy, sElemIsPod> Impl;
220 : friend struct VectorImpl<T, N, AllocPolicy, sElemIsPod>;
221 :
222 : bool calculateNewCapacity(size_t curLength, size_t lengthInc, size_t &newCap);
223 : bool growStorageBy(size_t lengthInc);
224 : bool growHeapStorageBy(size_t lengthInc);
225 : bool convertToHeapStorage(size_t lengthInc);
226 :
227 : template <bool InitNewElems> inline bool growByImpl(size_t inc);
228 :
229 : /* magic constants */
230 :
231 : static const int sMaxInlineBytes = 1024;
232 :
233 : /* compute constants */
234 :
235 : /*
236 : * Consider element size to be 1 for buffer sizing if there are
237 : * 0 inline elements. This allows us to compile when the definition
238 : * of the element type is not visible here.
239 : *
240 : * Explicit specialization is only allowed at namespace scope, so
241 : * in order to keep everything here, we use a dummy template
242 : * parameter with partial specialization.
243 : */
244 : template <int M, int Dummy>
245 : struct ElemSize {
246 : static const size_t result = sizeof(T);
247 : };
248 : template <int Dummy>
249 : struct ElemSize<0, Dummy> {
250 : static const size_t result = 1;
251 : };
252 :
253 : static const size_t sInlineCapacity =
254 : tl::Min<N, sMaxInlineBytes / ElemSize<N, 0>::result>::result;
255 :
256 : /* Calculate inline buffer size; avoid 0-sized array. */
257 : static const size_t sInlineBytes =
258 : tl::Max<1, sInlineCapacity * ElemSize<N, 0>::result>::result;
259 :
260 : /* member data */
261 :
262 : /*
263 : * Pointer to the buffer, be it inline or heap-allocated. Only [mBegin,
264 : * mBegin + mLength) hold valid constructed T objects. The range [mBegin +
265 : * mLength, mBegin + mCapacity) holds uninitialized memory. The range
266 : * [mBegin + mLength, mBegin + mReserved) also holds uninitialized memory
267 : * previously allocated by a call to reserve().
268 : */
269 : T *mBegin;
270 : size_t mLength; /* Number of elements in the Vector. */
271 : size_t mCapacity; /* Max number of elements storable in the Vector without resizing. */
272 : #ifdef DEBUG
273 : size_t mReserved; /* Max elements of reserved or used space in this vector. */
274 : #endif
275 :
276 : AlignedStorage<sInlineBytes> storage;
277 :
278 : #ifdef DEBUG
279 : friend class ReentrancyGuard;
280 : bool entered;
281 : #endif
282 :
283 : Vector(const Vector &) MOZ_DELETE;
284 : Vector &operator=(const Vector &) MOZ_DELETE;
285 :
286 : /* private accessors */
287 :
288 410803011 : bool usingInlineStorage() const {
289 410803011 : return mBegin == (T *)storage.addr();
290 : }
291 :
292 78779073 : T *beginNoCheck() const {
293 78779073 : return mBegin;
294 : }
295 :
296 587072619 : T *endNoCheck() {
297 587072619 : return mBegin + mLength;
298 : }
299 :
300 : const T *endNoCheck() const {
301 : return mBegin + mLength;
302 : }
303 :
304 : #ifdef DEBUG
305 673899170 : size_t reserved() const {
306 673899170 : JS_ASSERT(mReserved <= mCapacity);
307 673899170 : JS_ASSERT(mLength <= mReserved);
308 673899170 : return mReserved;
309 : }
310 : #endif
311 :
312 : /* Append operations guaranteed to succeed due to pre-reserved space. */
313 : template <class U> void internalAppend(U t);
314 : void internalAppendN(const T &t, size_t n);
315 : template <class U> void internalAppend(const U *begin, size_t length);
316 : template <class U, size_t O, class BP> void internalAppend(const Vector<U,O,BP> &other);
317 :
318 : public:
319 : static const size_t sMaxInlineStorage = N;
320 :
321 : typedef T ElementType;
322 :
323 : Vector(AllocPolicy = AllocPolicy());
324 : Vector(MoveRef<Vector>); /* Move constructor. */
325 : Vector &operator=(MoveRef<Vector>); /* Move assignment. */
326 : ~Vector();
327 :
328 : /* accessors */
329 :
330 1205824 : const AllocPolicy &allocPolicy() const {
331 1205824 : return *this;
332 : }
333 :
334 8416 : AllocPolicy &allocPolicy() {
335 8416 : return *this;
336 : }
337 :
338 : enum { InlineLength = N };
339 :
340 232956108 : size_t length() const {
341 232956108 : return mLength;
342 : }
343 :
344 130272553 : bool empty() const {
345 130272553 : return mLength == 0;
346 : }
347 :
348 155735 : size_t capacity() const {
349 155735 : return mCapacity;
350 : }
351 :
352 278197208 : T *begin() {
353 278197208 : JS_ASSERT(!entered);
354 278197208 : return mBegin;
355 : }
356 :
357 67692812 : const T *begin() const {
358 67692812 : JS_ASSERT(!entered);
359 67692812 : return mBegin;
360 : }
361 :
362 73334017 : T *end() {
363 73334017 : JS_ASSERT(!entered);
364 73334017 : return mBegin + mLength;
365 : }
366 :
367 877225 : const T *end() const {
368 877225 : JS_ASSERT(!entered);
369 877225 : return mBegin + mLength;
370 : }
371 :
372 266763384 : T &operator[](size_t i) {
373 266763384 : JS_ASSERT(!entered && i < mLength);
374 266763384 : return begin()[i];
375 : }
376 :
377 66735798 : const T &operator[](size_t i) const {
378 66735798 : JS_ASSERT(!entered && i < mLength);
379 66735798 : return begin()[i];
380 : }
381 :
382 47531985 : T &back() {
383 47531985 : JS_ASSERT(!entered && !empty());
384 47531985 : return *(end() - 1);
385 : }
386 :
387 102227 : const T &back() const {
388 102227 : JS_ASSERT(!entered && !empty());
389 102227 : return *(end() - 1);
390 : }
391 :
392 : class Range {
393 : friend class Vector;
394 : T *cur, *end;
395 1908 : Range(T *cur, T *end) : cur(cur), end(end) {}
396 : public:
397 : Range() {}
398 4941 : bool empty() const { return cur == end; }
399 3816 : size_t remain() const { return end - cur; }
400 : T &front() const { return *cur; }
401 459 : void popFront() { JS_ASSERT(!empty()); ++cur; }
402 1494 : T popCopyFront() { JS_ASSERT(!empty()); return *cur++; }
403 : };
404 :
405 1908 : Range all() {
406 1908 : return Range(begin(), end());
407 : }
408 :
409 : /* mutators */
410 :
411 : /* If reserve(length() + N) succeeds, the N next appends are guaranteed to succeed. */
412 : bool reserve(size_t capacity);
413 :
414 : /*
415 : * Destroy elements in the range [end() - incr, end()). Does not deallocate
416 : * or unreserve storage for those elements.
417 : */
418 : void shrinkBy(size_t incr);
419 :
420 : /* Grow the vector by incr elements. */
421 : bool growBy(size_t incr);
422 :
423 : /* Call shrinkBy or growBy based on whether newSize > length(). */
424 : bool resize(size_t newLength);
425 :
426 : /* Leave new elements as uninitialized memory. */
427 : bool growByUninitialized(size_t incr);
428 : bool resizeUninitialized(size_t newLength);
429 :
430 : /* Shorthand for shrinkBy(length()). */
431 : void clear();
432 :
433 : /* Clears and releases any heap-allocated storage. */
434 : void clearAndFree();
435 :
436 : /*
437 : * Potentially fallible append operations.
438 : *
439 : * The function templates that take an unspecified type U require a
440 : * const T & or a MoveRef<T>. The MoveRef<T> variants move their
441 : * operands into the vector, instead of copying them. If they fail, the
442 : * operand is left unmoved.
443 : */
444 : template <class U> bool append(U t);
445 : bool appendN(const T &t, size_t n);
446 : template <class U> bool append(const U *begin, const U *end);
447 : template <class U> bool append(const U *begin, size_t length);
448 : template <class U, size_t O, class BP> bool append(const Vector<U,O,BP> &other);
449 :
450 : /*
451 : * Guaranteed-infallible append operations for use upon vectors whose
452 : * memory has been pre-reserved.
453 : */
454 1516063 : void infallibleAppend(const T &t) {
455 1516063 : internalAppend(t);
456 1516063 : }
457 : void infallibleAppendN(const T &t, size_t n) {
458 : internalAppendN(t, n);
459 : }
460 18 : template <class U> void infallibleAppend(const U *begin, const U *end) {
461 18 : internalAppend(begin, PointerRangeSize(begin, end));
462 18 : }
463 3532992 : template <class U> void infallibleAppend(const U *begin, size_t length) {
464 3532992 : internalAppend(begin, length);
465 3532992 : }
466 72 : template <class U, size_t O, class BP> void infallibleAppend(const Vector<U,O,BP> &other) {
467 72 : internalAppend(other);
468 72 : }
469 :
470 : void popBack();
471 :
472 : T popCopy();
473 :
474 : /*
475 : * Transfers ownership of the internal buffer used by Vector to the caller.
476 : * After this call, the Vector is empty. Since the returned buffer may need
477 : * to be allocated (if the elements are currently stored in-place), the
478 : * call can fail, returning NULL.
479 : *
480 : * N.B. Although a T*, only the range [0, length()) is constructed.
481 : */
482 : T *extractRawBuffer();
483 :
484 : /*
485 : * Transfer ownership of an array of objects into the Vector.
486 : * N.B. This call assumes that there are no uninitialized elements in the
487 : * passed array.
488 : */
489 : void replaceRawBuffer(T *p, size_t length);
490 :
491 : /*
492 : * Places |val| at position |p|, shifting existing elements
493 : * from |p| onward one position higher.
494 : */
495 : bool insert(T *p, const T &val);
496 :
497 : /*
498 : * Removes the element |t|, which must fall in the bounds [begin, end),
499 : * shifting existing elements from |t + 1| onward one position lower.
500 : */
501 : void erase(T *t);
502 :
503 : /*
504 : * Measure the size of the Vector's heap-allocated storage.
505 : */
506 : size_t sizeOfExcludingThis(JSMallocSizeOfFun mallocSizeOf) const;
507 :
508 : /*
509 : * Like sizeOfExcludingThis, but also measures the size of the Vector
510 : * object (which must be heap-allocated) itself.
511 : */
512 : size_t sizeOfIncludingThis(JSMallocSizeOfFun mallocSizeOf) const;
513 : };
514 :
515 : /* This does the re-entrancy check plus several other sanity checks. */
516 : #define REENTRANCY_GUARD_ET_AL \
517 : ReentrancyGuard g(*this); \
518 : JS_ASSERT_IF(usingInlineStorage(), mCapacity == sInlineCapacity); \
519 : JS_ASSERT(reserved() <= mCapacity); \
520 : JS_ASSERT(mLength <= reserved()); \
521 : JS_ASSERT(mLength <= mCapacity)
522 :
523 : /* Vector Implementation */
524 :
525 : template <class T, size_t N, class AllocPolicy>
526 : JS_ALWAYS_INLINE
527 34348194 : Vector<T,N,AllocPolicy>::Vector(AllocPolicy ap)
528 : : AllocPolicy(ap), mBegin((T *)storage.addr()), mLength(0),
529 : mCapacity(sInlineCapacity)
530 : #ifdef DEBUG
531 34348194 : , mReserved(0), entered(false)
532 : #endif
533 34348194 : {}
534 :
535 : /* Move constructor. */
536 : template <class T, size_t N, class AllocPolicy>
537 : JS_ALWAYS_INLINE
538 9968712 : Vector<T, N, AllocPolicy>::Vector(MoveRef<Vector> rhs)
539 9968712 : : AllocPolicy(rhs)
540 : {
541 9968712 : mLength = rhs->mLength;
542 9968712 : mCapacity = rhs->mCapacity;
543 : #ifdef DEBUG
544 9968712 : mReserved = rhs->mReserved;
545 : #endif
546 :
547 9968712 : if (rhs->usingInlineStorage()) {
548 : /* We can't move the buffer over in this case, so copy elements. */
549 3346152 : mBegin = (T *)storage.addr();
550 3346152 : Impl::moveConstruct(mBegin, rhs->beginNoCheck(), rhs->endNoCheck());
551 : /*
552 : * Leave rhs's mLength, mBegin, mCapacity, and mReserved as they are.
553 : * The elements in its in-line storage still need to be destroyed.
554 : */
555 : } else {
556 : /*
557 : * Take src's buffer, and turn src into an empty vector using
558 : * in-line storage.
559 : */
560 6622560 : mBegin = rhs->mBegin;
561 6622560 : rhs->mBegin = (T *) rhs->storage.addr();
562 6622560 : rhs->mCapacity = sInlineCapacity;
563 6622560 : rhs->mLength = 0;
564 : #ifdef DEBUG
565 6622560 : rhs->mReserved = 0;
566 : #endif
567 : }
568 9968712 : }
569 :
570 : /* Move assignment. */
571 : template <class T, size_t N, class AP>
572 : JS_ALWAYS_INLINE
573 : Vector<T, N, AP> &
574 : Vector<T, N, AP>::operator=(MoveRef<Vector> rhs)
575 : {
576 : this->~Vector();
577 : new(this) Vector(rhs);
578 : return *this;
579 : }
580 :
581 : template <class T, size_t N, class AP>
582 : JS_ALWAYS_INLINE
583 44316906 : Vector<T,N,AP>::~Vector()
584 : {
585 88633812 : REENTRANCY_GUARD_ET_AL;
586 44316906 : Impl::destroy(beginNoCheck(), endNoCheck());
587 44316906 : if (!usingInlineStorage())
588 5441566 : this->free_(beginNoCheck());
589 44316906 : }
590 :
591 : /*
592 : * Calculate a new capacity that is at least lengthInc greater than
593 : * curLength and check for overflow.
594 : */
595 : template <class T, size_t N, class AP>
596 : STATIC_POSTCONDITION(!return || newCap >= curLength + lengthInc)
597 : inline bool
598 8235379 : Vector<T,N,AP>::calculateNewCapacity(size_t curLength, size_t lengthInc,
599 : size_t &newCap)
600 : {
601 8235379 : size_t newMinCap = curLength + lengthInc;
602 :
603 : /*
604 : * Check for overflow in the above addition, below CEILING_LOG2, and later
605 : * multiplication by sizeof(T).
606 : */
607 8235379 : if (newMinCap < curLength ||
608 : newMinCap & tl::MulOverflowMask<2 * sizeof(T)>::result) {
609 0 : this->reportAllocOverflow();
610 0 : return false;
611 : }
612 :
613 : /* Round up to next power of 2. */
614 8235379 : newCap = RoundUpPow2(newMinCap);
615 :
616 : /*
617 : * Do not allow a buffer large enough that the expression ((char *)end() -
618 : * (char *)begin()) overflows ptrdiff_t. See Bug 510319.
619 : */
620 8235379 : if (newCap & tl::UnsafeRangeSizeMask<T>::result) {
621 0 : this->reportAllocOverflow();
622 0 : return false;
623 : }
624 8235379 : return true;
625 : }
626 :
627 : /*
628 : * This function will grow the current heap capacity to have capacity
629 : * (mLength + lengthInc) and fail on OOM or integer overflow.
630 : */
631 : template <class T, size_t N, class AP>
632 : JS_ALWAYS_INLINE bool
633 2744519 : Vector<T,N,AP>::growHeapStorageBy(size_t lengthInc)
634 : {
635 2744519 : JS_ASSERT(!usingInlineStorage());
636 : size_t newCap;
637 : return calculateNewCapacity(mLength, lengthInc, newCap) &&
638 2744519 : Impl::growTo(*this, newCap);
639 : }
640 :
641 : /*
642 : * This function will create a new heap buffer with capacity (mLength +
643 : * lengthInc()), move all elements in the inline buffer to this new buffer,
644 : * and fail on OOM or integer overflow.
645 : */
646 : template <class T, size_t N, class AP>
647 : inline bool
648 5490860 : Vector<T,N,AP>::convertToHeapStorage(size_t lengthInc)
649 : {
650 5490860 : JS_ASSERT(usingInlineStorage());
651 : size_t newCap;
652 5490860 : if (!calculateNewCapacity(mLength, lengthInc, newCap))
653 0 : return false;
654 :
655 : /* Allocate buffer. */
656 5490860 : T *newBuf = reinterpret_cast<T *>(this->malloc_(newCap * sizeof(T)));
657 5490860 : if (!newBuf)
658 0 : return false;
659 :
660 : /* Copy inline elements into heap buffer. */
661 5490860 : Impl::moveConstruct(newBuf, beginNoCheck(), endNoCheck());
662 5490860 : Impl::destroy(beginNoCheck(), endNoCheck());
663 :
664 : /* Switch in heap buffer. */
665 5490860 : mBegin = newBuf;
666 : /* mLength is unchanged. */
667 5490860 : mCapacity = newCap;
668 5490860 : return true;
669 : }
670 :
671 : template <class T, size_t N, class AP>
672 : JS_NEVER_INLINE bool
673 8235379 : Vector<T,N,AP>::growStorageBy(size_t incr)
674 : {
675 8235379 : JS_ASSERT(mLength + incr > mCapacity);
676 : return usingInlineStorage()
677 : ? convertToHeapStorage(incr)
678 8235379 : : growHeapStorageBy(incr);
679 : }
680 :
681 : template <class T, size_t N, class AP>
682 : inline bool
683 2511430 : Vector<T,N,AP>::reserve(size_t request)
684 : {
685 5022860 : REENTRANCY_GUARD_ET_AL;
686 2511430 : if (request > mCapacity && !growStorageBy(request - mLength))
687 0 : return false;
688 :
689 : #ifdef DEBUG
690 2511430 : if (request > mReserved)
691 2281143 : mReserved = request;
692 2511430 : JS_ASSERT(mLength <= mReserved);
693 2511430 : JS_ASSERT(mReserved <= mCapacity);
694 : #endif
695 2511430 : return true;
696 : }
697 :
698 : template <class T, size_t N, class AP>
699 : inline void
700 2290140 : Vector<T,N,AP>::shrinkBy(size_t incr)
701 : {
702 4580280 : REENTRANCY_GUARD_ET_AL;
703 2290140 : JS_ASSERT(incr <= mLength);
704 2290140 : Impl::destroy(endNoCheck() - incr, endNoCheck());
705 2290140 : mLength -= incr;
706 2290140 : }
707 :
708 : template <class T, size_t N, class AP>
709 : template <bool InitNewElems>
710 : JS_ALWAYS_INLINE bool
711 6090984 : Vector<T,N,AP>::growByImpl(size_t incr)
712 : {
713 12181968 : REENTRANCY_GUARD_ET_AL;
714 6090984 : if (incr > mCapacity - mLength && !growStorageBy(incr))
715 0 : return false;
716 :
717 6090984 : JS_ASSERT(mLength + incr <= mCapacity);
718 6090984 : T *newend = endNoCheck() + incr;
719 : if (InitNewElems)
720 5800508 : Impl::initialize(endNoCheck(), newend);
721 6090984 : mLength += incr;
722 : #ifdef DEBUG
723 6090984 : if (mLength > mReserved)
724 6006803 : mReserved = mLength;
725 : #endif
726 6090984 : return true;
727 : }
728 :
729 : template <class T, size_t N, class AP>
730 : JS_ALWAYS_INLINE bool
731 5800508 : Vector<T,N,AP>::growBy(size_t incr)
732 : {
733 5800508 : return growByImpl<true>(incr);
734 : }
735 :
736 : template <class T, size_t N, class AP>
737 : JS_ALWAYS_INLINE bool
738 290476 : Vector<T,N,AP>::growByUninitialized(size_t incr)
739 : {
740 290476 : return growByImpl<false>(incr);
741 : }
742 :
743 : template <class T, size_t N, class AP>
744 : STATIC_POSTCONDITION(!return || ubound(this->begin()) >= newLength)
745 : inline bool
746 101028 : Vector<T,N,AP>::resize(size_t newLength)
747 : {
748 101028 : size_t curLength = mLength;
749 101028 : if (newLength > curLength)
750 22441 : return growBy(newLength - curLength);
751 78587 : shrinkBy(curLength - newLength);
752 78587 : return true;
753 : }
754 :
755 : template <class T, size_t N, class AP>
756 : JS_ALWAYS_INLINE bool
757 2253934 : Vector<T,N,AP>::resizeUninitialized(size_t newLength)
758 : {
759 2253934 : size_t curLength = mLength;
760 2253934 : if (newLength > curLength)
761 42525 : return growByUninitialized(newLength - curLength);
762 2211409 : shrinkBy(curLength - newLength);
763 2211409 : return true;
764 : }
765 :
766 : template <class T, size_t N, class AP>
767 : inline void
768 11133234 : Vector<T,N,AP>::clear()
769 : {
770 22266468 : REENTRANCY_GUARD_ET_AL;
771 11133234 : Impl::destroy(beginNoCheck(), endNoCheck());
772 11133234 : mLength = 0;
773 11133234 : }
774 :
775 : template <class T, size_t N, class AP>
776 : inline void
777 196742 : Vector<T,N,AP>::clearAndFree()
778 : {
779 196742 : clear();
780 :
781 196742 : if (usingInlineStorage())
782 196661 : return;
783 :
784 81 : this->free_(beginNoCheck());
785 81 : mBegin = (T *)storage.addr();
786 81 : mCapacity = sInlineCapacity;
787 : #ifdef DEBUG
788 81 : mReserved = 0;
789 : #endif
790 : }
791 :
792 : template <class T, size_t N, class AP>
793 : template <class U>
794 : JS_ALWAYS_INLINE bool
795 227974399 : Vector<T,N,AP>::append(U t)
796 : {
797 455948798 : REENTRANCY_GUARD_ET_AL;
798 227974399 : if (mLength == mCapacity && !growStorageBy(1))
799 0 : return false;
800 :
801 : #ifdef DEBUG
802 227974399 : if (mLength + 1 > mReserved)
803 204713976 : mReserved = mLength + 1;
804 : #endif
805 227974399 : internalAppend(t);
806 227974399 : return true;
807 : }
808 :
809 : template <class T, size_t N, class AP>
810 : template <class U>
811 : JS_ALWAYS_INLINE void
812 229490462 : Vector<T,N,AP>::internalAppend(U t)
813 : {
814 229490462 : JS_ASSERT(mLength + 1 <= mReserved);
815 229490462 : JS_ASSERT(mReserved <= mCapacity);
816 229490462 : new(endNoCheck()) T(t);
817 229490462 : ++mLength;
818 229490462 : }
819 :
820 : template <class T, size_t N, class AP>
821 : JS_ALWAYS_INLINE bool
822 216 : Vector<T,N,AP>::appendN(const T &t, size_t needed)
823 : {
824 432 : REENTRANCY_GUARD_ET_AL;
825 216 : if (mLength + needed > mCapacity && !growStorageBy(needed))
826 0 : return false;
827 :
828 : #ifdef DEBUG
829 216 : if (mLength + needed > mReserved)
830 63 : mReserved = mLength + needed;
831 : #endif
832 216 : internalAppendN(t, needed);
833 216 : return true;
834 : }
835 :
836 : template <class T, size_t N, class AP>
837 : JS_ALWAYS_INLINE void
838 216 : Vector<T,N,AP>::internalAppendN(const T &t, size_t needed)
839 : {
840 216 : JS_ASSERT(mLength + needed <= mReserved);
841 216 : JS_ASSERT(mReserved <= mCapacity);
842 216 : Impl::copyConstructN(endNoCheck(), needed, t);
843 216 : mLength += needed;
844 216 : }
845 :
846 : template <class T, size_t N, class AP>
847 : inline bool
848 864 : Vector<T,N,AP>::insert(T *p, const T &val)
849 : {
850 864 : JS_ASSERT(begin() <= p && p <= end());
851 864 : size_t pos = p - begin();
852 864 : JS_ASSERT(pos <= mLength);
853 864 : size_t oldLength = mLength;
854 864 : if (pos == oldLength)
855 0 : return append(val);
856 : {
857 864 : T oldBack = back();
858 864 : if (!append(oldBack)) /* Dup the last element. */
859 0 : return false;
860 : }
861 2196 : for (size_t i = oldLength; i > pos; --i)
862 1332 : (*this)[i] = (*this)[i - 1];
863 864 : (*this)[pos] = val;
864 864 : return true;
865 : }
866 :
867 : template<typename T, size_t N, class AP>
868 : inline void
869 4898 : Vector<T,N,AP>::erase(T *it)
870 : {
871 4898 : JS_ASSERT(begin() <= it && it < end());
872 17869 : while (it + 1 != end()) {
873 8073 : *it = *(it + 1);
874 8073 : ++it;
875 : }
876 4898 : popBack();
877 4898 : }
878 :
879 : template <class T, size_t N, class AP>
880 : template <class U>
881 : JS_ALWAYS_INLINE bool
882 2398923 : Vector<T,N,AP>::append(const U *insBegin, const U *insEnd)
883 : {
884 4797846 : REENTRANCY_GUARD_ET_AL;
885 2398923 : size_t needed = PointerRangeSize(insBegin, insEnd);
886 2398923 : if (mLength + needed > mCapacity && !growStorageBy(needed))
887 0 : return false;
888 :
889 : #ifdef DEBUG
890 2398923 : if (mLength + needed > mReserved)
891 1476464 : mReserved = mLength + needed;
892 : #endif
893 2398923 : internalAppend(insBegin, needed);
894 2398923 : return true;
895 : }
896 :
897 : template <class T, size_t N, class AP>
898 : template <class U>
899 : JS_ALWAYS_INLINE void
900 5932005 : Vector<T,N,AP>::internalAppend(const U *insBegin, size_t length)
901 : {
902 5932005 : JS_ASSERT(mLength + length <= mReserved);
903 5932005 : JS_ASSERT(mReserved <= mCapacity);
904 5932005 : Impl::copyConstruct(endNoCheck(), insBegin, insBegin + length);
905 5932005 : mLength += length;
906 5932005 : }
907 :
908 : template <class T, size_t N, class AP>
909 : template <class U, size_t O, class BP>
910 : inline bool
911 709261 : Vector<T,N,AP>::append(const Vector<U,O,BP> &other)
912 : {
913 709261 : return append(other.begin(), other.end());
914 : }
915 :
916 : template <class T, size_t N, class AP>
917 : template <class U, size_t O, class BP>
918 : inline void
919 72 : Vector<T,N,AP>::internalAppend(const Vector<U,O,BP> &other)
920 : {
921 72 : internalAppend(other.begin(), other.length());
922 72 : }
923 :
924 : template <class T, size_t N, class AP>
925 : template <class U>
926 : JS_ALWAYS_INLINE bool
927 1567403 : Vector<T,N,AP>::append(const U *insBegin, size_t length)
928 : {
929 1567403 : return this->append(insBegin, insBegin + length);
930 : }
931 :
932 : template <class T, size_t N, class AP>
933 : JS_ALWAYS_INLINE void
934 40233353 : Vector<T,N,AP>::popBack()
935 : {
936 80466706 : REENTRANCY_GUARD_ET_AL;
937 40233353 : JS_ASSERT(!empty());
938 40233353 : --mLength;
939 40233353 : endNoCheck()->~T();
940 40233353 : }
941 :
942 : template <class T, size_t N, class AP>
943 : JS_ALWAYS_INLINE T
944 40080033 : Vector<T,N,AP>::popCopy()
945 : {
946 40080033 : T ret = back();
947 40080033 : popBack();
948 36618062 : return ret;
949 : }
950 :
951 : template <class T, size_t N, class AP>
952 : inline T *
953 155789 : Vector<T,N,AP>::extractRawBuffer()
954 : {
955 : T *ret;
956 155789 : if (usingInlineStorage()) {
957 106576 : ret = reinterpret_cast<T *>(this->malloc_(mLength * sizeof(T)));
958 106576 : if (!ret)
959 0 : return NULL;
960 106576 : Impl::copyConstruct(ret, beginNoCheck(), endNoCheck());
961 106576 : Impl::destroy(beginNoCheck(), endNoCheck());
962 : /* mBegin, mCapacity are unchanged. */
963 106576 : mLength = 0;
964 : } else {
965 49213 : ret = mBegin;
966 49213 : mBegin = (T *)storage.addr();
967 49213 : mLength = 0;
968 49213 : mCapacity = sInlineCapacity;
969 : #ifdef DEBUG
970 49213 : mReserved = 0;
971 : #endif
972 : }
973 155789 : return ret;
974 : }
975 :
976 : template <class T, size_t N, class AP>
977 : inline void
978 : Vector<T,N,AP>::replaceRawBuffer(T *p, size_t length)
979 : {
980 : REENTRANCY_GUARD_ET_AL;
981 :
982 : /* Destroy what we have. */
983 : Impl::destroy(beginNoCheck(), endNoCheck());
984 : if (!usingInlineStorage())
985 : this->free_(beginNoCheck());
986 :
987 : /* Take in the new buffer. */
988 : if (length <= sInlineCapacity) {
989 : /*
990 : * We convert to inline storage if possible, even though p might
991 : * otherwise be acceptable. Maybe this behaviour should be
992 : * specifiable with an argument to this function.
993 : */
994 : mBegin = (T *)storage.addr();
995 : mLength = length;
996 : mCapacity = sInlineCapacity;
997 : Impl::moveConstruct(mBegin, p, p + length);
998 : Impl::destroy(p, p + length);
999 : this->free_(p);
1000 : } else {
1001 : mBegin = p;
1002 : mLength = length;
1003 : mCapacity = length;
1004 : }
1005 : #ifdef DEBUG
1006 : mReserved = length;
1007 : #endif
1008 : }
1009 :
1010 : template <class T, size_t N, class AP>
1011 : inline size_t
1012 0 : Vector<T,N,AP>::sizeOfExcludingThis(JSMallocSizeOfFun mallocSizeOf) const
1013 : {
1014 0 : return usingInlineStorage() ? 0 : mallocSizeOf(beginNoCheck());
1015 : }
1016 :
1017 : template <class T, size_t N, class AP>
1018 : inline size_t
1019 : Vector<T,N,AP>::sizeOfIncludingThis(JSMallocSizeOfFun mallocSizeOf) const
1020 : {
1021 : return mallocSizeOf(this) + sizeOfExcludingThis(mallocSizeOf);
1022 : }
1023 :
1024 : } /* namespace js */
1025 :
1026 : #ifdef _MSC_VER
1027 : #pragma warning(pop)
1028 : #endif
1029 :
1030 : #endif /* jsvector_h_ */
|