TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags
[wireshark-sm.git] / wsutil / glib-compat.h
blobed7511cfea35099e9257db064e391c7d170f86d9
1 /** @file
3 * Definitions to provide some functions that are not present in older
4 * GLIB versions we support (currently down to 2.50)
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
12 #ifndef GLIB_COMPAT_H
13 #define GLIB_COMPAT_H
15 #include "ws_symbol_export.h"
16 #include "ws_attributes.h"
18 #include <glib.h>
19 #include <string.h>
21 #ifdef __cplusplus
22 extern "C" {
23 #endif /* __cplusplus */
25 #if !GLIB_CHECK_VERSION(2, 61, 2)
27 typedef volatile gint gatomicrefcount;
29 typedef struct _GRealArray GRealArray;
30 struct _GRealArray
32 guint8 *data;
33 guint len;
34 guint alloc;
35 guint elt_size;
36 guint zero_terminated ;
37 guint clear;
38 gatomicrefcount ref_count;
39 GDestroyNotify clear_func;
42 static inline gboolean
43 g_array_binary_search (GArray *array,
44 const void * target,
45 GCompareFunc compare_func,
46 guint *out_match_index)
48 gboolean result = FALSE;
49 GRealArray *_array = (GRealArray *) array;
50 guint left, middle, right;
51 gint val;
53 g_return_val_if_fail (_array != NULL, FALSE);
54 g_return_val_if_fail (compare_func != NULL, FALSE);
56 if (G_LIKELY(_array->len))
58 left = 0;
59 right = _array->len - 1;
61 while (left <= right)
63 middle = left + (right - left) / 2;
65 val = compare_func (_array->data + (_array->elt_size * middle), target);
66 if (val == 0)
68 result = TRUE;
69 break;
71 else if (val < 0)
72 left = middle + 1;
73 else if (/* val > 0 && */ middle > 0)
74 right = middle - 1;
75 else
76 break; /* element not found */
80 if (result && out_match_index != NULL)
81 *out_match_index = middle;
83 return result;
85 #endif
87 #if !GLIB_CHECK_VERSION(2, 68, 0)
88 static inline void *
89 g_memdup2(const void *mem, size_t byte_size)
91 void * new_mem;
93 if (mem && byte_size != 0) {
94 new_mem = g_malloc(byte_size);
95 memcpy(new_mem, mem, byte_size);
97 else
98 new_mem = NULL;
100 return new_mem;
102 #endif
104 #ifdef __cplusplus
106 #endif /* __cplusplus */
108 #endif /* GLIB_COMPAT_H */