| 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.h |
| 29 |
Hash storage. |
| 30 |
Provides a data structure that stores chunks of data in |
| 31 |
its own storage, avoiding duplicates. |
| 32 |
|
| 33 |
Created September 22, 2007 Vasil Dimov |
| 34 |
*******************************************************/ |
| 35 |
|
| 36 |
#ifndef ha0storage_h |
| 37 |
#define ha0storage_h |
| 38 |
|
| 39 |
#include "univ.i" |
| 40 |
|
| 41 |
/** This value is used by default by ha_storage_create(). More memory |
| 42 |
is allocated later when/if it is needed. */ |
| 43 |
constexpr uint32_t HA_STORAGE_DEFAULT_HEAP_BYTES = 1024; |
| 44 |
|
| 45 |
/** This value is used by default by ha_storage_create(). It is a |
| 46 |
constant per ha_storage's lifetime. */ |
| 47 |
constexpr uint32_t HA_STORAGE_DEFAULT_HASH_CELLS = 4096; |
| 48 |
|
| 49 |
/** Hash storage */ |
| 50 |
struct ha_storage_t; |
| 51 |
|
| 52 |
/** Creates a hash storage. If any of the parameters is 0, then a default value |
| 53 |
is used. |
| 54 |
@param[in] initial_hash_cells initial number of cells in the hash |
| 55 |
table |
| 56 |
@param[in] initial_heap_bytes initial heap's size |
| 57 |
@return own: hash storage */ |
| 58 |
static inline ha_storage_t *ha_storage_create(ulint initial_heap_bytes, |
| 59 |
ulint initial_hash_cells); |
| 60 |
|
| 61 |
/** Copies data into the storage and returns a pointer to the copy. If the |
| 62 |
same data chunk is already present, then pointer to it is returned. |
| 63 |
Data chunks are considered to be equal if len1 == len2 and |
| 64 |
memcmp(data1, data2, len1) == 0. If "data" is not present (and thus |
| 65 |
data_len bytes need to be allocated) and the size of storage is going to |
| 66 |
become more than "memlim" then "data" is not added and NULL is returned. |
| 67 |
To disable this behavior "memlim" can be set to 0, which stands for |
| 68 |
"no limit". |
| 69 |
@return pointer to the copy */ |
| 70 |
const void *ha_storage_put_memlim( |
| 71 |
ha_storage_t *storage, /*!< in/out: hash storage */ |
| 72 |
const void *data, /*!< in: data to store */ |
| 73 |
ulint data_len, /*!< in: data length */ |
| 74 |
ulint memlim); /*!< in: memory limit to obey */ |
| 75 |
|
| 76 |
/** Same as ha_storage_put_memlim() but without memory limit. |
| 77 |
@param storage in/out: hash storage |
| 78 |
@param data in: data to store |
| 79 |
@param data_len in: data length |
| 80 |
@return pointer to the copy of the string */ |
| 81 |
static inline const void *ha_storage_put(ha_storage_t *storage, |
| 82 |
const void *data, ulint data_len) { |
| 83 |
return ha_storage_put_memlim(storage, data, data_len, 0); |
| 84 |
} |
| 85 |
|
| 86 |
/** Copies string into the storage and returns a pointer to the copy. If the |
| 87 |
same string is already present, then pointer to it is returned. |
| 88 |
Strings are considered to be equal if strcmp(str1, str2) == 0. |
| 89 |
@param storage in/out: hash storage |
| 90 |
@param str in: string to put |
| 91 |
@return pointer to the copy of the string */ |
| 92 |
static inline const char *ha_storage_put_str(ha_storage_t *storage, |
| 93 |
const char *str) { |
| 94 |
return (const char *)ha_storage_put(storage, str, strlen(str) + 1); |
| 95 |
} |
| 96 |
|
| 97 |
/** Copies string into the storage and returns a pointer to the copy obeying |
| 98 |
a memory limit. |
| 99 |
If the same string is already present, then pointer to it is returned. |
| 100 |
Strings are considered to be equal if strcmp(str1, str2) == 0. |
| 101 |
@param storage in/out: hash storage |
| 102 |
@param str in: string to put |
| 103 |
@param memlim in: memory limit to obey |
| 104 |
@return pointer to the copy of the string */ |
| 105 |
static inline const char *ha_storage_put_str_memlim(ha_storage_t *storage, |
| 106 |
const char *str, |
| 107 |
ulint memlim) { |
| 108 |
return (const char *)ha_storage_put_memlim(storage, str, strlen(str) + 1, |
| 109 |
memlim); |
| 110 |
} |
| 111 |
|
| 112 |
/** Empties a hash storage, freeing memory occupied by data chunks. |
| 113 |
This invalidates any pointers previously returned by ha_storage_put(). |
| 114 |
The hash storage is not invalidated itself and can be used again. */ |
| 115 |
static inline void ha_storage_empty( |
| 116 |
ha_storage_t **storage); /*!< in/out: hash storage */ |
| 117 |
|
| 118 |
/** Frees a hash storage and everything it contains, it cannot be used after |
| 119 |
this call. |
| 120 |
This invalidates any pointers previously returned by ha_storage_put(). */ |
| 121 |
static inline void ha_storage_free( |
| 122 |
ha_storage_t *storage); /*!< in, own: hash storage */ |
| 123 |
|
| 124 |
/** Gets the size of the memory used by a storage. |
| 125 |
@return bytes used */ |
| 126 |
static inline ulint ha_storage_get_size( |
| 127 |
const ha_storage_t *storage); /*!< in: hash storage */ |
| 128 |
|
| 129 |
#include "ha0storage.ic" |
| 130 |
|
| 131 |
#endif /* ha0storage_h */ |