InnoDB Plugin  1.0
ut0vec.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 2006, 2009, 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 as published by the Free Software
7 Foundation; version 2 of the License.
8 
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
16 
17 *****************************************************************************/
18 
19 /*******************************************************************/
26 #ifndef IB_VECTOR_H
27 #define IB_VECTOR_H
28 
29 #include "univ.i"
30 #include "mem0mem.h"
31 
32 struct ib_alloc_t;
33 struct ib_vector_t;
34 
35 typedef void* (*ib_mem_alloc_t)(
36  /* out: Pointer to allocated memory */
37  ib_alloc_t* allocator, /* in: Pointer to allocator instance */
38  ulint size); /* in: Number of bytes to allocate */
39 
40 typedef void (*ib_mem_free_t)(
41  ib_alloc_t* allocator, /* in: Pointer to allocator instance */
42  void* ptr); /* in: Memory to free */
43 
44 typedef void* (*ib_mem_resize_t)(
45  /* out: Pointer to resized memory */
46  ib_alloc_t* allocator, /* in: Pointer to allocator */
47  void* ptr, /* in: Memory to resize */
48  ulint old_size, /* in: Old memory size in bytes */
49  ulint new_size); /* in: New size in bytes */
50 
51 typedef int (*ib_compare_t)(const void*, const void*);
52 
53 /* An automatically resizing vector datatype with the following properties:
54 
55  -All memory allocation is done through an allocator, which is responsible for
56 freeing it when done with the vector.
57 */
58 
59 /* This is useful shorthand for elements of type void* */
60 #define ib_vector_getp(v, n) (*(void**) ib_vector_get(v, n))
61 #define ib_vector_getp_const(v, n) (*(void**) ib_vector_get_const(v, n))
62 
63 #define ib_vector_allocator(v) (v->allocator)
64 
65 /********************************************************************
66 Create a new vector with the given initial size. */
67 UNIV_INTERN
69 ib_vector_create(
70 /*=============*/
71  /* out: vector */
72  ib_alloc_t* alloc, /* in: Allocator */
73  /* in: size of the data item */
74  ulint sizeof_value,
75  ulint size); /* in: initial size */
76 
77 /********************************************************************
78 Destroy the vector. Make sure the vector owns the allocator, e.g.,
79 the heap in the the heap allocator. */
80 UNIV_INLINE
81 void
82 ib_vector_free(
83 /*===========*/
84  ib_vector_t* vec); /* in/out: vector */
85 
86 /********************************************************************
87 Push a new element to the vector, increasing its size if necessary,
88 if elem is not NULL then elem is copied to the vector.*/
89 UNIV_INLINE
90 void*
91 ib_vector_push(
92 /*===========*/
93  /* out: pointer the "new" element */
94  ib_vector_t* vec, /* in/out: vector */
95  const void* elem); /* in: data element */
96 
97 /********************************************************************
98 Pop the last element from the vector.*/
99 UNIV_INLINE
100 void*
102 /*==========*/
103  /* out: pointer to the "new" element */
104  ib_vector_t* vec); /* in/out: vector */
105 
106 /*******************************************************************/
109 UNIV_INLINE
110 void*
112 /*=============*/
113  ib_vector_t* vec,
114  const void* elem);
116 /********************************************************************
117 Get the number of elements in the vector. */
118 UNIV_INLINE
119 ulint
120 ib_vector_size(
121 /*===========*/
122  /* out: number of elements in vector */
123  const ib_vector_t* vec); /* in: vector */
124 
125 /********************************************************************
126 Increase the size of the vector. */
127 UNIV_INTERN
128 void
129 ib_vector_resize(
130 /*=============*/
131  /* out: number of elements in vector */
132  ib_vector_t* vec); /* in/out: vector */
133 
134 /********************************************************************
135 Test whether a vector is empty or not.
136 @return TRUE if empty */
137 UNIV_INLINE
138 ibool
140 /*===============*/
141  const ib_vector_t* vec);
143 /****************************************************************/
146 UNIV_INLINE
147 void*
149 /*==========*/
150  ib_vector_t* vec,
151  ulint n);
153 /********************************************************************
154 Const version of the get n'th element.
155 @return n'th element */
156 UNIV_INLINE
157 const void*
158 ib_vector_get_const(
159 /*================*/
160  const ib_vector_t* vec, /* in: vector */
161  ulint n); /* in: element index to get */
162 /****************************************************************/
165 UNIV_INLINE
166 void*
168 /*===============*/
169  ib_vector_t* vec);
170 /****************************************************************/
172 UNIV_INLINE
173 void
175 /*==========*/
176  ib_vector_t* vec,
177  ulint n,
178  void* elem);
180 /********************************************************************
181 Reset the vector size to 0 elements. */
182 UNIV_INLINE
183 void
184 ib_vector_reset(
185 /*============*/
186  ib_vector_t* vec); /* in/out: vector */
187 
188 /********************************************************************
189 Get the last element of the vector. */
190 UNIV_INLINE
191 void*
192 ib_vector_last(
193 /*===========*/
194  /* out: pointer to last element */
195  ib_vector_t* vec); /* in/out: vector */
196 
197 /********************************************************************
198 Get the last element of the vector. */
199 UNIV_INLINE
200 const void*
201 ib_vector_last_const(
202 /*=================*/
203  /* out: pointer to last element */
204  const ib_vector_t* vec); /* in: vector */
205 
206 /********************************************************************
207 Sort the vector elements. */
208 UNIV_INLINE
209 void
210 ib_vector_sort(
211 /*===========*/
212  ib_vector_t* vec, /* in/out: vector */
213  ib_compare_t compare); /* in: the comparator to use for sort */
214 
215 /********************************************************************
216 The default ib_vector_t heap free. Does nothing. */
217 UNIV_INLINE
218 void
219 ib_heap_free(
220 /*=========*/
221  ib_alloc_t* allocator, /* in: allocator */
222  void* ptr); /* in: size in bytes */
223 
224 /********************************************************************
225 The default ib_vector_t heap malloc. Uses mem_heap_alloc(). */
226 UNIV_INLINE
227 void*
228 ib_heap_malloc(
229 /*===========*/
230  /* out: pointer to allocated memory */
231  ib_alloc_t* allocator, /* in: allocator */
232  ulint size); /* in: size in bytes */
233 
234 /********************************************************************
235 The default ib_vector_t heap resize. Since we can't resize the heap
236 we have to copy the elements from the old ptr to the new ptr.
237 Uses mem_heap_alloc(). */
238 UNIV_INLINE
239 void*
240 ib_heap_resize(
241 /*===========*/
242  /* out: pointer to reallocated
243  memory */
244  ib_alloc_t* allocator, /* in: allocator */
245  void* old_ptr, /* in: pointer to memory */
246  ulint old_size, /* in: old size in bytes */
247  ulint new_size); /* in: new size in bytes */
248 
249 /********************************************************************
250 Create a heap allocator that uses the passed in heap. */
251 UNIV_INLINE
252 ib_alloc_t*
253 ib_heap_allocator_create(
254 /*=====================*/
255  /* out: heap allocator instance */
256  mem_heap_t* heap); /* in: heap to use */
257 
258 /********************************************************************
259 Free a heap allocator. */
260 UNIV_INLINE
261 void
262 ib_heap_allocator_free(
263 /*===================*/
264  ib_alloc_t* ib_ut_alloc); /* in: alloc instace to free */
265 
266 /********************************************************************
267 Wrapper for ut_free(). */
268 UNIV_INLINE
269 void
270 ib_ut_free(
271 /*=======*/
272  ib_alloc_t* allocator, /* in: allocator */
273  void* ptr); /* in: size in bytes */
274 
275 /********************************************************************
276 Wrapper for ut_malloc(). */
277 UNIV_INLINE
278 void*
279 ib_ut_malloc(
280 /*=========*/
281  /* out: pointer to allocated memory */
282  ib_alloc_t* allocator, /* in: allocator */
283  ulint size); /* in: size in bytes */
284 
285 /********************************************************************
286 Wrapper for ut_realloc(). */
287 UNIV_INLINE
288 void*
289 ib_ut_resize(
290 /*=========*/
291  /* out: pointer to reallocated
292  memory */
293  ib_alloc_t* allocator, /* in: allocator */
294  void* old_ptr, /* in: pointer to memory */
295  ulint old_size, /* in: old size in bytes */
296  ulint new_size); /* in: new size in bytes */
297 
298 /********************************************************************
299 Create a heap allocator that uses the passed in heap. */
300 UNIV_INLINE
301 ib_alloc_t*
302 ib_ut_allocator_create(void);
303 /*=========================*/
304 
305 /********************************************************************
306 Create a heap allocator that uses the passed in heap. */
307 UNIV_INLINE
308 void
309 ib_ut_allocator_free(
310 /*=================*/
311  ib_alloc_t* ib_ut_alloc); /* in: alloc instace to free */
312 
313 /* Allocator used by ib_vector_t. */
314 struct ib_alloc_t {
315  ib_mem_alloc_t mem_malloc; /* For allocating memory */
316  ib_mem_free_t mem_release; /* For freeing memory */
317  ib_mem_resize_t mem_resize; /* For resizing memory */
318  void* arg; /* Currently if not NULL then it
319  points to the heap instance */
320 };
321 
322 /* See comment at beginning of file. */
323 struct ib_vector_t {
324  ib_alloc_t* allocator; /* Allocator, because one size
325  doesn't fit all */
326  void* data; /* data elements */
327  ulint used; /* number of elements currently used */
328  ulint total; /* number of elements allocated */
329  /* Size of a data item */
330  ulint sizeof_value;
331 };
332 
333 #ifndef UNIV_NONINL
334 #include "ut0vec.ic"
335 #endif
336 
337 #endif /* IB_VECTOR_H */