MySQL 8.3.0
Source Code Documentation
ut0vec.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2006, 2023, 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 also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 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
39struct ib_alloc_t;
40struct ib_vector_t;
41
42typedef 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
47typedef void (*ib_mem_free_t)(
48 ib_alloc_t *allocator, /* in: Pointer to allocator instance */
49 void *ptr); /* in: Memory to free */
50
51typedef 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
58typedef 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
63freeing 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/********************************************************************
73Create 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/********************************************************************
81Destroy the vector. Make sure the vector owns the allocator, e.g.,
82the heap in the the heap allocator. */
83static inline void ib_vector_free(ib_vector_t *vec); /* in/out: vector */
84
85/********************************************************************
86Push a new element to the vector, increasing its size if necessary,
87if elem is not NULL then elem is copied to the vector.*/
88static inline void *ib_vector_push(
89 /* out: pointer the "new" element */
90 ib_vector_t *vec, /* in/out: vector */
91 const void *elem); /* in: data element */
92
93/********************************************************************
94Pop the last element from the vector.*/
95static inline void *ib_vector_pop(
96 /* out: pointer to the "new" element */
97 ib_vector_t *vec); /* in/out: vector */
98
99/** Remove an element to the vector
100@param[in] vec vector
101@param[in] elem value to remove
102@return pointer to the "removed" element */
103static inline void *ib_vector_remove(ib_vector_t *vec, const void *elem);
104
105/********************************************************************
106Get the number of elements in the vector. */
107static inline ulint ib_vector_size(
108 /* out: number of elements in vector */
109 const ib_vector_t *vec); /* in: vector */
110
111/********************************************************************
112Increase the size of the vector. */
114 /* out: number of elements in vector */
115 ib_vector_t *vec); /* in/out: vector */
116
117/********************************************************************
118Test whether a vector is empty or not.
119@return true if empty */
120static inline bool ib_vector_is_empty(
121 const ib_vector_t *vec); /*!< in: vector */
122
123/** Get the n'th element.
124@param[in] vec vector
125@param[in] n element index to get
126@return n'th element */
127static inline void *ib_vector_get(ib_vector_t *vec, ulint n);
128
129/********************************************************************
130Const version of the get n'th element.
131@return n'th element */
132static inline const void *ib_vector_get_const(
133 const ib_vector_t *vec, /* in: vector */
134 ulint n); /* in: element index to get */
135/** Get last element. The vector must not be empty.
136 @return last element */
137static inline void *ib_vector_get_last(ib_vector_t *vec); /*!< in: vector */
138
139/** Set the n'th element.
140@param[in] vec vector
141@param[in] n element index to set
142@param[in] elem data element */
143static inline void ib_vector_set(ib_vector_t *vec, ulint n, void *elem);
144
145/********************************************************************
146Reset the vector size to 0 elements. */
147static inline void ib_vector_reset(ib_vector_t *vec); /* in/out: vector */
148
149/********************************************************************
150Get the last element of the vector. */
151static inline void *ib_vector_last(
152 /* out: pointer to last element */
153 ib_vector_t *vec); /* in/out: vector */
154
155/********************************************************************
156Get the last element of the vector. */
157static inline const void *ib_vector_last_const(
158 /* out: pointer to last element */
159 const ib_vector_t *vec); /* in: vector */
160
161/********************************************************************
162Sort the vector elements. */
163static inline void ib_vector_sort(
164 ib_vector_t *vec, /* in/out: vector */
165 ib_compare_t compare); /* in: the comparator to use for sort */
166
167/********************************************************************
168The default ib_vector_t heap free. Does nothing. */
169static inline void ib_heap_free(ib_alloc_t *allocator, /* in: allocator */
170 void *ptr); /* in: size in bytes */
171
172/********************************************************************
173The default ib_vector_t heap malloc. Uses mem_heap_alloc(). */
174static inline void *ib_heap_malloc(
175 /* out: pointer to allocated memory */
176 ib_alloc_t *allocator, /* in: allocator */
177 ulint size); /* in: size in bytes */
178
179/********************************************************************
180The default ib_vector_t heap resize. Since we can't resize the heap
181we have to copy the elements from the old ptr to the new ptr.
182Uses mem_heap_alloc(). */
183static inline void *ib_heap_resize(
184 /* out: pointer to reallocated
185 memory */
186 ib_alloc_t *allocator, /* in: allocator */
187 void *old_ptr, /* in: pointer to memory */
188 ulint old_size, /* in: old size in bytes */
189 ulint new_size); /* in: new size in bytes */
190
191/********************************************************************
192Create a heap allocator that uses the passed in heap. */
194 /* out: heap allocator instance */
195 mem_heap_t *heap); /* in: heap to use */
196
197/********************************************************************
198Free a heap allocator. */
199static inline void ib_heap_allocator_free(
200 ib_alloc_t *ib_ut_alloc); /* in: alloc instance to free */
201
202/* Allocator used by ib_vector_t. */
204 ib_mem_alloc_t mem_malloc; /* For allocating memory */
205 ib_mem_free_t mem_release; /* For freeing memory */
206 ib_mem_resize_t mem_resize; /* For resizing memory */
207 void *arg; /* Currently if not NULL then it
208 points to the heap instance */
209};
210
211/* See comment at beginning of file. */
213 ib_alloc_t *allocator; /* Allocator, because one size
214 doesn't fit all */
215 void *data; /* data elements */
216 ulint used; /* number of elements currently used */
217 ulint total; /* number of elements allocated */
218 /* Size of a data item */
220};
221
222#include "ut0vec.ic"
223
224#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:106
Definition: ut0vec.h:203
ib_mem_free_t mem_release
Definition: ut0vec.h:205
void * arg
Definition: ut0vec.h:207
ib_mem_resize_t mem_resize
Definition: ut0vec.h:206
ib_mem_alloc_t mem_malloc
Definition: ut0vec.h:204
Definition: ut0vec.h:212
ib_alloc_t * allocator
Definition: ut0vec.h:213
ulint used
Definition: ut0vec.h:216
void * data
Definition: ut0vec.h:215
ulint sizeof_value
Definition: ut0vec.h:219
ulint total
Definition: ut0vec.h:217
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:301
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:405
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:51
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:42
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:47
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:38
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:58
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:65
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:508