1 /*****************************************************************************
2 
3 Copyright (c) 2007, 2009, 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 UNIV_INTERN
75 const void*
76 ha_storage_put_memlim(
77 /*==================*/
78 	ha_storage_t*	storage,	/*!< in/out: hash storage */
79 	const void*	data,		/*!< in: data to store */
80 	ulint		data_len,	/*!< in: data length */
81 	ulint		memlim);	/*!< in: memory limit to obey */
82 
83 /*******************************************************************//**
84 Same as ha_storage_put_memlim() but without memory limit.
85 @param storage	in/out: hash storage
86 @param data	in: data to store
87 @param data_len	in: data length
88 @return		pointer to the copy of the string */
89 #define ha_storage_put(storage, data, data_len)	\
90 	ha_storage_put_memlim((storage), (data), (data_len), 0)
91 
92 /*******************************************************************//**
93 Copies string into the storage and returns a pointer to the copy. If the
94 same string is already present, then pointer to it is returned.
95 Strings are considered to be equal if strcmp(str1, str2) == 0.
96 @param storage	in/out: hash storage
97 @param str	in: string to put
98 @return		pointer to the copy of the string */
99 #define ha_storage_put_str(storage, str)	\
100 	((const char*) ha_storage_put((storage), (str), strlen(str) + 1))
101 
102 /*******************************************************************//**
103 Copies string into the storage and returns a pointer to the copy obeying
104 a memory limit.
105 If the same string is already present, then pointer to it is returned.
106 Strings are considered to be equal if strcmp(str1, str2) == 0.
107 @param storage	in/out: hash storage
108 @param str	in: string to put
109 @param memlim	in: memory limit to obey
110 @return		pointer to the copy of the string */
111 #define ha_storage_put_str_memlim(storage, str, memlim)	\
112 	((const char*) ha_storage_put_memlim((storage), (str),	\
113 					     strlen(str) + 1, (memlim)))
114 
115 /*******************************************************************//**
116 Empties a hash storage, freeing memory occupied by data chunks.
117 This invalidates any pointers previously returned by ha_storage_put().
118 The hash storage is not invalidated itself and can be used again. */
119 UNIV_INLINE
120 void
121 ha_storage_empty(
122 /*=============*/
123 	ha_storage_t**	storage);	/*!< in/out: hash storage */
124 
125 /*******************************************************************//**
126 Frees a hash storage and everything it contains, it cannot be used after
127 this call.
128 This invalidates any pointers previously returned by ha_storage_put(). */
129 UNIV_INLINE
130 void
131 ha_storage_free(
132 /*============*/
133 	ha_storage_t*	storage);	/*!< in, own: hash storage */
134 
135 /*******************************************************************//**
136 Gets the size of the memory used by a storage.
137 @return	bytes used */
138 UNIV_INLINE
139 ulint
140 ha_storage_get_size(
141 /*================*/
142 	const ha_storage_t*	storage);	/*!< in: hash storage */
143 
144 #ifndef UNIV_NONINL
145 #include "ha0storage.ic"
146 #endif
147 
148 #endif /* ha0storage_h */
149