MySQL 8.0.39
Source Code Documentation
ha0storage.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2007, 2024, 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 designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 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
42is allocated later when/if it is needed. */
43constexpr uint32_t HA_STORAGE_DEFAULT_HEAP_BYTES = 1024;
44
45/** This value is used by default by ha_storage_create(). It is a
46constant per ha_storage's lifetime. */
47constexpr uint32_t HA_STORAGE_DEFAULT_HASH_CELLS = 4096;
48
49/** Hash storage */
50struct ha_storage_t;
51
52/** Creates a hash storage. If any of the parameters is 0, then a default value
53is 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 */
58static 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 */
70const 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 */
81static 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 */
92static 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 */
105static 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. */
115static 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(). */
121static 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 */
127 const ha_storage_t *storage); /*!< in: hash storage */
128
129#include "ha0storage.ic"
130
131#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:83
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:105
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:92
constexpr uint32_t HA_STORAGE_DEFAULT_HEAP_BYTES
This value is used by default by ha_storage_create().
Definition: ha0storage.h:43
constexpr uint32_t HA_STORAGE_DEFAULT_HASH_CELLS
This value is used by default by ha_storage_create().
Definition: ha0storage.h:47
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:81
Hash storage.
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1052
Hash storage for strings.
Definition: ha0storage.ic:42
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:406