aboutsummaryrefslogtreecommitdiffstats
path: root/wsutil/glib-compat.c
blob: b1181d380910ad8cbd2c33592192edfbb659cb4d (plain)
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
/*
* Provide some functions that are not present in older
* GLIB versions (down to 2.22)
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"

#include <glib.h>

#include "glib-compat.h"
#if !GLIB_CHECK_VERSION(2, 28, 0)
/**
* g_slist_free_full:
* @list: a pointer to a #GSList
* @free_func: the function to be called to free each element's data
*
* Convenience method, which frees all the memory used by a #GSList, and
* calls the specified destroy function on every element's data.
*
* Since: 2.28
**/
void
g_slist_free_full(GSList         *list,
    GDestroyNotify  free_func)
{
    g_slist_foreach(list, (GFunc)free_func, NULL);
    g_slist_free(list);
}

/**
* g_list_free_full:
* @list: a pointer to a #GList
* @free_func: the function to be called to free each element's data
*
* Convenience method, which frees all the memory used by a #GList,
* and calls @free_func on every element's data.
*
* Since: 2.28
*/
void
g_list_free_full(GList          *list,
    GDestroyNotify  free_func)
{
    g_list_foreach(list, (GFunc)free_func, NULL);
    g_list_free(list);
}

/**
* g_get_monotonic_time:
*
* Queries the system monotonic time.  Returns value in microseconds.
*
* Since: 2.28
*/
gint64 g_get_monotonic_time (void)
{
    GTimeVal result;
    g_get_current_time(&result);
    return result.tv_sec*1000000 + result.tv_usec;
}

#endif /* GLIB_CHECK_VERSION(2, 28, 0)*/

#if !GLIB_CHECK_VERSION(2, 30, 0)
/**
* g_ptr_array_new_full:
* @reserved_size: number of pointers preallocated
* @element_free_func: (allow-none): A function to free elements with
*     destroy @array or %NULL
*
* Creates a new #GPtrArray with @reserved_size pointers preallocated
* and a reference count of 1. This avoids frequent reallocation, if
* you are going to add many pointers to the array. Note however that
* the size of the array is still 0. It also set @element_free_func
* for freeing each element when the array is destroyed either via
* g_ptr_array_unref(), when g_ptr_array_free() is called with
* @free_segment set to %TRUE or when removing elements.
*
* Returns: A new #GPtrArray
*
* Since: 2.30
*/
GPtrArray*
g_ptr_array_new_full(guint          reserved_size,
    GDestroyNotify element_free_func)
{
    GPtrArray *array;

    array = g_ptr_array_sized_new(reserved_size);
    g_ptr_array_set_free_func(array, element_free_func);

    return array;
}
#endif /* GLIB_CHECK_VERSION(2, 30, 0)*/

#if !GLIB_CHECK_VERSION(2,31,18)
/**
Code copied from dumpcap.c
*/
gpointer
g_async_queue_timeout_pop(GAsyncQueue *queue,
    guint64      timeout)
{
    GTimeVal  wait_time;
    gpointer  q_status;

    g_get_current_time(&wait_time);
    g_time_val_add(&wait_time, timeout);
    q_status = g_async_queue_timed_pop(queue, &wait_time);

    return q_status;
}

#endif /* GLIB_CHECK_VERSION(2,31,18)*/


#if !GLIB_CHECK_VERSION(2,31,0)
GThread *g_thread_new(const gchar *name, GThreadFunc func, gpointer data)
{
    GError *error = NULL;
    GThread *thread;

    thread = g_thread_create(func, data, TRUE, &error);

    if G_UNLIKELY (thread == NULL)
        g_error ("creating thread '%s': %s", name ? name : "", error->message);

    return thread;
}
#endif /* GLIB_CHECK_VERSION(2,31,0)*/

/*
* Editor modelines  -  http://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/