File: ut0vec.h

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (254 lines) | stat: -rw-r--r-- 9,842 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*****************************************************************************

Copyright (c) 2006, 2025, Oracle and/or its affiliates.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2.0, as published by the
Free Software Foundation.

This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation.  The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

*****************************************************************************/

/** @file include/ut0vec.h
 A vector of pointers to data items

 Created 4/6/2006 Osku Salerma
 ************************************************************************/

#ifndef IB_VECTOR_H
#define IB_VECTOR_H

#include "mem0mem.h"
#include "univ.i"

struct ib_alloc_t;
struct ib_vector_t;

typedef void *(*ib_mem_alloc_t)(
    /* out: Pointer to allocated memory */
    ib_alloc_t *allocator, /* in: Pointer to allocator instance */
    ulint size);           /* in: Number of bytes to allocate */

typedef void (*ib_mem_free_t)(
    ib_alloc_t *allocator, /* in: Pointer to allocator instance */
    void *ptr);            /* in: Memory to free */

typedef void *(*ib_mem_resize_t)(
    /* out: Pointer to resized memory */
    ib_alloc_t *allocator, /* in: Pointer to allocator */
    void *ptr,             /* in: Memory to resize */
    ulint old_size,        /* in: Old memory size in bytes */
    ulint new_size);       /* in: New size in bytes */

typedef int (*ib_compare_t)(const void *, const void *);

/* An automatically resizing vector datatype with the following properties:

 -All memory allocation is done through an allocator, which is  responsible for
freeing it when done with the vector.
*/

/* This is useful shorthand for elements of type void* */
#define ib_vector_getp(v, n) (*(void **)ib_vector_get(v, n))
#define ib_vector_getp_const(v, n) (*(void **)ib_vector_get_const(v, n))

#define ib_vector_allocator(v) (v->allocator)

/********************************************************************
Create a new vector with the given initial size. */
ib_vector_t *ib_vector_create(
    /* out: vector */
    ib_alloc_t *alloc,               /* in: Allocator */
                                     /* in: size of the data item */
    ulint sizeof_value, ulint size); /* in: initial size */

/********************************************************************
Destroy the vector. Make sure the vector owns the allocator, e.g.,
the heap in the the heap allocator. */
static inline void ib_vector_free(ib_vector_t *vec); /* in/out: vector */

/********************************************************************
Push a new element to the vector, increasing its size if necessary,
if elem is not NULL then elem is copied to the vector.*/
static inline void *ib_vector_push(
    /* out: pointer the "new" element */
    ib_vector_t *vec,  /* in/out: vector */
    const void *elem); /* in: data element */

/********************************************************************
Pop the last element from the vector.*/
static inline void *ib_vector_pop(
    /* out: pointer to the "new" element */
    ib_vector_t *vec); /* in/out: vector */

/** Remove an element to the vector
@param[in]      vec     vector
@param[in]      elem    value to remove
@return pointer to the "removed" element */
static inline void *ib_vector_remove(ib_vector_t *vec, const void *elem);

/********************************************************************
Get the number of elements in the vector. */
static inline ulint ib_vector_size(
    /* out: number of elements in vector */
    const ib_vector_t *vec); /* in: vector */

/********************************************************************
Increase the size of the vector. */
void ib_vector_resize(
    /* out: number of elements in vector */
    ib_vector_t *vec); /* in/out: vector */

/********************************************************************
Test whether a vector is empty or not.
@return true if empty */
static inline bool ib_vector_is_empty(
    const ib_vector_t *vec); /*!< in: vector */

/** Get the n'th element.
@param[in]      vec     vector
@param[in]      n       element index to get
@return n'th element */
static inline void *ib_vector_get(ib_vector_t *vec, ulint n);

/********************************************************************
Const version of the get n'th element.
@return n'th element */
static inline const void *ib_vector_get_const(
    const ib_vector_t *vec, /* in: vector */
    ulint n);               /* in: element index to get */
/** Get last element. The vector must not be empty.
 @return last element */
static inline void *ib_vector_get_last(ib_vector_t *vec); /*!< in: vector */

