hash-table.h 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /* A type-safe hash table template.
  2. Copyright (C) 2012-2019 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. If a hash table is used only in some rare cases, it is possible
  120. to construct the hash_table lazily before first use. This is done
  121. through:
  122. hash_table <some_type_hasher, true> some_type_hash_table;
  123. which will cause whatever methods actually need the allocated entries
  124. array to allocate it later.
  125. EASY DESCRIPTORS FOR POINTERS
  126. There are four descriptors for pointer elements, one for each of
  127. the removal policies above:
  128. * nofree_ptr_hash (based on typed_noop_remove)
  129. * free_ptr_hash (based on typed_free_remove)
  130. * ggc_ptr_hash (based on ggc_remove)
  131. * ggc_cache_ptr_hash (based on ggc_cache_remove)
  132. These descriptors hash and compare elements by their pointer value,
  133. rather than what they point to. So, to instantiate a hash table over
  134. pointers to whatever_type, without freeing the whatever_types, use:
  135. hash_table <nofree_ptr_hash <whatever_type> > whatever_type_hash_table;
  136. HASH TABLE ITERATORS
  137. The hash table provides standard C++ iterators. For example, consider a
  138. hash table of some_info. We wish to consume each element of the table:
  139. extern void consume (some_info *);
  140. We define a convenience typedef and the hash table:
  141. typedef hash_table <some_info_hasher> info_table_type;
  142. info_table_type info_table;
  143. Then we write the loop in typical C++ style:
  144. for (info_table_type::iterator iter = info_table.begin ();
  145. iter != info_table.end ();
  146. ++iter)
  147. if ((*iter).status == INFO_READY)
  148. consume (&*iter);
  149. Or with common sub-expression elimination:
  150. for (info_table_type::iterator iter = info_table.begin ();
  151. iter != info_table.end ();
  152. ++iter)
  153. {
  154. some_info &elem = *iter;
  155. if (elem.status == INFO_READY)
  156. consume (&elem);
  157. }
  158. One can also use a more typical GCC style:
  159. typedef some_info *some_info_p;
  160. some_info *elem_ptr;
  161. info_table_type::iterator iter;
  162. FOR_EACH_HASH_TABLE_ELEMENT (info_table, elem_ptr, some_info_p, iter)
  163. if (elem_ptr->status == INFO_READY)
  164. consume (elem_ptr);
  165. */
  166. #ifndef TYPED_HASHTAB_H
  167. #define TYPED_HASHTAB_H
  168. #include "statistics.h"
  169. #include "ggc.h"
  170. #include "vec.h"
  171. #include "hashtab.h"
  172. #include "inchash.h"
  173. #include "mem-stats-traits.h"
  174. #include "hash-traits.h"
  175. #include "hash-map-traits.h"
  176. template<typename, typename, typename> class hash_map;
  177. template<typename, bool, typename> class hash_set;
  178. /* The ordinary memory allocator. */
  179. /* FIXME (crowl): This allocator may be extracted for wider sharing later. */
  180. template <typename Type>
  181. struct xcallocator
  182. {
  183. static Type *data_alloc (size_t count);
  184. static void data_free (Type *memory);
  185. };
  186. /* Allocate memory for COUNT data blocks. */
  187. template <typename Type>
  188. inline Type *
  189. xcallocator <Type>::data_alloc (size_t count)
  190. {
  191. return static_cast <Type *> (xcalloc (count, sizeof (Type)));
  192. }
  193. /* Free memory for data blocks. */
  194. template <typename Type>
  195. inline void
  196. xcallocator <Type>::data_free (Type *memory)
  197. {
  198. return ::free (memory);
  199. }
  200. /* Table of primes and their inversion information. */
  201. struct prime_ent
  202. {
  203. hashval_t prime;
  204. hashval_t inv;
  205. hashval_t inv_m2; /* inverse of prime-2 */
  206. hashval_t shift;
  207. };
  208. extern struct prime_ent const prime_tab[];
  209. /* Functions for computing hash table indexes. */
  210. extern unsigned int hash_table_higher_prime_index (unsigned long n)
  211. ATTRIBUTE_PURE;
  212. /* Return X % Y using multiplicative inverse values INV and SHIFT.
  213. The multiplicative inverses computed above are for 32-bit types,
  214. and requires that we be able to compute a highpart multiply.
  215. FIX: I am not at all convinced that
  216. 3 loads, 2 multiplications, 3 shifts, and 3 additions
  217. will be faster than
  218. 1 load and 1 modulus
  219. on modern systems running a compiler. */
  220. inline hashval_t
  221. mul_mod (hashval_t x, hashval_t y, hashval_t inv, int shift)
  222. {
  223. hashval_t t1, t2, t3, t4, q, r;
  224. t1 = ((uint64_t)x * inv) >> 32;
  225. t2 = x - t1;
  226. t3 = t2 >> 1;
  227. t4 = t1 + t3;
  228. q = t4 >> shift;
  229. r = x - (q * y);
  230. return r;
  231. }
  232. /* Compute the primary table index for HASH given current prime index. */
  233. inline hashval_t
  234. hash_table_mod1 (hashval_t hash, unsigned int index)
  235. {
  236. const struct prime_ent *p = &prime_tab[index];
  237. gcc_checking_assert (sizeof (hashval_t) * CHAR_BIT <= 32);
  238. return mul_mod (hash, p->prime, p->inv, p->shift);
  239. }
  240. /* Compute the secondary table index for HASH given current prime index. */
  241. inline hashval_t
  242. hash_table_mod2 (hashval_t hash, unsigned int index)
  243. {
  244. const struct prime_ent *p = &prime_tab[index];
  245. gcc_checking_assert (sizeof (hashval_t) * CHAR_BIT <= 32);
  246. return 1 + mul_mod (hash, p->prime - 2, p->inv_m2, p->shift);
  247. }
  248. class mem_usage;
  249. /* User-facing hash table type.
  250. The table stores elements of type Descriptor::value_type and uses
  251. the static descriptor functions described at the top of the file
  252. to hash, compare and remove elements.
  253. Specify the template Allocator to allocate and free memory.
  254. The default is xcallocator.
  255. Storage is an implementation detail and should not be used outside the
  256. hash table code.
  257. */
  258. template <typename Descriptor, bool Lazy = false,
  259. template<typename Type> class Allocator = xcallocator>
  260. class hash_table
  261. {
  262. typedef typename Descriptor::value_type value_type;
  263. typedef typename Descriptor::compare_type compare_type;
  264. public:
  265. explicit hash_table (size_t, bool ggc = false,
  266. bool gather_mem_stats = GATHER_STATISTICS,
  267. mem_alloc_origin origin = HASH_TABLE_ORIGIN
  268. CXX_MEM_STAT_INFO);
  269. explicit hash_table (const hash_table &, bool ggc = false,
  270. bool gather_mem_stats = GATHER_STATISTICS,
  271. mem_alloc_origin origin = HASH_TABLE_ORIGIN
  272. CXX_MEM_STAT_INFO);
  273. ~hash_table ();
  274. /* Create a hash_table in gc memory. */
  275. static hash_table *
  276. create_ggc (size_t n CXX_MEM_STAT_INFO)
  277. {
  278. hash_table *table = ggc_alloc<hash_table> ();
  279. new (table) hash_table (n, true, GATHER_STATISTICS,
  280. HASH_TABLE_ORIGIN PASS_MEM_STAT);
  281. return table;
  282. }
  283. /* Current size (in entries) of the hash table. */
  284. size_t size () const { return m_size; }
  285. /* Return the current number of elements in this hash table. */
  286. size_t elements () const { return m_n_elements - m_n_deleted; }
  287. /* Return the current number of elements in this hash table. */
  288. size_t elements_with_deleted () const { return m_n_elements; }
  289. /* This function clears all entries in this hash table. */
  290. void empty () { if (elements ()) empty_slow (); }
  291. /* This function clears a specified SLOT in a hash table. It is
  292. useful when you've already done the lookup and don't want to do it
  293. again. */
  294. void clear_slot (value_type *);
  295. /* This function searches for a hash table entry equal to the given
  296. COMPARABLE element starting with the given HASH value. It cannot
  297. be used to insert or delete an element. */
  298. value_type &find_with_hash (const compare_type &, hashval_t);
  299. /* Like find_slot_with_hash, but compute the hash value from the element. */
  300. value_type &find (const value_type &value)
  301. {
  302. return find_with_hash (value, Descriptor::hash (value));
  303. }
  304. value_type *find_slot (const value_type &value, insert_option insert)
  305. {
  306. return find_slot_with_hash (value, Descriptor::hash (value), insert);
  307. }
  308. /* This function searches for a hash table slot containing an entry
  309. equal to the given COMPARABLE element and starting with the given
  310. HASH. To delete an entry, call this with insert=NO_INSERT, then
  311. call clear_slot on the slot returned (possibly after doing some
  312. checks). To insert an entry, call this with insert=INSERT, then
  313. write the value you want into the returned slot. When inserting an
  314. entry, NULL may be returned if memory allocation fails. */
  315. value_type *find_slot_with_hash (const compare_type &comparable,
  316. hashval_t hash, enum insert_option insert);
  317. /* This function deletes an element with the given COMPARABLE value
  318. from hash table starting with the given HASH. If there is no
  319. matching element in the hash table, this function does nothing. */
  320. void remove_elt_with_hash (const compare_type &, hashval_t);
  321. /* Like remove_elt_with_hash, but compute the hash value from the
  322. element. */
  323. void remove_elt (const value_type &value)
  324. {
  325. remove_elt_with_hash (value, Descriptor::hash (value));
  326. }
  327. /* This function scans over the entire hash table calling CALLBACK for
  328. each live entry. If CALLBACK returns false, the iteration stops.
  329. ARGUMENT is passed as CALLBACK's second argument. */
  330. template <typename Argument,
  331. int (*Callback) (value_type *slot, Argument argument)>
  332. void traverse_noresize (Argument argument);
  333. /* Like traverse_noresize, but does resize the table when it is too empty
  334. to improve effectivity of subsequent calls. */
  335. template <typename Argument,
  336. int (*Callback) (value_type *slot, Argument argument)>
  337. void traverse (Argument argument);
  338. class iterator
  339. {
  340. public:
  341. iterator () : m_slot (NULL), m_limit (NULL) {}
  342. iterator (value_type *slot, value_type *limit) :
  343. m_slot (slot), m_limit (limit) {}
  344. inline value_type &operator * () { return *m_slot; }
  345. void slide ();
  346. inline iterator &operator ++ ();
  347. bool operator != (const iterator &other) const
  348. {
  349. return m_slot != other.m_slot || m_limit != other.m_limit;
  350. }
  351. private:
  352. value_type *m_slot;
  353. value_type *m_limit;
  354. };
  355. iterator begin () const
  356. {
  357. if (Lazy && m_entries == NULL)
  358. return iterator ();
  359. iterator iter (m_entries, m_entries + m_size);
  360. iter.slide ();
  361. return iter;
  362. }
  363. iterator end () const { return iterator (); }
  364. double collisions () const
  365. {
  366. return m_searches ? static_cast <double> (m_collisions) / m_searches : 0;
  367. }
  368. private:
  369. template<typename T> friend void gt_ggc_mx (hash_table<T> *);
  370. template<typename T> friend void gt_pch_nx (hash_table<T> *);
  371. template<typename T> friend void
  372. hashtab_entry_note_pointers (void *, void *, gt_pointer_operator, void *);
  373. template<typename T, typename U, typename V> friend void
  374. gt_pch_nx (hash_map<T, U, V> *, gt_pointer_operator, void *);
  375. template<typename T, typename U>
  376. friend void gt_pch_nx (hash_set<T, false, U> *, gt_pointer_operator, void *);
  377. template<typename T> friend void gt_pch_nx (hash_table<T> *,
  378. gt_pointer_operator, void *);
  379. template<typename T> friend void gt_cleare_cache (hash_table<T> *);
  380. void empty_slow ();
  381. value_type *alloc_entries (size_t n CXX_MEM_STAT_INFO) const;
  382. value_type *find_empty_slot_for_expand (hashval_t);
  383. bool too_empty_p (unsigned int);
  384. void expand ();
  385. static bool is_deleted (value_type &v)
  386. {
  387. return Descriptor::is_deleted (v);
  388. }
  389. static bool is_empty (value_type &v)
  390. {
  391. return Descriptor::is_empty (v);
  392. }
  393. static void mark_deleted (value_type &v)
  394. {
  395. Descriptor::mark_deleted (v);
  396. }
  397. static void mark_empty (value_type &v)
  398. {
  399. Descriptor::mark_empty (v);
  400. }
  401. /* Table itself. */
  402. typename Descriptor::value_type *m_entries;
  403. size_t m_size;
  404. /* Current number of elements including also deleted elements. */
  405. size_t m_n_elements;
  406. /* Current number of deleted elements in the table. */
  407. size_t m_n_deleted;
  408. /* The following member is used for debugging. Its value is number
  409. of all calls of `htab_find_slot' for the hash table. */
  410. unsigned int m_searches;
  411. /* The following member is used for debugging. Its value is number
  412. of collisions fixed for time of work with the hash table. */
  413. unsigned int m_collisions;
  414. /* Current size (in entries) of the hash table, as an index into the
  415. table of primes. */
  416. unsigned int m_size_prime_index;
  417. /* if m_entries is stored in ggc memory. */
  418. bool m_ggc;
  419. /* If we should gather memory statistics for the table. */
  420. #if GATHER_STATISTICS
  421. bool m_gather_mem_stats;
  422. #else
  423. static const bool m_gather_mem_stats = false;
  424. #endif
  425. };
  426. /* As mem-stats.h heavily utilizes hash maps (hash tables), we have to include
  427. mem-stats.h after hash_table declaration. */
  428. #include "mem-stats.h"
  429. #include "hash-map.h"
  430. extern mem_alloc_description<mem_usage>& hash_table_usage (void);
  431. /* Support function for statistics. */
  432. extern void dump_hash_table_loc_statistics (void);
  433. template<typename Descriptor, bool Lazy,
  434. template<typename Type> class Allocator>
  435. hash_table<Descriptor, Lazy, Allocator>::hash_table (size_t size, bool ggc,
  436. bool gather_mem_stats
  437. ATTRIBUTE_UNUSED,
  438. mem_alloc_origin origin
  439. MEM_STAT_DECL) :
  440. m_n_elements (0), m_n_deleted (0), m_searches (0), m_collisions (0),
  441. m_ggc (ggc)
  442. #if GATHER_STATISTICS
  443. , m_gather_mem_stats (gather_mem_stats)
  444. #endif
  445. {
  446. unsigned int size_prime_index;
  447. size_prime_index = hash_table_higher_prime_index (size);
  448. size = prime_tab[size_prime_index].prime;
  449. if (m_gather_mem_stats)
  450. hash_table_usage ().register_descriptor (this, origin, ggc
  451. FINAL_PASS_MEM_STAT);
  452. if (Lazy)
  453. m_entries = NULL;
  454. else
  455. m_entries = alloc_entries (size PASS_MEM_STAT);
  456. m_size = size;
  457. m_size_prime_index = size_prime_index;
  458. }
  459. template<typename Descriptor, bool Lazy,
  460. template<typename Type> class Allocator>
  461. hash_table<Descriptor, Lazy, Allocator>::hash_table (const hash_table &h,
  462. bool ggc,
  463. bool gather_mem_stats
  464. ATTRIBUTE_UNUSED,
  465. mem_alloc_origin origin
  466. MEM_STAT_DECL) :
  467. m_n_elements (h.m_n_elements), m_n_deleted (h.m_n_deleted),
  468. m_searches (0), m_collisions (0), m_ggc (ggc)
  469. #if GATHER_STATISTICS
  470. , m_gather_mem_stats (gather_mem_stats)
  471. #endif
  472. {
  473. size_t size = h.m_size;
  474. if (m_gather_mem_stats)
  475. hash_table_usage ().register_descriptor (this, origin, ggc
  476. FINAL_PASS_MEM_STAT);
  477. if (Lazy && h.m_entries == NULL)
  478. m_entries = NULL;
  479. else
  480. {
  481. value_type *nentries = alloc_entries (size PASS_MEM_STAT);
  482. for (size_t i = 0; i < size; ++i)
  483. {
  484. value_type &entry = h.m_entries[i];
  485. if (is_deleted (entry))
  486. mark_deleted (nentries[i]);
  487. else if (!is_empty (entry))
  488. nentries[i] = entry;
  489. }
  490. m_entries = nentries;
  491. }
  492. m_size = size;
  493. m_size_prime_index = h.m_size_prime_index;
  494. }
  495. template<typename Descriptor, bool Lazy,
  496. template<typename Type> class Allocator>
  497. hash_table<Descriptor, Lazy, Allocator>::~hash_table ()
  498. {
  499. if (!Lazy || m_entries)
  500. {
  501. for (size_t i = m_size - 1; i < m_size; i--)
  502. if (!is_empty (m_entries[i]) && !is_deleted (m_entries[i]))
  503. Descriptor::remove (m_entries[i]);
  504. if (!m_ggc)
  505. Allocator <value_type> ::data_free (m_entries);
  506. else
  507. ggc_free (m_entries);
  508. if (m_gather_mem_stats)
  509. hash_table_usage ().release_instance_overhead (this,
  510. sizeof (value_type)
  511. * m_size, true);
  512. }
  513. else if (m_gather_mem_stats)
  514. hash_table_usage ().unregister_descriptor (this);
  515. }
  516. /* This function returns an array of empty hash table elements. */
  517. template<typename Descriptor, bool Lazy,
  518. template<typename Type> class Allocator>
  519. inline typename hash_table<Descriptor, Lazy, Allocator>::value_type *
  520. hash_table<Descriptor, Lazy,
  521. Allocator>::alloc_entries (size_t n MEM_STAT_DECL) const
  522. {
  523. value_type *nentries;
  524. if (m_gather_mem_stats)
  525. hash_table_usage ().register_instance_overhead (sizeof (value_type) * n, this);
  526. if (!m_ggc)
  527. nentries = Allocator <value_type> ::data_alloc (n);
  528. else
  529. nentries = ::ggc_cleared_vec_alloc<value_type> (n PASS_MEM_STAT);
  530. gcc_assert (nentries != NULL);
  531. for (size_t i = 0; i < n; i++)
  532. mark_empty (nentries[i]);
  533. return nentries;
  534. }
  535. /* Similar to find_slot, but without several unwanted side effects:
  536. - Does not call equal when it finds an existing entry.
  537. - Does not change the count of elements/searches/collisions in the
  538. hash table.
  539. This function also assumes there are no deleted entries in the table.
  540. HASH is the hash value for the element to be inserted. */
  541. template<typename Descriptor, bool Lazy,
  542. template<typename Type> class Allocator>
  543. typename hash_table<Descriptor, Lazy, Allocator>::value_type *
  544. hash_table<Descriptor, Lazy,
  545. Allocator>::find_empty_slot_for_expand (hashval_t hash)
  546. {
  547. hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
  548. size_t size = m_size;
  549. value_type *slot = m_entries + index;
  550. hashval_t hash2;
  551. if (is_empty (*slot))
  552. return slot;
  553. gcc_checking_assert (!is_deleted (*slot));
  554. hash2 = hash_table_mod2 (hash, m_size_prime_index);
  555. for (;;)
  556. {
  557. index += hash2;
  558. if (index >= size)
  559. index -= size;
  560. slot = m_entries + index;
  561. if (is_empty (*slot))
  562. return slot;
  563. gcc_checking_assert (!is_deleted (*slot));
  564. }
  565. }
  566. /* Return true if the current table is excessively big for ELTS elements. */
  567. template<typename Descriptor, bool Lazy,
  568. template<typename Type> class Allocator>
  569. inline bool
  570. hash_table<Descriptor, Lazy, Allocator>::too_empty_p (unsigned int elts)
  571. {
  572. return elts * 8 < m_size && m_size > 32;
  573. }
  574. /* The following function changes size of memory allocated for the
  575. entries and repeatedly inserts the table elements. The occupancy
  576. of the table after the call will be about 50%. Naturally the hash
  577. table must already exist. Remember also that the place of the
  578. table entries is changed. If memory allocation fails, this function
  579. will abort. */
  580. template<typename Descriptor, bool Lazy,
  581. template<typename Type> class Allocator>
  582. void
  583. hash_table<Descriptor, Lazy, Allocator>::expand ()
  584. {
  585. value_type *oentries = m_entries;
  586. unsigned int oindex = m_size_prime_index;
  587. size_t osize = size ();
  588. value_type *olimit = oentries + osize;
  589. size_t elts = elements ();
  590. /* Resize only when table after removal of unused elements is either
  591. too full or too empty. */
  592. unsigned int nindex;
  593. size_t nsize;
  594. if (elts * 2 > osize || too_empty_p (elts))
  595. {
  596. nindex = hash_table_higher_prime_index (elts * 2);
  597. nsize = prime_tab[nindex].prime;
  598. }
  599. else
  600. {
  601. nindex = oindex;
  602. nsize = osize;
  603. }
  604. value_type *nentries = alloc_entries (nsize);
  605. if (m_gather_mem_stats)
  606. hash_table_usage ().release_instance_overhead (this, sizeof (value_type)
  607. * osize);
  608. m_entries = nentries;
  609. m_size = nsize;
  610. m_size_prime_index = nindex;
  611. m_n_elements -= m_n_deleted;
  612. m_n_deleted = 0;
  613. value_type *p = oentries;
  614. do
  615. {
  616. value_type &x = *p;
  617. if (!is_empty (x) && !is_deleted (x))
  618. {
  619. value_type *q = find_empty_slot_for_expand (Descriptor::hash (x));
  620. *q = x;
  621. }
  622. p++;
  623. }
  624. while (p < olimit);
  625. if (!m_ggc)
  626. Allocator <value_type> ::data_free (oentries);
  627. else
  628. ggc_free (oentries);
  629. }
  630. /* Implements empty() in cases where it isn't a no-op. */
  631. template<typename Descriptor, bool Lazy,
  632. template<typename Type> class Allocator>
  633. void
  634. hash_table<Descriptor, Lazy, Allocator>::empty_slow ()
  635. {
  636. size_t size = m_size;
  637. size_t nsize = size;
  638. value_type *entries = m_entries;
  639. int i;
  640. for (i = size - 1; i >= 0; i--)
  641. if (!is_empty (entries[i]) && !is_deleted (entries[i]))
  642. Descriptor::remove (entries[i]);
  643. /* Instead of clearing megabyte, downsize the table. */
  644. if (size > 1024*1024 / sizeof (value_type))
  645. nsize = 1024 / sizeof (value_type);
  646. else if (too_empty_p (m_n_elements))
  647. nsize = m_n_elements * 2;
  648. if (nsize != size)
  649. {
  650. int nindex = hash_table_higher_prime_index (nsize);
  651. int nsize = prime_tab[nindex].prime;
  652. if (!m_ggc)
  653. Allocator <value_type> ::data_free (m_entries);
  654. else
  655. ggc_free (m_entries);
  656. m_entries = alloc_entries (nsize);
  657. m_size = nsize;
  658. m_size_prime_index = nindex;
  659. }
  660. else
  661. {
  662. #ifndef BROKEN_VALUE_INITIALIZATION
  663. for ( ; size; ++entries, --size)
  664. *entries = value_type ();
  665. #else
  666. memset (entries, 0, size * sizeof (value_type));
  667. #endif
  668. }
  669. m_n_deleted = 0;
  670. m_n_elements = 0;
  671. }
  672. /* This function clears a specified SLOT in a hash table. It is
  673. useful when you've already done the lookup and don't want to do it
  674. again. */
  675. template<typename Descriptor, bool Lazy,
  676. template<typename Type> class Allocator>
  677. void
  678. hash_table<Descriptor, Lazy, Allocator>::clear_slot (value_type *slot)
  679. {
  680. gcc_checking_assert (!(slot < m_entries || slot >= m_entries + size ()
  681. || is_empty (*slot) || is_deleted (*slot)));
  682. Descriptor::remove (*slot);
  683. mark_deleted (*slot);
  684. m_n_deleted++;
  685. }
  686. /* This function searches for a hash table entry equal to the given
  687. COMPARABLE element starting with the given HASH value. It cannot
  688. be used to insert or delete an element. */
  689. template<typename Descriptor, bool Lazy,
  690. template<typename Type> class Allocator>
  691. typename hash_table<Descriptor, Lazy, Allocator>::value_type &
  692. hash_table<Descriptor, Lazy, Allocator>
  693. ::find_with_hash (const compare_type &comparable, hashval_t hash)
  694. {
  695. m_searches++;
  696. size_t size = m_size;
  697. hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
  698. if (Lazy && m_entries == NULL)
  699. m_entries = alloc_entries (size);
  700. value_type *entry = &m_entries[index];
  701. if (is_empty (*entry)
  702. || (!is_deleted (*entry) && Descriptor::equal (*entry, comparable)))
  703. return *entry;
  704. hashval_t hash2 = hash_table_mod2 (hash, m_size_prime_index);
  705. for (;;)
  706. {
  707. m_collisions++;
  708. index += hash2;
  709. if (index >= size)
  710. index -= size;
  711. entry = &m_entries[index];
  712. if (is_empty (*entry)
  713. || (!is_deleted (*entry) && Descriptor::equal (*entry, comparable)))
  714. return *entry;
  715. }
  716. }
  717. /* This function searches for a hash table slot containing an entry
  718. equal to the given COMPARABLE element and starting with the given
  719. HASH. To delete an entry, call this with insert=NO_INSERT, then
  720. call clear_slot on the slot returned (possibly after doing some
  721. checks). To insert an entry, call this with insert=INSERT, then
  722. write the value you want into the returned slot. When inserting an
  723. entry, NULL may be returned if memory allocation fails. */
  724. template<typename Descriptor, bool Lazy,
  725. template<typename Type> class Allocator>
  726. typename hash_table<Descriptor, Lazy, Allocator>::value_type *
  727. hash_table<Descriptor, Lazy, Allocator>
  728. ::find_slot_with_hash (const compare_type &comparable, hashval_t hash,
  729. enum insert_option insert)
  730. {
  731. if (Lazy && m_entries == NULL)
  732. {
  733. if (insert == INSERT)
  734. m_entries = alloc_entries (m_size);
  735. else
  736. return NULL;
  737. }
  738. if (insert == INSERT && m_size * 3 <= m_n_elements * 4)
  739. expand ();
  740. m_searches++;
  741. value_type *first_deleted_slot = NULL;
  742. hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
  743. hashval_t hash2 = hash_table_mod2 (hash, m_size_prime_index);
  744. value_type *entry = &m_entries[index];
  745. size_t size = m_size;
  746. if (is_empty (*entry))
  747. goto empty_entry;
  748. else if (is_deleted (*entry))
  749. first_deleted_slot = &m_entries[index];
  750. else if (Descriptor::equal (*entry, comparable))
  751. return &m_entries[index];
  752. for (;;)
  753. {
  754. m_collisions++;
  755. index += hash2;
  756. if (index >= size)
  757. index -= size;
  758. entry = &m_entries[index];
  759. if (is_empty (*entry))
  760. goto empty_entry;
  761. else if (is_deleted (*entry))
  762. {
  763. if (!first_deleted_slot)
  764. first_deleted_slot = &m_entries[index];
  765. }
  766. else if (Descriptor::equal (*entry, comparable))
  767. return &m_entries[index];
  768. }
  769. empty_entry:
  770. if (insert == NO_INSERT)
  771. return NULL;
  772. if (first_deleted_slot)
  773. {
  774. m_n_deleted--;
  775. mark_empty (*first_deleted_slot);
  776. return first_deleted_slot;
  777. }
  778. m_n_elements++;
  779. return &m_entries[index];
  780. }
  781. /* This function deletes an element with the given COMPARABLE value
  782. from hash table starting with the given HASH. If there is no
  783. matching element in the hash table, this function does nothing. */
  784. template<typename Descriptor, bool Lazy,
  785. template<typename Type> class Allocator>
  786. void
  787. hash_table<Descriptor, Lazy, Allocator>
  788. ::remove_elt_with_hash (const compare_type &comparable, hashval_t hash)
  789. {
  790. value_type *slot = find_slot_with_hash (comparable, hash, NO_INSERT);
  791. if (slot == NULL)
  792. return;
  793. Descriptor::remove (*slot);
  794. mark_deleted (*slot);
  795. m_n_deleted++;
  796. }
  797. /* This function scans over the entire hash table calling CALLBACK for
  798. each live entry. If CALLBACK returns false, the iteration stops.
  799. ARGUMENT is passed as CALLBACK's second argument. */
  800. template<typename Descriptor, bool Lazy,
  801. template<typename Type> class Allocator>
  802. template<typename Argument,
  803. int (*Callback)
  804. (typename hash_table<Descriptor, Lazy, Allocator>::value_type *slot,
  805. Argument argument)>
  806. void
  807. hash_table<Descriptor, Lazy, Allocator>::traverse_noresize (Argument argument)
  808. {
  809. if (Lazy && m_entries == NULL)
  810. return;
  811. value_type *slot = m_entries;
  812. value_type *limit = slot + size ();
  813. do
  814. {
  815. value_type &x = *slot;
  816. if (!is_empty (x) && !is_deleted (x))
  817. if (! Callback (slot, argument))
  818. break;
  819. }
  820. while (++slot < limit);
  821. }
  822. /* Like traverse_noresize, but does resize the table when it is too empty
  823. to improve effectivity of subsequent calls. */
  824. template <typename Descriptor, bool Lazy,
  825. template <typename Type> class Allocator>
  826. template <typename Argument,
  827. int (*Callback)
  828. (typename hash_table<Descriptor, Lazy, Allocator>::value_type *slot,
  829. Argument argument)>
  830. void
  831. hash_table<Descriptor, Lazy, Allocator>::traverse (Argument argument)
  832. {
  833. if (too_empty_p (elements ()) && (!Lazy || m_entries))
  834. expand ();
  835. traverse_noresize <Argument, Callback> (argument);
  836. }
  837. /* Slide down the iterator slots until an active entry is found. */
  838. template<typename Descriptor, bool Lazy,
  839. template<typename Type> class Allocator>
  840. void
  841. hash_table<Descriptor, Lazy, Allocator>::iterator::slide ()
  842. {
  843. for ( ; m_slot < m_limit; ++m_slot )
  844. {
  845. value_type &x = *m_slot;
  846. if (!is_empty (x) && !is_deleted (x))
  847. return;
  848. }
  849. m_slot = NULL;
  850. m_limit = NULL;
  851. }
  852. /* Bump the iterator. */
  853. template<typename Descriptor, bool Lazy,
  854. template<typename Type> class Allocator>
  855. inline typename hash_table<Descriptor, Lazy, Allocator>::iterator &
  856. hash_table<Descriptor, Lazy, Allocator>::iterator::operator ++ ()
  857. {
  858. ++m_slot;
  859. slide ();
  860. return *this;
  861. }
  862. /* Iterate through the elements of hash_table HTAB,
  863. using hash_table <....>::iterator ITER,
  864. storing each element in RESULT, which is of type TYPE. */
  865. #define FOR_EACH_HASH_TABLE_ELEMENT(HTAB, RESULT, TYPE, ITER) \
  866. for ((ITER) = (HTAB).begin (); \
  867. (ITER) != (HTAB).end () ? (RESULT = *(ITER) , true) : false; \
  868. ++(ITER))
  869. /* ggc walking routines. */
  870. template<typename E>
  871. static inline void
  872. gt_ggc_mx (hash_table<E> *h)
  873. {
  874. typedef hash_table<E> table;
  875. if (!ggc_test_and_set_mark (h->m_entries))
  876. return;
  877. for (size_t i = 0; i < h->m_size; i++)
  878. {
  879. if (table::is_empty (h->m_entries[i])
  880. || table::is_deleted (h->m_entries[i]))
  881. continue;
  882. /* Use ggc_maxbe_mx so we don't mark right away for cache tables; we'll
  883. mark in gt_cleare_cache if appropriate. */
  884. E::ggc_maybe_mx (h->m_entries[i]);
  885. }
  886. }
  887. template<typename D>
  888. static inline void
  889. hashtab_entry_note_pointers (void *obj, void *h, gt_pointer_operator op,
  890. void *cookie)
  891. {
  892. hash_table<D> *map = static_cast<hash_table<D> *> (h);
  893. gcc_checking_assert (map->m_entries == obj);
  894. for (size_t i = 0; i < map->m_size; i++)
  895. {
  896. typedef hash_table<D> table;
  897. if (table::is_empty (map->m_entries[i])
  898. || table::is_deleted (map->m_entries[i]))
  899. continue;
  900. D::pch_nx (map->m_entries[i], op, cookie);
  901. }
  902. }
  903. template<typename D>
  904. static void
  905. gt_pch_nx (hash_table<D> *h)
  906. {
  907. bool success
  908. = gt_pch_note_object (h->m_entries, h, hashtab_entry_note_pointers<D>);
  909. gcc_checking_assert (success);
  910. for (size_t i = 0; i < h->m_size; i++)
  911. {
  912. if (hash_table<D>::is_empty (h->m_entries[i])
  913. || hash_table<D>::is_deleted (h->m_entries[i]))
  914. continue;
  915. D::pch_nx (h->m_entries[i]);
  916. }
  917. }
  918. template<typename D>
  919. static inline void
  920. gt_pch_nx (hash_table<D> *h, gt_pointer_operator op, void *cookie)
  921. {
  922. op (&h->m_entries, cookie);
  923. }
  924. template<typename H>
  925. inline void
  926. gt_cleare_cache (hash_table<H> *h)
  927. {
  928. typedef hash_table<H> table;
  929. if (!h)
  930. return;
  931. for (typename table::iterator iter = h->begin (); iter != h->end (); ++iter)
  932. if (!table::is_empty (*iter) && !table::is_deleted (*iter))
  933. {
  934. int res = H::keep_cache_entry (*iter);
  935. if (res == 0)
  936. h->clear_slot (&*iter);
  937. else if (res != -1)
  938. H::ggc_mx (*iter);
  939. }
  940. }
  941. #endif /* TYPED_HASHTAB_H */