MySQL  8.0.20
Source Code Documentation
xdr_utils.h
Go to the documentation of this file.
1 /* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef XDR_UTILS_H
24 #define XDR_UTILS_H
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #include <assert.h>
31 
32 /**
33  Initialize an array
34  */
35 #define def_init_xdr_array(name) \
36  static inline void init_##name##_array(name##_array *x)
37 #define init_xdr_array(name) \
38  def_init_xdr_array(name) { \
39  x->name##_array_len = 2; \
40  x->name##_array_val = calloc((size_t)x->name##_array_len, sizeof(name)); \
41  }
42 
43 /**
44  Free the contents of an array
45  */
46 #define def_free_xdr_array(name) \
47  static inline void free_##name##_array(name##_array *x)
48 #define free_xdr_array(name) \
49  def_free_xdr_array(name) { \
50  free(x->name##_array_val); \
51  x->name##_array_val = 0; \
52  x->name##_array_len = 0; \
53  }
54 
55 #define in_range(x, name, n) ((n) >= 0 && (n) < ((x).name##_array_len))
56 
57 /**
58  Resize an array
59  */
60 #define expand_xdr_array(name) \
61  u_int old_length = x->name##_array_len; \
62  if (n + 1 > (x->name##_array_len)) { \
63  if (x->name##_array_len == 0) x->name##_array_len = 1; \
64  do { \
65  x->name##_array_len *= 2; \
66  } while (n + 1 > (x->name##_array_len)); \
67  x->name##_array_val = \
68  realloc(x->name##_array_val, x->name##_array_len * sizeof(name)); \
69  memset(&x->name##_array_val[old_length], 0, \
70  (x->name##_array_len - old_length) * sizeof(name)); \
71  }
72 
73 /**
74  Define a set function for an array
75  */
76 #define def_set_xdr_array(name) \
77  static inline void set_##name(name##_array *x, name a, u_int n)
78 #define set_xdr_array(name) \
79  def_set_xdr_array(name) { \
80  expand_xdr_array(name); \
81  assert(n < x->name##_array_len); \
82  x->name##_array_val[n] = a; \
83  }
84 
85 /**
86  Define a get function for an array
87  */
88 #define def_get_xdr_array(name) \
89  static inline name get_##name(name##_array *x, u_int n)
90 #define get_xdr_array(name) \
91  def_get_xdr_array(name) { \
92  expand_xdr_array(name); \
93  assert(n < x->name##_array_len); \
94  return x->name##_array_val[n]; \
95  }
96 
97 /**
98  Define a function to clone an array
99  */
100 #define def_clone_xdr_array(name) \
101  static inline name##_array clone_##name##_array(name##_array x)
102 #define clone_xdr_array(name) \
103  def_clone_xdr_array(name) { \
104  name##_array retval = x; \
105  u_int i; \
106  retval.name##_array_len = x.name##_array_len; \
107  DBGOUT(FN; STRLIT("clone_xdr_array"); NDBG(retval.name##_array_len, u)); \
108  if (retval.name##_array_len > 0) { \
109  retval.name##_array_val = \
110  calloc((size_t)x.name##_array_len, sizeof(name)); \
111  for (i = 0; i < retval.name##_array_len; i++) { \
112  retval.name##_array_val[i] = x.name##_array_val[i]; \
113  DBGOUT(FN; STRLIT("clone_xdr_array"); NDBG(i, u)); \
114  } \
115  } else { \
116  retval.name##_array_val = 0; \
117  } \
118  return retval; \
119  }
120 
121 /**
122  Declare all functions for an array
123  */
124 #define d_xdr_funcs(name) \
125  def_init_xdr_array(name); \
126  def_free_xdr_array(name); \
127  def_set_xdr_array(name); \
128  def_get_xdr_array(name); \
129  def_clone_xdr_array(name);
130 
131 /**
132  Define all functions for an array
133  */
134 #define define_xdr_funcs(name) \
135  init_xdr_array(name) free_xdr_array(name) set_xdr_array(name) \
136  get_xdr_array(name) clone_xdr_array(name)
137 
138 /**
139  Macro to do insertion sort
140  */
141 #define insert_sort(type, x, n) \
142  { \
143  int i, j; \
144  for (i = 1; i < n; i++) { /* x[0..i-1] is sorted */ \
145  type tmp; \
146  j = i; \
147  tmp = x[j]; \
148  while (j > 0 && insert_sort_gt(x[j - 1], tmp)) { \
149  x[j] = x[j - 1]; \
150  j--; \
151  } \
152  x[j] = tmp; \
153  } \
154  }
155 
156 /**
157  Macro to do binary search for first occurence
158 
159  Invariant: x[l] < key and x[u] >= key and l < u
160 */
161 #define bin_search_first_body(x, first, last, key, p) \
162  int l = first - 1; \
163  int u = last + 1; \
164  int m = 0; \
165  while (l + 1 != u) { \
166  m = (l + u) / 2; \
167  if (bin_search_lt((x)[m], (key))) { \
168  l = m; \
169  } else { \
170  u = m; \
171  } \
172  }
173 
174 /**
175  Macro to do binary search for last occurence.
176 
177  Invariant: x[l] <= key and x[u] > key and l < u
178 */
179 #define bin_search_last_body(x, first, last, key, p) \
180  int l = first - 1; \
181  int u = last + 1; \
182  int m = 0; \
183  while (l + 1 != u) { \
184  m = (l + u) / 2; \
185  if (bin_search_gt((x)[m], (key))) { \
186  u = m; \
187  } else { \
188  l = m; \
189  } \
190  }
191 
192 /**
193  Find first element which matches key
194 */
195 #define bin_search_first(x, first, last, key, p) \
196  { \
197  bin_search_first_body(x, first, last, key, p); \
198  p = u; \
199  if (p > last || (!bin_search_eq((x)[p], (key)))) p = -1; \
200  }
201 
202 /**
203  Find first element which is greater than key
204 */
205 #define bin_search_first_gt(x, first, last, key, p) \
206  { \
207  bin_search_last_body(x, first, last, key, p); \
208  p = u; \
209  if (p > last || (!bin_search_gt((x)[p], (key)))) p = -1; \
210  }
211 
212 /**
213  Find last element which matches key
214 */
215 #define bin_search_last(x, first, last, key, p) \
216  { \
217  bin_search_last_body(x, first, last, key, p); \
218  p = l; \
219  if (p < first || (!bin_search_eq((x)[p], (key)))) p = -1; \
220  }
221 
222 /**
223  Find first element which is less than key
224 */
225 #define bin_search_last_lt(x, first, last, key, p) \
226  { \
227  bin_search_first_body(x, first, last, key, p); \
228  p = l; \
229  if (p < first || (!bin_search_lt((x)[p], (key)))) p = -1; \
230  }
231 
232 #define diff_get(type, a, i) get_##type##_array(a, i)
233 #define diff_output(type, x) set_##type##_array(&retval, x, retval_i++)
234 #define diff_gt(x, y) insert_sort_gt(x, y)
235 
236 /**
237  Macro to compute diff of two arrays, which as a side effect will
238  be sorted after the operation has completed.
239  */
240 #define diff_xdr_array(type, x, y) \
241  type##_array diff_##type##_array(type##_array x, type##_array y) { \
242  int x_i = 0; \
243  int y_i = 0; \
244  type retval; \
245  int retval_i = 0; \
246  init_##type##_array(&retval); \
247  insert_sort(type, x.type##_val, x.type##_len); \
248  insert_sort(type, y.type##_val, y.type##_len); \
249  while (x_i < x.type##_len && y < y.type##_len) { \
250  if (diff_eq(diff_get(type, x, x_i), diff_get(type, y, y_i))) { \
251  x_i++; \
252  y_i++; \
253  } else if (diff_lt(diff_get(type, x, x_i), diff_get(type, y, y_i))) { \
254  diff_output(type, diff_get(type, x, x_i++)); \
255  } else { \
256  diff_output(type, diff_get(type, y, y_i++)); \
257  } \
258  } \
259  while (x_i < x.type##_len) { \
260  diff_output(type, diff_get(type, x, x_i++)); \
261  } \
262  while (y_i < y.type##_len) { \
263  diff_output(type, diff_get(type, y, y_i++)); \
264  } \
265  retval.type##_len = retval_i; \
266  return retval; \
267  }
268 
269 /* {{{ Reverse elements n1..n2 */
270 
271 #define x_reverse(type, x, in_n1, in_n2) \
272  { \
273  int n1 = in_n1; \
274  int n2 = in_n2; \
275  while ((n1) < (n2)) { \
276  type tmp = (x)[n1]; \
277  (x)[n1] = (x)[n2]; \
278  (x)[n2] = tmp; \
279  (n1)++; \
280  (n2)--; \
281  } \
282  }
283 
284 /* }}} */
285 
286 /* {{{ Move elements n1..n2 to after n3 */
287 
288 #define x_blkmove(type, x, n1, n2, n3) \
289  { \
290  if ((n3) < (n1)-1) { \
291  x_reverse(type, (x), (n3) + 1, (n1)-1); \
292  x_reverse(type, (x), (n1), (n2)); \
293  x_reverse(type, (x), (n3) + 1, (n2)); \
294  } else if ((n3) > (n2)) { \
295  x_reverse(type, (x), (n1), (n2)); \
296  x_reverse(type, (x), (n2) + 1, (n3)); \
297  x_reverse(type, (x), (n1), (n3)); \
298  } \
299  }
300 
301 /* }}} */
302 
303 #ifdef __cplusplus
304 }
305 #endif
306 
307 #endif