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.

148 lines
4.3 KiB

  1. /*****************************************************************************
  2. Copyright (c) 2007, 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/ha0storage.ic
  15. Hash storage.
  16. Provides a data structure that stores chunks of data in
  17. its own storage, avoiding duplicates.
  18. Created September 24, 2007 Vasil Dimov
  19. *******************************************************/
  20. #include "univ.i"
  21. #include "ha0storage.h"
  22. #include "hash0hash.h"
  23. #include "mem0mem.h"
  24. /** Hash storage for strings */
  25. struct ha_storage_struct {
  26. mem_heap_t* heap; /*!< memory heap from which memory is
  27. allocated */
  28. hash_table_t* hash; /*!< hash table used to avoid
  29. duplicates */
  30. };
  31. /** Objects of this type are stored in ha_storage_t */
  32. typedef struct ha_storage_node_struct ha_storage_node_t;
  33. /** Objects of this type are stored in ha_storage_struct */
  34. struct ha_storage_node_struct {
  35. ulint data_len;/*!< length of the data */
  36. const void* data; /*!< pointer to data */
  37. ha_storage_node_t* next; /*!< next node in hash chain */
  38. };
  39. /*******************************************************************//**
  40. Creates a hash storage. If any of the parameters is 0, then a default
  41. value is used.
  42. @return own: hash storage */
  43. UNIV_INLINE
  44. ha_storage_t*
  45. ha_storage_create(
  46. /*==============*/
  47. ulint initial_heap_bytes, /*!< in: initial heap's size */
  48. ulint initial_hash_cells) /*!< in: initial number of cells
  49. in the hash table */
  50. {
  51. ha_storage_t* storage;
  52. mem_heap_t* heap;
  53. if (initial_heap_bytes == 0) {
  54. initial_heap_bytes = HA_STORAGE_DEFAULT_HEAP_BYTES;
  55. }
  56. if (initial_hash_cells == 0) {
  57. initial_hash_cells = HA_STORAGE_DEFAULT_HASH_CELLS;
  58. }
  59. /* we put "storage" within "storage->heap" */
  60. heap = mem_heap_create(sizeof(ha_storage_t)
  61. + initial_heap_bytes);
  62. storage = (ha_storage_t*) mem_heap_alloc(heap,
  63. sizeof(ha_storage_t));
  64. storage->heap = heap;
  65. storage->hash = hash_create(initial_hash_cells);
  66. return(storage);
  67. }
  68. /*******************************************************************//**
  69. Empties a hash storage, freeing memory occupied by data chunks.
  70. This invalidates any pointers previously returned by ha_storage_put().
  71. The hash storage is not invalidated itself and can be used again. */
  72. UNIV_INLINE
  73. void
  74. ha_storage_empty(
  75. /*=============*/
  76. ha_storage_t** storage) /*!< in/out: hash storage */
  77. {
  78. ha_storage_t temp_storage;
  79. temp_storage.heap = (*storage)->heap;
  80. temp_storage.hash = (*storage)->hash;
  81. hash_table_clear(temp_storage.hash);
  82. mem_heap_empty(temp_storage.heap);
  83. *storage = (ha_storage_t*) mem_heap_alloc(temp_storage.heap,
  84. sizeof(ha_storage_t));
  85. (*storage)->heap = temp_storage.heap;
  86. (*storage)->hash = temp_storage.hash;
  87. }
  88. /*******************************************************************//**
  89. Frees a hash storage and everything it contains, it cannot be used after
  90. this call.
  91. This invalidates any pointers previously returned by ha_storage_put(). */
  92. UNIV_INLINE
  93. void
  94. ha_storage_free(
  95. /*============*/
  96. ha_storage_t* storage) /*!< in, own: hash storage */
  97. {
  98. /* order is important because the pointer storage->hash is
  99. within the heap */
  100. hash_table_free(storage->hash);
  101. mem_heap_free(storage->heap);
  102. }
  103. /*******************************************************************//**
  104. Gets the size of the memory used by a storage.
  105. @return bytes used */
  106. UNIV_INLINE
  107. ulint
  108. ha_storage_get_size(
  109. /*================*/
  110. const ha_storage_t* storage) /*!< in: hash storage */
  111. {
  112. ulint ret;
  113. ret = mem_heap_get_size(storage->heap);
  114. /* this assumes hash->heap and hash->heaps are NULL */
  115. ret += sizeof(hash_table_t);
  116. ret += sizeof(hash_cell_t) * hash_get_n_cells(storage->hash);
  117. return(ret);
  118. }