| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2007, 2026, Oracle and/or its affiliates. |
| 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, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/ha0storage.ic |
| 29 |
Hash storage. |
| 30 |
Provides a data structure that stores chunks of data in |
| 31 |
its own storage, avoiding duplicates. |
| 32 |
|
| 33 |
Created September 24, 2007 Vasil Dimov |
| 34 |
*******************************************************/ |
| 35 |
|
| 36 |
#include "ha0storage.h" |
| 37 |
#include "hash0hash.h" |
| 38 |
#include "mem0mem.h" |
| 39 |
#include "univ.i" |
| 40 |
|
| 41 |
/** Hash storage for strings */ |
| 42 |
struct ha_storage_t { |
| 43 |
mem_heap_t *heap; /*!< memory heap from which memory is |
| 44 |
allocated */ |
| 45 |
hash_table_t *hash; /*!< hash table used to avoid |
| 46 |
duplicates */ |
| 47 |
}; |
| 48 |
|
| 49 |
/** Objects of this type are stored in ha_storage_t */ |
| 50 |
struct ha_storage_node_t { |
| 51 |
ulint data_len; /*!< length of the data */ |
| 52 |
const void *data; /*!< pointer to data */ |
| 53 |
ha_storage_node_t *next; /*!< next node in hash chain */ |
| 54 |
}; |
| 55 |
|
| 56 |
/** Creates a hash storage. If any of the parameters is 0, then a default |
| 57 |
value is used. |
| 58 |
@return own: hash storage */ |
| 59 |
static inline ha_storage_t *ha_storage_create( |
| 60 |
ulint initial_heap_bytes, /*!< in: initial heap's size */ |
| 61 |
ulint initial_hash_cells) /*!< in: initial number of cells |
| 62 |
in the hash table */ |
| 63 |
{ |
| 64 |
ha_storage_t *storage; |
| 65 |
mem_heap_t *heap; |
| 66 |
|
| 67 |
if (initial_heap_bytes == 0) { |
| 68 |
initial_heap_bytes = HA_STORAGE_DEFAULT_HEAP_BYTES; |
| 69 |
} |
| 70 |
|
| 71 |
if (initial_hash_cells == 0) { |
| 72 |
initial_hash_cells = HA_STORAGE_DEFAULT_HASH_CELLS; |
| 73 |
} |
| 74 |
|
| 75 |
/* we put "storage" within "storage->heap" */ |
| 76 |
|
| 77 |
heap = mem_heap_create(sizeof(ha_storage_t) + initial_heap_bytes, |
| 78 |
UT_LOCATION_HERE); |
| 79 |
|
| 80 |
storage = (ha_storage_t *)mem_heap_alloc(heap, sizeof(ha_storage_t)); |
| 81 |
|
| 82 |
storage->heap = heap; |
| 83 |
storage->hash = ut::new_<hash_table_t>(initial_hash_cells); |
| 84 |
|
| 85 |
return (storage); |
| 86 |
} |
| 87 |
|
| 88 |
/** Empties a hash storage, freeing memory occupied by data chunks. |
| 89 |
This invalidates any pointers previously returned by ha_storage_put(). |
| 90 |
The hash storage is not invalidated itself and can be used again. */ |
| 91 |
static inline void ha_storage_empty( |
| 92 |
ha_storage_t **storage) /*!< in/out: hash storage */ |
| 93 |
{ |
| 94 |
ha_storage_t temp_storage; |
| 95 |
|
| 96 |
temp_storage.heap = (*storage)->heap; |
| 97 |
temp_storage.hash = (*storage)->hash; |
| 98 |
|
| 99 |
hash_table_clear(temp_storage.hash); |
| 100 |
mem_heap_empty(temp_storage.heap); |
| 101 |
|
| 102 |
*storage = |
| 103 |
(ha_storage_t *)mem_heap_alloc(temp_storage.heap, sizeof(ha_storage_t)); |
| 104 |
|
| 105 |
(*storage)->heap = temp_storage.heap; |
| 106 |
(*storage)->hash = temp_storage.hash; |
| 107 |
} |
| 108 |
|
| 109 |
/** Frees a hash storage and everything it contains, it cannot be used after |
| 110 |
this call. |
| 111 |
This invalidates any pointers previously returned by ha_storage_put(). */ |
| 112 |
static inline void ha_storage_free( |
| 113 |
ha_storage_t *storage) /*!< in, own: hash storage */ |
| 114 |
{ |
| 115 |
/* order is important because the pointer storage->hash is |
| 116 |
within the heap */ |
| 117 |
ut::delete_(storage->hash); |
| 118 |
mem_heap_free(storage->heap); |
| 119 |
} |
| 120 |
|
| 121 |
/** Gets the size of the memory used by a storage. |
| 122 |
@return bytes used */ |
| 123 |
static inline ulint ha_storage_get_size( |
| 124 |
const ha_storage_t *storage) /*!< in: hash storage */ |
| 125 |
{ |
| 126 |
ulint ret; |
| 127 |
|
| 128 |
ret = mem_heap_get_size(storage->heap); |
| 129 |
|
| 130 |
/* this assumes hash->heap and hash->heaps are NULL */ |
| 131 |
ret += sizeof(hash_table_t); |
| 132 |
ret += sizeof(hash_cell_t) * hash_get_n_cells(storage->hash); |
| 133 |
|
| 134 |
return (ret); |
| 135 |
} |