1 /*****************************************************************************
2 
3 Copyright (c) 2007, 2014, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8 
9 This program is also distributed 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 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License, version 2.0, 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 Street, Suite 500, Boston, MA 02110-1335 USA
24 
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 #define 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 #define HA_STORAGE_DEFAULT_HASH_CELLS	4096
48 
49 /** Hash storage */
50 struct ha_storage_t;
51 
52 /*******************************************************************//**
53 Creates a hash storage. If any of the parameters is 0, then a default
54 value is used.
55 @return own: hash storage */
56 UNIV_INLINE
57 ha_storage_t*
58 ha_storage_create(
59 /*==============*/
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 /*******************************************************************//**
65 Copies data into the storage and returns a pointer to the copy. If the
66 same data chunk is already present, then pointer to it is returned.
67 Data chunks are considered to be equal if len1 == len2 and
68 memcmp(data1, data2, len1) == 0. If "data" is not present (and thus
69 data_len bytes need to be allocated) and the size of storage is going to
70 become more than "memlim" then "data" is not added and NULL is returned.
71 To disable this behavior "memlim" can be set to 0, which stands for
72 "no limit".
73 @return pointer to the copy */
74 const void*
75 ha_storage_put_memlim(
76 /*==================*/
77 	ha_storage_t*	storage,	/*!< in/out: hash storage */
78 	const void*	data,		/*!< in: data to store */
79 	ulint		data_len,	/*!< in: data length */
80 	ulint		memlim);	/*!< in: memory limit to obey */
81 
82 /*******************************************************************//**
83 Same as ha_storage_put_memlim() but without memory limit.
84 @param storage in/out: hash storage
85 @param data in: data to store
86 @param data_len in: data length
87 @return pointer to the copy of the string */
88 #define ha_storage_put(storage, data, data_len)	\
89 	ha_storage_put_memlim((storage), (data), (data_len), 0)
90 
91 /*******************************************************************//**
92 Copies string into the storage and returns a pointer to the copy. If the
93 same string is already present, then pointer to it is returned.
94 Strings are considered to be equal if strcmp(str1, str2) == 0.
95 @param storage in/out: hash storage
96 @param str in: string to put
97 @return pointer to the copy of the string */
98 #define ha_storage_put_str(storage, str)	\
99 	((const char*) ha_storage_put((storage), (str), strlen(str) + 1))
100 
101 /*******************************************************************//**
102 Copies string into the storage and returns a pointer to the copy obeying
103 a memory limit.
104 If the same string is already present, then pointer to it is returned.
105 Strings are considered to be equal if strcmp(str1, str2) == 0.
106 @param storage in/out: hash storage
107 @param str in: string to put
108 @param memlim in: memory limit to obey
109 @return pointer to the copy of the string */
110 #define ha_storage_put_str_memlim(storage, str, memlim)	\
111 	((const char*) ha_storage_put_memlim((storage), (str),	\
112 					     strlen(str) + 1, (memlim)))
113 
114 /*******************************************************************//**
115 Empties a hash storage, freeing memory occupied by data chunks.
116 This invalidates any pointers previously returned by ha_storage_put().
117 The hash storage is not invalidated itself and can be used again. */
118 UNIV_INLINE
119 void
120 ha_storage_empty(
121 /*=============*/
122 	ha_storage_t**	storage);	/*!< in/out: hash storage */
123 
124 /*******************************************************************//**
125 Frees a hash storage and everything it contains, it cannot be used after
126 this call.
127 This invalidates any pointers previously returned by ha_storage_put(). */
128 UNIV_INLINE
129 void
130 ha_storage_free(
131 /*============*/
132 	ha_storage_t*	storage);	/*!< in, own: hash storage */
133 
134 /*******************************************************************//**
135 Gets the size of the memory used by a storage.
136 @return bytes used */
137 UNIV_INLINE
138 ulint
139 ha_storage_get_size(
140 /*================*/
141 	const ha_storage_t*	storage);	/*!< in: hash storage */
142 
143 #ifndef UNIV_NONINL
144 #include "ha0storage.ic"
145 #endif
146 
147 #endif /* ha0storage_h */
148