| 1 |
// { dg-do compile } |
| 2 |
// { dg-require-effective-target fpic } |
| 3 |
// { dg-require-visibility "" } |
| 4 |
// { dg-options "-fPIC -Wno-return-type" } |
| 5 |
|
| 6 |
|
| 7 |
typedef __SIZE_TYPE__ size_t; |
| 8 |
extern "C" void * |
| 9 |
malloc (size_t __size) |
| 10 |
throw () __attribute__ ((__malloc__)); |
| 11 |
namespace std __attribute__ ((__visibility__ ("default"))) |
| 12 |
{ |
| 13 |
using::size_t; |
| 14 |
} |
| 15 |
inline void *operator |
| 16 |
new (std::size_t, void *__p) |
| 17 |
throw () |
| 18 |
{ |
| 19 |
return __p; |
| 20 |
} |
| 21 |
template < class _T1, class _T2 > struct pair |
| 22 |
{ |
| 23 |
_T1 first; |
| 24 |
_T2 second; |
| 25 |
pair (const _T1 & __a, const _T2 & __b):first (__a), second (__b) |
| 26 |
{ |
| 27 |
} |
| 28 |
template < class _U1, class _U2 > |
| 29 |
pair (const pair < _U1, _U2 > &__p):first (__p.first), second (__p.second) |
| 30 |
{ |
| 31 |
} |
| 32 |
}; |
| 33 |
|
| 34 |
template < class _T1, class _T2 > |
| 35 |
inline pair < _T1, _T2 > make_pair (_T1 __x, _T2 __y) |
| 36 |
{ |
| 37 |
return pair < _T1, _T2 > (__x, __y); |
| 38 |
} |
| 39 |
template < typename _Tp > inline const _Tp & |
| 40 |
max (const _Tp & __a, const _Tp & __b) |
| 41 |
{ |
| 42 |
} |
| 43 |
typedef unsigned short int uint16_t; |
| 44 |
typedef unsigned long int uintptr_t; |
| 45 |
typedef uint16_t UChar; |
| 46 |
namespace std __attribute__ ((__visibility__ ("default"))) |
| 47 |
{ |
| 48 |
struct __numeric_limits_base |
| 49 |
{ |
| 50 |
}; |
| 51 |
template < typename _Tp > struct numeric_limits:public __numeric_limits_base |
| 52 |
{ |
| 53 |
static _Tp max () throw () |
| 54 |
{ |
| 55 |
} |
| 56 |
}; |
| 57 |
} |
| 58 |
|
| 59 |
template < typename T > class VectorBufferBase |
| 60 |
{ |
| 61 |
public: |
| 62 |
void allocateBuffer (size_t newCapacity) |
| 63 |
{ |
| 64 |
if (newCapacity > std::numeric_limits < size_t >::max () / sizeof (T)) |
| 65 |
*(int *) (uintptr_t) 0xbbadbeef = 0; |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
template < typename T, size_t inlineCapacity > class VectorBuffer; |
| 70 |
template < typename T > class VectorBuffer < T, 0 >:private VectorBufferBase < |
| 71 |
T > |
| 72 |
{ |
| 73 |
public: |
| 74 |
typedef VectorBufferBase < T > Base; |
| 75 |
using Base::allocateBuffer; |
| 76 |
}; |
| 77 |
|
| 78 |
template < typename T, size_t inlineCapacity = 0 > class Vector |
| 79 |
{ |
| 80 |
typedef VectorBuffer < T, inlineCapacity > Impl; |
| 81 |
public: |
| 82 |
typedef T *iterator; |
| 83 |
size_t size () const |
| 84 |
{ |
| 85 |
return m_size; |
| 86 |
} |
| 87 |
size_t capacity () const |
| 88 |
{ |
| 89 |
} |
| 90 |
iterator begin () |
| 91 |
{ |
| 92 |
} |
| 93 |
iterator end () |
| 94 |
{ |
| 95 |
return begin () + m_size; |
| 96 |
} |
| 97 |
void shrink (size_t size); |
| 98 |
void reserveCapacity (size_t newCapacity); |
| 99 |
void clear () |
| 100 |
{ |
| 101 |
shrink (0); |
| 102 |
} |
| 103 |
template < typename U > void append (const U &); |
| 104 |
void expandCapacity (size_t newMinCapacity); |
| 105 |
template < typename U > U * expandCapacity (size_t newMinCapacity, U *); |
| 106 |
size_t m_size; |
| 107 |
Impl m_impl; |
| 108 |
}; |
| 109 |
template < typename T, size_t inlineCapacity > |
| 110 |
void Vector < T, inlineCapacity >::expandCapacity (size_t newMinCapacity) |
| 111 |
{ |
| 112 |
reserveCapacity (max |
| 113 |
(newMinCapacity, |
| 114 |
max (static_cast < size_t > (16), |
| 115 |
capacity () + capacity () / 4 + 1))); |
| 116 |
} |
| 117 |
|
| 118 |
template < typename T, size_t inlineCapacity > |
| 119 |
template < typename U > |
| 120 |
inline U * Vector < T, |
| 121 |
inlineCapacity >::expandCapacity (size_t newMinCapacity, U * ptr) |
| 122 |
{ |
| 123 |
expandCapacity (newMinCapacity); |
| 124 |
} |
| 125 |
template < typename T, size_t inlineCapacity > |
| 126 |
void Vector < T, inlineCapacity >::reserveCapacity (size_t newCapacity) |
| 127 |
{ |
| 128 |
m_impl.allocateBuffer (newCapacity); |
| 129 |
} |
| 130 |
template < typename T, size_t inlineCapacity > |
| 131 |
template < typename U > |
| 132 |
inline void Vector < T, inlineCapacity >::append (const U & val) |
| 133 |
{ |
| 134 |
const U *ptr = &val; |
| 135 |
if (size () == capacity ()) |
| 136 |
ptr = expandCapacity (size () + 1, ptr); |
| 137 |
new (end ())T (*ptr); |
| 138 |
} |
| 139 |
|
| 140 |
class Range; |
| 141 |
class TextIterator |
| 142 |
{ |
| 143 |
public: |
| 144 |
explicit TextIterator (const Range *, |
| 145 |
bool emitCharactersBetweenAllVisiblePositions = |
| 146 |
false); |
| 147 |
bool atEnd () const |
| 148 |
{ |
| 149 |
} |
| 150 |
void advance (); |
| 151 |
int length () const |
| 152 |
{ |
| 153 |
} |
| 154 |
}; |
| 155 |
UChar * |
| 156 |
plainTextToMallocAllocatedBuffer (const Range * r, unsigned &bufferLength) |
| 157 |
{ |
| 158 |
static const unsigned cMaxSegmentSize = 1 << 16; |
| 159 |
typedef pair < UChar *, unsigned >TextSegment; |
| 160 |
Vector < TextSegment > *textSegments = 0; |
| 161 |
Vector < UChar > textBuffer; |
| 162 |
for (TextIterator it (r); !it.atEnd (); it.advance ()) |
| 163 |
{ |
| 164 |
if (textBuffer.size () |
| 165 |
&& textBuffer.size () + it.length () > cMaxSegmentSize) |
| 166 |
{ |
| 167 |
UChar *newSegmentBuffer = |
| 168 |
static_cast < |
| 169 |
UChar * >(malloc (textBuffer.size () * sizeof (UChar))); |
| 170 |
if (!textSegments) |
| 171 |
textSegments = new Vector < TextSegment >; |
| 172 |
textSegments-> |
| 173 |
append (make_pair (newSegmentBuffer, textBuffer.size ())); |
| 174 |
textBuffer.clear (); |
| 175 |
} |
| 176 |
} |
| 177 |
} |