/** Set the n'th element.
@param[in]      vec     vector
@param[in]      n       element index to set
@param[in]      elem    data element */
static inline void ib_vector_set(ib_vector_t *vec, ulint n, void *elem);

/********************************************************************
Reset the vector size to 0 elements. */
static inline void ib_vector_reset(ib_vector_t *vec); /* in/out: vector */

/********************************************************************
Get the last element of the vector. */
static inline void *ib_vector_last(
    /* out: pointer to last element */
    ib_vector_t *vec); /* in/out: vector */

/********************************************************************
Get the last element of the vector. */
static inline const void *ib_vector_last_const(
    /* out: pointer to last element */
    const ib_vector_t *vec); /* in: vector */

/********************************************************************
Sort the vector elements. */
static inline void ib_vector_sort(
    ib_vector_t *vec,      /* in/out: vector */
    ib_compare_t compare); /* in: the comparator to use for sort */

/********************************************************************
The default ib_vector_t heap free. Does nothing. */
static inline void ib_heap_free(ib_alloc_t *allocator, /* in: allocator */
                                void *ptr);            /* in: size in bytes */

/********************************************************************
The default ib_vector_t heap malloc. Uses mem_heap_alloc(). */
static inline void *ib_heap_malloc(
    /* out: pointer to allocated memory */
    ib_alloc_t *allocator, /* in: allocator */
    ulint size);           /* in: size in bytes */

/********************************************************************
The default ib_vector_t heap resize. Since we can't resize the heap
we have to copy the elements from the old ptr to the new ptr.
Uses mem_heap_alloc(). */
static inline void *ib_heap_resize(
    /* out: pointer to reallocated
    memory */
    ib_alloc_t *allocator, /* in: allocator */
    void *old_ptr,         /* in: pointer to memory */
    ulint old_size,        /* in: old size in bytes */
    ulint new_size);       /* in: new size in bytes */

/********************************************************************
Create a heap allocator that uses the passed in heap. */
static inline ib_alloc_t *ib_heap_allocator_create(
    /* out: heap allocator instance */
    mem_heap_t *heap); /* in: heap to use */

/********************************************************************
Free a heap allocator. */
static inline void ib_heap_allocator_free(
    ib_alloc_t *ib_ut_alloc); /* in: alloc instance to free */

/* Allocator used by ib_vector_t. */
struct ib_alloc_t {
  ib_mem_alloc_t mem_malloc;  /* For allocating memory */
  ib_mem_free_t mem_release;  /* For freeing memory */
  ib_mem_resize_t mem_resize; /* For resizing memory */
  void *arg;                  /* Currently if not NULL then it
                              points to the heap instance */
};

/* See comment at beginning of file. */
struct ib_vector_t {
  ib_alloc_t *allocator; /* Allocator, because one size
                         doesn't fit all */
  void *data;            /* data elements */
  ulint used;            /* number of elements currently used */
  ulint total;           /* number of elements allocated */
                         /* Size of a data item */
  ulint sizeof_value;
};

/* A helper to make it easier to use the legacy ib_vector_t struct. */
template <typename T>
class Vector_wrapper {
 private:
  ib_vector_t &m_ib_vector;

 public:
  Vector_wrapper(ib_vector_t &ib_vector) : m_ib_vector(ib_vector) {}

  size_t size() const { return ib_vector_size(&m_ib_vector); }

  bool empty() const { return size() == 0; }

  const T &operator[](size_t idx) const {
    return *static_cast<const T *>(ib_vector_get(&m_ib_vector, idx));
  }

  T &operator[](size_t idx) {
    return *static_cast<T *>(ib_vector_get(&m_ib_vector, idx));
  }

  T *begin() { return empty() ? nullptr : &((*this)[0]); }

  T *end() { return empty() ? begin() : begin() + size(); }

  const T *begin() const { return empty() ? nullptr : &((*this)[0]); }

  const T *end() const { return empty() ? begin() : begin() + size(); }
};
#include "ut0vec.ic"

#endif /* IB_VECTOR_H */