MySQL  8.0.19
Source Code Documentation
ut0vec.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 2006, 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/ut0vec.h
28  A vector of pointers to data items
29 
30  Created 4/6/2006 Osku Salerma
31  ************************************************************************/
32 
33 #ifndef IB_VECTOR_H
34 #define IB_VECTOR_H
35 
36 #include "mem0mem.h"
37 #include "univ.i"
38 
39 struct ib_alloc_t;
40 struct ib_vector_t;
41 
42 typedef void *(*ib_mem_alloc_t)(
43  /* out: Pointer to allocated memory */
44  ib_alloc_t *allocator, /* in: Pointer to allocator instance */
45  ulint size); /* in: Number of bytes to allocate */
46 
47 typedef void (*ib_mem_free_t)(
48  ib_alloc_t *allocator, /* in: Pointer to allocator instance */
49  void *ptr); /* in: Memory to free */
50 
51 typedef void *(*ib_mem_resize_t)(
52  /* out: Pointer to resized memory */
53  ib_alloc_t *allocator, /* in: Pointer to allocator */
54  void *ptr, /* in: Memory to resize */
55  ulint old_size, /* in: Old memory size in bytes */
56  ulint new_size); /* in: New size in bytes */
57 
58 typedef int (*ib_compare_t)(const void *, const void *);
59 
60 /* An automatically resizing vector datatype with the following properties:
61 
62  -All memory allocation is done through an allocator, which is responsible for
63 freeing it when done with the vector.
64 */
65 
66 /* This is useful shorthand for elements of type void* */
67 #define ib_vector_getp(v, n) (*(void **)ib_vector_get(v, n))
68 #define ib_vector_getp_const(v, n) (*(void **)ib_vector_get_const(v, n))
69 
70 #define ib_vector_allocator(v) (v->allocator)
71 
72 /********************************************************************
73 Create a new vector with the given initial size. */
75  /* out: vector */
76  ib_alloc_t *alloc, /* in: Allocator */
77  /* in: size of the data item */
78  ulint sizeof_value, ulint size); /* in: initial size */
79 
80 /********************************************************************
81 Destroy the vector. Make sure the vector owns the allocator, e.g.,
82 the heap in the the heap allocator. */
83 UNIV_INLINE
84 void ib_vector_free(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 *ib_vector_push(
91  /* out: pointer the "new" element */
92  ib_vector_t *vec, /* in/out: vector */
93  const void *elem); /* in: data element */
94 
95 /********************************************************************
96 Pop the last element from the vector.*/
97 UNIV_INLINE
98 void *ib_vector_pop(
99  /* out: pointer to the "new" element */
100  ib_vector_t *vec); /* in/out: vector */
101 
102 /** Remove an element to the vector
103 @param[in] vec vector
104 @param[in] elem value to remove
105 @return pointer to the "removed" element */
106 UNIV_INLINE
107 void *ib_vector_remove(ib_vector_t *vec, const void *elem);
108 
109 /********************************************************************
110 Get the number of elements in the vector. */
111 UNIV_INLINE
112 ulint ib_vector_size(
113  /* out: number of elements in vector */
114  const ib_vector_t *vec); /* in: vector */
115 
116 /********************************************************************
117 Increase the size of the vector. */
118 void ib_vector_resize(
119  /* out: number of elements in vector */
120  ib_vector_t *vec); /* in/out: vector */
121 
122 /********************************************************************
123 Test whether a vector is empty or not.
124 @return true if empty */
125 UNIV_INLINE
126 ibool ib_vector_is_empty(const ib_vector_t *vec); /*!< in: vector */
127 
128 /** Get the n'th element.
129 @param[in] vec vector
130 @param[in] n element index to get
131 @return n'th element */
132 UNIV_INLINE
133 void *ib_vector_get(ib_vector_t *vec, ulint n);
134 
135 /********************************************************************
136 Const version of the get n'th element.
137 @return n'th element */
138 UNIV_INLINE
139 const void *ib_vector_get_const(const ib_vector_t *vec, /* in: vector */
140  ulint n); /* in: element index to get */
141 /** Get last element. The vector must not be empty.
142  @return last element */
143 UNIV_INLINE
144 void *ib_vector_get_last(ib_vector_t *vec); /*!< in: vector */
145 
146 /** Set the n'th element.
147 @param[in] vec vector
148 @param[in] n element index to set
149 @param[in] elem data element */
150 UNIV_INLINE
151 void ib_vector_set(ib_vector_t *vec, ulint n, void *elem);
152 
153 /********************************************************************
154 Reset the vector size to 0 elements. */
155 UNIV_INLINE
156 void ib_vector_reset(ib_vector_t *vec); /* in/out: vector */
157 
158 /********************************************************************
159 Get the last element of the vector. */
160 UNIV_INLINE
161 void *ib_vector_last(
162  /* out: pointer to last element */
163  ib_vector_t *vec); /* in/out: vector */
164 
165 /********************************************************************
166 Get the last element of the vector. */
167 UNIV_INLINE
168 const void *ib_vector_last_const(
169  /* out: pointer to last element */
170  const ib_vector_t *vec); /* in: vector */
171 
172 /********************************************************************
173 Sort the vector elements. */
174 UNIV_INLINE
175 void ib_vector_sort(
176  ib_vector_t *vec, /* in/out: vector */
177  ib_compare_t compare); /* in: the comparator to use for sort */
178 
179 /********************************************************************
180 The default ib_vector_t heap free. Does nothing. */
181 UNIV_INLINE
182 void ib_heap_free(ib_alloc_t *allocator, /* in: allocator */
183  void *ptr); /* in: size in bytes */
184 
185 /********************************************************************
186 The default ib_vector_t heap malloc. Uses mem_heap_alloc(). */
187 UNIV_INLINE
188 void *ib_heap_malloc(
189  /* out: pointer to allocated memory */
190  ib_alloc_t *allocator, /* in: allocator */
191  ulint size); /* in: size in bytes */
192 
193 /********************************************************************
194 The default ib_vector_t heap resize. Since we can't resize the heap
195 we have to copy the elements from the old ptr to the new ptr.
196 Uses mem_heap_alloc(). */
197 UNIV_INLINE
198 void *ib_heap_resize(
199  /* out: pointer to reallocated
200  memory */
201  ib_alloc_t *allocator, /* in: allocator */
202  void *old_ptr, /* in: pointer to memory */
203  ulint old_size, /* in: old size in bytes */
204  ulint new_size); /* in: new size in bytes */
205 
206 /********************************************************************
207 Create a heap allocator that uses the passed in heap. */
208 UNIV_INLINE
210  /* out: heap allocator instance */
211  mem_heap_t *heap); /* in: heap to use */
212 
213 /********************************************************************
214 Free a heap allocator. */
215 UNIV_INLINE
217  ib_alloc_t *ib_ut_alloc); /* in: alloc instace to free */
218 
219 /* Allocator used by ib_vector_t. */
220 struct ib_alloc_t {
221  ib_mem_alloc_t mem_malloc; /* For allocating memory */
222  ib_mem_free_t mem_release; /* For freeing memory */
223  ib_mem_resize_t mem_resize; /* For resizing memory */
224  void *arg; /* Currently if not NULL then it
225  points to the heap instance */
226 };
227 
228 /* See comment at beginning of file. */
229 struct ib_vector_t {
230  ib_alloc_t *allocator; /* Allocator, because one size
231  doesn't fit all */
232  void *data; /* data elements */
233  ulint used; /* number of elements currently used */
234  ulint total; /* number of elements allocated */
235  /* Size of a data item */
237 };
238 
239 #include "ut0vec.ic"
240 
241 #endif /* IB_VECTOR_H */
ib_vector_t::sizeof_value
ulint sizeof_value
Definition: ut0vec.h:236
mem0mem.h
ib_vector_last_const
const UNIV_INLINE void * ib_vector_last_const(const ib_vector_t *vec)
ib_alloc_t::mem_malloc
ib_mem_alloc_t mem_malloc
Definition: ut0vec.h:221
ib_vector_size
UNIV_INLINE ulint ib_vector_size(const ib_vector_t *vec)
ib_alloc_t::arg
void * arg
Definition: ut0vec.h:224
ib_vector_t::used
ulint used
Definition: ut0vec.h:233
ib_vector_get_const
const UNIV_INLINE void * ib_vector_get_const(const ib_vector_t *vec, ulint n)
ib_vector_create
ib_vector_t * ib_vector_create(ib_alloc_t *alloc, ulint sizeof_value, ulint size)
Definition: ut0vec.cc:38
ib_heap_allocator_free
UNIV_INLINE void ib_heap_allocator_free(ib_alloc_t *ib_ut_alloc)
ib_alloc_t::mem_release
ib_mem_free_t mem_release
Definition: ut0vec.h:222
ib_vector_free
UNIV_INLINE void ib_vector_free(ib_vector_t *vec)
mem_block_info_t
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:337
ib_heap_malloc
UNIV_INLINE void * ib_heap_malloc(ib_alloc_t *allocator, ulint size)
ib_vector_push
UNIV_INLINE void * ib_vector_push(ib_vector_t *vec, const void *elem)
ib_mem_alloc_t
void *(* ib_mem_alloc_t)(ib_alloc_t *allocator, ulint size)
Definition: ut0vec.h:42
ib_alloc_t::mem_resize
ib_mem_resize_t mem_resize
Definition: ut0vec.h:223
ib_vector_remove
UNIV_INLINE void * ib_vector_remove(ib_vector_t *vec, const void *elem)
Remove an element to the vector.
ib_vector_get
UNIV_INLINE void * ib_vector_get(ib_vector_t *vec, ulint n)
Get the n'th element.
ib_compare_t
int(* ib_compare_t)(const void *, const void *)
Definition: ut0vec.h:58
ib_vector_resize
void ib_vector_resize(ib_vector_t *vec)
Definition: ut0vec.cc:65
ib_vector_t::total
ulint total
Definition: ut0vec.h:234
ib_heap_resize
UNIV_INLINE void * ib_heap_resize(ib_alloc_t *allocator, void *old_ptr, ulint old_size, ulint new_size)
ib_vector_is_empty
UNIV_INLINE ibool ib_vector_is_empty(const ib_vector_t *vec)
in: vector
ib_vector_set
UNIV_INLINE void ib_vector_set(ib_vector_t *vec, ulint n, void *elem)
Set the n'th element.
ib_vector_t::allocator
ib_alloc_t * allocator
Definition: ut0vec.h:230
ib_vector_t
Definition: ut0vec.h:229
ib_vector_last
UNIV_INLINE void * ib_vector_last(ib_vector_t *vec)
ib_heap_free
UNIV_INLINE void ib_heap_free(ib_alloc_t *allocator, void *ptr)
ib_vector_sort
UNIV_INLINE void ib_vector_sort(ib_vector_t *vec, ib_compare_t compare)
ib_mem_free_t
void(* ib_mem_free_t)(ib_alloc_t *allocator, void *ptr)
Definition: ut0vec.h:47
compare
static int compare(size_t a, size_t b)
Function to compare two size_t integers for their relative order.
Definition: rpl_utility.cc:103
ib_vector_pop
UNIV_INLINE void * ib_vector_pop(ib_vector_t *vec)
n
int n
Definition: xcom_base.c:425
ib_heap_allocator_create
UNIV_INLINE ib_alloc_t * ib_heap_allocator_create(mem_heap_t *heap)
ib_vector_t::data
void * data
Definition: ut0vec.h:232
ib_alloc_t
Definition: ut0vec.h:220
ib_vector_get_last
UNIV_INLINE void * ib_vector_get_last(ib_vector_t *vec)
Get last element.
ib_mem_resize_t
void *(* ib_mem_resize_t)(ib_alloc_t *allocator, void *ptr, ulint old_size, ulint new_size)
Definition: ut0vec.h:51
ib_vector_reset
UNIV_INLINE void ib_vector_reset(ib_vector_t *vec)