1 /*****************************************************************************
2 
3 Copyright (c) 2007, 2018, 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, version 2.0, as published by the
7 Free Software Foundation.
8 
9 This program is also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 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
41 is allocated later when/if it is needed. */
42 #define HA_STORAGE_DEFAULT_HEAP_BYTES 1024
43 
44 /** This value is used by default by ha_storage_create(). It is a
45 constant per ha_storage's lifetime. */
46 #define HA_STORAGE_DEFAULT_HASH_CELLS 4096
47 
48 /** Hash storage */
49 struct ha_storage_t;
50 
51 /** Creates a hash storage. If any of the parameters is 0, then a default value
52 is 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 */
57 UNIV_INLINE
58 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 #define ha_storage_put(storage, data, data_len) \
82   ha_storage_put_memlim((storage), (data), (data_len), 0)
83 
84 /** Copies string into the storage and returns a pointer to the copy. If the
85  same string is already present, then pointer to it is returned.
86  Strings are considered to be equal if strcmp(str1, str2) == 0.
87  @param storage in/out: hash storage
88  @param str in: string to put
89  @return pointer to the copy of the string */
90 #define ha_storage_put_str(storage, str) \
91   ((const char *)ha_storage_put((storage), (str), strlen(str) + 1))
92 
93 /** Copies string into the storage and returns a pointer to the copy obeying
94  a memory limit.
95  If the same string is already present, then pointer to it is returned.
96  Strings are considered to be equal if strcmp(str1, str2) == 0.
97  @param storage in/out: hash storage
98  @param str in: string to put
99  @param memlim in: memory limit to obey
100  @return pointer to the copy of the string */
101 #define ha_storage_put_str_memlim(storage, str, memlim)                   \
102   ((const char *)ha_storage_put_memlim((storage), (str), strlen(str) + 1, \
103                                        (memlim)))
104 
105 /** Empties a hash storage, freeing memory occupied by data chunks.
106  This invalidates any pointers previously returned by ha_storage_put().
107  The hash storage is not invalidated itself and can be used again. */
108 UNIV_INLINE
109 void ha_storage_empty(ha_storage_t **storage); /*!< in/out: hash storage */
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_INLINE
115 void ha_storage_free(ha_storage_t *storage); /*!< in, own: hash storage */
116 
117 /** Gets the size of the memory used by a storage.
118  @return bytes used */
119 UNIV_INLINE
120 ulint ha_storage_get_size(const ha_storage_t *storage); /*!< in: hash storage */
121 
122 #include "ha0storage.ic"
123 
124 #endif /* ha0storage_h */
125