| 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.ic |
| 29 |
A vector of pointers to data items |
| 30 |
|
| 31 |
Created 4/6/2006 Osku Salerma |
| 32 |
************************************************************************/ |
| 33 |
|
| 34 |
#include "ut0new.h" |
| 35 |
|
| 36 |
#define IB_VEC_OFFSET(v, i) (vec->sizeof_value * i) |
| 37 |
|
| 38 |
/******************************************************************** |
| 39 |
The default ib_vector_t heap malloc. Uses mem_heap_alloc(). */ |
| 40 |
static inline void *ib_heap_malloc(ib_alloc_t *allocator, /* in: allocator */ |
| 41 |
ulint size) /* in: size in bytes */ |
| 42 |
{ |
| 43 |
mem_heap_t *heap = (mem_heap_t *)allocator->arg; |
| 44 |
|
| 45 |
return (mem_heap_alloc(heap, size)); |
| 46 |
} |
| 47 |
|
| 48 |
/******************************************************************** |
| 49 |
The default ib_vector_t heap free. Does nothing. */ |
| 50 |
static inline void ib_heap_free(ib_alloc_t *allocator |
| 51 |
[[maybe_unused]], /* in: allocator */ |
| 52 |
void *ptr |
| 53 |
[[maybe_unused]]) /* in: size in bytes */ |
| 54 |
{ |
| 55 |
/* We can't free individual elements. */ |
| 56 |
} |
| 57 |
|
| 58 |
/******************************************************************** |
| 59 |
The default ib_vector_t heap resize. Since we can't resize the heap |
| 60 |
we have to copy the elements from the old ptr to the new ptr. |
| 61 |
We always assume new_size >= old_size, so the buffer won't overflow. |
| 62 |
Uses mem_heap_alloc(). */ |
| 63 |
static inline void *ib_heap_resize(ib_alloc_t *allocator, /* in: allocator */ |
| 64 |
void *old_ptr, /* in: pointer to memory */ |
| 65 |
ulint old_size, /* in: old size in bytes */ |
| 66 |
ulint new_size) /* in: new size in bytes */ |
| 67 |
{ |
| 68 |
void *new_ptr; |
| 69 |
mem_heap_t *heap = (mem_heap_t *)allocator->arg; |
| 70 |
|
| 71 |
ut_a(new_size >= old_size); |
| 72 |
new_ptr = mem_heap_alloc(heap, new_size); |
| 73 |
memcpy(new_ptr, old_ptr, old_size); |
| 74 |
|
| 75 |
return (new_ptr); |
| 76 |
} |
| 77 |
|
| 78 |
/******************************************************************** |
| 79 |
Create a heap allocator that uses the passed in heap. */ |
| 80 |
static inline ib_alloc_t *ib_heap_allocator_create( |
| 81 |
mem_heap_t *heap) /* in: heap to use */ |
| 82 |
{ |
| 83 |
ib_alloc_t *heap_alloc; |
| 84 |
|
| 85 |
heap_alloc = (ib_alloc_t *)mem_heap_alloc(heap, sizeof(*heap_alloc)); |
| 86 |
|
| 87 |
heap_alloc->arg = heap; |
| 88 |
heap_alloc->mem_release = ib_heap_free; |
| 89 |
heap_alloc->mem_malloc = ib_heap_malloc; |
| 90 |
heap_alloc->mem_resize = ib_heap_resize; |
| 91 |
|
| 92 |
return (heap_alloc); |
| 93 |
} |
| 94 |
|
| 95 |
/******************************************************************** |
| 96 |
Free a heap allocator. */ |
| 97 |
static inline void ib_heap_allocator_free( |
| 98 |
ib_alloc_t *ib_ut_alloc) /* in: alloc instance to free */ |
| 99 |
{ |
| 100 |
mem_heap_free((mem_heap_t *)ib_ut_alloc->arg); |
| 101 |
} |
| 102 |
|
| 103 |
/******************************************************************** |
| 104 |
Get number of elements in vector. */ |
| 105 |
static inline ulint ib_vector_size( |
| 106 |
/* out: number of elements in vector*/ |
| 107 |
const ib_vector_t *vec) /* in: vector */ |
| 108 |
{ |
| 109 |
return (vec->used); |
| 110 |
} |
| 111 |
|
| 112 |
/** Get n'th element. */ |
| 113 |
static inline void *ib_vector_get(ib_vector_t *vec, /*!< in: vector */ |
| 114 |
ulint n) /*!< in: element index to get */ |
| 115 |
{ |
| 116 |
ut_a(n < vec->used); |
| 117 |
|
| 118 |
return ((byte *)vec->data + IB_VEC_OFFSET(vec, n)); |
| 119 |
} |
| 120 |
|
| 121 |
/******************************************************************** |
| 122 |
Const version of the get n'th element. |
| 123 |
@return n'th element */ |
| 124 |
static inline const void *ib_vector_get_const( |
| 125 |
const ib_vector_t *vec, /* in: vector */ |
| 126 |
ulint n) /* in: element index to get */ |
| 127 |
{ |
| 128 |
ut_a(n < vec->used); |
| 129 |
|
| 130 |
return ((byte *)vec->data + IB_VEC_OFFSET(vec, n)); |
| 131 |
} |
| 132 |
/** Get last element. The vector must not be empty. |
| 133 |
@return last element */ |
| 134 |
static inline void *ib_vector_get_last(ib_vector_t *vec) /*!< in: vector */ |
| 135 |
{ |
| 136 |
ut_a(vec->used > 0); |
| 137 |
|
| 138 |
return ((byte *)ib_vector_get(vec, vec->used - 1)); |
| 139 |
} |
| 140 |
|
| 141 |
/** Set the n'th element. */ |
| 142 |
static inline void ib_vector_set(ib_vector_t *vec, /*!< in/out: vector */ |
| 143 |
ulint n, /*!< in: element index to set */ |
| 144 |
void *elem) /*!< in: data element */ |
| 145 |
{ |
| 146 |
void *slot; |
| 147 |
|
| 148 |
ut_a(n < vec->used); |
| 149 |
|
| 150 |
slot = ((byte *)vec->data + IB_VEC_OFFSET(vec, n)); |
| 151 |
memcpy(slot, elem, vec->sizeof_value); |
| 152 |
} |
| 153 |
|
| 154 |
/******************************************************************** |
| 155 |
Reset the vector size to 0 elements. */ |
| 156 |
static inline void ib_vector_reset( |
| 157 |
/* out: void */ |
| 158 |
ib_vector_t *vec) /* in: vector */ |
| 159 |
{ |
| 160 |
vec->used = 0; |
| 161 |
} |
| 162 |
|
| 163 |
/******************************************************************** |
| 164 |
Get the last element of the vector. */ |
| 165 |
static inline void *ib_vector_last( |
| 166 |
/* out: void */ |
| 167 |
ib_vector_t *vec) /* in: vector */ |
| 168 |
{ |
| 169 |
ut_a(ib_vector_size(vec) > 0); |
| 170 |
|
| 171 |
return (ib_vector_get(vec, ib_vector_size(vec) - 1)); |
| 172 |
} |
| 173 |
|
| 174 |
/******************************************************************** |
| 175 |
Get the last element of the vector. */ |
| 176 |
static inline const void *ib_vector_last_const( |
| 177 |
/* out: void */ |
| 178 |
const ib_vector_t *vec) /* in: vector */ |
| 179 |
{ |
| 180 |
ut_a(ib_vector_size(vec) > 0); |
| 181 |
|
| 182 |
return (ib_vector_get_const(vec, ib_vector_size(vec) - 1)); |
| 183 |
} |
| 184 |
|
| 185 |
/** Remove the last element from the vector. |
| 186 |
@return last vector element */ |
| 187 |
static inline void *ib_vector_pop( |
| 188 |
/* out: pointer to element */ |
| 189 |
ib_vector_t *vec) /* in: vector */ |
| 190 |
{ |
| 191 |
void *elem; |
| 192 |
|
| 193 |
ut_a(vec->used > 0); |
| 194 |
|
| 195 |
elem = ib_vector_last(vec); |
| 196 |
--vec->used; |
| 197 |
|
| 198 |
return (elem); |
| 199 |
} |
| 200 |
|
| 201 |
/******************************************************************** |
| 202 |
Append an element to the vector, if elem != NULL then copy the data |
| 203 |
from elem.*/ |
| 204 |
static inline void *ib_vector_push( |
| 205 |
/* out: pointer to the "new" element */ |
| 206 |
ib_vector_t *vec, /* in: vector */ |
| 207 |
const void *elem) /* in: element to add (can be NULL) */ |
| 208 |
{ |
| 209 |
void *last; |
| 210 |
|
| 211 |
if (vec->used >= vec->total) { |
| 212 |
ib_vector_resize(vec); |
| 213 |
} |
| 214 |
|
| 215 |
last = (byte *)vec->data + IB_VEC_OFFSET(vec, vec->used); |
| 216 |
|
| 217 |
#ifdef UNIV_DEBUG |
| 218 |
memset(last, 0, vec->sizeof_value); |
| 219 |
#endif |
| 220 |
|
| 221 |
if (elem) { |
| 222 |
memcpy(last, elem, vec->sizeof_value); |
| 223 |
} |
| 224 |
|
| 225 |
++vec->used; |
| 226 |
|
| 227 |
return (last); |
| 228 |
} |
| 229 |
|
| 230 |
/** Remove an element to the vector |
| 231 |
@return pointer to the "removed" element */ |
| 232 |
static inline void *ib_vector_remove( |
| 233 |
ib_vector_t *vec, /*!< in: vector */ |
| 234 |
const void *elem) /*!< in: value to remove */ |
| 235 |
{ |
| 236 |
void *current = nullptr; |
| 237 |
void *next; |
| 238 |
ulint i; |
| 239 |
ulint old_used_count = vec->used; |
| 240 |
|
| 241 |
for (i = 0; i < vec->used; i++) { |
| 242 |
current = ib_vector_get(vec, i); |
| 243 |
|
| 244 |
if (*(void **)current == elem) { |
| 245 |
if (i == vec->used - 1) { |
| 246 |
return (ib_vector_pop(vec)); |
| 247 |
} |
| 248 |
|
| 249 |
next = ib_vector_get(vec, i + 1); |
| 250 |
memmove(current, next, vec->sizeof_value * (vec->used - i - 1)); |
| 251 |
--vec->used; |
| 252 |
break; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
return ((old_used_count != vec->used) ? current : nullptr); |
| 257 |
} |
| 258 |
|
| 259 |
/******************************************************************** |
| 260 |
Sort the vector elements. */ |
| 261 |
static inline void ib_vector_sort( |
| 262 |
/* out: void */ |
| 263 |
ib_vector_t *vec, /* in: vector */ |
| 264 |
ib_compare_t compare) /* in: the comparator to use for sort */ |
| 265 |
{ |
| 266 |
qsort(vec->data, vec->used, vec->sizeof_value, compare); |
| 267 |
} |
| 268 |
|
| 269 |
/******************************************************************** |
| 270 |
Destroy the vector. Make sure the vector owns the allocator, e.g., |
| 271 |
the heap in the the heap allocator. */ |
| 272 |
static inline void ib_vector_free(ib_vector_t *vec) /* in, own: vector */ |
| 273 |
{ |
| 274 |
/* Currently we only support one type of allocator - heap, |
| 275 |
when the heap is freed all the elements are freed too. */ |
| 276 |
|
| 277 |
/* Only the heap allocator uses the arg field. */ |
| 278 |
ut_ad(vec->allocator->arg != nullptr); |
| 279 |
|
| 280 |
mem_heap_free((mem_heap_t *)vec->allocator->arg); |
| 281 |
} |
| 282 |
|
| 283 |
/******************************************************************** |
| 284 |
Test whether a vector is empty or not. |
| 285 |
@return true if empty */ |
| 286 |
static inline bool ib_vector_is_empty(const ib_vector_t *vec) /*!< in: vector */ |
| 287 |
{ |
| 288 |
return (ib_vector_size(vec) == 0); |
| 289 |
} |