hash-table.h 37 KB

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