MySQL 8.0.31
Source Code Documentation
ha0storage.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2007, 2022, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/** @file include/ha0storage.h
28 Hash storage.
29 Provides a data structure that stores chunks of data in
30 its own storage, avoiding duplicates.
31
32 Created September 22, 2007 Vasil Dimov
33 *******************************************************/
34
35#ifndef ha0storage_h
36#define ha0storage_h
37
38#include "univ.i"
39
40/** This value is used by default by ha_storage_create(). More memory
41is allocated later when/if it is needed. */
42constexpr uint32_t HA_STORAGE_DEFAULT_HEAP_BYTES = 1024;
43
44/** This value is used by default by ha_storage_create(). It is a
45constant per ha_storage's lifetime. */
46constexpr uint32_t HA_STORAGE_DEFAULT_HASH_CELLS = 4096;
47
48/** Hash storage */
49struct ha_storage_t;
50
51/** Creates a hash storage. If any of the parameters is 0, then a default value
52is used.
53@param[in] initial_hash_cells initial number of cells in the hash
54 table
55@param[in] initial_heap_bytes initial heap's size
56@return own: hash storage */
57static inline ha_storage_t *ha_storage_create(ulint initial_heap_bytes,
58 ulint initial_hash_cells);
59
60/** Copies data into the storage and returns a pointer to the copy. If the
61 same data chunk is already present, then pointer to it is returned.
62 Data chunks are considered to be equal if len1 == len2 and
63 memcmp(data1, data2, len1) == 0. If "data" is not present (and thus
64 data_len bytes need to be allocated) and the size of storage is going to
65 become more than "memlim" then "data" is not added and NULL is returned.
66 To disable this behavior "memlim" can be set to 0, which stands for
67 "no limit".
68 @return pointer to the copy */
69const void *ha_storage_put_memlim(
70 ha_storage_t *storage, /*!< in/out: hash storage */
71 const void *data, /*!< in: data to store */
72 ulint data_len, /*!< in: data length */
73 ulint memlim); /*!< in: memory limit to obey */
74
75/** Same as ha_storage_put_memlim() but without memory limit.
76 @param storage in/out: hash storage
77 @param data in: data to store
78 @param data_len in: data length
79 @return pointer to the copy of the string */
80static inline const void *ha_storage_put(ha_storage_t *storage,
81 const void *data, ulint data_len) {
82 return ha_storage_put_memlim(storage, data, data_len, 0);
83}
84
85/** Copies string into the storage and returns a pointer to the copy. If the
86 same string is already present, then pointer to it is returned.
87 Strings are considered to be equal if strcmp(str1, str2) == 0.
88 @param storage in/out: hash storage
89 @param str in: string to put
90 @return pointer to the copy of the string */
91static inline const char *ha_storage_put_str(ha_storage_t *storage,
92 const char *str) {
93 return (const char *)ha_storage_put(storage, str, strlen(str) + 1);
94}
95
96/** Copies string into the storage and returns a pointer to the copy obeying
97 a memory limit.
98 If the same string is already present, then pointer to it is returned.
99 Strings are considered to be equal if strcmp(str1, str2) == 0.
100 @param storage in/out: hash storage
101 @param str in: string to put
102 @param memlim in: memory limit to obey
103 @return pointer to the copy of the string */
104static inline const char *ha_storage_put_str_memlim(ha_storage_t *storage,
105 const char *str,
106 ulint memlim) {
107 return (const char *)ha_storage_put_memlim(storage, str, strlen(str) + 1,
108 memlim);
109}
110
111/** Empties a hash storage, freeing memory occupied by data chunks.
112 This invalidates any pointers previously returned by ha_storage_put().
113 The hash storage is not invalidated itself and can be used again. */
114static inline void ha_storage_empty(
115 ha_storage_t **storage); /*!< in/out: hash storage */
116
117/** Frees a hash storage and everything it contains, it cannot be used after
118 this call.
119 This invalidates any pointers previously returned by ha_storage_put(). */
120static inline void ha_storage_free(
121 ha_storage_t *storage); /*!< in, own: hash storage */
122
123/** Gets the size of the memory used by a storage.
124 @return bytes used */
126 const ha_storage_t *storage); /*!< in: hash storage */
127
128#include "ha0storage.ic"
129
130#endif /* ha0storage_h */
const void * ha_storage_put_memlim(ha_storage_t *storage, const void *data, ulint data_len, ulint memlim)
Copies data into the storage and returns a pointer to the copy.
Definition: ha0storage.cc:82
static void ha_storage_empty(ha_storage_t **storage)
Empties a hash storage, freeing memory occupied by data chunks.
static ulint ha_storage_get_size(const ha_storage_t *storage)
Gets the size of the memory used by a storage.
static const char * ha_storage_put_str_memlim(ha_storage_t *storage, const char *str, ulint memlim)
Copies string into the storage and returns a pointer to the copy obeying a memory limit.
Definition: ha0storage.h:104
static const char * ha_storage_put_str(ha_storage_t *storage, const char *str)
Copies string into the storage and returns a pointer to the copy.
Definition: ha0storage.h:91
constexpr uint32_t HA_STORAGE_DEFAULT_HEAP_BYTES
This value is used by default by ha_storage_create().
Definition: ha0storage.h:42
constexpr uint32_t HA_STORAGE_DEFAULT_HASH_CELLS
This value is used by default by ha_storage_create().
Definition: ha0storage.h:46
static ha_storage_t * ha_storage_create(ulint initial_heap_bytes, ulint initial_hash_cells)
Creates a hash storage.
static void ha_storage_free(ha_storage_t *storage)
Frees a hash storage and everything it contains, it cannot be used after this call.
static const void * ha_storage_put(ha_storage_t *storage, const void *data, ulint data_len)
Same as ha_storage_put_memlim() but without memory limit.
Definition: ha0storage.h:80
Hash storage.
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1063
Hash storage for strings.
Definition: ha0storage.ic:41
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:407