hash-table.h 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. /* A type-safe hash table template.
  2. Copyright (C) 2012-2018 Free Software Foundation, Inc.
  3. Contributed by Lawrence Crowl <crowl@google.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* This file implements a typed hash table.
  17. The implementation borrows from libiberty's htab_t in hashtab.h.
  18. INTRODUCTION TO TYPES
  19. Users of the hash table generally need to be aware of three types.
  20. 1. The type being placed into the hash table. This type is called
  21. the value type.
  22. 2. The type used to describe how to handle the value type within
  23. the hash table. This descriptor type provides the hash table with
  24. several things.
  25. - A typedef named 'value_type' to the value type (from above).
  26. - A static member function named 'hash' that takes a value_type
  27. (or 'const value_type &') and returns a hashval_t value.
  28. - A typedef named 'compare_type' that is used to test when a value
  29. is found. This type is the comparison type. Usually, it will be the
  30. same as value_type. If it is not the same type, you must generally
  31. explicitly compute hash values and pass them to the hash table.
  32. - A static member function named 'equal' that takes a value_type
  33. and a compare_type, and returns a bool. Both arguments can be
  34. const references.
  35. - A static function named 'remove' that takes an value_type pointer
  36. and frees the memory allocated by it. This function is used when
  37. individual elements of the table need to be disposed of (e.g.,
  38. when deleting a hash table, removing elements from the table, etc).
  39. - An optional static function named 'keep_cache_entry'. This
  40. function is provided only for garbage-collected elements that
  41. are not marked by the normal gc mark pass. It describes what
  42. what should happen to the element at the end of the gc mark phase.
  43. The return value should be:
  44. - 0 if the element should be deleted
  45. - 1 if the element should be kept and needs to be marked
  46. - -1 if the element should be kept and is already marked.
  47. Returning -1 rather than 1 is purely an optimization.
  48. 3. The type of the hash table itself. (More later.)
  49. In very special circumstances, users may need to know about a fourth type.
  50. 4. The template type used to describe how hash table memory
  51. is allocated. This type is called the allocator type. It is
  52. parameterized on the value type. It provides two functions:
  53. - A static member function named 'data_alloc'. This function
  54. allocates the data elements in the table.
  55. - A static member function named 'data_free'. This function
  56. deallocates the data elements in the table.
  57. Hash table are instantiated with two type arguments.
  58. * The descriptor type, (2) above.
  59. * The allocator type, (4) above. In general, you will not need to
  60. provide your own allocator type. By default, hash tables will use
  61. the class template xcallocator, which uses malloc/free for allocation.
  62. DEFINING A DESCRIPTOR TYPE
  63. The first task in using the hash table is to describe the element type.
  64. We compose this into a few steps.
  65. 1. Decide on a removal policy for values stored in the table.
  66. hash-traits.h provides class templates for the four most common
  67. policies:
  68. * typed_free_remove implements the static 'remove' member function
  69. by calling free().
  70. * typed_noop_remove implements the static 'remove' member function
  71. by doing nothing.
  72. * ggc_remove implements the static 'remove' member by doing nothing,
  73. but instead provides routines for gc marking and for PCH streaming.
  74. Use this for garbage-collected data that needs to be preserved across
  75. collections.
  76. * ggc_cache_remove is like ggc_remove, except that it does not
  77. mark the entries during the normal gc mark phase. Instead it
  78. uses 'keep_cache_entry' (described above) to keep elements that
  79. were not collected and delete those that were. Use this for
  80. garbage-collected caches that should not in themselves stop
  81. the data from being collected.
  82. You can use these policies by simply deriving the descriptor type
  83. from one of those class template, with the appropriate argument.
  84. Otherwise, you need to write the static 'remove' member function
  85. in the descriptor class.
  86. 2. Choose a hash function. Write the static 'hash' member function.
  87. 3. Decide whether the lookup function should take as input an object
  88. of type value_type or something more restricted. Define compare_type
  89. accordingly.
  90. 4. Choose an equality testing function 'equal' that compares a value_type
  91. and a compare_type.
  92. If your elements are pointers, it is usually easiest to start with one
  93. of the generic pointer descriptors described below and override the bits
  94. you need to change.
  95. AN EXAMPLE DESCRIPTOR TYPE
  96. Suppose you want to put some_type into the hash table. You could define
  97. the descriptor type as follows.
  98. struct some_type_hasher : nofree_ptr_hash <some_type>
  99. // Deriving from nofree_ptr_hash means that we get a 'remove' that does
  100. // nothing. This choice is good for raw values.
  101. {
  102. static inline hashval_t hash (const value_type *);
  103. static inline bool equal (const value_type *, const compare_type *);
  104. };
  105. inline hashval_t
  106. some_type_hasher::hash (const value_type *e)
  107. { ... compute and return a hash value for E ... }
  108. inline bool
  109. some_type_hasher::equal (const value_type *p1, const compare_type *p2)
  110. { ... compare P1 vs P2. Return true if they are the 'same' ... }
  111. AN EXAMPLE HASH_TABLE DECLARATION
  112. To instantiate a hash table for some_type:
  113. hash_table <some_type_hasher> some_type_hash_table;
  114. There is no need to mention some_type directly, as the hash table will
  115. obtain it using some_type_hasher::value_type.
  116. You can then use any of the functions in hash_table's public interface.
  117. See hash_table for details. The interface is very similar to libiberty's
  118. htab_t.
  119. EASY DESCRIPTORS FOR POINTERS
  120. There are four descriptors for pointer elements, one for each of
  121. the removal policies above:
  122. * nofree_ptr_hash (based on typed_noop_remove)
  123. * free_ptr_hash (based on typed_free_remove)
  124. * ggc_ptr_hash (based on ggc_remove)
  125. * ggc_cache_ptr_hash (based on ggc_cache_remove)
  126. These descriptors hash and compare elements by their pointer value,
  127. rather than what they point to. So, to instantiate a hash table over
  128. pointers to whatever_type, without freeing the whatever_types, use:
  129. hash_table <nofree_ptr_hash <whatever_type> > whatever_type_hash_table;
  130. HASH TABLE ITERATORS
  131. The hash table provides standard C++ iterators. For example, consider a
  132. hash table of some_info. We wish to consume each element of the table:
  133. extern void consume (some_info *);
  134. We define a convenience typedef and the hash table:
  135. typedef hash_table <some_info_hasher> info_table_type;
  136. info_table_type info_table;
  137. Then we write the loop in typical C++ style:
  138. for (info_table_type::iterator iter = info_table.begin ();
  139. iter != info_table.end ();
  140. ++iter)
  141. if ((*iter).status == INFO_READY)
  142. consume (&*iter);
  143. Or with common sub-expression elimination:
  144. for (info_table_type::iterator iter = info_table.begin ();
  145. iter != info_table.end ();
  146. ++iter)
  147. {
  148. some_info &elem = *iter;
  149. if (elem.status == INFO_READY)
  150. consume (&elem);
  151. }
  152. One can also use a more typical GCC style:
  153. typedef some_info *some_info_p;
  154. some_info *elem_ptr;
  155. info_table_type::iterator iter;
  156. FOR_EACH_HASH_TABLE_ELEMENT (info_table, elem_ptr, some_info_p, iter)
  157. if (elem_ptr->status == INFO_READY)
  158. consume (elem_ptr);
  159. */
  160. #ifndef TYPED_HASHTAB_H
  161. #define TYPED_HASHTAB_H
  162. #include "statistics.h"
  163. #include "ggc.h"
  164. #include "vec.h"
  165. #include "hashtab.h"
  166. #include "inchash.h"
  167. #include "mem-stats-traits.h"
  168. #include "hash-traits.h"
  169. #include "hash-map-traits.h"
  170. template<typename, typename, typename> class hash_map;
  171. template<typename, typename> class hash_set;
  172. /* The ordinary memory allocator. */
  173. /* FIXME (crowl): This allocator may be extracted for wider sharing later. */
  174. template <typename Type>
  175. struct xcallocator
  176. {
  177. static Type *data_alloc (size_t count);
  178. static void data_free (Type *memory);
  179. };
  180. /* Allocate memory for COUNT data blocks. */
  181. template <typename Type>
  182. inline Type *
  183. xcallocator <Type>::data_alloc (size_t count)
  184. {
  185. return static_cast <Type *> (xcalloc (count, sizeof (Type)));
  186. }
  187. /* Free memory for data blocks. */
  188. template <typename Type>
  189. inline void
  190. xcallocator <Type>::data_free (Type *memory)
  191. {
  192. return ::free (memory);
  193. }
  194. /* Table of primes and their inversion information. */
  195. struct prime_ent
  196. {
  197. hashval_t prime;
  198. hashval_t inv;
  199. hashval_t inv_m2; /* inverse of prime-2 */
  200. hashval_t shift;
  201. };
  202. extern struct prime_ent const prime_tab[];
  203. /* Functions for computing hash table indexes. */
  204. extern unsigned int hash_table_higher_prime_index (unsigned long n)
  205. ATTRIBUTE_PURE;
  206. /* Return X % Y using multiplicative inverse values INV and SHIFT.
  207. The multiplicative inverses computed above are for 32-bit types,
  208. and requires that we be able to compute a highpart multiply.
  209. FIX: I am not at all convinced that
  210. 3 loads, 2 multiplications, 3 shifts, and 3 additions
  211. will be faster than
  212. 1 load and 1 modulus
  213. on modern systems running a compiler. */
  214. inline hashval_t
  215. mul_mod (hashval_t x, hashval_t y, hashval_t inv, int shift)
  216. {
  217. hashval_t t1, t2, t3, t4, q, r;
  218. t1 = ((uint64_t)x * inv) >> 32;
  219. t2 = x - t1;
  220. t3 = t2 >> 1;
  221. t4 = t1 + t3;
  222. q = t4 >> shift;
  223. r = x - (q * y);
  224. return r;
  225. }
  226. /* Compute the primary table index for HASH given current prime index. */
  227. inline hashval_t
  228. hash_table_mod1 (hashval_t hash, unsigned int index)
  229. {
  230. const struct prime_ent *p = &prime_tab[index];
  231. gcc_checking_assert (sizeof (hashval_t) * CHAR_BIT <= 32);
  232. return mul_mod (hash, p->prime, p->inv, p->shift);
  233. }
  234. /* Compute the secondary table index for HASH given current prime index. */
  235. inline hashval_t
  236. hash_table_mod2 (hashval_t hash, unsigned int index)
  237. {
  238. const struct prime_ent *p = &prime_tab[index];
  239. gcc_checking_assert (sizeof (hashval_t) * CHAR_BIT <= 32);
  240. return 1 + mul_mod (hash, p->prime - 2, p->inv_m2, p->shift);
  241. }
  242. class mem_usage;
  243. /* User-facing hash table type.
  244. The table stores elements of type Descriptor::value_type and uses
  245. the static descriptor functions described at the top of the file
  246. to hash, compare and remove elements.
  247. Specify the template Allocator to allocate and free memory.
  248. The default is xcallocator.
  249. Storage is an implementation detail and should not be used outside the
  250. hash table code.
  251. */
  252. template <typename Descriptor,
  253. template<typename Type> class Allocator = xcallocator>
  254. class hash_table
  255. {
  256. typedef typename Descriptor::value_type value_type;
  257. typedef typename Descriptor::compare_type compare_type;
  258. public:
  259. explicit hash_table (size_t, bool ggc = false,
  260. bool gather_mem_stats = GATHER_STATISTICS,
  261. mem_alloc_origin origin = HASH_TABLE_ORIGIN
  262. CXX_MEM_STAT_INFO);
  263. explicit hash_table (const hash_table &, bool ggc = false,
  264. bool gather_mem_stats = GATHER_STATISTICS,
  265. mem_alloc_origin origin = HASH_TABLE_ORIGIN
  266. CXX_MEM_STAT_INFO);
  267. ~hash_table ();
  268. /* Create a hash_table in gc memory. */
  269. static hash_table *
  270. create_ggc (size_t n CXX_MEM_STAT_INFO)
  271. {
  272. hash_table *table = ggc_alloc<hash_table> ();
  273. new (table) hash_table (n, true, GATHER_STATISTICS,
  274. HASH_TABLE_ORIGIN PASS_MEM_STAT);
  275. return table;
  276. }
  277. /* Current size (in entries) of the hash table. */
  278. size_t size () const { return m_size; }
  279. /* Return the current number of elements in this hash table. */
  280. size_t elements () const { return m_n_elements - m_n_deleted; }
  281. /* Return the current number of elements in this hash table. */
  282. size_t elements_with_deleted () const { return m_n_elements; }
  283. /* This function clears all entries in this hash table. */
  284. void empty () { if (elements ()) empty_slow (); }
  285. /* This function clears a specified SLOT in a hash table. It is
  286. useful when you've already done the lookup and don't want to do it
  287. again. */
  288. void clear_slot (value_type *);
  289. /* This function searches for a hash table entry equal to the given
  290. COMPARABLE element starting with the given HASH value. It cannot
  291. be used to insert or delete an element. */
  292. value_type &find_with_hash (const compare_type &, hashval_t);
  293. /* Like find_slot_with_hash, but compute the hash value from the element. */
  294. value_type &find (const value_type &value)
  295. {
  296. return find_with_hash (value, Descriptor::hash (value));
  297. }
  298. value_type *find_slot (const value_type &value, insert_option insert)
  299. {
  300. return find_slot_with_hash (value, Descriptor::hash (value), insert);
  301. }
  302. /* This function searches for a hash table slot containing an entry
  303. equal to the given COMPARABLE element and starting with the given
  304. HASH. To delete an entry, call this with insert=NO_INSERT, then
  305. call clear_slot on the slot returned (possibly after doing some
  306. checks). To insert an entry, call this with insert=INSERT, then
  307. write the value you want into the returned slot. When inserting an
  308. entry, NULL may be returned if memory allocation fails. */
  309. value_type *find_slot_with_hash (const compare_type &comparable,
  310. hashval_t hash, enum insert_option insert);
  311. /* This function deletes an element with the given COMPARABLE value
  312. from hash table starting with the given HASH. If there is no
  313. matching element in the hash table, this function does nothing. */
  314. void remove_elt_with_hash (const compare_type &, hashval_t);
  315. /* Like remove_elt_with_hash, but compute the hash value from the
  316. element. */
  317. void remove_elt (const value_type &value)
  318. {
  319. remove_elt_with_hash (value, Descriptor::hash (value));
  320. }
  321. /* This function scans over the entire hash table calling CALLBACK for
  322. each live entry. If CALLBACK returns false, the iteration stops.
  323. ARGUMENT is passed as CALLBACK's second argument. */
  324. template <typename Argument,
  325. int (*Callback) (value_type *slot, Argument argument)>
  326. void traverse_noresize (Argument argument);
  327. /* Like traverse_noresize, but does resize the table when it is too empty
  328. to improve effectivity of subsequent calls. */
  329. template <typename Argument,
  330. int (*Callback) (value_type *slot, Argument argument)>
  331. void traverse (Argument argument);
  332. class iterator
  333. {
  334. public:
  335. iterator () : m_slot (NULL), m_limit (NULL) {}
  336. iterator (value_type *slot, value_type *limit) :
  337. m_slot (slot), m_limit (limit) {}
  338. inline value_type &operator * () { return *m_slot; }
  339. void slide ();
  340. inline iterator &operator ++ ();
  341. bool operator != (const iterator &other) const
  342. {
  343. return m_slot != other.m_slot || m_limit != other.m_limit;
  344. }
  345. private:
  346. value_type *m_slot;
  347. value_type *m_limit;
  348. };
  349. iterator begin () const
  350. {
  351. iterator iter (m_entries, m_entries + m_size);
  352. iter.slide ();
  353. return iter;
  354. }
  355. iterator end () const { return iterator (); }
  356. double collisions () const
  357. {
  358. return m_searches ? static_cast <double> (m_collisions) / m_searches : 0;
  359. }
  360. private:
  361. template<typename T> friend void gt_ggc_mx (hash_table<T> *);
  362. template<typename T> friend void gt_pch_nx (hash_table<T> *);
  363. template<typename T> friend void
  364. hashtab_entry_note_pointers (void *, void *, gt_pointer_operator, void *);
  365. template<typename T, typename U, typename V> friend void
  366. gt_pch_nx (hash_map<T, U, V> *, gt_pointer_operator, void *);
  367. template<typename T, typename U> friend void gt_pch_nx (hash_set<T, U> *,
  368. gt_pointer_operator,
  369. void *);
  370. template<typename T> friend void gt_pch_nx (hash_table<T> *,
  371. gt_pointer_operator, void *);
  372. template<typename T> friend void gt_cleare_cache (hash_table<T> *);
  373. void empty_slow ();
  374. value_type *alloc_entries (size_t n CXX_MEM_STAT_INFO) const;
  375. value_type *find_empty_slot_for_expand (hashval_t);
  376. bool too_empty_p (unsigned int);
  377. void expand ();
  378. static bool is_deleted (value_type &v)
  379. {
  380. return Descriptor::is_deleted (v);
  381. }
  382. static bool is_empty (value_type &v)
  383. {
  384. return Descriptor::is_empty (v);
  385. }
  386. static void mark_deleted (value_type &v)
  387. {
  388. Descriptor::mark_deleted (v);
  389. }
  390. static void mark_empty (value_type &v)
  391. {
  392. Descriptor::mark_empty (v);
  393. }
  394. /* Table itself. */
  395. typename Descriptor::value_type *m_entries;
  396. size_t m_size;
  397. /* Current number of elements including also deleted elements. */
  398. size_t m_n_elements;
  399. /* Current number of deleted elements in the table. */
  400. size_t m_n_deleted;
  401. /* The following member is used for debugging. Its value is number
  402. of all calls of `htab_find_slot' for the hash table. */
  403. unsigned int m_searches;
  404. /* The following member is used for debugging. Its value is number
  405. of collisions fixed for time of work with the hash table. */
  406. unsigned int m_collisions;
  407. /* Current size (in entries) of the hash table, as an index into the
  408. table of primes. */
  409. unsigned int m_size_prime_index;
  410. /* if m_entries is stored in ggc memory. */
  411. bool m_ggc;
  412. /* If we should gather memory statistics for the table. */
  413. bool m_gather_mem_stats;
  414. };
  415. /* As mem-stats.h heavily utilizes hash maps (hash tables), we have to include
  416. mem-stats.h after hash_table declaration. */
  417. #include "mem-stats.h"
  418. #include "hash-map.h"
  419. extern mem_alloc_description<mem_usage> hash_table_usage;
  420. /* Support function for statistics. */
  421. extern void dump_hash_table_loc_statistics (void);
  422. template<typename Descriptor, template<typename Type> class Allocator>
  423. hash_table<Descriptor, Allocator>::hash_table (size_t size, bool ggc, bool
  424. gather_mem_stats,
  425. mem_alloc_origin origin
  426. MEM_STAT_DECL) :
  427. m_n_elements (0), m_n_deleted (0), m_searches (0), m_collisions (0),
  428. m_ggc (ggc), m_gather_mem_stats (gather_mem_stats)
  429. {
  430. unsigned int size_prime_index;
  431. size_prime_index = hash_table_higher_prime_index (size);
  432. size = prime_tab[size_prime_index].prime;
  433. if (m_gather_mem_stats)
  434. hash_table_usage.register_descriptor (this, origin, ggc
  435. FINAL_PASS_MEM_STAT);
  436. m_entries = alloc_entries (size PASS_MEM_STAT);
  437. m_size = size;
  438. m_size_prime_index = size_prime_index;
  439. }
  440. template<typename Descriptor, template<typename Type> class Allocator>
  441. hash_table<Descriptor, Allocator>::hash_table (const hash_table &h, bool ggc,
  442. bool gather_mem_stats,
  443. mem_alloc_origin origin
  444. MEM_STAT_DECL) :
  445. m_n_elements (h.m_n_elements), m_n_deleted (h.m_n_deleted),
  446. m_searches (0), m_collisions (0), m_ggc (ggc),
  447. m_gather_mem_stats (gather_mem_stats)
  448. {
  449. size_t size = h.m_size;
  450. if (m_gather_mem_stats)
  451. hash_table_usage.register_descriptor (this, origin, ggc
  452. FINAL_PASS_MEM_STAT);
  453. value_type *nentries = alloc_entries (size PASS_MEM_STAT);
  454. for (size_t i = 0; i < size; ++i)
  455. {
  456. value_type &entry = h.m_entries[i];
  457. if (is_deleted (entry))
  458. mark_deleted (nentries[i]);
  459. else if (!is_empty (entry))
  460. nentries[i] = entry;
  461. }
  462. m_entries = nentries;
  463. m_size = size;
  464. m_size_prime_index = h.m_size_prime_index;
  465. }
  466. template<typename Descriptor, template<typename Type> class Allocator>
  467. hash_table<Descriptor, Allocator>::~hash_table ()
  468. {
  469. for (size_t i = m_size - 1; i < m_size; i--)
  470. if (!is_empty (m_entries[i]) && !is_deleted (m_entries[i]))
  471. Descriptor::remove (m_entries[i]);
  472. if (!m_ggc)
  473. Allocator <value_type> ::data_free (m_entries);
  474. else
  475. ggc_free (m_entries);
  476. if (m_gather_mem_stats)
  477. hash_table_usage.release_instance_overhead (this,
  478. sizeof (value_type) * m_size,
  479. true);
  480. }
  481. /* This function returns an array of empty hash table elements. */
  482. template<typename Descriptor, template<typename Type> class Allocator>
  483. inline typename hash_table<Descriptor, Allocator>::value_type *
  484. hash_table<Descriptor, Allocator>::alloc_entries (size_t n MEM_STAT_DECL) const
  485. {
  486. value_type *nentries;
  487. if (m_gather_mem_stats)
  488. hash_table_usage.register_instance_overhead (sizeof (value_type) * n, this);
  489. if (!m_ggc)
  490. nentries = Allocator <value_type> ::data_alloc (n);
  491. else
  492. nentries = ::ggc_cleared_vec_alloc<value_type> (n PASS_MEM_STAT);
  493. gcc_assert (nentries != NULL);
  494. for (size_t i = 0; i < n; i++)
  495. mark_empty (nentries[i]);
  496. return nentries;
  497. }
  498. /* Similar to find_slot, but without several unwanted side effects:
  499. - Does not call equal when it finds an existing entry.
  500. - Does not change the count of elements/searches/collisions in the
  501. hash table.
  502. This function also assumes there are no deleted entries in the table.
  503. HASH is the hash value for the element to be inserted. */
  504. template<typename Descriptor, template<typename Type> class Allocator>
  505. typename hash_table<Descriptor, Allocator>::value_type *
  506. hash_table<Descriptor, Allocator>::find_empty_slot_for_expand (hashval_t hash)
  507. {
  508. hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
  509. size_t size = m_size;
  510. value_type *slot = m_entries + index;
  511. hashval_t hash2;
  512. if (is_empty (*slot))
  513. return slot;
  514. gcc_checking_assert (!is_deleted (*slot));
  515. hash2 = hash_table_mod2 (hash, m_size_prime_index);
  516. for (;;)
  517. {
  518. index += hash2;
  519. if (index >= size)
  520. index -= size;
  521. slot = m_entries + index;
  522. if (is_empty (*slot))
  523. return slot;
  524. gcc_checking_assert (!is_deleted (*slot));
  525. }
  526. }
  527. /* Return true if the current table is excessively big for ELTS elements. */
  528. template<typename Descriptor, template<typename Type> class Allocator>
  529. inline bool
  530. hash_table<Descriptor, Allocator>::too_empty_p (unsigned int elts)
  531. {
  532. return elts * 8 < m_size && m_size > 32;
  533. }
  534. /* The following function changes size of memory allocated for the
  535. entries and repeatedly inserts the table elements. The occupancy
  536. of the table after the call will be about 50%. Naturally the hash
  537. table must already exist. Remember also that the place of the
  538. table entries is changed. If memory allocation fails, this function
  539. will abort. */
  540. template<typename Descriptor, template<typename Type> class Allocator>
  541. void
  542. hash_table<Descriptor, Allocator>::expand ()
  543. {
  544. value_type *oentries = m_entries;
  545. unsigned int oindex = m_size_prime_index;
  546. size_t osize = size ();
  547. value_type *olimit = oentries + osize;
  548. size_t elts = elements ();
  549. /* Resize only when table after removal of unused elements is either
  550. too full or too empty. */
  551. unsigned int nindex;
  552. size_t nsize;
  553. if (elts * 2 > osize || too_empty_p (elts))
  554. {
  555. nindex = hash_table_higher_prime_index (elts * 2);
  556. nsize = prime_tab[nindex].prime;
  557. }
  558. else
  559. {
  560. nindex = oindex;
  561. nsize = osize;
  562. }
  563. value_type *nentries = alloc_entries (nsize);
  564. if (m_gather_mem_stats)
  565. hash_table_usage.release_instance_overhead (this, sizeof (value_type)
  566. * osize);
  567. m_entries = nentries;
  568. m_size = nsize;
  569. m_size_prime_index = nindex;
  570. m_n_elements -= m_n_deleted;
  571. m_n_deleted = 0;
  572. value_type *p = oentries;
  573. do
  574. {
  575. value_type &x = *p;
  576. if (!is_empty (x) && !is_deleted (x))
  577. {
  578. value_type *q = find_empty_slot_for_expand (Descriptor::hash (x));
  579. *q = x;
  580. }
  581. p++;
  582. }
  583. while (p < olimit);
  584. if (!m_ggc)
  585. Allocator <value_type> ::data_free (oentries);
  586. else
  587. ggc_free (oentries);
  588. }
  589. /* Implements empty() in cases where it isn't a no-op. */
  590. template<typename Descriptor, template<typename Type> class Allocator>
  591. void
  592. hash_table<Descriptor, Allocator>::empty_slow ()
  593. {
  594. size_t size = m_size;
  595. size_t nsize = size;
  596. value_type *entries = m_entries;
  597. int i;
  598. for (i = size - 1; i >= 0; i--)
  599. if (!is_empty (entries[i]) && !is_deleted (entries[i]))
  600. Descriptor::remove (entries[i]);
  601. /* Instead of clearing megabyte, downsize the table. */
  602. if (size > 1024*1024 / sizeof (value_type))
  603. nsize = 1024 / sizeof (value_type);
  604. else if (too_empty_p (m_n_elements))
  605. nsize = m_n_elements * 2;
  606. if (nsize != size)
  607. {
  608. int nindex = hash_table_higher_prime_index (nsize);
  609. int nsize = prime_tab[nindex].prime;
  610. if (!m_ggc)
  611. Allocator <value_type> ::data_free (m_entries);
  612. else
  613. ggc_free (m_entries);
  614. m_entries = alloc_entries (nsize);
  615. m_size = nsize;
  616. m_size_prime_index = nindex;
  617. }
  618. else
  619. {
  620. #ifndef BROKEN_VALUE_INITIALIZATION
  621. for ( ; size; ++entries, --size)
  622. *entries = value_type ();
  623. #else
  624. memset (entries, 0, size * sizeof (value_type));
  625. #endif
  626. }
  627. m_n_deleted = 0;
  628. m_n_elements = 0;
  629. }
  630. /* This function clears a specified SLOT in a hash table. It is
  631. useful when you've already done the lookup and don't want to do it
  632. again. */
  633. template<typename Descriptor, template<typename Type> class Allocator>
  634. void
  635. hash_table<Descriptor, Allocator>::clear_slot (value_type *slot)
  636. {
  637. gcc_checking_assert (!(slot < m_entries || slot >= m_entries + size ()
  638. || is_empty (*slot) || is_deleted (*slot)));
  639. Descriptor::remove (*slot);
  640. mark_deleted (*slot);
  641. m_n_deleted++;
  642. }
  643. /* This function searches for a hash table entry equal to the given
  644. COMPARABLE element starting with the given HASH value. It cannot
  645. be used to insert or delete an element. */
  646. template<typename Descriptor, template<typename Type> class Allocator>
  647. typename hash_table<Descriptor, Allocator>::value_type &
  648. hash_table<Descriptor, Allocator>
  649. ::find_with_hash (const compare_type &comparable, hashval_t hash)
  650. {
  651. m_searches++;
  652. size_t size = m_size;
  653. hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
  654. value_type *entry = &m_entries[index];
  655. if (is_empty (*entry)
  656. || (!is_deleted (*entry) && Descriptor::equal (*entry, comparable)))
  657. return *entry;
  658. hashval_t hash2 = hash_table_mod2 (hash, m_size_prime_index);
  659. for (;;)
  660. {
  661. m_collisions++;
  662. index += hash2;
  663. if (index >= size)
  664. index -= size;
  665. entry = &m_entries[index];
  666. if (is_empty (*entry)
  667. || (!is_deleted (*entry) && Descriptor::equal (*entry, comparable)))
  668. return *entry;
  669. }
  670. }
  671. /* This function searches for a hash table slot containing an entry
  672. equal to the given COMPARABLE element and starting with the given
  673. HASH. To delete an entry, call this with insert=NO_INSERT, then
  674. call clear_slot on the slot returned (possibly after doing some
  675. checks). To insert an entry, call this with insert=INSERT, then
  676. write the value you want into the returned slot. When inserting an
  677. entry, NULL may be returned if memory allocation fails. */
  678. template<typename Descriptor, template<typename Type> class Allocator>
  679. typename hash_table<Descriptor, Allocator>::value_type *
  680. hash_table<Descriptor, Allocator>
  681. ::find_slot_with_hash (const compare_type &comparable, hashval_t hash,
  682. enum insert_option insert)
  683. {
  684. if (insert == INSERT && m_size * 3 <= m_n_elements * 4)
  685. expand ();
  686. m_searches++;
  687. value_type *first_deleted_slot = NULL;
  688. hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
  689. hashval_t hash2 = hash_table_mod2 (hash, m_size_prime_index);
  690. value_type *entry = &m_entries[index];
  691. size_t size = m_size;
  692. if (is_empty (*entry))
  693. goto empty_entry;
  694. else if (is_deleted (*entry))
  695. first_deleted_slot = &m_entries[index];
  696. else if (Descriptor::equal (*entry, comparable))
  697. return &m_entries[index];
  698. for (;;)
  699. {
  700. m_collisions++;
  701. index += hash2;
  702. if (index >= size)
  703. index -= size;
  704. entry = &m_entries[index];
  705. if (is_empty (*entry))
  706. goto empty_entry;
  707. else if (is_deleted (*entry))
  708. {
  709. if (!first_deleted_slot)
  710. first_deleted_slot = &m_entries[index];
  711. }
  712. else if (Descriptor::equal (*entry, comparable))
  713. return &m_entries[index];
  714. }
  715. empty_entry:
  716. if (insert == NO_INSERT)
  717. return NULL;
  718. if (first_deleted_slot)
  719. {
  720. m_n_deleted--;
  721. mark_empty (*first_deleted_slot);
  722. return first_deleted_slot;
  723. }
  724. m_n_elements++;
  725. return &m_entries[index];
  726. }
  727. /* This function deletes an element with the given COMPARABLE value
  728. from hash table starting with the given HASH. If there is no
  729. matching element in the hash table, this function does nothing. */
  730. template<typename Descriptor, template<typename Type> class Allocator>
  731. void
  732. hash_table<Descriptor, Allocator>
  733. ::remove_elt_with_hash (const compare_type &comparable, hashval_t hash)
  734. {
  735. value_type *slot = find_slot_with_hash (comparable, hash, NO_INSERT);
  736. if (is_empty (*slot))
  737. return;
  738. Descriptor::remove (*slot);
  739. mark_deleted (*slot);
  740. m_n_deleted++;
  741. }
  742. /* This function scans over the entire hash table calling CALLBACK for
  743. each live entry. If CALLBACK returns false, the iteration stops.
  744. ARGUMENT is passed as CALLBACK's second argument. */
  745. template<typename Descriptor,
  746. template<typename Type> class Allocator>
  747. template<typename Argument,
  748. int (*Callback)
  749. (typename hash_table<Descriptor, Allocator>::value_type *slot,
  750. Argument argument)>
  751. void
  752. hash_table<Descriptor, Allocator>::traverse_noresize (Argument argument)
  753. {
  754. value_type *slot = m_entries;
  755. value_type *limit = slot + size ();
  756. do
  757. {
  758. value_type &x = *slot;
  759. if (!is_empty (x) && !is_deleted (x))
  760. if (! Callback (slot, argument))
  761. break;
  762. }
  763. while (++slot < limit);
  764. }
  765. /* Like traverse_noresize, but does resize the table when it is too empty
  766. to improve effectivity of subsequent calls. */
  767. template <typename Descriptor,
  768. template <typename Type> class Allocator>
  769. template <typename Argument,
  770. int (*Callback)
  771. (typename hash_table<Descriptor, Allocator>::value_type *slot,
  772. Argument argument)>
  773. void
  774. hash_table<Descriptor, Allocator>::traverse (Argument argument)
  775. {
  776. if (too_empty_p (elements ()))
  777. expand ();
  778. traverse_noresize <Argument, Callback> (argument);
  779. }
  780. /* Slide down the iterator slots until an active entry is found. */
  781. template<typename Descriptor, template<typename Type> class Allocator>
  782. void
  783. hash_table<Descriptor, Allocator>::iterator::slide ()
  784. {
  785. for ( ; m_slot < m_limit; ++m_slot )
  786. {
  787. value_type &x = *m_slot;
  788. if (!is_empty (x) && !is_deleted (x))
  789. return;
  790. }
  791. m_slot = NULL;
  792. m_limit = NULL;
  793. }
  794. /* Bump the iterator. */
  795. template<typename Descriptor, template<typename Type> class Allocator>
  796. inline typename hash_table<Descriptor, Allocator>::iterator &
  797. hash_table<Descriptor, Allocator>::iterator::operator ++ ()
  798. {
  799. ++m_slot;
  800. slide ();
  801. return *this;
  802. }
  803. /* Iterate through the elements of hash_table HTAB,
  804. using hash_table <....>::iterator ITER,
  805. storing each element in RESULT, which is of type TYPE. */
  806. #define FOR_EACH_HASH_TABLE_ELEMENT(HTAB, RESULT, TYPE, ITER) \
  807. for ((ITER) = (HTAB).begin (); \
  808. (ITER) != (HTAB).end () ? (RESULT = *(ITER) , true) : false; \
  809. ++(ITER))
  810. /* ggc walking routines. */
  811. template<typename E>
  812. static inline void
  813. gt_ggc_mx (hash_table<E> *h)
  814. {
  815. typedef hash_table<E> table;
  816. if (!ggc_test_and_set_mark (h->m_entries))
  817. return;
  818. for (size_t i = 0; i < h->m_size; i++)
  819. {
  820. if (table::is_empty (h->m_entries[i])
  821. || table::is_deleted (h->m_entries[i]))
  822. continue;
  823. /* Use ggc_maxbe_mx so we don't mark right away for cache tables; we'll
  824. mark in gt_cleare_cache if appropriate. */
  825. E::ggc_maybe_mx (h->m_entries[i]);
  826. }
  827. }
  828. template<typename D>
  829. static inline void
  830. hashtab_entry_note_pointers (void *obj, void *h, gt_pointer_operator op,
  831. void *cookie)
  832. {
  833. hash_table<D> *map = static_cast<hash_table<D> *> (h);
  834. gcc_checking_assert (map->m_entries == obj);
  835. for (size_t i = 0; i < map->m_size; i++)
  836. {
  837. typedef hash_table<D> table;
  838. if (table::is_empty (map->m_entries[i])
  839. || table::is_deleted (map->m_entries[i]))
  840. continue;
  841. D::pch_nx (map->m_entries[i], op, cookie);
  842. }
  843. }
  844. template<typename D>
  845. static void
  846. gt_pch_nx (hash_table<D> *h)
  847. {
  848. bool success
  849. = gt_pch_note_object (h->m_entries, h, hashtab_entry_note_pointers<D>);
  850. gcc_checking_assert (success);
  851. for (size_t i = 0; i < h->m_size; i++)
  852. {
  853. if (hash_table<D>::is_empty (h->m_entries[i])
  854. || hash_table<D>::is_deleted (h->m_entries[i]))
  855. continue;
  856. D::pch_nx (h->m_entries[i]);
  857. }
  858. }
  859. template<typename D>
  860. static inline void
  861. gt_pch_nx (hash_table<D> *h, gt_pointer_operator op, void *cookie)
  862. {
  863. op (&h->m_entries, cookie);
  864. }
  865. template<typename H>
  866. inline void
  867. gt_cleare_cache (hash_table<H> *h)
  868. {
  869. typedef hash_table<H> table;
  870. if (!h)
  871. return;
  872. for (typename table::iterator iter = h->begin (); iter != h->end (); ++iter)
  873. if (!table::is_empty (*iter) && !table::is_deleted (*iter))
  874. {
  875. int res = H::keep_cache_entry (*iter);
  876. if (res == 0)
  877. h->clear_slot (&*iter);
  878. else if (res != -1)
  879. H::ggc_mx (*iter);
  880. }
  881. }
  882. #endif /* TYPED_HASHTAB_H */