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:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
17 * The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
20 * The Initial Developer of the Original Code is
21 * the Mozilla Corporation.
24 * Luke Wagner <lw@mozilla.com>
25 * Nicholas Nethercote <nnethercote@mozilla.com>
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.
39 * ***** END LICENSE BLOCK ***** */
44 #include "mozilla/Attributes.h"
46 #include "TemplateLib.h"
49 /* Silence dire "bugs in previous versions of MSVC have been fixed" warnings */
52 #pragma warning(disable:4345)
57 class TempAllocPolicy
;
60 size_t MinInlineCapacity
= 0,
61 class AllocPolicy
= TempAllocPolicy
>
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.
68 template <class T
, size_t N
, class AP
, bool IsPod
>
71 /* Destroys constructed objects in the range [begin, end). */
72 static inline void destroy(T
*begin
, T
*end
) {
73 for (T
*p
= begin
; p
!= end
; ++p
)
77 /* Constructs objects in the uninitialized range [begin, end). */
78 static inline void initialize(T
*begin
, T
*end
) {
79 for (T
*p
= begin
; p
!= end
; ++p
)
84 * Copy-constructs objects in the uninitialized range
85 * [dst, dst+(srcend-srcbeg)) from the range [srcbeg, srcend).
88 static inline void copyConstruct(T
*dst
, const U
*srcbeg
, const U
*srcend
) {
89 for (const U
*p
= srcbeg
; p
!= srcend
; ++p
, ++dst
)
94 * Move-constructs objects in the uninitialized range
95 * [dst, dst+(srcend-srcbeg)) from the range [srcbeg, srcend).
98 static inline void moveConstruct(T
*dst
, const U
*srcbeg
, const U
*srcend
) {
99 for (const U
*p
= srcbeg
; p
!= srcend
; ++p
, ++dst
)
100 new(dst
) T(Move(*p
));
104 * Copy-constructs objects in the uninitialized range [dst, dst+n) from the
108 static inline void copyConstructN(T
*dst
, size_t n
, const U
&u
) {
109 for (T
*end
= dst
+ n
; dst
!= end
; ++dst
)
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
119 static inline bool growTo(Vector
<T
,N
,AP
> &v
, size_t newcap
) {
120 JS_ASSERT(!v
.usingInlineStorage());
121 T
*newbuf
= reinterpret_cast<T
*>(v
.malloc_(newcap
* sizeof(T
)));
124 for (T
*dst
= newbuf
, *src
= v
.beginNoCheck(); src
!= v
.endNoCheck(); ++dst
, ++src
)
125 new(dst
) T(Move(*src
));
126 VectorImpl::destroy(v
.beginNoCheck(), v
.endNoCheck());
129 /* v.mLength is unchanged. */
130 v
.mCapacity
= newcap
;
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
140 template <class T
, size_t N
, class AP
>
141 struct VectorImpl
<T
, N
, AP
, true>
143 static inline void destroy(T
*, T
*) {}
145 static inline void initialize(T
*begin
, T
*end
) {
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.
152 * memset(begin, 0, sizeof(T) * (end-begin));
154 for (T
*p
= begin
; p
!= end
; ++p
)
159 static inline void copyConstruct(T
*dst
, const U
*srcbeg
, const U
*srcend
) {
161 * See above memset comment. Also, notice that copyConstruct is
162 * currently templated (T != U), so memcpy won't work without
165 * memcpy(dst, srcbeg, sizeof(T) * (srcend - srcbeg));
167 for (const U
*p
= srcbeg
; p
!= srcend
; ++p
, ++dst
)
172 static inline void moveConstruct(T
*dst
, const U
*srcbeg
, const U
*srcend
) {
173 copyConstruct(dst
, srcbeg
, srcend
);
176 static inline void copyConstructN(T
*dst
, size_t n
, const T
&t
) {
177 for (T
*p
= dst
, *end
= dst
+ n
; p
!= end
; ++p
)
181 static inline bool growTo(Vector
<T
,N
,AP
> &v
, size_t newcap
) {
182 JS_ASSERT(!v
.usingInlineStorage());
183 size_t bytes
= sizeof(T
) * newcap
;
184 size_t oldBytes
= sizeof(T
) * v
.mCapacity
;
185 T
*newbuf
= reinterpret_cast<T
*>(v
.realloc_(v
.mBegin
, oldBytes
, bytes
));
189 /* v.mLength is unchanged. */
190 v
.mCapacity
= newcap
;
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.
203 * - default and copy constructible, assignable, destructible
204 * - operations do not throw
206 * - any value, however, N is clamped to min/max values
208 * - see "Allocation policies" in jsalloc.h (default js::TempAllocPolicy)
210 * N.B: Vector is not reentrant: T member functions called during Vector member
211 * functions must not call back into the same object.
213 template <class T
, size_t N
, class AllocPolicy
>
214 class Vector
: private AllocPolicy
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
>;
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
);
227 template <bool InitNewElems
> inline bool growByImpl(size_t inc
);
229 /* magic constants */
231 static const int sMaxInlineBytes
= 1024;
233 /* compute constants */
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.
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.
244 template <int M
, int Dummy
>
246 static const size_t result
= sizeof(T
);
249 struct ElemSize
<0, Dummy
> {
250 static const size_t result
= 1;
253 static const size_t sInlineCapacity
=
254 tl::Min
<N
, sMaxInlineBytes
/ ElemSize
<N
, 0>::result
>::result
;
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
;
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().
270 size_t mLength
; /* Number of elements in the Vector. */
271 size_t mCapacity
; /* Max number of elements storable in the Vector without resizing. */
273 size_t mReserved
; /* Max elements of reserved or used space in this vector. */
276 AlignedStorage
<sInlineBytes
> storage
;
279 friend class ReentrancyGuard
;
283 Vector(const Vector
&) MOZ_DELETE
;
284 Vector
&operator=(const Vector
&) MOZ_DELETE
;
286 /* private accessors */
288 bool usingInlineStorage() const {
289 return mBegin
== (T
*)storage
.addr();
292 T
*beginNoCheck() const {
297 return mBegin
+ mLength
;
300 const T
*endNoCheck() const {
301 return mBegin
+ mLength
;
305 size_t reserved() const {
306 JS_ASSERT(mReserved
<= mCapacity
);
307 JS_ASSERT(mLength
<= mReserved
);
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
);
319 static const size_t sMaxInlineStorage
= N
;
321 typedef T ElementType
;
323 Vector(AllocPolicy
= AllocPolicy());
324 Vector(MoveRef
<Vector
>); /* Move constructor. */
325 Vector
&operator=(MoveRef
<Vector
>); /* Move assignment. */
330 const AllocPolicy
&allocPolicy() const {
334 AllocPolicy
&allocPolicy() {
338 enum { InlineLength
= N
};
340 size_t length() const {
348 size_t capacity() const {
357 const T
*begin() const {
364 return mBegin
+ mLength
;
367 const T
*end() const {
369 return mBegin
+ mLength
;
372 T
&operator[](size_t i
) {
373 JS_ASSERT(!entered
&& i
< mLength
);
377 const T
&operator[](size_t i
) const {
378 JS_ASSERT(!entered
&& i
< mLength
);
383 JS_ASSERT(!entered
&& !empty());
387 const T
&back() const {
388 JS_ASSERT(!entered
&& !empty());
395 Range(T
*cur
, T
*end
) : cur(cur
), end(end
) {}
398 bool empty() const { return cur
== end
; }
399 size_t remain() const { return end
- cur
; }
400 T
&front() const { return *cur
; }
401 void popFront() { JS_ASSERT(!empty()); ++cur
; }
402 T
popCopyFront() { JS_ASSERT(!empty()); return *cur
++; }
406 return Range(begin(), end());
411 /* If reserve(length() + N) succeeds, the N next appends are guaranteed to succeed. */
412 bool reserve(size_t capacity
);
415 * Destroy elements in the range [end() - incr, end()). Does not deallocate
416 * or unreserve storage for those elements.
418 void shrinkBy(size_t incr
);
420 /* Grow the vector by incr elements. */
421 bool growBy(size_t incr
);
423 /* Call shrinkBy or growBy based on whether newSize > length(). */
424 bool resize(size_t newLength
);
426 /* Leave new elements as uninitialized memory. */
427 bool growByUninitialized(size_t incr
);
428 bool resizeUninitialized(size_t newLength
);
430 /* Shorthand for shrinkBy(length()). */
433 /* Clears and releases any heap-allocated storage. */
437 * Potentially fallible append operations.
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.
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
);
451 * Guaranteed-infallible append operations for use upon vectors whose
452 * memory has been pre-reserved.
454 void infallibleAppend(const T
&t
) {
457 void infallibleAppendN(const T
&t
, size_t n
) {
458 internalAppendN(t
, n
);
460 template <class U
> void infallibleAppend(const U
*begin
, const U
*end
) {
461 internalAppend(begin
, PointerRangeSize(begin
, end
));
463 template <class U
> void infallibleAppend(const U
*begin
, size_t length
) {
464 internalAppend(begin
, length
);
466 template <class U
, size_t O
, class BP
> void infallibleAppend(const Vector
<U
,O
,BP
> &other
) {
467 internalAppend(other
);
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.
480 * N.B. Although a T*, only the range [0, length()) is constructed.
482 T
*extractRawBuffer();
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
489 void replaceRawBuffer(T
*p
, size_t length
);
492 * Places |val| at position |p|, shifting existing elements
493 * from |p| onward one position higher.
495 bool insert(T
*p
, const T
&val
);
498 * Removes the element |t|, which must fall in the bounds [begin, end),
499 * shifting existing elements from |t + 1| onward one position lower.
504 * Measure the size of the Vector's heap-allocated storage.
506 size_t sizeOfExcludingThis(JSMallocSizeOfFun mallocSizeOf
) const;
509 * Like sizeOfExcludingThis, but also measures the size of the Vector
510 * object (which must be heap-allocated) itself.
512 size_t sizeOfIncludingThis(JSMallocSizeOfFun mallocSizeOf
) const;
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)
523 /* Vector Implementation */
525 template <class T
, size_t N
, class AllocPolicy
>
527 Vector
<T
,N
,AllocPolicy
>::Vector(AllocPolicy ap
)
528 : AllocPolicy(ap
), mBegin((T
*)storage
.addr()), mLength(0),
529 mCapacity(sInlineCapacity
)
531 , mReserved(0), entered(false)
535 /* Move constructor. */
536 template <class T
, size_t N
, class AllocPolicy
>
538 Vector
<T
, N
, AllocPolicy
>::Vector(MoveRef
<Vector
> rhs
)
541 mLength
= rhs
->mLength
;
542 mCapacity
= rhs
->mCapacity
;
544 mReserved
= rhs
->mReserved
;
547 if (rhs
->usingInlineStorage()) {
548 /* We can't move the buffer over in this case, so copy elements. */
549 mBegin
= (T
*)storage
.addr();
550 Impl::moveConstruct(mBegin
, rhs
->beginNoCheck(), rhs
->endNoCheck());
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.
557 * Take src's buffer, and turn src into an empty vector using
560 mBegin
= rhs
->mBegin
;
561 rhs
->mBegin
= (T
*) rhs
->storage
.addr();
562 rhs
->mCapacity
= sInlineCapacity
;
570 /* Move assignment. */
571 template <class T
, size_t N
, class AP
>
574 Vector
<T
, N
, AP
>::operator=(MoveRef
<Vector
> rhs
)
577 new(this) Vector(rhs
);
581 template <class T
, size_t N
, class AP
>
583 Vector
<T
,N
,AP
>::~Vector()
585 REENTRANCY_GUARD_ET_AL
;
586 Impl::destroy(beginNoCheck(), endNoCheck());
587 if (!usingInlineStorage())
588 this->free_(beginNoCheck());
592 * Calculate a new capacity that is at least lengthInc greater than
593 * curLength and check for overflow.
595 template <class T
, size_t N
, class AP
>
596 STATIC_POSTCONDITION(!return || newCap
>= curLength
+ lengthInc
)
598 Vector
<T
,N
,AP
>::calculateNewCapacity(size_t curLength
, size_t lengthInc
,
601 size_t newMinCap
= curLength
+ lengthInc
;
604 * Check for overflow in the above addition, below CEILING_LOG2, and later
605 * multiplication by sizeof(T).
607 if (newMinCap
< curLength
||
608 newMinCap
& tl::MulOverflowMask
<2 * sizeof(T
)>::result
) {
609 this->reportAllocOverflow();
613 /* Round up to next power of 2. */
614 newCap
= RoundUpPow2(newMinCap
);
617 * Do not allow a buffer large enough that the expression ((char *)end() -
618 * (char *)begin()) overflows ptrdiff_t. See Bug 510319.
620 if (newCap
& tl::UnsafeRangeSizeMask
<T
>::result
) {
621 this->reportAllocOverflow();
628 * This function will grow the current heap capacity to have capacity
629 * (mLength + lengthInc) and fail on OOM or integer overflow.
631 template <class T
, size_t N
, class AP
>
632 JS_ALWAYS_INLINE
bool
633 Vector
<T
,N
,AP
>::growHeapStorageBy(size_t lengthInc
)
635 JS_ASSERT(!usingInlineStorage());
637 return calculateNewCapacity(mLength
, lengthInc
, newCap
) &&
638 Impl::growTo(*this, newCap
);
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.
646 template <class T
, size_t N
, class AP
>
648 Vector
<T
,N
,AP
>::convertToHeapStorage(size_t lengthInc
)
650 JS_ASSERT(usingInlineStorage());
652 if (!calculateNewCapacity(mLength
, lengthInc
, newCap
))
655 /* Allocate buffer. */
656 T
*newBuf
= reinterpret_cast<T
*>(this->malloc_(newCap
* sizeof(T
)));
660 /* Copy inline elements into heap buffer. */
661 Impl::moveConstruct(newBuf
, beginNoCheck(), endNoCheck());
662 Impl::destroy(beginNoCheck(), endNoCheck());
664 /* Switch in heap buffer. */
666 /* mLength is unchanged. */
671 template <class T
, size_t N
, class AP
>
673 Vector
<T
,N
,AP
>::growStorageBy(size_t incr
)
675 JS_ASSERT(mLength
+ incr
> mCapacity
);
676 return usingInlineStorage()
677 ? convertToHeapStorage(incr
)
678 : growHeapStorageBy(incr
);
681 template <class T
, size_t N
, class AP
>
683 Vector
<T
,N
,AP
>::reserve(size_t request
)
685 REENTRANCY_GUARD_ET_AL
;
686 if (request
> mCapacity
&& !growStorageBy(request
- mLength
))
690 if (request
> mReserved
)
692 JS_ASSERT(mLength
<= mReserved
);
693 JS_ASSERT(mReserved
<= mCapacity
);
698 template <class T
, size_t N
, class AP
>
700 Vector
<T
,N
,AP
>::shrinkBy(size_t incr
)
702 REENTRANCY_GUARD_ET_AL
;
703 JS_ASSERT(incr
<= mLength
);
704 Impl::destroy(endNoCheck() - incr
, endNoCheck());
708 template <class T
, size_t N
, class AP
>
709 template <bool InitNewElems
>
710 JS_ALWAYS_INLINE
bool
711 Vector
<T
,N
,AP
>::growByImpl(size_t incr
)
713 REENTRANCY_GUARD_ET_AL
;
714 if (incr
> mCapacity
- mLength
&& !growStorageBy(incr
))
717 JS_ASSERT(mLength
+ incr
<= mCapacity
);
718 T
*newend
= endNoCheck() + incr
;
720 Impl::initialize(endNoCheck(), newend
);
723 if (mLength
> mReserved
)
729 template <class T
, size_t N
, class AP
>
730 JS_ALWAYS_INLINE
bool
731 Vector
<T
,N
,AP
>::growBy(size_t incr
)
733 return growByImpl
<true>(incr
);
736 template <class T
, size_t N
, class AP
>
737 JS_ALWAYS_INLINE
bool
738 Vector
<T
,N
,AP
>::growByUninitialized(size_t incr
)
740 return growByImpl
<false>(incr
);
743 template <class T
, size_t N
, class AP
>
744 STATIC_POSTCONDITION(!return || ubound(this->begin()) >= newLength
)
746 Vector
<T
,N
,AP
>::resize(size_t newLength
)
748 size_t curLength
= mLength
;
749 if (newLength
> curLength
)
750 return growBy(newLength
- curLength
);
751 shrinkBy(curLength
- newLength
);
755 template <class T
, size_t N
, class AP
>
756 JS_ALWAYS_INLINE
bool
757 Vector
<T
,N
,AP
>::resizeUninitialized(size_t newLength
)
759 size_t curLength
= mLength
;
760 if (newLength
> curLength
)
761 return growByUninitialized(newLength
- curLength
);
762 shrinkBy(curLength
- newLength
);
766 template <class T
, size_t N
, class AP
>
768 Vector
<T
,N
,AP
>::clear()
770 REENTRANCY_GUARD_ET_AL
;
771 Impl::destroy(beginNoCheck(), endNoCheck());
775 template <class T
, size_t N
, class AP
>
777 Vector
<T
,N
,AP
>::clearAndFree()
781 if (usingInlineStorage())
784 this->free_(beginNoCheck());
785 mBegin
= (T
*)storage
.addr();
786 mCapacity
= sInlineCapacity
;
792 template <class T
, size_t N
, class AP
>
794 JS_ALWAYS_INLINE
bool
795 Vector
<T
,N
,AP
>::append(U t
)
797 REENTRANCY_GUARD_ET_AL
;
798 if (mLength
== mCapacity
&& !growStorageBy(1))
802 if (mLength
+ 1 > mReserved
)
803 mReserved
= mLength
+ 1;
809 template <class T
, size_t N
, class AP
>
811 JS_ALWAYS_INLINE
void
812 Vector
<T
,N
,AP
>::internalAppend(U t
)
814 JS_ASSERT(mLength
+ 1 <= mReserved
);
815 JS_ASSERT(mReserved
<= mCapacity
);
816 new(endNoCheck()) T(t
);
820 template <class T
, size_t N
, class AP
>
821 JS_ALWAYS_INLINE
bool
822 Vector
<T
,N
,AP
>::appendN(const T
&t
, size_t needed
)
824 REENTRANCY_GUARD_ET_AL
;
825 if (mLength
+ needed
> mCapacity
&& !growStorageBy(needed
))
829 if (mLength
+ needed
> mReserved
)
830 mReserved
= mLength
+ needed
;
832 internalAppendN(t
, needed
);
836 template <class T
, size_t N
, class AP
>
837 JS_ALWAYS_INLINE
void
838 Vector
<T
,N
,AP
>::internalAppendN(const T
&t
, size_t needed
)
840 JS_ASSERT(mLength
+ needed
<= mReserved
);
841 JS_ASSERT(mReserved
<= mCapacity
);
842 Impl::copyConstructN(endNoCheck(), needed
, t
);
846 template <class T
, size_t N
, class AP
>
848 Vector
<T
,N
,AP
>::insert(T
*p
, const T
&val
)
850 JS_ASSERT(begin() <= p
&& p
<= end());
851 size_t pos
= p
- begin();
852 JS_ASSERT(pos
<= mLength
);
853 size_t oldLength
= mLength
;
854 if (pos
== oldLength
)
858 if (!append(oldBack
)) /* Dup the last element. */
861 for (size_t i
= oldLength
; i
> pos
; --i
)
862 (*this)[i
] = (*this)[i
- 1];
867 template<typename T
, size_t N
, class AP
>
869 Vector
<T
,N
,AP
>::erase(T
*it
)
871 JS_ASSERT(begin() <= it
&& it
< end());
872 while (it
+ 1 != end()) {
879 template <class T
, size_t N
, class AP
>
881 JS_ALWAYS_INLINE
bool
882 Vector
<T
,N
,AP
>::append(const U
*insBegin
, const U
*insEnd
)
884 REENTRANCY_GUARD_ET_AL
;
885 size_t needed
= PointerRangeSize(insBegin
, insEnd
);
886 if (mLength
+ needed
> mCapacity
&& !growStorageBy(needed
))
890 if (mLength
+ needed
> mReserved
)
891 mReserved
= mLength
+ needed
;
893 internalAppend(insBegin
, needed
);
897 template <class T
, size_t N
, class AP
>
899 JS_ALWAYS_INLINE
void
900 Vector
<T
,N
,AP
>::internalAppend(const U
*insBegin
, size_t length
)
902 JS_ASSERT(mLength
+ length
<= mReserved
);
903 JS_ASSERT(mReserved
<= mCapacity
);
904 Impl::copyConstruct(endNoCheck(), insBegin
, insBegin
+ length
);
908 template <class T
, size_t N
, class AP
>
909 template <class U
, size_t O
, class BP
>
911 Vector
<T
,N
,AP
>::append(const Vector
<U
,O
,BP
> &other
)
913 return append(other
.begin(), other
.end());
916 template <class T
, size_t N
, class AP
>
917 template <class U
, size_t O
, class BP
>
919 Vector
<T
,N
,AP
>::internalAppend(const Vector
<U
,O
,BP
> &other
)
921 internalAppend(other
.begin(), other
.length());
924 template <class T
, size_t N
, class AP
>
926 JS_ALWAYS_INLINE
bool
927 Vector
<T
,N
,AP
>::append(const U
*insBegin
, size_t length
)
929 return this->append(insBegin
, insBegin
+ length
);
932 template <class T
, size_t N
, class AP
>
933 JS_ALWAYS_INLINE
void
934 Vector
<T
,N
,AP
>::popBack()
936 REENTRANCY_GUARD_ET_AL
;
942 template <class T
, size_t N
, class AP
>
944 Vector
<T
,N
,AP
>::popCopy()
951 template <class T
, size_t N
, class AP
>
953 Vector
<T
,N
,AP
>::extractRawBuffer()
956 if (usingInlineStorage()) {
957 ret
= reinterpret_cast<T
*>(this->malloc_(mLength
* sizeof(T
)));
960 Impl::copyConstruct(ret
, beginNoCheck(), endNoCheck());
961 Impl::destroy(beginNoCheck(), endNoCheck());
962 /* mBegin, mCapacity are unchanged. */
966 mBegin
= (T
*)storage
.addr();
968 mCapacity
= sInlineCapacity
;
976 template <class T
, size_t N
, class AP
>
978 Vector
<T
,N
,AP
>::replaceRawBuffer(T
*p
, size_t length
)
980 REENTRANCY_GUARD_ET_AL
;
982 /* Destroy what we have. */
983 Impl::destroy(beginNoCheck(), endNoCheck());
984 if (!usingInlineStorage())
985 this->free_(beginNoCheck());
987 /* Take in the new buffer. */
988 if (length
<= sInlineCapacity
) {
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.
994 mBegin
= (T
*)storage
.addr();
996 mCapacity
= sInlineCapacity
;
997 Impl::moveConstruct(mBegin
, p
, p
+ length
);
998 Impl::destroy(p
, p
+ length
);
1010 template <class T
, size_t N
, class AP
>
1012 Vector
<T
,N
,AP
>::sizeOfExcludingThis(JSMallocSizeOfFun mallocSizeOf
) const
1014 return usingInlineStorage() ? 0 : mallocSizeOf(beginNoCheck());
1017 template <class T
, size_t N
, class AP
>
1019 Vector
<T
,N
,AP
>::sizeOfIncludingThis(JSMallocSizeOfFun mallocSizeOf
) const
1021 return mallocSizeOf(this) + sizeOfExcludingThis(mallocSizeOf
);
1024 } /* namespace js */
1027 #pragma warning(pop)
1030 #endif /* jsvector_h_ */