| 1 | /***************************************************************************** |
| 2 | |
| 3 | Copyright (c) 2007, 2013, Oracle and/or its affiliates. All Rights Reserved. |
| 4 | |
| 5 | This program 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 Software |
| 7 | Foundation; version 2 of the License. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License along with |
| 14 | this program; if not, write to the Free Software Foundation, Inc., |
| 15 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA |
| 16 | |
| 17 | *****************************************************************************/ |
| 18 | |
| 19 | /**************************************************//** |
| 20 | @file include/ha0storage.ic |
| 21 | Hash storage. |
| 22 | Provides a data structure that stores chunks of data in |
| 23 | its own storage, avoiding duplicates. |
| 24 | |
| 25 | Created September 24, 2007 Vasil Dimov |
| 26 | *******************************************************/ |
| 27 | |
| 28 | #include "hash0hash.h"cxx . . $ `<file>/storage/innobase/include/hash0hash.h`/ |
| 29 | #include "mem0mem.h"cxx . . $ `<file>/storage/innobase/include/mem0mem.h`/ |
| 30 | |
| 31 | /** Hash storage for strings */ |
| 32 | struct ha_storage_tcxx . . $ ha_storage_t# { |
| 33 | mem_heap_tcxx . . $ mem_heap_t#* heapcxx . . $ ha_storage_t#heap.; /*!< memory heap from which memory is |
| 34 | allocated */ |
| 35 | hash_table_tcxx . . $ hash_table_t# hashcxx . . $ ha_storage_t#hash.; /*!< hash table used to avoid |
| 36 | duplicates */ |
| 37 | }; |
| 38 | |
| 39 | /** Objects of this type are stored in ha_storage_t */ |
| 40 | struct ha_storage_node_tcxx . . $ ha_storage_node_t# { |
| 41 | ulintcxx . . $ ulint# data_lencxx . . $ ha_storage_node_t#data_len.;/*!< length of the data */ |
| 42 | const void* datacxx . . $ ha_storage_node_t#data.; /*!< pointer to data */ |
| 43 | ha_storage_node_tcxx . . $ ha_storage_node_t#* nextcxx . . $ ha_storage_node_t#next.; /*!< next node in hash chain */ |
| 44 | }; |
| 45 | |
| 46 | /*******************************************************************//** |
| 47 | Creates a hash storage. If any of the parameters is 0, then a default |
| 48 | value is used. |
| 49 | @return own: hash storage */ |
| 50 | UNIV_INLINEcxx . . $ `storage/innobase/include/univ.i:168:9`! |
| 51 | ha_storage_tcxx . . $ ha_storage_t#* |
| 52 | ha_storage_createcxx . . $ ha_storage_create(c2b9cbbe89195c74).( |
| 53 | /*==============*/ |
| 54 | ulintcxx . . $ ulint# initial_heap_bytes, /*!< in: initial heap's size */ |
| 55 | ulintcxx . . $ ulint# initial_hash_cells) /*!< in: initial number of cells |
| 56 | in the hash table */ |
| 57 | { |
| 58 | ha_storage_tcxx . . $ ha_storage_t#* storage; |
| 59 | mem_heap_tcxx . . $ mem_heap_t#* heap; |
| 60 | |
| 61 | if (initial_heap_bytes == 0) { |
| 62 | |
| 63 | initial_heap_bytes = HA_STORAGE_DEFAULT_HEAP_BYTEScxx . . $ `storage/innobase/include/ha0storage.h:35:9`!; |
| 64 | } |
| 65 | |
| 66 | if (initial_hash_cells == 0) { |
| 67 | |
| 68 | initial_hash_cells = HA_STORAGE_DEFAULT_HASH_CELLScxx . . $ `storage/innobase/include/ha0storage.h:39:9`!; |
| 69 | } |
| 70 | |
| 71 | /* we put "storage" within "storage->heap" */ |
| 72 | |
| 73 | heap = mem_heap_createcxx . . $ `storage/innobase/include/mem0mem.h:85:10`!(sizeof(ha_storage_t) |
| 74 | + initial_heap_bytes); |
| 75 | |
| 76 | storage = (ha_storage_tcxx . . $ ha_storage_t#*) mem_heap_alloccxx . . $ mem_heap_alloc(3f50e4c8b9ca77c0).(heap, |
| 77 | sizeof(ha_storage_tcxx . . $ ha_storage_t#)); |
| 78 | |
| 79 | storage->heapcxx . . $ ha_storage_t#heap. = heap; |
| 80 | storage->hashcxx . . $ ha_storage_t#hash..createcxx . . $ hash_table_t#create(203741fbd813a59e).(initial_hash_cells); |
| 81 | |
| 82 | return(storage); |
| 83 | } |
| 84 | |
| 85 | /*******************************************************************//** |
| 86 | Empties a hash storage, freeing memory occupied by data chunks. |
| 87 | This invalidates any pointers previously returned by ha_storage_put(). |
| 88 | The hash storage is not invalidated itself and can be used again. */ |
| 89 | UNIV_INLINEcxx . . $ `storage/innobase/include/univ.i:168:9`! |
| 90 | void |
| 91 | ha_storage_emptycxx . . $ ha_storage_empty(843bdd633fef96ab).( |
| 92 | /*=============*/ |
| 93 | ha_storage_tcxx . . $ ha_storage_t#** storage) /*!< in/out: hash storage */ |
| 94 | { |
| 95 | ha_storage_tcxx . . $ ha_storage_t# temp_storage; |
| 96 | |
| 97 | temp_storage.heapcxx . . $ ha_storage_t#heap. = (*storage)->heapcxx . . $ ha_storage_t#heap.; |
| 98 | temp_storage.hashcxx . . $ ha_storage_t#hash. =cxx . . $ hash_table_t#`operator=`(f40eb137bec38880). (*storage)->hashcxx . . $ ha_storage_t#hash.; |
| 99 | |
| 100 | temp_storage.hashcxx . . $ ha_storage_t#hash..clearcxx . . $ hash_table_t#clear(ced63f7c635d850d).(); |
| 101 | mem_heap_emptycxx . . $ mem_heap_empty(5231cf397463bfab).(temp_storage.heapcxx . . $ ha_storage_t#heap.); |
| 102 | |
| 103 | *storage = (ha_storage_tcxx . . $ ha_storage_t#*) mem_heap_alloccxx . . $ mem_heap_alloc(3f50e4c8b9ca77c0).(temp_storage.heapcxx . . $ ha_storage_t#heap., |
| 104 | sizeof(ha_storage_tcxx . . $ ha_storage_t#)); |
| 105 | |
| 106 | (*storage)->heapcxx . . $ ha_storage_t#heap. = temp_storage.heapcxx . . $ ha_storage_t#heap.; |
| 107 | (*storage)->hashcxx . . $ ha_storage_t#hash. =cxx . . $ hash_table_t#`operator=`(f40eb137bec38880). temp_storage.hashcxx . . $ ha_storage_t#hash.; |
| 108 | } |
| 109 | |
| 110 | /*******************************************************************//** |
| 111 | Frees a hash storage and everything it contains, it cannot be used after |
| 112 | this call. |
| 113 | This invalidates any pointers previously returned by ha_storage_put(). */ |
| 114 | UNIV_INLINEcxx . . $ `storage/innobase/include/univ.i:168:9`! |
| 115 | void |
| 116 | ha_storage_freecxx . . $ ha_storage_free(12725048f16c5649).( |
| 117 | /*============*/ |
| 118 | ha_storage_tcxx . . $ ha_storage_t#* storage) /*!< in, own: hash storage */ |
| 119 | { |
| 120 | storage->hashcxx . . $ ha_storage_t#hash..freecxx . . $ hash_table_t#free(ced63f7c635d850d).(); |
| 121 | mem_heap_freecxx . . $ mem_heap_free(5231cf397463bfab).(storage->heapcxx . . $ ha_storage_t#heap.); |
| 122 | } |
| 123 | |
| 124 | /*******************************************************************//** |
| 125 | Gets the size of the memory used by a storage. |
| 126 | @return bytes used */ |
| 127 | UNIV_INLINEcxx . . $ `storage/innobase/include/univ.i:168:9`! |
| 128 | ulintcxx . . $ ulint# |
| 129 | ha_storage_get_sizecxx . . $ ha_storage_get_size(5be7a28dafddb64e).( |
| 130 | /*================*/ |
| 131 | const ha_storage_tcxx . . $ ha_storage_t#* storage) /*!< in: hash storage */ |
| 132 | { |
| 133 | ulintcxx . . $ ulint# ret; |
| 134 | |
| 135 | ret = mem_heap_get_sizecxx . . $ mem_heap_get_size(ad7b671f85d77495).(storage->heapcxx . . $ ha_storage_t#heap.); |
| 136 | |
| 137 | /* this assumes hash->heap and hash->heaps are NULL */ |
| 138 | ret += sizeof(hash_table_tcxx . . $ hash_table_t#); |
| 139 | ret += sizeof(hash_cell_tcxx . . $ hash_cell_t#) * storage->hashcxx . . $ ha_storage_t#hash..n_cellscxx . . $ hash_table_t#n_cells.; |
| 140 | |
| 141 | return(ret); |
| 142 | } |