You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.1 KiB

  1. /*****************************************************************************
  2. Copyright (c) 1997, 2009, Innobase Oy. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  11. Place, Suite 330, Boston, MA 02111-1307 USA
  12. *****************************************************************************/
  13. /**************************************************//**
  14. @file include/hash0hash.ic
  15. The simple hash table utility
  16. Created 5/20/1997 Heikki Tuuri
  17. *******************************************************/
  18. #include "ut0rnd.h"
  19. /************************************************************//**
  20. Gets the nth cell in a hash table.
  21. @return pointer to cell */
  22. UNIV_INLINE
  23. hash_cell_t*
  24. hash_get_nth_cell(
  25. /*==============*/
  26. hash_table_t* table, /*!< in: hash table */
  27. ulint n) /*!< in: cell index */
  28. {
  29. ut_ad(n < table->n_cells);
  30. return(table->array + n);
  31. }
  32. /*************************************************************//**
  33. Clears a hash table so that all the cells become empty. */
  34. UNIV_INLINE
  35. void
  36. hash_table_clear(
  37. /*=============*/
  38. hash_table_t* table) /*!< in/out: hash table */
  39. {
  40. memset(table->array, 0x0,
  41. table->n_cells * sizeof(*table->array));
  42. }
  43. /*************************************************************//**
  44. Returns the number of cells in a hash table.
  45. @return number of cells */
  46. UNIV_INLINE
  47. ulint
  48. hash_get_n_cells(
  49. /*=============*/
  50. hash_table_t* table) /*!< in: table */
  51. {
  52. return(table->n_cells);
  53. }
  54. /**************************************************************//**
  55. Calculates the hash value from a folded value.
  56. @return hashed value */
  57. UNIV_INLINE
  58. ulint
  59. hash_calc_hash(
  60. /*===========*/
  61. ulint fold, /*!< in: folded value */
  62. hash_table_t* table) /*!< in: hash table */
  63. {
  64. return(ut_hash_ulint(fold, table->n_cells));
  65. }
  66. #ifndef UNIV_HOTBACKUP
  67. /************************************************************//**
  68. Gets the mutex index for a fold value in a hash table.
  69. @return mutex number */
  70. UNIV_INLINE
  71. ulint
  72. hash_get_mutex_no(
  73. /*==============*/
  74. hash_table_t* table, /*!< in: hash table */
  75. ulint fold) /*!< in: fold */
  76. {
  77. ut_ad(ut_is_2pow(table->n_mutexes));
  78. return(ut_2pow_remainder(hash_calc_hash(fold, table),
  79. table->n_mutexes));
  80. }
  81. /************************************************************//**
  82. Gets the nth heap in a hash table.
  83. @return mem heap */
  84. UNIV_INLINE
  85. mem_heap_t*
  86. hash_get_nth_heap(
  87. /*==============*/
  88. hash_table_t* table, /*!< in: hash table */
  89. ulint i) /*!< in: index of the heap */
  90. {
  91. ut_ad(i < table->n_mutexes);
  92. return(table->heaps[i]);
  93. }
  94. /************************************************************//**
  95. Gets the heap for a fold value in a hash table.
  96. @return mem heap */
  97. UNIV_INLINE
  98. mem_heap_t*
  99. hash_get_heap(
  100. /*==========*/
  101. hash_table_t* table, /*!< in: hash table */
  102. ulint fold) /*!< in: fold */
  103. {
  104. ulint i;
  105. if (table->heap) {
  106. return(table->heap);
  107. }
  108. i = hash_get_mutex_no(table, fold);
  109. return(hash_get_nth_heap(table, i));
  110. }
  111. /************************************************************//**
  112. Gets the nth mutex in a hash table.
  113. @return mutex */
  114. UNIV_INLINE
  115. mutex_t*
  116. hash_get_nth_mutex(
  117. /*===============*/
  118. hash_table_t* table, /*!< in: hash table */
  119. ulint i) /*!< in: index of the mutex */
  120. {
  121. ut_ad(i < table->n_mutexes);
  122. return(table->mutexes + i);
  123. }
  124. /************************************************************//**
  125. Gets the mutex for a fold value in a hash table.
  126. @return mutex */
  127. UNIV_INLINE
  128. mutex_t*
  129. hash_get_mutex(
  130. /*===========*/
  131. hash_table_t* table, /*!< in: hash table */
  132. ulint fold) /*!< in: fold */
  133. {
  134. ulint i;
  135. i = hash_get_mutex_no(table, fold);
  136. return(hash_get_nth_mutex(table, i));
  137. }
  138. #endif /* !UNIV_HOTBACKUP */