vec.h 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  1. /* Vector API for GNU compiler.
  2. Copyright (C) 2004-2019 Free Software Foundation, Inc.
  3. Contributed by Nathan Sidwell <nathan@codesourcery.com>
  4. Re-implemented in C++ by Diego Novillo <dnovillo@google.com>
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #ifndef GCC_VEC_H
  18. #define GCC_VEC_H
  19. /* Some gen* file have no ggc support as the header file gtype-desc.h is
  20. missing. Provide these definitions in case ggc.h has not been included.
  21. This is not a problem because any code that runs before gengtype is built
  22. will never need to use GC vectors.*/
  23. extern void ggc_free (void *);
  24. extern size_t ggc_round_alloc_size (size_t requested_size);
  25. extern void *ggc_realloc (void *, size_t MEM_STAT_DECL);
  26. /* Templated vector type and associated interfaces.
  27. The interface functions are typesafe and use inline functions,
  28. sometimes backed by out-of-line generic functions. The vectors are
  29. designed to interoperate with the GTY machinery.
  30. There are both 'index' and 'iterate' accessors. The index accessor
  31. is implemented by operator[]. The iterator returns a boolean
  32. iteration condition and updates the iteration variable passed by
  33. reference. Because the iterator will be inlined, the address-of
  34. can be optimized away.
  35. Each operation that increases the number of active elements is
  36. available in 'quick' and 'safe' variants. The former presumes that
  37. there is sufficient allocated space for the operation to succeed
  38. (it dies if there is not). The latter will reallocate the
  39. vector, if needed. Reallocation causes an exponential increase in
  40. vector size. If you know you will be adding N elements, it would
  41. be more efficient to use the reserve operation before adding the
  42. elements with the 'quick' operation. This will ensure there are at
  43. least as many elements as you ask for, it will exponentially
  44. increase if there are too few spare slots. If you want reserve a
  45. specific number of slots, but do not want the exponential increase
  46. (for instance, you know this is the last allocation), use the
  47. reserve_exact operation. You can also create a vector of a
  48. specific size from the get go.
  49. You should prefer the push and pop operations, as they append and
  50. remove from the end of the vector. If you need to remove several
  51. items in one go, use the truncate operation. The insert and remove
  52. operations allow you to change elements in the middle of the
  53. vector. There are two remove operations, one which preserves the
  54. element ordering 'ordered_remove', and one which does not
  55. 'unordered_remove'. The latter function copies the end element
  56. into the removed slot, rather than invoke a memmove operation. The
  57. 'lower_bound' function will determine where to place an item in the
  58. array using insert that will maintain sorted order.
  59. Vectors are template types with three arguments: the type of the
  60. elements in the vector, the allocation strategy, and the physical
  61. layout to use
  62. Four allocation strategies are supported:
  63. - Heap: allocation is done using malloc/free. This is the
  64. default allocation strategy.
  65. - GC: allocation is done using ggc_alloc/ggc_free.
  66. - GC atomic: same as GC with the exception that the elements
  67. themselves are assumed to be of an atomic type that does
  68. not need to be garbage collected. This means that marking
  69. routines do not need to traverse the array marking the
  70. individual elements. This increases the performance of
  71. GC activities.
  72. Two physical layouts are supported:
  73. - Embedded: The vector is structured using the trailing array
  74. idiom. The last member of the structure is an array of size
  75. 1. When the vector is initially allocated, a single memory
  76. block is created to hold the vector's control data and the
  77. array of elements. These vectors cannot grow without
  78. reallocation (see discussion on embeddable vectors below).
  79. - Space efficient: The vector is structured as a pointer to an
  80. embedded vector. This is the default layout. It means that
  81. vectors occupy a single word of storage before initial
  82. allocation. Vectors are allowed to grow (the internal
  83. pointer is reallocated but the main vector instance does not
  84. need to relocate).
  85. The type, allocation and layout are specified when the vector is
  86. declared.
  87. If you need to directly manipulate a vector, then the 'address'
  88. accessor will return the address of the start of the vector. Also
  89. the 'space' predicate will tell you whether there is spare capacity
  90. in the vector. You will not normally need to use these two functions.
  91. Notes on the different layout strategies
  92. * Embeddable vectors (vec<T, A, vl_embed>)
  93. These vectors are suitable to be embedded in other data
  94. structures so that they can be pre-allocated in a contiguous
  95. memory block.
  96. Embeddable vectors are implemented using the trailing array
  97. idiom, thus they are not resizeable without changing the address
  98. of the vector object itself. This means you cannot have
  99. variables or fields of embeddable vector type -- always use a
  100. pointer to a vector. The one exception is the final field of a
  101. structure, which could be a vector type.
  102. You will have to use the embedded_size & embedded_init calls to
  103. create such objects, and they will not be resizeable (so the
  104. 'safe' allocation variants are not available).
  105. Properties of embeddable vectors:
  106. - The whole vector and control data are allocated in a single
  107. contiguous block. It uses the trailing-vector idiom, so
  108. allocation must reserve enough space for all the elements
  109. in the vector plus its control data.
  110. - The vector cannot be re-allocated.
  111. - The vector cannot grow nor shrink.
  112. - No indirections needed for access/manipulation.
  113. - It requires 2 words of storage (prior to vector allocation).
  114. * Space efficient vector (vec<T, A, vl_ptr>)
  115. These vectors can grow dynamically and are allocated together
  116. with their control data. They are suited to be included in data
  117. structures. Prior to initial allocation, they only take a single
  118. word of storage.
  119. These vectors are implemented as a pointer to embeddable vectors.
  120. The semantics allow for this pointer to be NULL to represent
  121. empty vectors. This way, empty vectors occupy minimal space in
  122. the structure containing them.
  123. Properties:
  124. - The whole vector and control data are allocated in a single
  125. contiguous block.
  126. - The whole vector may be re-allocated.
  127. - Vector data may grow and shrink.
  128. - Access and manipulation requires a pointer test and
  129. indirection.
  130. - It requires 1 word of storage (prior to vector allocation).
  131. An example of their use would be,
  132. struct my_struct {
  133. // A space-efficient vector of tree pointers in GC memory.
  134. vec<tree, va_gc, vl_ptr> v;
  135. };
  136. struct my_struct *s;
  137. if (s->v.length ()) { we have some contents }
  138. s->v.safe_push (decl); // append some decl onto the end
  139. for (ix = 0; s->v.iterate (ix, &elt); ix++)
  140. { do something with elt }
  141. */
  142. /* Support function for statistics. */
  143. extern void dump_vec_loc_statistics (void);
  144. /* Hashtable mapping vec addresses to descriptors. */
  145. extern htab_t vec_mem_usage_hash;
  146. /* Control data for vectors. This contains the number of allocated
  147. and used slots inside a vector. */
  148. struct vec_prefix
  149. {
  150. /* FIXME - These fields should be private, but we need to cater to
  151. compilers that have stricter notions of PODness for types. */
  152. /* Memory allocation support routines in vec.c. */
  153. void register_overhead (void *, size_t, size_t CXX_MEM_STAT_INFO);
  154. void release_overhead (void *, size_t, size_t, bool CXX_MEM_STAT_INFO);
  155. static unsigned calculate_allocation (vec_prefix *, unsigned, bool);
  156. static unsigned calculate_allocation_1 (unsigned, unsigned);
  157. /* Note that vec_prefix should be a base class for vec, but we use
  158. offsetof() on vector fields of tree structures (e.g.,
  159. tree_binfo::base_binfos), and offsetof only supports base types.
  160. To compensate, we make vec_prefix a field inside vec and make
  161. vec a friend class of vec_prefix so it can access its fields. */
  162. template <typename, typename, typename> friend struct vec;
  163. /* The allocator types also need access to our internals. */
  164. friend struct va_gc;
  165. friend struct va_gc_atomic;
  166. friend struct va_heap;
  167. unsigned m_alloc : 31;
  168. unsigned m_using_auto_storage : 1;
  169. unsigned m_num;
  170. };
  171. /* Calculate the number of slots to reserve a vector, making sure that
  172. RESERVE slots are free. If EXACT grow exactly, otherwise grow
  173. exponentially. PFX is the control data for the vector. */
  174. inline unsigned
  175. vec_prefix::calculate_allocation (vec_prefix *pfx, unsigned reserve,
  176. bool exact)
  177. {
  178. if (exact)
  179. return (pfx ? pfx->m_num : 0) + reserve;
  180. else if (!pfx)
  181. return MAX (4, reserve);
  182. return calculate_allocation_1 (pfx->m_alloc, pfx->m_num + reserve);
  183. }
  184. template<typename, typename, typename> struct vec;
  185. /* Valid vector layouts
  186. vl_embed - Embeddable vector that uses the trailing array idiom.
  187. vl_ptr - Space efficient vector that uses a pointer to an
  188. embeddable vector. */
  189. struct vl_embed { };
  190. struct vl_ptr { };
  191. /* Types of supported allocations
  192. va_heap - Allocation uses malloc/free.
  193. va_gc - Allocation uses ggc_alloc.
  194. va_gc_atomic - Same as GC, but individual elements of the array
  195. do not need to be marked during collection. */
  196. /* Allocator type for heap vectors. */
  197. struct va_heap
  198. {
  199. /* Heap vectors are frequently regular instances, so use the vl_ptr
  200. layout for them. */
  201. typedef vl_ptr default_layout;
  202. template<typename T>
  203. static void reserve (vec<T, va_heap, vl_embed> *&, unsigned, bool
  204. CXX_MEM_STAT_INFO);
  205. template<typename T>
  206. static void release (vec<T, va_heap, vl_embed> *&);
  207. };
  208. /* Allocator for heap memory. Ensure there are at least RESERVE free
  209. slots in V. If EXACT is true, grow exactly, else grow
  210. exponentially. As a special case, if the vector had not been
  211. allocated and RESERVE is 0, no vector will be created. */
  212. template<typename T>
  213. inline void
  214. va_heap::reserve (vec<T, va_heap, vl_embed> *&v, unsigned reserve, bool exact
  215. MEM_STAT_DECL)
  216. {
  217. size_t elt_size = sizeof (T);
  218. unsigned alloc
  219. = vec_prefix::calculate_allocation (v ? &v->m_vecpfx : 0, reserve, exact);
  220. gcc_checking_assert (alloc);
  221. if (GATHER_STATISTICS && v)
  222. v->m_vecpfx.release_overhead (v, elt_size * v->allocated (),
  223. v->allocated (), false);
  224. size_t size = vec<T, va_heap, vl_embed>::embedded_size (alloc);
  225. unsigned nelem = v ? v->length () : 0;
  226. v = static_cast <vec<T, va_heap, vl_embed> *> (xrealloc (v, size));
  227. v->embedded_init (alloc, nelem);
  228. if (GATHER_STATISTICS)
  229. v->m_vecpfx.register_overhead (v, alloc, elt_size PASS_MEM_STAT);
  230. }
  231. /* Free the heap space allocated for vector V. */
  232. template<typename T>
  233. void
  234. va_heap::release (vec<T, va_heap, vl_embed> *&v)
  235. {
  236. size_t elt_size = sizeof (T);
  237. if (v == NULL)
  238. return;
  239. if (GATHER_STATISTICS)
  240. v->m_vecpfx.release_overhead (v, elt_size * v->allocated (),
  241. v->allocated (), true);
  242. ::free (v);
  243. v = NULL;
  244. }
  245. /* Allocator type for GC vectors. Notice that we need the structure
  246. declaration even if GC is not enabled. */
  247. struct va_gc
  248. {
  249. /* Use vl_embed as the default layout for GC vectors. Due to GTY
  250. limitations, GC vectors must always be pointers, so it is more
  251. efficient to use a pointer to the vl_embed layout, rather than
  252. using a pointer to a pointer as would be the case with vl_ptr. */
  253. typedef vl_embed default_layout;
  254. template<typename T, typename A>
  255. static void reserve (vec<T, A, vl_embed> *&, unsigned, bool
  256. CXX_MEM_STAT_INFO);
  257. template<typename T, typename A>
  258. static void release (vec<T, A, vl_embed> *&v);
  259. };
  260. /* Free GC memory used by V and reset V to NULL. */
  261. template<typename T, typename A>
  262. inline void
  263. va_gc::release (vec<T, A, vl_embed> *&v)
  264. {
  265. if (v)
  266. ::ggc_free (v);
  267. v = NULL;
  268. }
  269. /* Allocator for GC memory. Ensure there are at least RESERVE free
  270. slots in V. If EXACT is true, grow exactly, else grow
  271. exponentially. As a special case, if the vector had not been
  272. allocated and RESERVE is 0, no vector will be created. */
  273. template<typename T, typename A>
  274. void
  275. va_gc::reserve (vec<T, A, vl_embed> *&v, unsigned reserve, bool exact
  276. MEM_STAT_DECL)
  277. {
  278. unsigned alloc
  279. = vec_prefix::calculate_allocation (v ? &v->m_vecpfx : 0, reserve, exact);
  280. if (!alloc)
  281. {
  282. ::ggc_free (v);
  283. v = NULL;
  284. return;
  285. }
  286. /* Calculate the amount of space we want. */
  287. size_t size = vec<T, A, vl_embed>::embedded_size (alloc);
  288. /* Ask the allocator how much space it will really give us. */
  289. size = ::ggc_round_alloc_size (size);
  290. /* Adjust the number of slots accordingly. */
  291. size_t vec_offset = sizeof (vec_prefix);
  292. size_t elt_size = sizeof (T);
  293. alloc = (size - vec_offset) / elt_size;
  294. /* And finally, recalculate the amount of space we ask for. */
  295. size = vec_offset + alloc * elt_size;
  296. unsigned nelem = v ? v->length () : 0;
  297. v = static_cast <vec<T, A, vl_embed> *> (::ggc_realloc (v, size
  298. PASS_MEM_STAT));
  299. v->embedded_init (alloc, nelem);
  300. }
  301. /* Allocator type for GC vectors. This is for vectors of types
  302. atomics w.r.t. collection, so allocation and deallocation is
  303. completely inherited from va_gc. */
  304. struct va_gc_atomic : va_gc
  305. {
  306. };
  307. /* Generic vector template. Default values for A and L indicate the
  308. most commonly used strategies.
  309. FIXME - Ideally, they would all be vl_ptr to encourage using regular
  310. instances for vectors, but the existing GTY machinery is limited
  311. in that it can only deal with GC objects that are pointers
  312. themselves.
  313. This means that vector operations that need to deal with
  314. potentially NULL pointers, must be provided as free
  315. functions (see the vec_safe_* functions above). */
  316. template<typename T,
  317. typename A = va_heap,
  318. typename L = typename A::default_layout>
  319. struct GTY((user)) vec
  320. {
  321. };
  322. /* Generic vec<> debug helpers.
  323. These need to be instantiated for each vec<TYPE> used throughout
  324. the compiler like this:
  325. DEFINE_DEBUG_VEC (TYPE)
  326. The reason we have a debug_helper() is because GDB can't
  327. disambiguate a plain call to debug(some_vec), and it must be called
  328. like debug<TYPE>(some_vec). */
  329. template<typename T>
  330. void
  331. debug_helper (vec<T> &ref)
  332. {
  333. unsigned i;
  334. for (i = 0; i < ref.length (); ++i)
  335. {
  336. fprintf (stderr, "[%d] = ", i);
  337. debug_slim (ref[i]);
  338. fputc ('\n', stderr);
  339. }
  340. }
  341. /* We need a separate va_gc variant here because default template
  342. argument for functions cannot be used in c++-98. Once this
  343. restriction is removed, those variant should be folded with the
  344. above debug_helper. */
  345. template<typename T>
  346. void
  347. debug_helper (vec<T, va_gc> &ref)
  348. {
  349. unsigned i;
  350. for (i = 0; i < ref.length (); ++i)
  351. {
  352. fprintf (stderr, "[%d] = ", i);
  353. debug_slim (ref[i]);
  354. fputc ('\n', stderr);
  355. }
  356. }
  357. /* Macro to define debug(vec<T>) and debug(vec<T, va_gc>) helper
  358. functions for a type T. */
  359. #define DEFINE_DEBUG_VEC(T) \
  360. template void debug_helper (vec<T> &); \
  361. template void debug_helper (vec<T, va_gc> &); \
  362. /* Define the vec<T> debug functions. */ \
  363. DEBUG_FUNCTION void \
  364. debug (vec<T> &ref) \
  365. { \
  366. debug_helper <T> (ref); \
  367. } \
  368. DEBUG_FUNCTION void \
  369. debug (vec<T> *ptr) \
  370. { \
  371. if (ptr) \
  372. debug (*ptr); \
  373. else \
  374. fprintf (stderr, "<nil>\n"); \
  375. } \
  376. /* Define the vec<T, va_gc> debug functions. */ \
  377. DEBUG_FUNCTION void \
  378. debug (vec<T, va_gc> &ref) \
  379. { \
  380. debug_helper <T> (ref); \
  381. } \
  382. DEBUG_FUNCTION void \
  383. debug (vec<T, va_gc> *ptr) \
  384. { \
  385. if (ptr) \
  386. debug (*ptr); \
  387. else \
  388. fprintf (stderr, "<nil>\n"); \
  389. }
  390. /* Default-construct N elements in DST. */
  391. template <typename T>
  392. inline void
  393. vec_default_construct (T *dst, unsigned n)
  394. {
  395. #ifdef BROKEN_VALUE_INITIALIZATION
  396. /* Versions of GCC before 4.4 sometimes leave certain objects
  397. uninitialized when value initialized, though if the type has
  398. user defined default ctor, that ctor is invoked. As a workaround
  399. perform clearing first and then the value initialization, which
  400. fixes the case when value initialization doesn't initialize due to
  401. the bugs and should initialize to all zeros, but still allows
  402. vectors for types with user defined default ctor that initializes
  403. some or all elements to non-zero. If T has no user defined
  404. default ctor and some non-static data members have user defined
  405. default ctors that initialize to non-zero the workaround will
  406. still not work properly; in that case we just need to provide
  407. user defined default ctor. */
  408. memset (dst, '\0', sizeof (T) * n);
  409. #endif
  410. for ( ; n; ++dst, --n)
  411. ::new (static_cast<void*>(dst)) T ();
  412. }
  413. /* Copy-construct N elements in DST from *SRC. */
  414. template <typename T>
  415. inline void
  416. vec_copy_construct (T *dst, const T *src, unsigned n)
  417. {
  418. for ( ; n; ++dst, ++src, --n)
  419. ::new (static_cast<void*>(dst)) T (*src);
  420. }
  421. /* Type to provide NULL values for vec<T, A, L>. This is used to
  422. provide nil initializers for vec instances. Since vec must be
  423. a POD, we cannot have proper ctor/dtor for it. To initialize
  424. a vec instance, you can assign it the value vNULL. This isn't
  425. needed for file-scope and function-local static vectors, which
  426. are zero-initialized by default. */
  427. struct vnull
  428. {
  429. template <typename T, typename A, typename L>
  430. CONSTEXPR operator vec<T, A, L> () { return vec<T, A, L>(); }
  431. };
  432. extern vnull vNULL;
  433. /* Embeddable vector. These vectors are suitable to be embedded
  434. in other data structures so that they can be pre-allocated in a
  435. contiguous memory block.
  436. Embeddable vectors are implemented using the trailing array idiom,
  437. thus they are not resizeable without changing the address of the
  438. vector object itself. This means you cannot have variables or
  439. fields of embeddable vector type -- always use a pointer to a
  440. vector. The one exception is the final field of a structure, which
  441. could be a vector type.
  442. You will have to use the embedded_size & embedded_init calls to
  443. create such objects, and they will not be resizeable (so the 'safe'
  444. allocation variants are not available).
  445. Properties:
  446. - The whole vector and control data are allocated in a single
  447. contiguous block. It uses the trailing-vector idiom, so
  448. allocation must reserve enough space for all the elements
  449. in the vector plus its control data.
  450. - The vector cannot be re-allocated.
  451. - The vector cannot grow nor shrink.
  452. - No indirections needed for access/manipulation.
  453. - It requires 2 words of storage (prior to vector allocation). */
  454. template<typename T, typename A>
  455. struct GTY((user)) vec<T, A, vl_embed>
  456. {
  457. public:
  458. unsigned allocated (void) const { return m_vecpfx.m_alloc; }
  459. unsigned length (void) const { return m_vecpfx.m_num; }
  460. bool is_empty (void) const { return m_vecpfx.m_num == 0; }
  461. T *address (void) { return m_vecdata; }
  462. const T *address (void) const { return m_vecdata; }
  463. T *begin () { return address (); }
  464. const T *begin () const { return address (); }
  465. T *end () { return address () + length (); }
  466. const T *end () const { return address () + length (); }
  467. const T &operator[] (unsigned) const;
  468. T &operator[] (unsigned);
  469. T &last (void);
  470. bool space (unsigned) const;
  471. bool iterate (unsigned, T *) const;
  472. bool iterate (unsigned, T **) const;
  473. vec *copy (ALONE_CXX_MEM_STAT_INFO) const;
  474. void splice (const vec &);
  475. void splice (const vec *src);
  476. T *quick_push (const T &);
  477. T &pop (void);
  478. void truncate (unsigned);
  479. void quick_insert (unsigned, const T &);
  480. void ordered_remove (unsigned);
  481. void unordered_remove (unsigned);
  482. void block_remove (unsigned, unsigned);
  483. void qsort (int (*) (const void *, const void *));
  484. T *bsearch (const void *key, int (*compar)(const void *, const void *));
  485. unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
  486. bool contains (const T &search) const;
  487. static size_t embedded_size (unsigned);
  488. void embedded_init (unsigned, unsigned = 0, unsigned = 0);
  489. void quick_grow (unsigned len);
  490. void quick_grow_cleared (unsigned len);
  491. /* vec class can access our internal data and functions. */
  492. template <typename, typename, typename> friend struct vec;
  493. /* The allocator types also need access to our internals. */
  494. friend struct va_gc;
  495. friend struct va_gc_atomic;
  496. friend struct va_heap;
  497. /* FIXME - These fields should be private, but we need to cater to
  498. compilers that have stricter notions of PODness for types. */
  499. vec_prefix m_vecpfx;
  500. T m_vecdata[1];
  501. };
  502. /* Convenience wrapper functions to use when dealing with pointers to
  503. embedded vectors. Some functionality for these vectors must be
  504. provided via free functions for these reasons:
  505. 1- The pointer may be NULL (e.g., before initial allocation).
  506. 2- When the vector needs to grow, it must be reallocated, so
  507. the pointer will change its value.
  508. Because of limitations with the current GC machinery, all vectors
  509. in GC memory *must* be pointers. */
  510. /* If V contains no room for NELEMS elements, return false. Otherwise,
  511. return true. */
  512. template<typename T, typename A>
  513. inline bool
  514. vec_safe_space (const vec<T, A, vl_embed> *v, unsigned nelems)
  515. {
  516. return v ? v->space (nelems) : nelems == 0;
  517. }
  518. /* If V is NULL, return 0. Otherwise, return V->length(). */
  519. template<typename T, typename A>
  520. inline unsigned
  521. vec_safe_length (const vec<T, A, vl_embed> *v)
  522. {
  523. return v ? v->length () : 0;
  524. }
  525. /* If V is NULL, return NULL. Otherwise, return V->address(). */
  526. template<typename T, typename A>
  527. inline T *
  528. vec_safe_address (vec<T, A, vl_embed> *v)
  529. {
  530. return v ? v->address () : NULL;
  531. }
  532. /* If V is NULL, return true. Otherwise, return V->is_empty(). */
  533. template<typename T, typename A>
  534. inline bool
  535. vec_safe_is_empty (vec<T, A, vl_embed> *v)
  536. {
  537. return v ? v->is_empty () : true;
  538. }
  539. /* If V does not have space for NELEMS elements, call
  540. V->reserve(NELEMS, EXACT). */
  541. template<typename T, typename A>
  542. inline bool
  543. vec_safe_reserve (vec<T, A, vl_embed> *&v, unsigned nelems, bool exact = false
  544. CXX_MEM_STAT_INFO)
  545. {
  546. bool extend = nelems ? !vec_safe_space (v, nelems) : false;
  547. if (extend)
  548. A::reserve (v, nelems, exact PASS_MEM_STAT);
  549. return extend;
  550. }
  551. template<typename T, typename A>
  552. inline bool
  553. vec_safe_reserve_exact (vec<T, A, vl_embed> *&v, unsigned nelems
  554. CXX_MEM_STAT_INFO)
  555. {
  556. return vec_safe_reserve (v, nelems, true PASS_MEM_STAT);
  557. }
  558. /* Allocate GC memory for V with space for NELEMS slots. If NELEMS
  559. is 0, V is initialized to NULL. */
  560. template<typename T, typename A>
  561. inline void
  562. vec_alloc (vec<T, A, vl_embed> *&v, unsigned nelems CXX_MEM_STAT_INFO)
  563. {
  564. v = NULL;
  565. vec_safe_reserve (v, nelems, false PASS_MEM_STAT);
  566. }
  567. /* Free the GC memory allocated by vector V and set it to NULL. */
  568. template<typename T, typename A>
  569. inline void
  570. vec_free (vec<T, A, vl_embed> *&v)
  571. {
  572. A::release (v);
  573. }
  574. /* Grow V to length LEN. Allocate it, if necessary. */
  575. template<typename T, typename A>
  576. inline void
  577. vec_safe_grow (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO)
  578. {
  579. unsigned oldlen = vec_safe_length (v);
  580. gcc_checking_assert (len >= oldlen);
  581. vec_safe_reserve_exact (v, len - oldlen PASS_MEM_STAT);
  582. v->quick_grow (len);
  583. }
  584. /* If V is NULL, allocate it. Call V->safe_grow_cleared(LEN). */
  585. template<typename T, typename A>
  586. inline void
  587. vec_safe_grow_cleared (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO)
  588. {
  589. unsigned oldlen = vec_safe_length (v);
  590. vec_safe_grow (v, len PASS_MEM_STAT);
  591. vec_default_construct (v->address () + oldlen, len - oldlen);
  592. }
  593. /* Assume V is not NULL. */
  594. template<typename T>
  595. inline void
  596. vec_safe_grow_cleared (vec<T, va_heap, vl_ptr> *&v,
  597. unsigned len CXX_MEM_STAT_INFO)
  598. {
  599. v->safe_grow_cleared (len PASS_MEM_STAT);
  600. }
  601. /* If V is NULL return false, otherwise return V->iterate(IX, PTR). */
  602. template<typename T, typename A>
  603. inline bool
  604. vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T **ptr)
  605. {
  606. if (v)
  607. return v->iterate (ix, ptr);
  608. else
  609. {
  610. *ptr = 0;
  611. return false;
  612. }
  613. }
  614. template<typename T, typename A>
  615. inline bool
  616. vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T *ptr)
  617. {
  618. if (v)
  619. return v->iterate (ix, ptr);
  620. else
  621. {
  622. *ptr = 0;
  623. return false;
  624. }
  625. }
  626. /* If V has no room for one more element, reallocate it. Then call
  627. V->quick_push(OBJ). */
  628. template<typename T, typename A>
  629. inline T *
  630. vec_safe_push (vec<T, A, vl_embed> *&v, const T &obj CXX_MEM_STAT_INFO)
  631. {
  632. vec_safe_reserve (v, 1, false PASS_MEM_STAT);
  633. return v->quick_push (obj);
  634. }
  635. /* if V has no room for one more element, reallocate it. Then call
  636. V->quick_insert(IX, OBJ). */
  637. template<typename T, typename A>
  638. inline void
  639. vec_safe_insert (vec<T, A, vl_embed> *&v, unsigned ix, const T &obj
  640. CXX_MEM_STAT_INFO)
  641. {
  642. vec_safe_reserve (v, 1, false PASS_MEM_STAT);
  643. v->quick_insert (ix, obj);
  644. }
  645. /* If V is NULL, do nothing. Otherwise, call V->truncate(SIZE). */
  646. template<typename T, typename A>
  647. inline void
  648. vec_safe_truncate (vec<T, A, vl_embed> *v, unsigned size)
  649. {
  650. if (v)
  651. v->truncate (size);
  652. }
  653. /* If SRC is not NULL, return a pointer to a copy of it. */
  654. template<typename T, typename A>
  655. inline vec<T, A, vl_embed> *
  656. vec_safe_copy (vec<T, A, vl_embed> *src CXX_MEM_STAT_INFO)
  657. {
  658. return src ? src->copy (ALONE_PASS_MEM_STAT) : NULL;
  659. }
  660. /* Copy the elements from SRC to the end of DST as if by memcpy.
  661. Reallocate DST, if necessary. */
  662. template<typename T, typename A>
  663. inline void
  664. vec_safe_splice (vec<T, A, vl_embed> *&dst, const vec<T, A, vl_embed> *src
  665. CXX_MEM_STAT_INFO)
  666. {
  667. unsigned src_len = vec_safe_length (src);
  668. if (src_len)
  669. {
  670. vec_safe_reserve_exact (dst, vec_safe_length (dst) + src_len
  671. PASS_MEM_STAT);
  672. dst->splice (*src);
  673. }
  674. }
  675. /* Return true if SEARCH is an element of V. Note that this is O(N) in the
  676. size of the vector and so should be used with care. */
  677. template<typename T, typename A>
  678. inline bool
  679. vec_safe_contains (vec<T, A, vl_embed> *v, const T &search)
  680. {
  681. return v ? v->contains (search) : false;
  682. }
  683. /* Index into vector. Return the IX'th element. IX must be in the
  684. domain of the vector. */
  685. template<typename T, typename A>
  686. inline const T &
  687. vec<T, A, vl_embed>::operator[] (unsigned ix) const
  688. {
  689. gcc_checking_assert (ix < m_vecpfx.m_num);
  690. return m_vecdata[ix];
  691. }
  692. template<typename T, typename A>
  693. inline T &
  694. vec<T, A, vl_embed>::operator[] (unsigned ix)
  695. {
  696. gcc_checking_assert (ix < m_vecpfx.m_num);
  697. return m_vecdata[ix];
  698. }
  699. /* Get the final element of the vector, which must not be empty. */
  700. template<typename T, typename A>
  701. inline T &
  702. vec<T, A, vl_embed>::last (void)
  703. {
  704. gcc_checking_assert (m_vecpfx.m_num > 0);
  705. return (*this)[m_vecpfx.m_num - 1];
  706. }
  707. /* If this vector has space for NELEMS additional entries, return
  708. true. You usually only need to use this if you are doing your
  709. own vector reallocation, for instance on an embedded vector. This
  710. returns true in exactly the same circumstances that vec::reserve
  711. will. */
  712. template<typename T, typename A>
  713. inline bool
  714. vec<T, A, vl_embed>::space (unsigned nelems) const
  715. {
  716. return m_vecpfx.m_alloc - m_vecpfx.m_num >= nelems;
  717. }
  718. /* Return iteration condition and update PTR to point to the IX'th
  719. element of this vector. Use this to iterate over the elements of a
  720. vector as follows,
  721. for (ix = 0; vec<T, A>::iterate (v, ix, &ptr); ix++)
  722. continue; */
  723. template<typename T, typename A>
  724. inline bool
  725. vec<T, A, vl_embed>::iterate (unsigned ix, T *ptr) const
  726. {
  727. if (ix < m_vecpfx.m_num)
  728. {
  729. *ptr = m_vecdata[ix];
  730. return true;
  731. }
  732. else
  733. {
  734. *ptr = 0;
  735. return false;
  736. }
  737. }
  738. /* Return iteration condition and update *PTR to point to the
  739. IX'th element of this vector. Use this to iterate over the
  740. elements of a vector as follows,
  741. for (ix = 0; v->iterate (ix, &ptr); ix++)
  742. continue;
  743. This variant is for vectors of objects. */
  744. template<typename T, typename A>
  745. inline bool
  746. vec<T, A, vl_embed>::iterate (unsigned ix, T **ptr) const
  747. {
  748. if (ix < m_vecpfx.m_num)
  749. {
  750. *ptr = CONST_CAST (T *, &m_vecdata[ix]);
  751. return true;
  752. }
  753. else
  754. {
  755. *ptr = 0;
  756. return false;
  757. }
  758. }
  759. /* Return a pointer to a copy of this vector. */
  760. template<typename T, typename A>
  761. inline vec<T, A, vl_embed> *
  762. vec<T, A, vl_embed>::copy (ALONE_MEM_STAT_DECL) const
  763. {
  764. vec<T, A, vl_embed> *new_vec = NULL;
  765. unsigned len = length ();
  766. if (len)
  767. {
  768. vec_alloc (new_vec, len PASS_MEM_STAT);
  769. new_vec->embedded_init (len, len);
  770. vec_copy_construct (new_vec->address (), m_vecdata, len);
  771. }
  772. return new_vec;
  773. }
  774. /* Copy the elements from SRC to the end of this vector as if by memcpy.
  775. The vector must have sufficient headroom available. */
  776. template<typename T, typename A>
  777. inline void
  778. vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> &src)
  779. {
  780. unsigned len = src.length ();
  781. if (len)
  782. {
  783. gcc_checking_assert (space (len));
  784. vec_copy_construct (end (), src.address (), len);
  785. m_vecpfx.m_num += len;
  786. }
  787. }
  788. template<typename T, typename A>
  789. inline void
  790. vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> *src)
  791. {
  792. if (src)
  793. splice (*src);
  794. }
  795. /* Push OBJ (a new element) onto the end of the vector. There must be
  796. sufficient space in the vector. Return a pointer to the slot
  797. where OBJ was inserted. */
  798. template<typename T, typename A>
  799. inline T *
  800. vec<T, A, vl_embed>::quick_push (const T &obj)
  801. {
  802. gcc_checking_assert (space (1));
  803. T *slot = &m_vecdata[m_vecpfx.m_num++];
  804. *slot = obj;
  805. return slot;
  806. }
  807. /* Pop and return the last element off the end of the vector. */
  808. template<typename T, typename A>
  809. inline T &
  810. vec<T, A, vl_embed>::pop (void)
  811. {
  812. gcc_checking_assert (length () > 0);
  813. return m_vecdata[--m_vecpfx.m_num];
  814. }
  815. /* Set the length of the vector to SIZE. The new length must be less
  816. than or equal to the current length. This is an O(1) operation. */
  817. template<typename T, typename A>
  818. inline void
  819. vec<T, A, vl_embed>::truncate (unsigned size)
  820. {
  821. gcc_checking_assert (length () >= size);
  822. m_vecpfx.m_num = size;
  823. }
  824. /* Insert an element, OBJ, at the IXth position of this vector. There
  825. must be sufficient space. */
  826. template<typename T, typename A>
  827. inline void
  828. vec<T, A, vl_embed>::quick_insert (unsigned ix, const T &obj)
  829. {
  830. gcc_checking_assert (length () < allocated ());
  831. gcc_checking_assert (ix <= length ());
  832. T *slot = &m_vecdata[ix];
  833. memmove (slot + 1, slot, (m_vecpfx.m_num++ - ix) * sizeof (T));
  834. *slot = obj;
  835. }
  836. /* Remove an element from the IXth position of this vector. Ordering of
  837. remaining elements is preserved. This is an O(N) operation due to
  838. memmove. */
  839. template<typename T, typename A>
  840. inline void
  841. vec<T, A, vl_embed>::ordered_remove (unsigned ix)
  842. {
  843. gcc_checking_assert (ix < length ());
  844. T *slot = &m_vecdata[ix];
  845. memmove (slot, slot + 1, (--m_vecpfx.m_num - ix) * sizeof (T));
  846. }
  847. /* Remove elements in [START, END) from VEC for which COND holds. Ordering of
  848. remaining elements is preserved. This is an O(N) operation. */
  849. #define VEC_ORDERED_REMOVE_IF_FROM_TO(vec, read_index, write_index, \
  850. elem_ptr, start, end, cond) \
  851. { \
  852. gcc_assert ((end) <= (vec).length ()); \
  853. for (read_index = write_index = (start); read_index < (end); \
  854. ++read_index) \
  855. { \
  856. elem_ptr = &(vec)[read_index]; \
  857. bool remove_p = (cond); \
  858. if (remove_p) \
  859. continue; \
  860. \
  861. if (read_index != write_index) \
  862. (vec)[write_index] = (vec)[read_index]; \
  863. \
  864. write_index++; \
  865. } \
  866. \
  867. if (read_index - write_index > 0) \
  868. (vec).block_remove (write_index, read_index - write_index); \
  869. }
  870. /* Remove elements from VEC for which COND holds. Ordering of remaining
  871. elements is preserved. This is an O(N) operation. */
  872. #define VEC_ORDERED_REMOVE_IF(vec, read_index, write_index, elem_ptr, \
  873. cond) \
  874. VEC_ORDERED_REMOVE_IF_FROM_TO ((vec), read_index, write_index, \
  875. elem_ptr, 0, (vec).length (), (cond))
  876. /* Remove an element from the IXth position of this vector. Ordering of
  877. remaining elements is destroyed. This is an O(1) operation. */
  878. template<typename T, typename A>
  879. inline void
  880. vec<T, A, vl_embed>::unordered_remove (unsigned ix)
  881. {
  882. gcc_checking_assert (ix < length ());
  883. m_vecdata[ix] = m_vecdata[--m_vecpfx.m_num];
  884. }
  885. /* Remove LEN elements starting at the IXth. Ordering is retained.
  886. This is an O(N) operation due to memmove. */
  887. template<typename T, typename A>
  888. inline void
  889. vec<T, A, vl_embed>::block_remove (unsigned ix, unsigned len)
  890. {
  891. gcc_checking_assert (ix + len <= length ());
  892. T *slot = &m_vecdata[ix];
  893. m_vecpfx.m_num -= len;
  894. memmove (slot, slot + len, (m_vecpfx.m_num - ix) * sizeof (T));
  895. }
  896. /* Sort the contents of this vector with qsort. CMP is the comparison
  897. function to pass to qsort. */
  898. template<typename T, typename A>
  899. inline void
  900. vec<T, A, vl_embed>::qsort (int (*cmp) (const void *, const void *))
  901. {
  902. if (length () > 1)
  903. ::qsort (address (), length (), sizeof (T), cmp);
  904. }
  905. /* Search the contents of the sorted vector with a binary search.
  906. CMP is the comparison function to pass to bsearch. */
  907. template<typename T, typename A>
  908. inline T *
  909. vec<T, A, vl_embed>::bsearch (const void *key,
  910. int (*compar) (const void *, const void *))
  911. {
  912. const void *base = this->address ();
  913. size_t nmemb = this->length ();
  914. size_t size = sizeof (T);
  915. /* The following is a copy of glibc stdlib-bsearch.h. */
  916. size_t l, u, idx;
  917. const void *p;
  918. int comparison;
  919. l = 0;
  920. u = nmemb;
  921. while (l < u)
  922. {
  923. idx = (l + u) / 2;
  924. p = (const void *) (((const char *) base) + (idx * size));
  925. comparison = (*compar) (key, p);
  926. if (comparison < 0)
  927. u = idx;
  928. else if (comparison > 0)
  929. l = idx + 1;
  930. else
  931. return (T *)const_cast<void *>(p);
  932. }
  933. return NULL;
  934. }
  935. /* Return true if SEARCH is an element of V. Note that this is O(N) in the
  936. size of the vector and so should be used with care. */
  937. template<typename T, typename A>
  938. inline bool
  939. vec<T, A, vl_embed>::contains (const T &search) const
  940. {
  941. unsigned int len = length ();
  942. for (unsigned int i = 0; i < len; i++)
  943. if ((*this)[i] == search)
  944. return true;
  945. return false;
  946. }
  947. /* Find and return the first position in which OBJ could be inserted
  948. without changing the ordering of this vector. LESSTHAN is a
  949. function that returns true if the first argument is strictly less
  950. than the second. */
  951. template<typename T, typename A>
  952. unsigned
  953. vec<T, A, vl_embed>::lower_bound (T obj, bool (*lessthan)(const T &, const T &))
  954. const
  955. {
  956. unsigned int len = length ();
  957. unsigned int half, middle;
  958. unsigned int first = 0;
  959. while (len > 0)
  960. {
  961. half = len / 2;
  962. middle = first;
  963. middle += half;
  964. T middle_elem = (*this)[middle];
  965. if (lessthan (middle_elem, obj))
  966. {
  967. first = middle;
  968. ++first;
  969. len = len - half - 1;
  970. }
  971. else
  972. len = half;
  973. }
  974. return first;
  975. }
  976. /* Return the number of bytes needed to embed an instance of an
  977. embeddable vec inside another data structure.
  978. Use these methods to determine the required size and initialization
  979. of a vector V of type T embedded within another structure (as the
  980. final member):
  981. size_t vec<T, A, vl_embed>::embedded_size (unsigned alloc);
  982. void v->embedded_init (unsigned alloc, unsigned num);
  983. These allow the caller to perform the memory allocation. */
  984. template<typename T, typename A>
  985. inline size_t
  986. vec<T, A, vl_embed>::embedded_size (unsigned alloc)
  987. {
  988. typedef vec<T, A, vl_embed> vec_embedded;
  989. return offsetof (vec_embedded, m_vecdata) + alloc * sizeof (T);
  990. }
  991. /* Initialize the vector to contain room for ALLOC elements and
  992. NUM active elements. */
  993. template<typename T, typename A>
  994. inline void
  995. vec<T, A, vl_embed>::embedded_init (unsigned alloc, unsigned num, unsigned aut)
  996. {
  997. m_vecpfx.m_alloc = alloc;
  998. m_vecpfx.m_using_auto_storage = aut;
  999. m_vecpfx.m_num = num;
  1000. }
  1001. /* Grow the vector to a specific length. LEN must be as long or longer than
  1002. the current length. The new elements are uninitialized. */
  1003. template<typename T, typename A>
  1004. inline void
  1005. vec<T, A, vl_embed>::quick_grow (unsigned len)
  1006. {
  1007. gcc_checking_assert (length () <= len && len <= m_vecpfx.m_alloc);
  1008. m_vecpfx.m_num = len;
  1009. }
  1010. /* Grow the vector to a specific length. LEN must be as long or longer than
  1011. the current length. The new elements are initialized to zero. */
  1012. template<typename T, typename A>
  1013. inline void
  1014. vec<T, A, vl_embed>::quick_grow_cleared (unsigned len)
  1015. {
  1016. unsigned oldlen = length ();
  1017. size_t growby = len - oldlen;
  1018. quick_grow (len);
  1019. if (growby != 0)
  1020. vec_default_construct (address () + oldlen, growby);
  1021. }
  1022. /* Garbage collection support for vec<T, A, vl_embed>. */
  1023. template<typename T>
  1024. void
  1025. gt_ggc_mx (vec<T, va_gc> *v)
  1026. {
  1027. extern void gt_ggc_mx (T &);
  1028. for (unsigned i = 0; i < v->length (); i++)
  1029. gt_ggc_mx ((*v)[i]);
  1030. }
  1031. template<typename T>
  1032. void
  1033. gt_ggc_mx (vec<T, va_gc_atomic, vl_embed> *v ATTRIBUTE_UNUSED)
  1034. {
  1035. /* Nothing to do. Vectors of atomic types wrt GC do not need to
  1036. be traversed. */
  1037. }
  1038. /* PCH support for vec<T, A, vl_embed>. */
  1039. template<typename T, typename A>
  1040. void
  1041. gt_pch_nx (vec<T, A, vl_embed> *v)
  1042. {
  1043. extern void gt_pch_nx (T &);
  1044. for (unsigned i = 0; i < v->length (); i++)
  1045. gt_pch_nx ((*v)[i]);
  1046. }
  1047. template<typename T, typename A>
  1048. void
  1049. gt_pch_nx (vec<T *, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
  1050. {
  1051. for (unsigned i = 0; i < v->length (); i++)
  1052. op (&((*v)[i]), cookie);
  1053. }
  1054. template<typename T, typename A>
  1055. void
  1056. gt_pch_nx (vec<T, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
  1057. {
  1058. extern void gt_pch_nx (T *, gt_pointer_operator, void *);
  1059. for (unsigned i = 0; i < v->length (); i++)
  1060. gt_pch_nx (&((*v)[i]), op, cookie);
  1061. }
  1062. /* Space efficient vector. These vectors can grow dynamically and are
  1063. allocated together with their control data. They are suited to be
  1064. included in data structures. Prior to initial allocation, they
  1065. only take a single word of storage.
  1066. These vectors are implemented as a pointer to an embeddable vector.
  1067. The semantics allow for this pointer to be NULL to represent empty
  1068. vectors. This way, empty vectors occupy minimal space in the
  1069. structure containing them.
  1070. Properties:
  1071. - The whole vector and control data are allocated in a single
  1072. contiguous block.
  1073. - The whole vector may be re-allocated.
  1074. - Vector data may grow and shrink.
  1075. - Access and manipulation requires a pointer test and
  1076. indirection.
  1077. - It requires 1 word of storage (prior to vector allocation).
  1078. Limitations:
  1079. These vectors must be PODs because they are stored in unions.
  1080. (http://en.wikipedia.org/wiki/Plain_old_data_structures).
  1081. As long as we use C++03, we cannot have constructors nor
  1082. destructors in classes that are stored in unions. */
  1083. template<typename T>
  1084. struct vec<T, va_heap, vl_ptr>
  1085. {
  1086. public:
  1087. /* Memory allocation and deallocation for the embedded vector.
  1088. Needed because we cannot have proper ctors/dtors defined. */
  1089. void create (unsigned nelems CXX_MEM_STAT_INFO);
  1090. void release (void);
  1091. /* Vector operations. */
  1092. bool exists (void) const
  1093. { return m_vec != NULL; }
  1094. bool is_empty (void) const
  1095. { return m_vec ? m_vec->is_empty () : true; }
  1096. unsigned length (void) const
  1097. { return m_vec ? m_vec->length () : 0; }
  1098. T *address (void)
  1099. { return m_vec ? m_vec->m_vecdata : NULL; }
  1100. const T *address (void) const
  1101. { return m_vec ? m_vec->m_vecdata : NULL; }
  1102. T *begin () { return address (); }
  1103. const T *begin () const { return address (); }
  1104. T *end () { return begin () + length (); }
  1105. const T *end () const { return begin () + length (); }
  1106. const T &operator[] (unsigned ix) const
  1107. { return (*m_vec)[ix]; }
  1108. bool operator!=(const vec &other) const
  1109. { return !(*this == other); }
  1110. bool operator==(const vec &other) const
  1111. { return address () == other.address (); }
  1112. T &operator[] (unsigned ix)
  1113. { return (*m_vec)[ix]; }
  1114. T &last (void)
  1115. { return m_vec->last (); }
  1116. bool space (int nelems) const
  1117. { return m_vec ? m_vec->space (nelems) : nelems == 0; }
  1118. bool iterate (unsigned ix, T *p) const;
  1119. bool iterate (unsigned ix, T **p) const;
  1120. vec copy (ALONE_CXX_MEM_STAT_INFO) const;
  1121. bool reserve (unsigned, bool = false CXX_MEM_STAT_INFO);
  1122. bool reserve_exact (unsigned CXX_MEM_STAT_INFO);
  1123. void splice (const vec &);
  1124. void safe_splice (const vec & CXX_MEM_STAT_INFO);
  1125. T *quick_push (const T &);
  1126. T *safe_push (const T &CXX_MEM_STAT_INFO);
  1127. T &pop (void);
  1128. void truncate (unsigned);
  1129. void safe_grow (unsigned CXX_MEM_STAT_INFO);
  1130. void safe_grow_cleared (unsigned CXX_MEM_STAT_INFO);
  1131. void quick_grow (unsigned);
  1132. void quick_grow_cleared (unsigned);
  1133. void quick_insert (unsigned, const T &);
  1134. void safe_insert (unsigned, const T & CXX_MEM_STAT_INFO);
  1135. void ordered_remove (unsigned);
  1136. void unordered_remove (unsigned);
  1137. void block_remove (unsigned, unsigned);
  1138. void qsort (int (*) (const void *, const void *));
  1139. T *bsearch (const void *key, int (*compar)(const void *, const void *));
  1140. unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
  1141. bool contains (const T &search) const;
  1142. void reverse (void);
  1143. bool using_auto_storage () const;
  1144. /* FIXME - This field should be private, but we need to cater to
  1145. compilers that have stricter notions of PODness for types. */
  1146. vec<T, va_heap, vl_embed> *m_vec;
  1147. };
  1148. /* auto_vec is a subclass of vec that automatically manages creating and
  1149. releasing the internal vector. If N is non zero then it has N elements of
  1150. internal storage. The default is no internal storage, and you probably only
  1151. want to ask for internal storage for vectors on the stack because if the
  1152. size of the vector is larger than the internal storage that space is wasted.
  1153. */
  1154. template<typename T, size_t N = 0>
  1155. class auto_vec : public vec<T, va_heap>
  1156. {
  1157. public:
  1158. auto_vec ()
  1159. {
  1160. m_auto.embedded_init (MAX (N, 2), 0, 1);
  1161. this->m_vec = &m_auto;
  1162. }
  1163. auto_vec (size_t s)
  1164. {
  1165. if (s > N)
  1166. {
  1167. this->create (s);
  1168. return;
  1169. }
  1170. m_auto.embedded_init (MAX (N, 2), 0, 1);
  1171. this->m_vec = &m_auto;
  1172. }
  1173. ~auto_vec ()
  1174. {
  1175. this->release ();
  1176. }
  1177. private:
  1178. vec<T, va_heap, vl_embed> m_auto;
  1179. T m_data[MAX (N - 1, 1)];
  1180. };
  1181. /* auto_vec is a sub class of vec whose storage is released when it is
  1182. destroyed. */
  1183. template<typename T>
  1184. class auto_vec<T, 0> : public vec<T, va_heap>
  1185. {
  1186. public:
  1187. auto_vec () { this->m_vec = NULL; }
  1188. auto_vec (size_t n) { this->create (n); }
  1189. ~auto_vec () { this->release (); }
  1190. };
  1191. /* Allocate heap memory for pointer V and create the internal vector
  1192. with space for NELEMS elements. If NELEMS is 0, the internal
  1193. vector is initialized to empty. */
  1194. template<typename T>
  1195. inline void
  1196. vec_alloc (vec<T> *&v, unsigned nelems CXX_MEM_STAT_INFO)
  1197. {
  1198. v = new vec<T>;
  1199. v->create (nelems PASS_MEM_STAT);
  1200. }
  1201. /* A subclass of auto_vec <char *> that frees all of its elements on
  1202. deletion. */
  1203. class auto_string_vec : public auto_vec <char *>
  1204. {
  1205. public:
  1206. ~auto_string_vec ();
  1207. };
  1208. /* Conditionally allocate heap memory for VEC and its internal vector. */
  1209. template<typename T>
  1210. inline void
  1211. vec_check_alloc (vec<T, va_heap> *&vec, unsigned nelems CXX_MEM_STAT_INFO)
  1212. {
  1213. if (!vec)
  1214. vec_alloc (vec, nelems PASS_MEM_STAT);
  1215. }
  1216. /* Free the heap memory allocated by vector V and set it to NULL. */
  1217. template<typename T>
  1218. inline void
  1219. vec_free (vec<T> *&v)
  1220. {
  1221. if (v == NULL)
  1222. return;
  1223. v->release ();
  1224. delete v;
  1225. v = NULL;
  1226. }
  1227. /* Return iteration condition and update PTR to point to the IX'th
  1228. element of this vector. Use this to iterate over the elements of a
  1229. vector as follows,
  1230. for (ix = 0; v.iterate (ix, &ptr); ix++)
  1231. continue; */
  1232. template<typename T>
  1233. inline bool
  1234. vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T *ptr) const
  1235. {
  1236. if (m_vec)
  1237. return m_vec->iterate (ix, ptr);
  1238. else
  1239. {
  1240. *ptr = 0;
  1241. return false;
  1242. }
  1243. }
  1244. /* Return iteration condition and update *PTR to point to the
  1245. IX'th element of this vector. Use this to iterate over the
  1246. elements of a vector as follows,
  1247. for (ix = 0; v->iterate (ix, &ptr); ix++)
  1248. continue;
  1249. This variant is for vectors of objects. */
  1250. template<typename T>
  1251. inline bool
  1252. vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T **ptr) const
  1253. {
  1254. if (m_vec)
  1255. return m_vec->iterate (ix, ptr);
  1256. else
  1257. {
  1258. *ptr = 0;
  1259. return false;
  1260. }
  1261. }
  1262. /* Convenience macro for forward iteration. */
  1263. #define FOR_EACH_VEC_ELT(V, I, P) \
  1264. for (I = 0; (V).iterate ((I), &(P)); ++(I))
  1265. #define FOR_EACH_VEC_SAFE_ELT(V, I, P) \
  1266. for (I = 0; vec_safe_iterate ((V), (I), &(P)); ++(I))
  1267. /* Likewise, but start from FROM rather than 0. */
  1268. #define FOR_EACH_VEC_ELT_FROM(V, I, P, FROM) \
  1269. for (I = (FROM); (V).iterate ((I), &(P)); ++(I))
  1270. /* Convenience macro for reverse iteration. */
  1271. #define FOR_EACH_VEC_ELT_REVERSE(V, I, P) \
  1272. for (I = (V).length () - 1; \
  1273. (V).iterate ((I), &(P)); \
  1274. (I)--)
  1275. #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P) \
  1276. for (I = vec_safe_length (V) - 1; \
  1277. vec_safe_iterate ((V), (I), &(P)); \
  1278. (I)--)
  1279. /* auto_string_vec's dtor, freeing all contained strings, automatically
  1280. chaining up to ~auto_vec <char *>, which frees the internal buffer. */
  1281. inline
  1282. auto_string_vec::~auto_string_vec ()
  1283. {
  1284. int i;
  1285. char *str;
  1286. FOR_EACH_VEC_ELT (*this, i, str)
  1287. free (str);
  1288. }
  1289. /* Return a copy of this vector. */
  1290. template<typename T>
  1291. inline vec<T, va_heap, vl_ptr>
  1292. vec<T, va_heap, vl_ptr>::copy (ALONE_MEM_STAT_DECL) const
  1293. {
  1294. vec<T, va_heap, vl_ptr> new_vec = vNULL;
  1295. if (length ())
  1296. new_vec.m_vec = m_vec->copy ();
  1297. return new_vec;
  1298. }
  1299. /* Ensure that the vector has at least RESERVE slots available (if
  1300. EXACT is false), or exactly RESERVE slots available (if EXACT is
  1301. true).
  1302. This may create additional headroom if EXACT is false.
  1303. Note that this can cause the embedded vector to be reallocated.
  1304. Returns true iff reallocation actually occurred. */
  1305. template<typename T>
  1306. inline bool
  1307. vec<T, va_heap, vl_ptr>::reserve (unsigned nelems, bool exact MEM_STAT_DECL)
  1308. {
  1309. if (space (nelems))
  1310. return false;
  1311. /* For now play a game with va_heap::reserve to hide our auto storage if any,
  1312. this is necessary because it doesn't have enough information to know the
  1313. embedded vector is in auto storage, and so should not be freed. */
  1314. vec<T, va_heap, vl_embed> *oldvec = m_vec;
  1315. unsigned int oldsize = 0;
  1316. bool handle_auto_vec = m_vec && using_auto_storage ();
  1317. if (handle_auto_vec)
  1318. {
  1319. m_vec = NULL;
  1320. oldsize = oldvec->length ();
  1321. nelems += oldsize;
  1322. }
  1323. va_heap::reserve (m_vec, nelems, exact PASS_MEM_STAT);
  1324. if (handle_auto_vec)
  1325. {
  1326. vec_copy_construct (m_vec->address (), oldvec->address (), oldsize);
  1327. m_vec->m_vecpfx.m_num = oldsize;
  1328. }
  1329. return true;
  1330. }
  1331. /* Ensure that this vector has exactly NELEMS slots available. This
  1332. will not create additional headroom. Note this can cause the
  1333. embedded vector to be reallocated. Returns true iff reallocation
  1334. actually occurred. */
  1335. template<typename T>
  1336. inline bool
  1337. vec<T, va_heap, vl_ptr>::reserve_exact (unsigned nelems MEM_STAT_DECL)
  1338. {
  1339. return reserve (nelems, true PASS_MEM_STAT);
  1340. }
  1341. /* Create the internal vector and reserve NELEMS for it. This is
  1342. exactly like vec::reserve, but the internal vector is
  1343. unconditionally allocated from scratch. The old one, if it
  1344. existed, is lost. */
  1345. template<typename T>
  1346. inline void
  1347. vec<T, va_heap, vl_ptr>::create (unsigned nelems MEM_STAT_DECL)
  1348. {
  1349. m_vec = NULL;
  1350. if (nelems > 0)
  1351. reserve_exact (nelems PASS_MEM_STAT);
  1352. }
  1353. /* Free the memory occupied by the embedded vector. */
  1354. template<typename T>
  1355. inline void
  1356. vec<T, va_heap, vl_ptr>::release (void)
  1357. {
  1358. if (!m_vec)
  1359. return;
  1360. if (using_auto_storage ())
  1361. {
  1362. m_vec->m_vecpfx.m_num = 0;
  1363. return;
  1364. }
  1365. va_heap::release (m_vec);
  1366. }
  1367. /* Copy the elements from SRC to the end of this vector as if by memcpy.
  1368. SRC and this vector must be allocated with the same memory
  1369. allocation mechanism. This vector is assumed to have sufficient
  1370. headroom available. */
  1371. template<typename T>
  1372. inline void
  1373. vec<T, va_heap, vl_ptr>::splice (const vec<T, va_heap, vl_ptr> &src)
  1374. {
  1375. if (src.length ())
  1376. m_vec->splice (*(src.m_vec));
  1377. }
  1378. /* Copy the elements in SRC to the end of this vector as if by memcpy.
  1379. SRC and this vector must be allocated with the same mechanism.
  1380. If there is not enough headroom in this vector, it will be reallocated
  1381. as needed. */
  1382. template<typename T>
  1383. inline void
  1384. vec<T, va_heap, vl_ptr>::safe_splice (const vec<T, va_heap, vl_ptr> &src
  1385. MEM_STAT_DECL)
  1386. {
  1387. if (src.length ())
  1388. {
  1389. reserve_exact (src.length ());
  1390. splice (src);
  1391. }
  1392. }
  1393. /* Push OBJ (a new element) onto the end of the vector. There must be
  1394. sufficient space in the vector. Return a pointer to the slot
  1395. where OBJ was inserted. */
  1396. template<typename T>
  1397. inline T *
  1398. vec<T, va_heap, vl_ptr>::quick_push (const T &obj)
  1399. {
  1400. return m_vec->quick_push (obj);
  1401. }
  1402. /* Push a new element OBJ onto the end of this vector. Reallocates
  1403. the embedded vector, if needed. Return a pointer to the slot where
  1404. OBJ was inserted. */
  1405. template<typename T>
  1406. inline T *
  1407. vec<T, va_heap, vl_ptr>::safe_push (const T &obj MEM_STAT_DECL)
  1408. {
  1409. reserve (1, false PASS_MEM_STAT);
  1410. return quick_push (obj);
  1411. }
  1412. /* Pop and return the last element off the end of the vector. */
  1413. template<typename T>
  1414. inline T &
  1415. vec<T, va_heap, vl_ptr>::pop (void)
  1416. {
  1417. return m_vec->pop ();
  1418. }
  1419. /* Set the length of the vector to LEN. The new length must be less
  1420. than or equal to the current length. This is an O(1) operation. */
  1421. template<typename T>
  1422. inline void
  1423. vec<T, va_heap, vl_ptr>::truncate (unsigned size)
  1424. {
  1425. if (m_vec)
  1426. m_vec->truncate (size);
  1427. else
  1428. gcc_checking_assert (size == 0);
  1429. }
  1430. /* Grow the vector to a specific length. LEN must be as long or
  1431. longer than the current length. The new elements are
  1432. uninitialized. Reallocate the internal vector, if needed. */
  1433. template<typename T>
  1434. inline void
  1435. vec<T, va_heap, vl_ptr>::safe_grow (unsigned len MEM_STAT_DECL)
  1436. {
  1437. unsigned oldlen = length ();
  1438. gcc_checking_assert (oldlen <= len);
  1439. reserve_exact (len - oldlen PASS_MEM_STAT);
  1440. if (m_vec)
  1441. m_vec->quick_grow (len);
  1442. else
  1443. gcc_checking_assert (len == 0);
  1444. }
  1445. /* Grow the embedded vector to a specific length. LEN must be as
  1446. long or longer than the current length. The new elements are
  1447. initialized to zero. Reallocate the internal vector, if needed. */
  1448. template<typename T>
  1449. inline void
  1450. vec<T, va_heap, vl_ptr>::safe_grow_cleared (unsigned len MEM_STAT_DECL)
  1451. {
  1452. unsigned oldlen = length ();
  1453. size_t growby = len - oldlen;
  1454. safe_grow (len PASS_MEM_STAT);
  1455. if (growby != 0)
  1456. vec_default_construct (address () + oldlen, growby);
  1457. }
  1458. /* Same as vec::safe_grow but without reallocation of the internal vector.
  1459. If the vector cannot be extended, a runtime assertion will be triggered. */
  1460. template<typename T>
  1461. inline void
  1462. vec<T, va_heap, vl_ptr>::quick_grow (unsigned len)
  1463. {
  1464. gcc_checking_assert (m_vec);
  1465. m_vec->quick_grow (len);
  1466. }
  1467. /* Same as vec::quick_grow_cleared but without reallocation of the
  1468. internal vector. If the vector cannot be extended, a runtime
  1469. assertion will be triggered. */
  1470. template<typename T>
  1471. inline void
  1472. vec<T, va_heap, vl_ptr>::quick_grow_cleared (unsigned len)
  1473. {
  1474. gcc_checking_assert (m_vec);
  1475. m_vec->quick_grow_cleared (len);
  1476. }
  1477. /* Insert an element, OBJ, at the IXth position of this vector. There
  1478. must be sufficient space. */
  1479. template<typename T>
  1480. inline void
  1481. vec<T, va_heap, vl_ptr>::quick_insert (unsigned ix, const T &obj)
  1482. {
  1483. m_vec->quick_insert (ix, obj);
  1484. }
  1485. /* Insert an element, OBJ, at the IXth position of the vector.
  1486. Reallocate the embedded vector, if necessary. */
  1487. template<typename T>
  1488. inline void
  1489. vec<T, va_heap, vl_ptr>::safe_insert (unsigned ix, const T &obj MEM_STAT_DECL)
  1490. {
  1491. reserve (1, false PASS_MEM_STAT);
  1492. quick_insert (ix, obj);
  1493. }
  1494. /* Remove an element from the IXth position of this vector. Ordering of
  1495. remaining elements is preserved. This is an O(N) operation due to
  1496. a memmove. */
  1497. template<typename T>
  1498. inline void
  1499. vec<T, va_heap, vl_ptr>::ordered_remove (unsigned ix)
  1500. {
  1501. m_vec->ordered_remove (ix);
  1502. }
  1503. /* Remove an element from the IXth position of this vector. Ordering
  1504. of remaining elements is destroyed. This is an O(1) operation. */
  1505. template<typename T>
  1506. inline void
  1507. vec<T, va_heap, vl_ptr>::unordered_remove (unsigned ix)
  1508. {
  1509. m_vec->unordered_remove (ix);
  1510. }
  1511. /* Remove LEN elements starting at the IXth. Ordering is retained.
  1512. This is an O(N) operation due to memmove. */
  1513. template<typename T>
  1514. inline void
  1515. vec<T, va_heap, vl_ptr>::block_remove (unsigned ix, unsigned len)
  1516. {
  1517. m_vec->block_remove (ix, len);
  1518. }
  1519. /* Sort the contents of this vector with qsort. CMP is the comparison
  1520. function to pass to qsort. */
  1521. template<typename T>
  1522. inline void
  1523. vec<T, va_heap, vl_ptr>::qsort (int (*cmp) (const void *, const void *))
  1524. {
  1525. if (m_vec)
  1526. m_vec->qsort (cmp);
  1527. }
  1528. /* Search the contents of the sorted vector with a binary search.
  1529. CMP is the comparison function to pass to bsearch. */
  1530. template<typename T>
  1531. inline T *
  1532. vec<T, va_heap, vl_ptr>::bsearch (const void *key,
  1533. int (*cmp) (const void *, const void *))
  1534. {
  1535. if (m_vec)
  1536. return m_vec->bsearch (key, cmp);
  1537. return NULL;
  1538. }
  1539. /* Find and return the first position in which OBJ could be inserted
  1540. without changing the ordering of this vector. LESSTHAN is a
  1541. function that returns true if the first argument is strictly less
  1542. than the second. */
  1543. template<typename T>
  1544. inline unsigned
  1545. vec<T, va_heap, vl_ptr>::lower_bound (T obj,
  1546. bool (*lessthan)(const T &, const T &))
  1547. const
  1548. {
  1549. return m_vec ? m_vec->lower_bound (obj, lessthan) : 0;
  1550. }
  1551. /* Return true if SEARCH is an element of V. Note that this is O(N) in the
  1552. size of the vector and so should be used with care. */
  1553. template<typename T>
  1554. inline bool
  1555. vec<T, va_heap, vl_ptr>::contains (const T &search) const
  1556. {
  1557. return m_vec ? m_vec->contains (search) : false;
  1558. }
  1559. /* Reverse content of the vector. */
  1560. template<typename T>
  1561. inline void
  1562. vec<T, va_heap, vl_ptr>::reverse (void)
  1563. {
  1564. unsigned l = length ();
  1565. T *ptr = address ();
  1566. for (unsigned i = 0; i < l / 2; i++)
  1567. std::swap (ptr[i], ptr[l - i - 1]);
  1568. }
  1569. template<typename T>
  1570. inline bool
  1571. vec<T, va_heap, vl_ptr>::using_auto_storage () const
  1572. {
  1573. return m_vec->m_vecpfx.m_using_auto_storage;
  1574. }
  1575. /* Release VEC and call release of all element vectors. */
  1576. template<typename T>
  1577. inline void
  1578. release_vec_vec (vec<vec<T> > &vec)
  1579. {
  1580. for (unsigned i = 0; i < vec.length (); i++)
  1581. vec[i].release ();
  1582. vec.release ();
  1583. }
  1584. #if (GCC_VERSION >= 3000)
  1585. # pragma GCC poison m_vec m_vecpfx m_vecdata
  1586. #endif
  1587. #endif // GCC_VEC_H