vec.h 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892
  1. /* Vector API for GNU compiler.
  2. Copyright (C) 2004-2018 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, 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. unsigned alloc
  218. = vec_prefix::calculate_allocation (v ? &v->m_vecpfx : 0, reserve, exact);
  219. gcc_checking_assert (alloc);
  220. if (GATHER_STATISTICS && v)
  221. v->m_vecpfx.release_overhead (v, v->allocated (), false);
  222. size_t size = vec<T, va_heap, vl_embed>::embedded_size (alloc);
  223. unsigned nelem = v ? v->length () : 0;
  224. v = static_cast <vec<T, va_heap, vl_embed> *> (xrealloc (v, size));
  225. v->embedded_init (alloc, nelem);
  226. if (GATHER_STATISTICS)
  227. v->m_vecpfx.register_overhead (v, alloc, nelem PASS_MEM_STAT);
  228. }
  229. /* Free the heap space allocated for vector V. */
  230. template<typename T>
  231. void
  232. va_heap::release (vec<T, va_heap, vl_embed> *&v)
  233. {
  234. if (v == NULL)
  235. return;
  236. if (GATHER_STATISTICS)
  237. v->m_vecpfx.release_overhead (v, v->allocated (), true);
  238. ::free (v);
  239. v = NULL;
  240. }
  241. /* Allocator type for GC vectors. Notice that we need the structure
  242. declaration even if GC is not enabled. */
  243. struct va_gc
  244. {
  245. /* Use vl_embed as the default layout for GC vectors. Due to GTY
  246. limitations, GC vectors must always be pointers, so it is more
  247. efficient to use a pointer to the vl_embed layout, rather than
  248. using a pointer to a pointer as would be the case with vl_ptr. */
  249. typedef vl_embed default_layout;
  250. template<typename T, typename A>
  251. static void reserve (vec<T, A, vl_embed> *&, unsigned, bool
  252. CXX_MEM_STAT_INFO);
  253. template<typename T, typename A>
  254. static void release (vec<T, A, vl_embed> *&v);
  255. };
  256. /* Free GC memory used by V and reset V to NULL. */
  257. template<typename T, typename A>
  258. inline void
  259. va_gc::release (vec<T, A, vl_embed> *&v)
  260. {
  261. if (v)
  262. ::ggc_free (v);
  263. v = NULL;
  264. }
  265. /* Allocator for GC memory. Ensure there are at least RESERVE free
  266. slots in V. If EXACT is true, grow exactly, else grow
  267. exponentially. As a special case, if the vector had not been
  268. allocated and RESERVE is 0, no vector will be created. */
  269. template<typename T, typename A>
  270. void
  271. va_gc::reserve (vec<T, A, vl_embed> *&v, unsigned reserve, bool exact
  272. MEM_STAT_DECL)
  273. {
  274. unsigned alloc
  275. = vec_prefix::calculate_allocation (v ? &v->m_vecpfx : 0, reserve, exact);
  276. if (!alloc)
  277. {
  278. ::ggc_free (v);
  279. v = NULL;
  280. return;
  281. }
  282. /* Calculate the amount of space we want. */
  283. size_t size = vec<T, A, vl_embed>::embedded_size (alloc);
  284. /* Ask the allocator how much space it will really give us. */
  285. size = ::ggc_round_alloc_size (size);
  286. /* Adjust the number of slots accordingly. */
  287. size_t vec_offset = sizeof (vec_prefix);
  288. size_t elt_size = sizeof (T);
  289. alloc = (size - vec_offset) / elt_size;
  290. /* And finally, recalculate the amount of space we ask for. */
  291. size = vec_offset + alloc * elt_size;
  292. unsigned nelem = v ? v->length () : 0;
  293. v = static_cast <vec<T, A, vl_embed> *> (::ggc_realloc (v, size
  294. PASS_MEM_STAT));
  295. v->embedded_init (alloc, nelem);
  296. }
  297. /* Allocator type for GC vectors. This is for vectors of types
  298. atomics w.r.t. collection, so allocation and deallocation is
  299. completely inherited from va_gc. */
  300. struct va_gc_atomic : va_gc
  301. {
  302. };
  303. /* Generic vector template. Default values for A and L indicate the
  304. most commonly used strategies.
  305. FIXME - Ideally, they would all be vl_ptr to encourage using regular
  306. instances for vectors, but the existing GTY machinery is limited
  307. in that it can only deal with GC objects that are pointers
  308. themselves.
  309. This means that vector operations that need to deal with
  310. potentially NULL pointers, must be provided as free
  311. functions (see the vec_safe_* functions above). */
  312. template<typename T,
  313. typename A = va_heap,
  314. typename L = typename A::default_layout>
  315. struct GTY((user)) vec
  316. {
  317. };
  318. /* Generic vec<> debug helpers.
  319. These need to be instantiated for each vec<TYPE> used throughout
  320. the compiler like this:
  321. DEFINE_DEBUG_VEC (TYPE)
  322. The reason we have a debug_helper() is because GDB can't
  323. disambiguate a plain call to debug(some_vec), and it must be called
  324. like debug<TYPE>(some_vec). */
  325. template<typename T>
  326. void
  327. debug_helper (vec<T> &ref)
  328. {
  329. unsigned i;
  330. for (i = 0; i < ref.length (); ++i)
  331. {
  332. fprintf (stderr, "[%d] = ", i);
  333. debug_slim (ref[i]);
  334. fputc ('\n', stderr);
  335. }
  336. }
  337. /* We need a separate va_gc variant here because default template
  338. argument for functions cannot be used in c++-98. Once this
  339. restriction is removed, those variant should be folded with the
  340. above debug_helper. */
  341. template<typename T>
  342. void
  343. debug_helper (vec<T, va_gc> &ref)
  344. {
  345. unsigned i;
  346. for (i = 0; i < ref.length (); ++i)
  347. {
  348. fprintf (stderr, "[%d] = ", i);
  349. debug_slim (ref[i]);
  350. fputc ('\n', stderr);
  351. }
  352. }
  353. /* Macro to define debug(vec<T>) and debug(vec<T, va_gc>) helper
  354. functions for a type T. */
  355. #define DEFINE_DEBUG_VEC(T) \
  356. template void debug_helper (vec<T> &); \
  357. template void debug_helper (vec<T, va_gc> &); \
  358. /* Define the vec<T> debug functions. */ \
  359. DEBUG_FUNCTION void \
  360. debug (vec<T> &ref) \
  361. { \
  362. debug_helper <T> (ref); \
  363. } \
  364. DEBUG_FUNCTION void \
  365. debug (vec<T> *ptr) \
  366. { \
  367. if (ptr) \
  368. debug (*ptr); \
  369. else \
  370. fprintf (stderr, "<nil>\n"); \
  371. } \
  372. /* Define the vec<T, va_gc> debug functions. */ \
  373. DEBUG_FUNCTION void \
  374. debug (vec<T, va_gc> &ref) \
  375. { \
  376. debug_helper <T> (ref); \
  377. } \
  378. DEBUG_FUNCTION void \
  379. debug (vec<T, va_gc> *ptr) \
  380. { \
  381. if (ptr) \
  382. debug (*ptr); \
  383. else \
  384. fprintf (stderr, "<nil>\n"); \
  385. }
  386. /* Default-construct N elements in DST. */
  387. template <typename T>
  388. inline void
  389. vec_default_construct (T *dst, unsigned n)
  390. {
  391. #ifdef BROKEN_VALUE_INITIALIZATION
  392. /* Versions of GCC before 4.4 sometimes leave certain objects
  393. uninitialized when value initialized, though if the type has
  394. user defined default ctor, that ctor is invoked. As a workaround
  395. perform clearing first and then the value initialization, which
  396. fixes the case when value initialization doesn't initialize due to
  397. the bugs and should initialize to all zeros, but still allows
  398. vectors for types with user defined default ctor that initializes
  399. some or all elements to non-zero. If T has no user defined
  400. default ctor and some non-static data members have user defined
  401. default ctors that initialize to non-zero the workaround will
  402. still not work properly; in that case we just need to provide
  403. user defined default ctor. */
  404. memset (dst, '\0', sizeof (T) * n);
  405. #endif
  406. for ( ; n; ++dst, --n)
  407. ::new (static_cast<void*>(dst)) T ();
  408. }
  409. /* Copy-construct N elements in DST from *SRC. */
  410. template <typename T>
  411. inline void
  412. vec_copy_construct (T *dst, const T *src, unsigned n)
  413. {
  414. for ( ; n; ++dst, ++src, --n)
  415. ::new (static_cast<void*>(dst)) T (*src);
  416. }
  417. /* Type to provide NULL values for vec<T, A, L>. This is used to
  418. provide nil initializers for vec instances. Since vec must be
  419. a POD, we cannot have proper ctor/dtor for it. To initialize
  420. a vec instance, you can assign it the value vNULL. This isn't
  421. needed for file-scope and function-local static vectors, which
  422. are zero-initialized by default. */
  423. struct vnull
  424. {
  425. template <typename T, typename A, typename L>
  426. CONSTEXPR operator vec<T, A, L> () { return vec<T, A, L>(); }
  427. };
  428. extern vnull vNULL;
  429. /* Embeddable vector. These vectors are suitable to be embedded
  430. in other data structures so that they can be pre-allocated in a
  431. contiguous memory block.
  432. Embeddable vectors are implemented using the trailing array idiom,
  433. thus they are not resizeable without changing the address of the
  434. vector object itself. This means you cannot have variables or
  435. fields of embeddable vector type -- always use a pointer to a
  436. vector. The one exception is the final field of a structure, which
  437. could be a vector type.
  438. You will have to use the embedded_size & embedded_init calls to
  439. create such objects, and they will not be resizeable (so the 'safe'
  440. allocation variants are not available).
  441. Properties:
  442. - The whole vector and control data are allocated in a single
  443. contiguous block. It uses the trailing-vector idiom, so
  444. allocation must reserve enough space for all the elements
  445. in the vector plus its control data.
  446. - The vector cannot be re-allocated.
  447. - The vector cannot grow nor shrink.
  448. - No indirections needed for access/manipulation.
  449. - It requires 2 words of storage (prior to vector allocation). */
  450. template<typename T, typename A>
  451. struct GTY((user)) vec<T, A, vl_embed>
  452. {
  453. public:
  454. unsigned allocated (void) const { return m_vecpfx.m_alloc; }
  455. unsigned length (void) const { return m_vecpfx.m_num; }
  456. bool is_empty (void) const { return m_vecpfx.m_num == 0; }
  457. T *address (void) { return m_vecdata; }
  458. const T *address (void) const { return m_vecdata; }
  459. T *begin () { return address (); }
  460. const T *begin () const { return address (); }
  461. T *end () { return address () + length (); }
  462. const T *end () const { return address () + length (); }
  463. const T &operator[] (unsigned) const;
  464. T &operator[] (unsigned);
  465. T &last (void);
  466. bool space (unsigned) const;
  467. bool iterate (unsigned, T *) const;
  468. bool iterate (unsigned, T **) const;
  469. vec *copy (ALONE_CXX_MEM_STAT_INFO) const;
  470. void splice (const vec &);
  471. void splice (const vec *src);
  472. T *quick_push (const T &);
  473. T &pop (void);
  474. void truncate (unsigned);
  475. void quick_insert (unsigned, const T &);
  476. void ordered_remove (unsigned);
  477. void unordered_remove (unsigned);
  478. void block_remove (unsigned, unsigned);
  479. void qsort (int (*) (const void *, const void *));
  480. T *bsearch (const void *key, int (*compar)(const void *, const void *));
  481. unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
  482. bool contains (const T &search) const;
  483. static size_t embedded_size (unsigned);
  484. void embedded_init (unsigned, unsigned = 0, unsigned = 0);
  485. void quick_grow (unsigned len);
  486. void quick_grow_cleared (unsigned len);
  487. /* vec class can access our internal data and functions. */
  488. template <typename, typename, typename> friend struct vec;
  489. /* The allocator types also need access to our internals. */
  490. friend struct va_gc;
  491. friend struct va_gc_atomic;
  492. friend struct va_heap;
  493. /* FIXME - These fields should be private, but we need to cater to
  494. compilers that have stricter notions of PODness for types. */
  495. vec_prefix m_vecpfx;
  496. T m_vecdata[1];
  497. };
  498. /* Convenience wrapper functions to use when dealing with pointers to
  499. embedded vectors. Some functionality for these vectors must be
  500. provided via free functions for these reasons:
  501. 1- The pointer may be NULL (e.g., before initial allocation).
  502. 2- When the vector needs to grow, it must be reallocated, so
  503. the pointer will change its value.
  504. Because of limitations with the current GC machinery, all vectors
  505. in GC memory *must* be pointers. */
  506. /* If V contains no room for NELEMS elements, return false. Otherwise,
  507. return true. */
  508. template<typename T, typename A>
  509. inline bool
  510. vec_safe_space (const vec<T, A, vl_embed> *v, unsigned nelems)
  511. {
  512. return v ? v->space (nelems) : nelems == 0;
  513. }
  514. /* If V is NULL, return 0. Otherwise, return V->length(). */
  515. template<typename T, typename A>
  516. inline unsigned
  517. vec_safe_length (const vec<T, A, vl_embed> *v)
  518. {
  519. return v ? v->length () : 0;
  520. }
  521. /* If V is NULL, return NULL. Otherwise, return V->address(). */
  522. template<typename T, typename A>
  523. inline T *
  524. vec_safe_address (vec<T, A, vl_embed> *v)
  525. {
  526. return v ? v->address () : NULL;
  527. }
  528. /* If V is NULL, return true. Otherwise, return V->is_empty(). */
  529. template<typename T, typename A>
  530. inline bool
  531. vec_safe_is_empty (vec<T, A, vl_embed> *v)
  532. {
  533. return v ? v->is_empty () : true;
  534. }
  535. /* If V does not have space for NELEMS elements, call
  536. V->reserve(NELEMS, EXACT). */
  537. template<typename T, typename A>
  538. inline bool
  539. vec_safe_reserve (vec<T, A, vl_embed> *&v, unsigned nelems, bool exact = false
  540. CXX_MEM_STAT_INFO)
  541. {
  542. bool extend = nelems ? !vec_safe_space (v, nelems) : false;
  543. if (extend)
  544. A::reserve (v, nelems, exact PASS_MEM_STAT);
  545. return extend;
  546. }
  547. template<typename T, typename A>
  548. inline bool
  549. vec_safe_reserve_exact (vec<T, A, vl_embed> *&v, unsigned nelems
  550. CXX_MEM_STAT_INFO)
  551. {
  552. return vec_safe_reserve (v, nelems, true PASS_MEM_STAT);
  553. }
  554. /* Allocate GC memory for V with space for NELEMS slots. If NELEMS
  555. is 0, V is initialized to NULL. */
  556. template<typename T, typename A>
  557. inline void
  558. vec_alloc (vec<T, A, vl_embed> *&v, unsigned nelems CXX_MEM_STAT_INFO)
  559. {
  560. v = NULL;
  561. vec_safe_reserve (v, nelems, false PASS_MEM_STAT);
  562. }
  563. /* Free the GC memory allocated by vector V and set it to NULL. */
  564. template<typename T, typename A>
  565. inline void
  566. vec_free (vec<T, A, vl_embed> *&v)
  567. {
  568. A::release (v);
  569. }
  570. /* Grow V to length LEN. Allocate it, if necessary. */
  571. template<typename T, typename A>
  572. inline void
  573. vec_safe_grow (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO)
  574. {
  575. unsigned oldlen = vec_safe_length (v);
  576. gcc_checking_assert (len >= oldlen);
  577. vec_safe_reserve_exact (v, len - oldlen PASS_MEM_STAT);
  578. v->quick_grow (len);
  579. }
  580. /* If V is NULL, allocate it. Call V->safe_grow_cleared(LEN). */
  581. template<typename T, typename A>
  582. inline void
  583. vec_safe_grow_cleared (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO)
  584. {
  585. unsigned oldlen = vec_safe_length (v);
  586. vec_safe_grow (v, len PASS_MEM_STAT);
  587. vec_default_construct (v->address () + oldlen, len - oldlen);
  588. }
  589. /* If V is NULL return false, otherwise return V->iterate(IX, PTR). */
  590. template<typename T, typename A>
  591. inline bool
  592. vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T **ptr)
  593. {
  594. if (v)
  595. return v->iterate (ix, ptr);
  596. else
  597. {
  598. *ptr = 0;
  599. return false;
  600. }
  601. }
  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. /* If V has no room for one more element, reallocate it. Then call
  615. V->quick_push(OBJ). */
  616. template<typename T, typename A>
  617. inline T *
  618. vec_safe_push (vec<T, A, vl_embed> *&v, const T &obj CXX_MEM_STAT_INFO)
  619. {
  620. vec_safe_reserve (v, 1, false PASS_MEM_STAT);
  621. return v->quick_push (obj);
  622. }
  623. /* if V has no room for one more element, reallocate it. Then call
  624. V->quick_insert(IX, OBJ). */
  625. template<typename T, typename A>
  626. inline void
  627. vec_safe_insert (vec<T, A, vl_embed> *&v, unsigned ix, const T &obj
  628. CXX_MEM_STAT_INFO)
  629. {
  630. vec_safe_reserve (v, 1, false PASS_MEM_STAT);
  631. v->quick_insert (ix, obj);
  632. }
  633. /* If V is NULL, do nothing. Otherwise, call V->truncate(SIZE). */
  634. template<typename T, typename A>
  635. inline void
  636. vec_safe_truncate (vec<T, A, vl_embed> *v, unsigned size)
  637. {
  638. if (v)
  639. v->truncate (size);
  640. }
  641. /* If SRC is not NULL, return a pointer to a copy of it. */
  642. template<typename T, typename A>
  643. inline vec<T, A, vl_embed> *
  644. vec_safe_copy (vec<T, A, vl_embed> *src CXX_MEM_STAT_INFO)
  645. {
  646. return src ? src->copy (ALONE_PASS_MEM_STAT) : NULL;
  647. }
  648. /* Copy the elements from SRC to the end of DST as if by memcpy.
  649. Reallocate DST, if necessary. */
  650. template<typename T, typename A>
  651. inline void
  652. vec_safe_splice (vec<T, A, vl_embed> *&dst, const vec<T, A, vl_embed> *src
  653. CXX_MEM_STAT_INFO)
  654. {
  655. unsigned src_len = vec_safe_length (src);
  656. if (src_len)
  657. {
  658. vec_safe_reserve_exact (dst, vec_safe_length (dst) + src_len
  659. PASS_MEM_STAT);
  660. dst->splice (*src);
  661. }
  662. }
  663. /* Return true if SEARCH is an element of V. Note that this is O(N) in the
  664. size of the vector and so should be used with care. */
  665. template<typename T, typename A>
  666. inline bool
  667. vec_safe_contains (vec<T, A, vl_embed> *v, const T &search)
  668. {
  669. return v ? v->contains (search) : false;
  670. }
  671. /* Index into vector. Return the IX'th element. IX must be in the
  672. domain of the vector. */
  673. template<typename T, typename A>
  674. inline const T &
  675. vec<T, A, vl_embed>::operator[] (unsigned ix) const
  676. {
  677. gcc_checking_assert (ix < m_vecpfx.m_num);
  678. return m_vecdata[ix];
  679. }
  680. template<typename T, typename A>
  681. inline T &
  682. vec<T, A, vl_embed>::operator[] (unsigned ix)
  683. {
  684. gcc_checking_assert (ix < m_vecpfx.m_num);
  685. return m_vecdata[ix];
  686. }
  687. /* Get the final element of the vector, which must not be empty. */
  688. template<typename T, typename A>
  689. inline T &
  690. vec<T, A, vl_embed>::last (void)
  691. {
  692. gcc_checking_assert (m_vecpfx.m_num > 0);
  693. return (*this)[m_vecpfx.m_num - 1];
  694. }
  695. /* If this vector has space for NELEMS additional entries, return
  696. true. You usually only need to use this if you are doing your
  697. own vector reallocation, for instance on an embedded vector. This
  698. returns true in exactly the same circumstances that vec::reserve
  699. will. */
  700. template<typename T, typename A>
  701. inline bool
  702. vec<T, A, vl_embed>::space (unsigned nelems) const
  703. {
  704. return m_vecpfx.m_alloc - m_vecpfx.m_num >= nelems;
  705. }
  706. /* Return iteration condition and update PTR to point to the IX'th
  707. element of this vector. Use this to iterate over the elements of a
  708. vector as follows,
  709. for (ix = 0; vec<T, A>::iterate (v, ix, &ptr); ix++)
  710. continue; */
  711. template<typename T, typename A>
  712. inline bool
  713. vec<T, A, vl_embed>::iterate (unsigned ix, T *ptr) const
  714. {
  715. if (ix < m_vecpfx.m_num)
  716. {
  717. *ptr = m_vecdata[ix];
  718. return true;
  719. }
  720. else
  721. {
  722. *ptr = 0;
  723. return false;
  724. }
  725. }
  726. /* Return iteration condition and update *PTR to point to the
  727. IX'th element of this vector. Use this to iterate over the
  728. elements of a vector as follows,
  729. for (ix = 0; v->iterate (ix, &ptr); ix++)
  730. continue;
  731. This variant is for vectors of objects. */
  732. template<typename T, typename A>
  733. inline bool
  734. vec<T, A, vl_embed>::iterate (unsigned ix, T **ptr) const
  735. {
  736. if (ix < m_vecpfx.m_num)
  737. {
  738. *ptr = CONST_CAST (T *, &m_vecdata[ix]);
  739. return true;
  740. }
  741. else
  742. {
  743. *ptr = 0;
  744. return false;
  745. }
  746. }
  747. /* Return a pointer to a copy of this vector. */
  748. template<typename T, typename A>
  749. inline vec<T, A, vl_embed> *
  750. vec<T, A, vl_embed>::copy (ALONE_MEM_STAT_DECL) const
  751. {
  752. vec<T, A, vl_embed> *new_vec = NULL;
  753. unsigned len = length ();
  754. if (len)
  755. {
  756. vec_alloc (new_vec, len PASS_MEM_STAT);
  757. new_vec->embedded_init (len, len);
  758. vec_copy_construct (new_vec->address (), m_vecdata, len);
  759. }
  760. return new_vec;
  761. }
  762. /* Copy the elements from SRC to the end of this vector as if by memcpy.
  763. The vector must have sufficient headroom available. */
  764. template<typename T, typename A>
  765. inline void
  766. vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> &src)
  767. {
  768. unsigned len = src.length ();
  769. if (len)
  770. {
  771. gcc_checking_assert (space (len));
  772. vec_copy_construct (end (), src.address (), len);
  773. m_vecpfx.m_num += len;
  774. }
  775. }
  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. if (src)
  781. splice (*src);
  782. }
  783. /* Push OBJ (a new element) onto the end of the vector. There must be
  784. sufficient space in the vector. Return a pointer to the slot
  785. where OBJ was inserted. */
  786. template<typename T, typename A>
  787. inline T *
  788. vec<T, A, vl_embed>::quick_push (const T &obj)
  789. {
  790. gcc_checking_assert (space (1));
  791. T *slot = &m_vecdata[m_vecpfx.m_num++];
  792. *slot = obj;
  793. return slot;
  794. }
  795. /* Pop and return the last element off the end of the vector. */
  796. template<typename T, typename A>
  797. inline T &
  798. vec<T, A, vl_embed>::pop (void)
  799. {
  800. gcc_checking_assert (length () > 0);
  801. return m_vecdata[--m_vecpfx.m_num];
  802. }
  803. /* Set the length of the vector to SIZE. The new length must be less
  804. than or equal to the current length. This is an O(1) operation. */
  805. template<typename T, typename A>
  806. inline void
  807. vec<T, A, vl_embed>::truncate (unsigned size)
  808. {
  809. gcc_checking_assert (length () >= size);
  810. m_vecpfx.m_num = size;
  811. }
  812. /* Insert an element, OBJ, at the IXth position of this vector. There
  813. must be sufficient space. */
  814. template<typename T, typename A>
  815. inline void
  816. vec<T, A, vl_embed>::quick_insert (unsigned ix, const T &obj)
  817. {
  818. gcc_checking_assert (length () < allocated ());
  819. gcc_checking_assert (ix <= length ());
  820. T *slot = &m_vecdata[ix];
  821. memmove (slot + 1, slot, (m_vecpfx.m_num++ - ix) * sizeof (T));
  822. *slot = obj;
  823. }
  824. /* Remove an element from the IXth position of this vector. Ordering of
  825. remaining elements is preserved. This is an O(N) operation due to
  826. memmove. */
  827. template<typename T, typename A>
  828. inline void
  829. vec<T, A, vl_embed>::ordered_remove (unsigned ix)
  830. {
  831. gcc_checking_assert (ix < length ());
  832. T *slot = &m_vecdata[ix];
  833. memmove (slot, slot + 1, (--m_vecpfx.m_num - ix) * sizeof (T));
  834. }
  835. /* Remove an element from the IXth position of this vector. Ordering of
  836. remaining elements is destroyed. This is an O(1) operation. */
  837. template<typename T, typename A>
  838. inline void
  839. vec<T, A, vl_embed>::unordered_remove (unsigned ix)
  840. {
  841. gcc_checking_assert (ix < length ());
  842. m_vecdata[ix] = m_vecdata[--m_vecpfx.m_num];
  843. }
  844. /* Remove LEN elements starting at the IXth. Ordering is retained.
  845. This is an O(N) operation due to memmove. */
  846. template<typename T, typename A>
  847. inline void
  848. vec<T, A, vl_embed>::block_remove (unsigned ix, unsigned len)
  849. {
  850. gcc_checking_assert (ix + len <= length ());
  851. T *slot = &m_vecdata[ix];
  852. m_vecpfx.m_num -= len;
  853. memmove (slot, slot + len, (m_vecpfx.m_num - ix) * sizeof (T));
  854. }
  855. /* Sort the contents of this vector with qsort. CMP is the comparison
  856. function to pass to qsort. */
  857. template<typename T, typename A>
  858. inline void
  859. vec<T, A, vl_embed>::qsort (int (*cmp) (const void *, const void *))
  860. {
  861. if (length () > 1)
  862. ::qsort (address (), length (), sizeof (T), cmp);
  863. }
  864. /* Search the contents of the sorted vector with a binary search.
  865. CMP is the comparison function to pass to bsearch. */
  866. template<typename T, typename A>
  867. inline T *
  868. vec<T, A, vl_embed>::bsearch (const void *key,
  869. int (*compar) (const void *, const void *))
  870. {
  871. const void *base = this->address ();
  872. size_t nmemb = this->length ();
  873. size_t size = sizeof (T);
  874. /* The following is a copy of glibc stdlib-bsearch.h. */
  875. size_t l, u, idx;
  876. const void *p;
  877. int comparison;
  878. l = 0;
  879. u = nmemb;
  880. while (l < u)
  881. {
  882. idx = (l + u) / 2;
  883. p = (const void *) (((const char *) base) + (idx * size));
  884. comparison = (*compar) (key, p);
  885. if (comparison < 0)
  886. u = idx;
  887. else if (comparison > 0)
  888. l = idx + 1;
  889. else
  890. return (T *)const_cast<void *>(p);
  891. }
  892. return NULL;
  893. }
  894. /* Return true if SEARCH is an element of V. Note that this is O(N) in the
  895. size of the vector and so should be used with care. */
  896. template<typename T, typename A>
  897. inline bool
  898. vec<T, A, vl_embed>::contains (const T &search) const
  899. {
  900. unsigned int len = length ();
  901. for (unsigned int i = 0; i < len; i++)
  902. if ((*this)[i] == search)
  903. return true;
  904. return false;
  905. }
  906. /* Find and return the first position in which OBJ could be inserted
  907. without changing the ordering of this vector. LESSTHAN is a
  908. function that returns true if the first argument is strictly less
  909. than the second. */
  910. template<typename T, typename A>
  911. unsigned
  912. vec<T, A, vl_embed>::lower_bound (T obj, bool (*lessthan)(const T &, const T &))
  913. const
  914. {
  915. unsigned int len = length ();
  916. unsigned int half, middle;
  917. unsigned int first = 0;
  918. while (len > 0)
  919. {
  920. half = len / 2;
  921. middle = first;
  922. middle += half;
  923. T middle_elem = (*this)[middle];
  924. if (lessthan (middle_elem, obj))
  925. {
  926. first = middle;
  927. ++first;
  928. len = len - half - 1;
  929. }
  930. else
  931. len = half;
  932. }
  933. return first;
  934. }
  935. /* Return the number of bytes needed to embed an instance of an
  936. embeddable vec inside another data structure.
  937. Use these methods to determine the required size and initialization
  938. of a vector V of type T embedded within another structure (as the
  939. final member):
  940. size_t vec<T, A, vl_embed>::embedded_size (unsigned alloc);
  941. void v->embedded_init (unsigned alloc, unsigned num);
  942. These allow the caller to perform the memory allocation. */
  943. template<typename T, typename A>
  944. inline size_t
  945. vec<T, A, vl_embed>::embedded_size (unsigned alloc)
  946. {
  947. typedef vec<T, A, vl_embed> vec_embedded;
  948. return offsetof (vec_embedded, m_vecdata) + alloc * sizeof (T);
  949. }
  950. /* Initialize the vector to contain room for ALLOC elements and
  951. NUM active elements. */
  952. template<typename T, typename A>
  953. inline void
  954. vec<T, A, vl_embed>::embedded_init (unsigned alloc, unsigned num, unsigned aut)
  955. {
  956. m_vecpfx.m_alloc = alloc;
  957. m_vecpfx.m_using_auto_storage = aut;
  958. m_vecpfx.m_num = num;
  959. }
  960. /* Grow the vector to a specific length. LEN must be as long or longer than
  961. the current length. The new elements are uninitialized. */
  962. template<typename T, typename A>
  963. inline void
  964. vec<T, A, vl_embed>::quick_grow (unsigned len)
  965. {
  966. gcc_checking_assert (length () <= len && len <= m_vecpfx.m_alloc);
  967. m_vecpfx.m_num = len;
  968. }
  969. /* Grow the vector to a specific length. LEN must be as long or longer than
  970. the current length. The new elements are initialized to zero. */
  971. template<typename T, typename A>
  972. inline void
  973. vec<T, A, vl_embed>::quick_grow_cleared (unsigned len)
  974. {
  975. unsigned oldlen = length ();
  976. size_t growby = len - oldlen;
  977. quick_grow (len);
  978. if (growby != 0)
  979. vec_default_construct (address () + oldlen, growby);
  980. }
  981. /* Garbage collection support for vec<T, A, vl_embed>. */
  982. template<typename T>
  983. void
  984. gt_ggc_mx (vec<T, va_gc> *v)
  985. {
  986. extern void gt_ggc_mx (T &);
  987. for (unsigned i = 0; i < v->length (); i++)
  988. gt_ggc_mx ((*v)[i]);
  989. }
  990. template<typename T>
  991. void
  992. gt_ggc_mx (vec<T, va_gc_atomic, vl_embed> *v ATTRIBUTE_UNUSED)
  993. {
  994. /* Nothing to do. Vectors of atomic types wrt GC do not need to
  995. be traversed. */
  996. }
  997. /* PCH support for vec<T, A, vl_embed>. */
  998. template<typename T, typename A>
  999. void
  1000. gt_pch_nx (vec<T, A, vl_embed> *v)
  1001. {
  1002. extern void gt_pch_nx (T &);
  1003. for (unsigned i = 0; i < v->length (); i++)
  1004. gt_pch_nx ((*v)[i]);
  1005. }
  1006. template<typename T, typename A>
  1007. void
  1008. gt_pch_nx (vec<T *, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
  1009. {
  1010. for (unsigned i = 0; i < v->length (); i++)
  1011. op (&((*v)[i]), cookie);
  1012. }
  1013. template<typename T, typename A>
  1014. void
  1015. gt_pch_nx (vec<T, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
  1016. {
  1017. extern void gt_pch_nx (T *, gt_pointer_operator, void *);
  1018. for (unsigned i = 0; i < v->length (); i++)
  1019. gt_pch_nx (&((*v)[i]), op, cookie);
  1020. }
  1021. /* Space efficient vector. These vectors can grow dynamically and are
  1022. allocated together with their control data. They are suited to be
  1023. included in data structures. Prior to initial allocation, they
  1024. only take a single word of storage.
  1025. These vectors are implemented as a pointer to an embeddable vector.
  1026. The semantics allow for this pointer to be NULL to represent empty
  1027. vectors. This way, empty vectors occupy minimal space in the
  1028. structure containing them.
  1029. Properties:
  1030. - The whole vector and control data are allocated in a single
  1031. contiguous block.
  1032. - The whole vector may be re-allocated.
  1033. - Vector data may grow and shrink.
  1034. - Access and manipulation requires a pointer test and
  1035. indirection.
  1036. - It requires 1 word of storage (prior to vector allocation).
  1037. Limitations:
  1038. These vectors must be PODs because they are stored in unions.
  1039. (http://en.wikipedia.org/wiki/Plain_old_data_structures).
  1040. As long as we use C++03, we cannot have constructors nor
  1041. destructors in classes that are stored in unions. */
  1042. template<typename T>
  1043. struct vec<T, va_heap, vl_ptr>
  1044. {
  1045. public:
  1046. /* Memory allocation and deallocation for the embedded vector.
  1047. Needed because we cannot have proper ctors/dtors defined. */
  1048. void create (unsigned nelems CXX_MEM_STAT_INFO);
  1049. void release (void);
  1050. /* Vector operations. */
  1051. bool exists (void) const
  1052. { return m_vec != NULL; }
  1053. bool is_empty (void) const
  1054. { return m_vec ? m_vec->is_empty () : true; }
  1055. unsigned length (void) const
  1056. { return m_vec ? m_vec->length () : 0; }
  1057. T *address (void)
  1058. { return m_vec ? m_vec->m_vecdata : NULL; }
  1059. const T *address (void) const
  1060. { return m_vec ? m_vec->m_vecdata : NULL; }
  1061. T *begin () { return address (); }
  1062. const T *begin () const { return address (); }
  1063. T *end () { return begin () + length (); }
  1064. const T *end () const { return begin () + length (); }
  1065. const T &operator[] (unsigned ix) const
  1066. { return (*m_vec)[ix]; }
  1067. bool operator!=(const vec &other) const
  1068. { return !(*this == other); }
  1069. bool operator==(const vec &other) const
  1070. { return address () == other.address (); }
  1071. T &operator[] (unsigned ix)
  1072. { return (*m_vec)[ix]; }
  1073. T &last (void)
  1074. { return m_vec->last (); }
  1075. bool space (int nelems) const
  1076. { return m_vec ? m_vec->space (nelems) : nelems == 0; }
  1077. bool iterate (unsigned ix, T *p) const;
  1078. bool iterate (unsigned ix, T **p) const;
  1079. vec copy (ALONE_CXX_MEM_STAT_INFO) const;
  1080. bool reserve (unsigned, bool = false CXX_MEM_STAT_INFO);
  1081. bool reserve_exact (unsigned CXX_MEM_STAT_INFO);
  1082. void splice (const vec &);
  1083. void safe_splice (const vec & CXX_MEM_STAT_INFO);
  1084. T *quick_push (const T &);
  1085. T *safe_push (const T &CXX_MEM_STAT_INFO);
  1086. T &pop (void);
  1087. void truncate (unsigned);
  1088. void safe_grow (unsigned CXX_MEM_STAT_INFO);
  1089. void safe_grow_cleared (unsigned CXX_MEM_STAT_INFO);
  1090. void quick_grow (unsigned);
  1091. void quick_grow_cleared (unsigned);
  1092. void quick_insert (unsigned, const T &);
  1093. void safe_insert (unsigned, const T & CXX_MEM_STAT_INFO);
  1094. void ordered_remove (unsigned);
  1095. void unordered_remove (unsigned);
  1096. void block_remove (unsigned, unsigned);
  1097. void qsort (int (*) (const void *, const void *));
  1098. T *bsearch (const void *key, int (*compar)(const void *, const void *));
  1099. unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
  1100. bool contains (const T &search) const;
  1101. bool using_auto_storage () const;
  1102. /* FIXME - This field should be private, but we need to cater to
  1103. compilers that have stricter notions of PODness for types. */
  1104. vec<T, va_heap, vl_embed> *m_vec;
  1105. };
  1106. /* auto_vec is a subclass of vec that automatically manages creating and
  1107. releasing the internal vector. If N is non zero then it has N elements of
  1108. internal storage. The default is no internal storage, and you probably only
  1109. want to ask for internal storage for vectors on the stack because if the
  1110. size of the vector is larger than the internal storage that space is wasted.
  1111. */
  1112. template<typename T, size_t N = 0>
  1113. class auto_vec : public vec<T, va_heap>
  1114. {
  1115. public:
  1116. auto_vec ()
  1117. {
  1118. m_auto.embedded_init (MAX (N, 2), 0, 1);
  1119. this->m_vec = &m_auto;
  1120. }
  1121. auto_vec (size_t s)
  1122. {
  1123. if (s > N)
  1124. {
  1125. this->create (s);
  1126. return;
  1127. }
  1128. m_auto.embedded_init (MAX (N, 2), 0, 1);
  1129. this->m_vec = &m_auto;
  1130. }
  1131. ~auto_vec ()
  1132. {
  1133. this->release ();
  1134. }
  1135. private:
  1136. vec<T, va_heap, vl_embed> m_auto;
  1137. T m_data[MAX (N - 1, 1)];
  1138. };
  1139. /* auto_vec is a sub class of vec whose storage is released when it is
  1140. destroyed. */
  1141. template<typename T>
  1142. class auto_vec<T, 0> : public vec<T, va_heap>
  1143. {
  1144. public:
  1145. auto_vec () { this->m_vec = NULL; }
  1146. auto_vec (size_t n) { this->create (n); }
  1147. ~auto_vec () { this->release (); }
  1148. };
  1149. /* Allocate heap memory for pointer V and create the internal vector
  1150. with space for NELEMS elements. If NELEMS is 0, the internal
  1151. vector is initialized to empty. */
  1152. template<typename T>
  1153. inline void
  1154. vec_alloc (vec<T> *&v, unsigned nelems CXX_MEM_STAT_INFO)
  1155. {
  1156. v = new vec<T>;
  1157. v->create (nelems PASS_MEM_STAT);
  1158. }
  1159. /* Conditionally allocate heap memory for VEC and its internal vector. */
  1160. template<typename T>
  1161. inline void
  1162. vec_check_alloc (vec<T, va_heap> *&vec, unsigned nelems CXX_MEM_STAT_INFO)
  1163. {
  1164. if (!vec)
  1165. vec_alloc (vec, nelems PASS_MEM_STAT);
  1166. }
  1167. /* Free the heap memory allocated by vector V and set it to NULL. */
  1168. template<typename T>
  1169. inline void
  1170. vec_free (vec<T> *&v)
  1171. {
  1172. if (v == NULL)
  1173. return;
  1174. v->release ();
  1175. delete v;
  1176. v = NULL;
  1177. }
  1178. /* Return iteration condition and update PTR to point to the IX'th
  1179. element of this vector. Use this to iterate over the elements of a
  1180. vector as follows,
  1181. for (ix = 0; v.iterate (ix, &ptr); ix++)
  1182. continue; */
  1183. template<typename T>
  1184. inline bool
  1185. vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T *ptr) const
  1186. {
  1187. if (m_vec)
  1188. return m_vec->iterate (ix, ptr);
  1189. else
  1190. {
  1191. *ptr = 0;
  1192. return false;
  1193. }
  1194. }
  1195. /* Return iteration condition and update *PTR to point to the
  1196. IX'th element of this vector. Use this to iterate over the
  1197. elements of a vector as follows,
  1198. for (ix = 0; v->iterate (ix, &ptr); ix++)
  1199. continue;
  1200. This variant is for vectors of objects. */
  1201. template<typename T>
  1202. inline bool
  1203. vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T **ptr) const
  1204. {
  1205. if (m_vec)
  1206. return m_vec->iterate (ix, ptr);
  1207. else
  1208. {
  1209. *ptr = 0;
  1210. return false;
  1211. }
  1212. }
  1213. /* Convenience macro for forward iteration. */
  1214. #define FOR_EACH_VEC_ELT(V, I, P) \
  1215. for (I = 0; (V).iterate ((I), &(P)); ++(I))
  1216. #define FOR_EACH_VEC_SAFE_ELT(V, I, P) \
  1217. for (I = 0; vec_safe_iterate ((V), (I), &(P)); ++(I))
  1218. /* Likewise, but start from FROM rather than 0. */
  1219. #define FOR_EACH_VEC_ELT_FROM(V, I, P, FROM) \
  1220. for (I = (FROM); (V).iterate ((I), &(P)); ++(I))
  1221. /* Convenience macro for reverse iteration. */
  1222. #define FOR_EACH_VEC_ELT_REVERSE(V, I, P) \
  1223. for (I = (V).length () - 1; \
  1224. (V).iterate ((I), &(P)); \
  1225. (I)--)
  1226. #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P) \
  1227. for (I = vec_safe_length (V) - 1; \
  1228. vec_safe_iterate ((V), (I), &(P)); \
  1229. (I)--)
  1230. /* Return a copy of this vector. */
  1231. template<typename T>
  1232. inline vec<T, va_heap, vl_ptr>
  1233. vec<T, va_heap, vl_ptr>::copy (ALONE_MEM_STAT_DECL) const
  1234. {
  1235. vec<T, va_heap, vl_ptr> new_vec = vNULL;
  1236. if (length ())
  1237. new_vec.m_vec = m_vec->copy ();
  1238. return new_vec;
  1239. }
  1240. /* Ensure that the vector has at least RESERVE slots available (if
  1241. EXACT is false), or exactly RESERVE slots available (if EXACT is
  1242. true).
  1243. This may create additional headroom if EXACT is false.
  1244. Note that this can cause the embedded vector to be reallocated.
  1245. Returns true iff reallocation actually occurred. */
  1246. template<typename T>
  1247. inline bool
  1248. vec<T, va_heap, vl_ptr>::reserve (unsigned nelems, bool exact MEM_STAT_DECL)
  1249. {
  1250. if (space (nelems))
  1251. return false;
  1252. /* For now play a game with va_heap::reserve to hide our auto storage if any,
  1253. this is necessary because it doesn't have enough information to know the
  1254. embedded vector is in auto storage, and so should not be freed. */
  1255. vec<T, va_heap, vl_embed> *oldvec = m_vec;
  1256. unsigned int oldsize = 0;
  1257. bool handle_auto_vec = m_vec && using_auto_storage ();
  1258. if (handle_auto_vec)
  1259. {
  1260. m_vec = NULL;
  1261. oldsize = oldvec->length ();
  1262. nelems += oldsize;
  1263. }
  1264. va_heap::reserve (m_vec, nelems, exact PASS_MEM_STAT);
  1265. if (handle_auto_vec)
  1266. {
  1267. vec_copy_construct (m_vec->address (), oldvec->address (), oldsize);
  1268. m_vec->m_vecpfx.m_num = oldsize;
  1269. }
  1270. return true;
  1271. }
  1272. /* Ensure that this vector has exactly NELEMS slots available. This
  1273. will not create additional headroom. Note this can cause the
  1274. embedded vector to be reallocated. Returns true iff reallocation
  1275. actually occurred. */
  1276. template<typename T>
  1277. inline bool
  1278. vec<T, va_heap, vl_ptr>::reserve_exact (unsigned nelems MEM_STAT_DECL)
  1279. {
  1280. return reserve (nelems, true PASS_MEM_STAT);
  1281. }
  1282. /* Create the internal vector and reserve NELEMS for it. This is
  1283. exactly like vec::reserve, but the internal vector is
  1284. unconditionally allocated from scratch. The old one, if it
  1285. existed, is lost. */
  1286. template<typename T>
  1287. inline void
  1288. vec<T, va_heap, vl_ptr>::create (unsigned nelems MEM_STAT_DECL)
  1289. {
  1290. m_vec = NULL;
  1291. if (nelems > 0)
  1292. reserve_exact (nelems PASS_MEM_STAT);
  1293. }
  1294. /* Free the memory occupied by the embedded vector. */
  1295. template<typename T>
  1296. inline void
  1297. vec<T, va_heap, vl_ptr>::release (void)
  1298. {
  1299. if (!m_vec)
  1300. return;
  1301. if (using_auto_storage ())
  1302. {
  1303. m_vec->m_vecpfx.m_num = 0;
  1304. return;
  1305. }
  1306. va_heap::release (m_vec);
  1307. }
  1308. /* Copy the elements from SRC to the end of this vector as if by memcpy.
  1309. SRC and this vector must be allocated with the same memory
  1310. allocation mechanism. This vector is assumed to have sufficient
  1311. headroom available. */
  1312. template<typename T>
  1313. inline void
  1314. vec<T, va_heap, vl_ptr>::splice (const vec<T, va_heap, vl_ptr> &src)
  1315. {
  1316. if (src.m_vec)
  1317. m_vec->splice (*(src.m_vec));
  1318. }
  1319. /* Copy the elements in SRC to the end of this vector as if by memcpy.
  1320. SRC and this vector must be allocated with the same mechanism.
  1321. If there is not enough headroom in this vector, it will be reallocated
  1322. as needed. */
  1323. template<typename T>
  1324. inline void
  1325. vec<T, va_heap, vl_ptr>::safe_splice (const vec<T, va_heap, vl_ptr> &src
  1326. MEM_STAT_DECL)
  1327. {
  1328. if (src.length ())
  1329. {
  1330. reserve_exact (src.length ());
  1331. splice (src);
  1332. }
  1333. }
  1334. /* Push OBJ (a new element) onto the end of the vector. There must be
  1335. sufficient space in the vector. Return a pointer to the slot
  1336. where OBJ was inserted. */
  1337. template<typename T>
  1338. inline T *
  1339. vec<T, va_heap, vl_ptr>::quick_push (const T &obj)
  1340. {
  1341. return m_vec->quick_push (obj);
  1342. }
  1343. /* Push a new element OBJ onto the end of this vector. Reallocates
  1344. the embedded vector, if needed. Return a pointer to the slot where
  1345. OBJ was inserted. */
  1346. template<typename T>
  1347. inline T *
  1348. vec<T, va_heap, vl_ptr>::safe_push (const T &obj MEM_STAT_DECL)
  1349. {
  1350. reserve (1, false PASS_MEM_STAT);
  1351. return quick_push (obj);
  1352. }
  1353. /* Pop and return the last element off the end of the vector. */
  1354. template<typename T>
  1355. inline T &
  1356. vec<T, va_heap, vl_ptr>::pop (void)
  1357. {
  1358. return m_vec->pop ();
  1359. }
  1360. /* Set the length of the vector to LEN. The new length must be less
  1361. than or equal to the current length. This is an O(1) operation. */
  1362. template<typename T>
  1363. inline void
  1364. vec<T, va_heap, vl_ptr>::truncate (unsigned size)
  1365. {
  1366. if (m_vec)
  1367. m_vec->truncate (size);
  1368. else
  1369. gcc_checking_assert (size == 0);
  1370. }
  1371. /* Grow the vector to a specific length. LEN must be as long or
  1372. longer than the current length. The new elements are
  1373. uninitialized. Reallocate the internal vector, if needed. */
  1374. template<typename T>
  1375. inline void
  1376. vec<T, va_heap, vl_ptr>::safe_grow (unsigned len MEM_STAT_DECL)
  1377. {
  1378. unsigned oldlen = length ();
  1379. gcc_checking_assert (oldlen <= len);
  1380. reserve_exact (len - oldlen PASS_MEM_STAT);
  1381. if (m_vec)
  1382. m_vec->quick_grow (len);
  1383. else
  1384. gcc_checking_assert (len == 0);
  1385. }
  1386. /* Grow the embedded vector to a specific length. LEN must be as
  1387. long or longer than the current length. The new elements are
  1388. initialized to zero. Reallocate the internal vector, if needed. */
  1389. template<typename T>
  1390. inline void
  1391. vec<T, va_heap, vl_ptr>::safe_grow_cleared (unsigned len MEM_STAT_DECL)
  1392. {
  1393. unsigned oldlen = length ();
  1394. size_t growby = len - oldlen;
  1395. safe_grow (len PASS_MEM_STAT);
  1396. if (growby != 0)
  1397. vec_default_construct (address () + oldlen, growby);
  1398. }
  1399. /* Same as vec::safe_grow but without reallocation of the internal vector.
  1400. If the vector cannot be extended, a runtime assertion will be triggered. */
  1401. template<typename T>
  1402. inline void
  1403. vec<T, va_heap, vl_ptr>::quick_grow (unsigned len)
  1404. {
  1405. gcc_checking_assert (m_vec);
  1406. m_vec->quick_grow (len);
  1407. }
  1408. /* Same as vec::quick_grow_cleared but without reallocation of the
  1409. internal vector. If the vector cannot be extended, a runtime
  1410. assertion will be triggered. */
  1411. template<typename T>
  1412. inline void
  1413. vec<T, va_heap, vl_ptr>::quick_grow_cleared (unsigned len)
  1414. {
  1415. gcc_checking_assert (m_vec);
  1416. m_vec->quick_grow_cleared (len);
  1417. }
  1418. /* Insert an element, OBJ, at the IXth position of this vector. There
  1419. must be sufficient space. */
  1420. template<typename T>
  1421. inline void
  1422. vec<T, va_heap, vl_ptr>::quick_insert (unsigned ix, const T &obj)
  1423. {
  1424. m_vec->quick_insert (ix, obj);
  1425. }
  1426. /* Insert an element, OBJ, at the IXth position of the vector.
  1427. Reallocate the embedded vector, if necessary. */
  1428. template<typename T>
  1429. inline void
  1430. vec<T, va_heap, vl_ptr>::safe_insert (unsigned ix, const T &obj MEM_STAT_DECL)
  1431. {
  1432. reserve (1, false PASS_MEM_STAT);
  1433. quick_insert (ix, obj);
  1434. }
  1435. /* Remove an element from the IXth position of this vector. Ordering of
  1436. remaining elements is preserved. This is an O(N) operation due to
  1437. a memmove. */
  1438. template<typename T>
  1439. inline void
  1440. vec<T, va_heap, vl_ptr>::ordered_remove (unsigned ix)
  1441. {
  1442. m_vec->ordered_remove (ix);
  1443. }
  1444. /* Remove an element from the IXth position of this vector. Ordering
  1445. of remaining elements is destroyed. This is an O(1) operation. */
  1446. template<typename T>
  1447. inline void
  1448. vec<T, va_heap, vl_ptr>::unordered_remove (unsigned ix)
  1449. {
  1450. m_vec->unordered_remove (ix);
  1451. }
  1452. /* Remove LEN elements starting at the IXth. Ordering is retained.
  1453. This is an O(N) operation due to memmove. */
  1454. template<typename T>
  1455. inline void
  1456. vec<T, va_heap, vl_ptr>::block_remove (unsigned ix, unsigned len)
  1457. {
  1458. m_vec->block_remove (ix, len);
  1459. }
  1460. /* Sort the contents of this vector with qsort. CMP is the comparison
  1461. function to pass to qsort. */
  1462. template<typename T>
  1463. inline void
  1464. vec<T, va_heap, vl_ptr>::qsort (int (*cmp) (const void *, const void *))
  1465. {
  1466. if (m_vec)
  1467. m_vec->qsort (cmp);
  1468. }
  1469. /* Search the contents of the sorted vector with a binary search.
  1470. CMP is the comparison function to pass to bsearch. */
  1471. template<typename T>
  1472. inline T *
  1473. vec<T, va_heap, vl_ptr>::bsearch (const void *key,
  1474. int (*cmp) (const void *, const void *))
  1475. {
  1476. if (m_vec)
  1477. return m_vec->bsearch (key, cmp);
  1478. return NULL;
  1479. }
  1480. /* Find and return the first position in which OBJ could be inserted
  1481. without changing the ordering of this vector. LESSTHAN is a
  1482. function that returns true if the first argument is strictly less
  1483. than the second. */
  1484. template<typename T>
  1485. inline unsigned
  1486. vec<T, va_heap, vl_ptr>::lower_bound (T obj,
  1487. bool (*lessthan)(const T &, const T &))
  1488. const
  1489. {
  1490. return m_vec ? m_vec->lower_bound (obj, lessthan) : 0;
  1491. }
  1492. /* Return true if SEARCH is an element of V. Note that this is O(N) in the
  1493. size of the vector and so should be used with care. */
  1494. template<typename T>
  1495. inline bool
  1496. vec<T, va_heap, vl_ptr>::contains (const T &search) const
  1497. {
  1498. return m_vec ? m_vec->contains (search) : false;
  1499. }
  1500. template<typename T>
  1501. inline bool
  1502. vec<T, va_heap, vl_ptr>::using_auto_storage () const
  1503. {
  1504. return m_vec->m_vecpfx.m_using_auto_storage;
  1505. }
  1506. /* Release VEC and call release of all element vectors. */
  1507. template<typename T>
  1508. inline void
  1509. release_vec_vec (vec<vec<T> > &vec)
  1510. {
  1511. for (unsigned i = 0; i < vec.length (); i++)
  1512. vec[i].release ();
  1513. vec.release ();
  1514. }
  1515. #if (GCC_VERSION >= 3000)
  1516. # pragma GCC poison m_vec m_vecpfx m_vecdata
  1517. #endif
  1518. #endif // GCC_VEC_H