MySQL 8.0.39
Source Code Documentation
ut0vec.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2006, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 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
40struct ib_alloc_t;
41struct ib_vector_t;
42
43typedef 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
48typedef void (*ib_mem_free_t)(
49 ib_alloc_t *allocator, /* in: Pointer to allocator instance */
50 void *ptr); /* in: Memory to free */
51
52typedef 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
59typedef 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
64freeing 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/********************************************************************
74Create a new vector with the given initial size. */
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/********************************************************************
82Destroy the vector. Make sure the vector owns the allocator, e.g.,
83the heap in the the heap allocator. */
84static inline void ib_vector_free(ib_vector_t *vec); /* in/out: vector */
85
86/********************************************************************
87Push a new element to the vector, increasing its size if necessary,
88if elem is not NULL then elem is copied to the vector.*/
89static 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/********************************************************************
95Pop the last element from the vector.*/
96static 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 */
104static inline void *ib_vector_remove(ib_vector_t *vec, const void *elem);
105
106/********************************************************************
107Get the number of elements in the vector. */
108static inline ulint ib_vector_size(
109 /* out: number of elements in vector */
110 const ib_vector_t *vec); /* in: vector */
111
112/********************************************************************
113Increase the size of the vector. */
115 /* out: number of elements in vector */
116 ib_vector_t *vec); /* in/out: vector */
117
118/********************************************************************
119Test whether a vector is empty or not.
120@return true if empty */
121static 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 */
128static inline void *ib_vector_get(ib_vector_t *vec, ulint n);
129
130/********************************************************************
131Const version of the get n'th element.
132@return n'th element */
133static 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 */
138static 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 */
144static inline void ib_vector_set(ib_vector_t *vec, ulint n, void *elem);
145
146/********************************************************************
147Reset the vector size to 0 elements. */
148static inline void ib_vector_reset(ib_vector_t *vec); /* in/out: vector */
149
150/********************************************************************
151Get the last element of the vector. */
152static inline void *ib_vector_last(
153 /* out: pointer to last element */
154 ib_vector_t *vec); /* in/out: vector */
155
156/********************************************************************
157Get the last element of the vector. */
158static inline const void *ib_vector_last_const(
159 /* out: pointer to last element */
160 const ib_vector_t *vec); /* in: vector */
161
162/********************************************************************
163Sort the vector elements. */
164static 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/********************************************************************
169The default ib_vector_t heap free. Does nothing. */
170static inline void ib_heap_free(ib_alloc_t *allocator, /* in: allocator */
171 void *ptr); /* in: size in bytes */
172
173/********************************************************************
174The default ib_vector_t heap malloc. Uses mem_heap_alloc(). */
175static 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/********************************************************************
181The default ib_vector_t heap resize. Since we can't resize the heap
182we have to copy the elements from the old ptr to the new ptr.
183Uses mem_heap_alloc(). */
184static 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/********************************************************************
193Create a heap allocator that uses the passed in heap. */
195 /* out: heap allocator instance */
196 mem_heap_t *heap); /* in: heap to use */
197
198/********************************************************************
199Free a heap allocator. */
200static 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. */
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. */
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 */
221};
222
223#include "ut0vec.ic"
224
225#endif /* IB_VECTOR_H */
The memory management.
static int compare(size_t a, size_t b)
Function to compare two size_t integers for their relative order.
Definition: rpl_utility.cc:107
Definition: ut0vec.h:204
ib_mem_free_t mem_release
Definition: ut0vec.h:206
void * arg
Definition: ut0vec.h:208
ib_mem_resize_t mem_resize
Definition: ut0vec.h:207
ib_mem_alloc_t mem_malloc
Definition: ut0vec.h:205
Definition: ut0vec.h:213
ib_alloc_t * allocator
Definition: ut0vec.h:214
ulint used
Definition: ut0vec.h:217
void * data
Definition: ut0vec.h:216
ulint sizeof_value
Definition: ut0vec.h:220
ulint total
Definition: ut0vec.h:218
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:302
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:406
static void ib_heap_allocator_free(ib_alloc_t *ib_ut_alloc)
static const void * ib_vector_get_const(const ib_vector_t *vec, ulint n)
static void ib_vector_set(ib_vector_t *vec, ulint n, void *elem)
Set the n'th element.
static void * ib_vector_remove(ib_vector_t *vec, const void *elem)
Remove an element to the vector.
static void ib_vector_reset(ib_vector_t *vec)
static void ib_vector_sort(ib_vector_t *vec, ib_compare_t compare)
static void ib_vector_free(ib_vector_t *vec)
static ulint ib_vector_size(const ib_vector_t *vec)
void *(* ib_mem_resize_t)(ib_alloc_t *allocator, void *ptr, ulint old_size, ulint new_size)
Definition: ut0vec.h:52
static void ib_heap_free(ib_alloc_t *allocator, void *ptr)
static void * ib_vector_get_last(ib_vector_t *vec)
Get last element.
static void * ib_vector_last(ib_vector_t *vec)
static void * ib_vector_pop(ib_vector_t *vec)
void *(* ib_mem_alloc_t)(ib_alloc_t *allocator, ulint size)
Definition: ut0vec.h:43
static void * ib_heap_resize(ib_alloc_t *allocator, void *old_ptr, ulint old_size, ulint new_size)
void(* ib_mem_free_t)(ib_alloc_t *allocator, void *ptr)
Definition: ut0vec.h:48
static void * ib_heap_malloc(ib_alloc_t *allocator, ulint size)
ib_vector_t * ib_vector_create(ib_alloc_t *alloc, ulint sizeof_value, ulint size)
Definition: ut0vec.cc:39
static ib_alloc_t * ib_heap_allocator_create(mem_heap_t *heap)
static void * ib_vector_push(ib_vector_t *vec, const void *elem)
static const void * ib_vector_last_const(const ib_vector_t *vec)
int(* ib_compare_t)(const void *, const void *)
Definition: ut0vec.h:59
static void * ib_vector_get(ib_vector_t *vec, ulint n)
Get the n'th element.
void ib_vector_resize(ib_vector_t *vec)
Definition: ut0vec.cc:66
static bool ib_vector_is_empty(const ib_vector_t *vec)
in: vector
A vector of pointers to data items.
int n
Definition: xcom_base.cc:509