Debian Codegraph @

storage/innobase/include/ut0vec.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2006, 2026, Oracle and/or its affiliates.
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 designed to work 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 either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/ut0vec.h
29 A vector of pointers to data items
30
31 Created 4/6/2006 Osku Salerma
32 ************************************************************************/
33
34 #ifndef IB_VECTOR_H
35 #define IB_VECTOR_H
36
37 #include "mem0mem.h"
38 #include "univ.i"
39
40 struct ib_alloc_t;
41 struct ib_vector_t;
42
43 typedef void *(*ib_mem_alloc_t)(
44 /* out: Pointer to allocated memory */
45 ib_alloc_t *allocator, /* in: Pointer to allocator instance */
46 ulint size); /* in: Number of bytes to allocate */
47
48 typedef void (*ib_mem_free_t)(
49 ib_alloc_t *allocator, /* in: Pointer to allocator instance */
50 void *ptr); /* in: Memory to free */
51
52 typedef void *(*ib_mem_resize_t)(
53 /* out: Pointer to resized memory */
54 ib_alloc_t *allocator, /* in: Pointer to allocator */
55 void *ptr, /* in: Memory to resize */
56 ulint old_size, /* in: Old memory size in bytes */
57 ulint new_size); /* in: New size in bytes */
58
59 typedef int (*ib_compare_t)(const void *, const void *);
60
61 /* An automatically resizing vector datatype with the following properties:
62
63 -All memory allocation is done through an allocator, which is responsible for
64 freeing it when done with the vector.
65 */
66
67 /* This is useful shorthand for elements of type void* */
68 #define ib_vector_getp(v, n) (*(void **)ib_vector_get(v, n))
69 #define ib_vector_getp_const(v, n) (*(void **)ib_vector_get_const(v, n))
70
71 #define ib_vector_allocator(v) (v->allocator)
72
73 /********************************************************************
74 Create a new vector with the given initial size. */
75 ib_vector_t *ib_vector_create(
76 /* out: vector */
77 ib_alloc_t *alloc, /* in: Allocator */
78 /* in: size of the data item */
79 ulint sizeof_value, ulint size); /* in: initial size */
80
81 /********************************************************************
82 Destroy the vector. Make sure the vector owns the allocator, e.g.,
83 the heap in the the heap allocator. */
84 static inline 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 static inline void *ib_vector_push(
90 /* out: pointer the "new" element */
91 ib_vector_t *vec, /* in/out: vector */
92 const void *elem); /* in: data element */
93
94 /********************************************************************
95 Pop the last element from the vector.*/
96 static inline void *ib_vector_pop(
97 /* out: pointer to the "new" element */
98 ib_vector_t *vec); /* in/out: vector */
99
100 /** Remove an element to the vector
101 @param[in] vec vector
102 @param[in] elem value to remove
103 @return pointer to the "removed" element */
104 static inline void *ib_vector_remove(ib_vector_t *vec, const void *elem);
105
106 /********************************************************************
107 Get the number of elements in the vector. */
108 static inline ulint ib_vector_size(
109 /* out: number of elements in vector */
110 const ib_vector_t *vec); /* in: vector */
111
112 /********************************************************************
113 Increase the size of the vector. */
114 void ib_vector_resize(
115 /* out: number of elements in vector */
116 ib_vector_t *vec); /* in/out: vector */
117
118 /********************************************************************
119 Test whether a vector is empty or not.
120 @return true if empty */
121 static inline bool ib_vector_is_empty(
122 const ib_vector_t *vec); /*!< in: vector */
123
124 /** Get the n'th element.
125 @param[in] vec vector
126 @param[in] n element index to get
127 @return n'th element */
128 static inline void *ib_vector_get(ib_vector_t *vec, ulint n);
129
130 /********************************************************************
131 Const version of the get n'th element.
132 @return n'th element */
133 static inline const void *ib_vector_get_const(
134 const ib_vector_t *vec, /* in: vector */
135 ulint n); /* in: element index to get */
136 /** Get last element. The vector must not be empty.
137 @return last element */
138 static inline void *ib_vector_get_last(ib_vector_t *vec); /*!< in: vector */
139
140 /** Set the n'th element.
141 @param[in] vec vector
142 @param[in] n element index to set
143 @param[in] elem data element */
144 static inline void ib_vector_set(ib_vector_t *vec, ulint n, void *elem);
145
146 /********************************************************************
147 Reset the vector size to 0 elements. */
148 static inline void ib_vector_reset(ib_vector_t *vec); /* in/out: vector */
149
150 /********************************************************************
151 Get the last element of the vector. */
152 static inline void *ib_vector_last(
153 /* out: pointer to last element */
154 ib_vector_t *vec); /* in/out: vector */
155
156 /********************************************************************
157 Get the last element of the vector. */
158 static inline const void *ib_vector_last_const(
159 /* out: pointer to last element */
160 const ib_vector_t *vec); /* in: vector */
161
162 /********************************************************************
163 Sort the vector elements. */
164 static inline void ib_vector_sort(
165 ib_vector_t *vec, /* in/out: vector */
166 ib_compare_t compare); /* in: the comparator to use for sort */
167
168 /********************************************************************
169 The default ib_vector_t heap free. Does nothing. */
170 static inline void ib_heap_free(ib_alloc_t *allocator, /* in: allocator */
171 void *ptr); /* in: size in bytes */
172
173 /********************************************************************
174 The default ib_vector_t heap malloc. Uses mem_heap_alloc(). */
175 static inline void *ib_heap_malloc(
176 /* out: pointer to allocated memory */
177 ib_alloc_t *allocator, /* in: allocator */
178 ulint size); /* in: size in bytes */
179
180 /********************************************************************
181 The default ib_vector_t heap resize. Since we can't resize the heap
182 we have to copy the elements from the old ptr to the new ptr.
183 Uses mem_heap_alloc(). */
184 static inline void *ib_heap_resize(
185 /* out: pointer to reallocated
186 memory */
187 ib_alloc_t *allocator, /* in: allocator */
188 void *old_ptr, /* in: pointer to memory */
189 ulint old_size, /* in: old size in bytes */
190 ulint new_size); /* in: new size in bytes */
191
192 /********************************************************************
193 Create a heap allocator that uses the passed in heap. */
194 static inline ib_alloc_t *ib_heap_allocator_create(
195 /* out: heap allocator instance */
196 mem_heap_t *heap); /* in: heap to use */
197
198 /********************************************************************
199 Free a heap allocator. */
200 static inline void ib_heap_allocator_free(
201 ib_alloc_t *ib_ut_alloc); /* in: alloc instance to free */
202
203 /* Allocator used by ib_vector_t. */
204 struct ib_alloc_t {
205 ib_mem_alloc_t mem_malloc; /* For allocating memory */
206 ib_mem_free_t mem_release; /* For freeing memory */
207 ib_mem_resize_t mem_resize; /* For resizing memory */
208 void *arg; /* Currently if not NULL then it
209 points to the heap instance */
210 };
211
212 /* See comment at beginning of file. */
213 struct ib_vector_t {
214 ib_alloc_t *allocator; /* Allocator, because one size
215 doesn't fit all */
216 void *data; /* data elements */
217 ulint used; /* number of elements currently used */
218 ulint total; /* number of elements allocated */
219 /* Size of a data item */
220 ulint sizeof_value;
221 };
222
223 /* A helper to make it easier to use the legacy ib_vector_t struct. */
224 template <typename T>
225 class Vector_wrapper {
226 private:
227 ib_vector_t &m_ib_vector;
228
229 public:
230 Vector_wrapper(ib_vector_t &ib_vector) : m_ib_vector(ib_vector) {}
231
232 size_t size() const { return ib_vector_size(&m_ib_vector); }
233
234 bool empty() const { return size() == 0; }
235
236 const T &operator[](size_t idx) const {
237 return *static_cast<const T *>(ib_vector_get(&m_ib_vector, idx));
238 }
239
240 T &operator[](size_t idx) {
241 return *static_cast<T *>(ib_vector_get(&m_ib_vector, idx));
242 }
243
244 T *begin() { return empty() ? nullptr : &((*this)[0]); }
245
246 T *end() { return empty() ? begin() : begin() + size(); }
247
248 const T *begin() const { return empty() ? nullptr : &((*this)[0]); }
249
250 const T *end() const { return empty() ? begin() : begin() + size(); }
251 };
252 #include "ut0vec.ic"
253
254 #endif /* IB_VECTOR_H */