1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 * Copyright 2008 - 2011 Red Hat, Inc.
29 #undef G_GNUC_BEGIN_IGNORE_DEPRECATIONS
30 #undef G_GNUC_END_IGNORE_DEPRECATIONS
32 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
33 _Pragma("clang diagnostic push") \
34 _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
36 #define G_GNUC_END_IGNORE_DEPRECATIONS \
37 _Pragma("clang diagnostic pop")
42 __g_type_ensure (GType type)
44 #if !GLIB_CHECK_VERSION(2,34,0)
45 if (G_UNLIKELY (type == (GType)-1))
46 g_error ("can't happen");
48 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
50 G_GNUC_END_IGNORE_DEPRECATIONS;
53 #define g_type_ensure __g_type_ensure
55 #if !GLIB_CHECK_VERSION(2,34,0)
57 #define g_clear_pointer(pp, destroy) \
59 G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
60 /* Only one access, please */ \
61 gpointer *_pp = (gpointer *) (pp); \
63 /* This assignment is needed to avoid a gcc warning */ \
64 GDestroyNotify _destroy = (GDestroyNotify) (destroy); \
74 /* These are used to clean up the output of test programs; we can just let
75 * them no-op in older glib.
77 #define g_test_expect_message(log_domain, log_level, pattern)
78 #define g_test_assert_expected_messages()
82 /* We build with -DGLIB_MAX_ALLOWED_VERSION set to 2.32 to make sure we don't
83 * accidentally use new API that we shouldn't. But we don't want warnings for
84 * the APIs that we emulate above.
87 #define g_test_expect_message(domain, level, format...) \
89 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
90 g_test_expect_message (domain, level, format); \
91 G_GNUC_END_IGNORE_DEPRECATIONS \
94 #define g_test_assert_expected_messages_internal(domain, file, line, func) \
96 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
97 g_test_assert_expected_messages_internal (domain, file, line, func); \
98 G_GNUC_END_IGNORE_DEPRECATIONS \
104 #if GLIB_CHECK_VERSION (2, 35, 0)
105 /* For glib >= 2.36, g_type_init() is deprecated.
106 * But since 2.35.1 (7c42ab23b55c43ab96d0ac2124b550bf1f49c1ec) this function
107 * does nothing. Replace the call with empty statement. */
108 #define nm_g_type_init() G_STMT_START { (void) 0; } G_STMT_END
110 #define nm_g_type_init() G_STMT_START { g_type_init (); } G_STMT_END
114 /* g_test_initialized() is only available since glib 2.36. */
115 #if !GLIB_CHECK_VERSION (2, 36, 0)
116 #define g_test_initialized() (g_test_config_vars->test_initialized)
119 /* g_assert_cmpmem() is only available since glib 2.46. */
120 #if !GLIB_CHECK_VERSION (2, 45, 7)
121 #define g_assert_cmpmem(m1, l1, m2, l2) G_STMT_START {\
122 gconstpointer __m1 = m1, __m2 = m2; \
123 int __l1 = l1, __l2 = l2; \
125 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
126 #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", __l1, "==", __l2, 'i'); \
127 else if (memcmp (__m1, __m2, __l1) != 0) \
128 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
129 "assertion failed (" #m1 " == " #m2 ")"); \
133 /* Rumtime check for glib version. First do a compile time check which
134 * (if satisfied) shortcuts the runtime check. */
135 #define nm_glib_check_version(major, minor, micro) \
136 ( GLIB_CHECK_VERSION ((major), (minor), (micro)) \
137 || ( ( glib_major_version > (major)) \
138 || ( glib_major_version == (major) \
139 && glib_minor_version > (minor)) \
140 || ( glib_major_version == (major) \
141 && glib_minor_version == (minor) \
142 && glib_micro_version >= (micro))))
144 /* g_test_skip() is only available since glib 2.38. Add a compatibility wrapper. */
146 __nmtst_g_test_skip (const gchar *msg)
148 #if GLIB_CHECK_VERSION (2, 38, 0)
149 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
151 G_GNUC_END_IGNORE_DEPRECATIONS
156 #define g_test_skip __nmtst_g_test_skip
159 /* g_test_add_data_func_full() is only available since glib 2.34. Add a compatibility wrapper. */
161 __g_test_add_data_func_full (const char *testpath,
163 GTestDataFunc test_func,
164 GDestroyNotify data_free_func)
166 #if GLIB_CHECK_VERSION (2, 34, 0)
167 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
168 g_test_add_data_func_full (testpath, test_data, test_func, data_free_func);
169 G_GNUC_END_IGNORE_DEPRECATIONS
171 g_return_if_fail (testpath != NULL);
172 g_return_if_fail (testpath[0] == '/');
173 g_return_if_fail (test_func != NULL);
175 g_test_add_vtable (testpath, 0, test_data, NULL,
176 (GTestFixtureFunc) test_func,
177 (GTestFixtureFunc) data_free_func);
180 #define g_test_add_data_func_full __g_test_add_data_func_full
183 #if !GLIB_CHECK_VERSION (2, 34, 0)
184 #define G_DEFINE_QUARK(QN, q_n) \
190 if G_UNLIKELY (q == 0) \
191 q = g_quark_from_static_string (#QN); \
198 static inline gboolean
199 nm_g_hash_table_replace (GHashTable *hash, gpointer key, gpointer value)
201 /* glib 2.40 added a return value indicating whether the key already existed
202 * (910191597a6c2e5d5d460e9ce9efb4f47d9cc63c). */
203 #if GLIB_CHECK_VERSION(2, 40, 0)
204 return g_hash_table_replace (hash, key, value);
206 gboolean contained = g_hash_table_contains (hash, key);
208 g_hash_table_replace (hash, key, value);
213 static inline gboolean
214 nm_g_hash_table_insert (GHashTable *hash, gpointer key, gpointer value)
216 /* glib 2.40 added a return value indicating whether the key already existed
217 * (910191597a6c2e5d5d460e9ce9efb4f47d9cc63c). */
218 #if GLIB_CHECK_VERSION(2, 40, 0)
219 return g_hash_table_insert (hash, key, value);
221 gboolean contained = g_hash_table_contains (hash, key);
223 g_hash_table_insert (hash, key, value);
228 static inline gboolean
229 nm_g_hash_table_add (GHashTable *hash, gpointer key)
231 /* glib 2.40 added a return value indicating whether the key already existed
232 * (910191597a6c2e5d5d460e9ce9efb4f47d9cc63c). */
233 #if GLIB_CHECK_VERSION(2, 40, 0)
234 return g_hash_table_add (hash, key);
236 gboolean contained = g_hash_table_contains (hash, key);
238 g_hash_table_add (hash, key);
243 #if !GLIB_CHECK_VERSION(2, 40, 0) || defined (NM_GLIB_COMPAT_H_TEST)
245 _nm_g_ptr_array_insert (GPtrArray *array,
249 g_return_if_fail (array);
250 g_return_if_fail (index_ >= -1);
251 g_return_if_fail (index_ <= (gint) array->len);
253 g_ptr_array_add (array, data);
255 if (index_ != -1 && index_ != (gint) (array->len - 1)) {
256 memmove (&(array->pdata[index_ + 1]),
257 &(array->pdata[index_]),
258 (array->len - index_ - 1) * sizeof (gpointer));
259 array->pdata[index_] = data;
263 #if !GLIB_CHECK_VERSION(2, 40, 0)
264 #define g_ptr_array_insert(array, index, data) G_STMT_START { _nm_g_ptr_array_insert (array, index, data); } G_STMT_END
266 #define g_ptr_array_insert(array, index, data) \
268 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
269 g_ptr_array_insert (array, index, data); \
270 G_GNUC_END_IGNORE_DEPRECATIONS \
275 #if !GLIB_CHECK_VERSION (2, 40, 0)
276 inline static gboolean
277 _g_key_file_save_to_file (GKeyFile *key_file,
278 const gchar *filename,
285 g_return_val_if_fail (key_file != NULL, FALSE);
286 g_return_val_if_fail (filename != NULL, FALSE);
287 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
289 contents = g_key_file_to_data (key_file, &length, NULL);
290 g_assert (contents != NULL);
292 success = g_file_set_contents (filename, contents, length, error);
297 #define g_key_file_save_to_file(key_file, filename, error) \
298 _g_key_file_save_to_file (key_file, filename, error)
300 #define g_key_file_save_to_file(key_file, filename, error) \
304 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
305 _success = g_key_file_save_to_file (key_file, filename, error); \
306 G_GNUC_END_IGNORE_DEPRECATIONS \
312 #if GLIB_CHECK_VERSION (2, 36, 0)
313 #define g_credentials_get_unix_pid(creds, error) \
314 G_GNUC_EXTENSION ({ \
315 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
316 (g_credentials_get_unix_pid) ((creds), (error)); \
317 G_GNUC_END_IGNORE_DEPRECATIONS \
320 #define g_credentials_get_unix_pid(creds, error) \
321 G_GNUC_EXTENSION ({ \
322 struct ucred *native_creds; \
324 native_creds = g_credentials_get_native ((creds), G_CREDENTIALS_TYPE_LINUX_UCRED); \
325 g_assert (native_creds); \
331 #if !GLIB_CHECK_VERSION(2, 40, 0) || defined (NM_GLIB_COMPAT_H_TEST)
332 static inline gpointer *
333 _nm_g_hash_table_get_keys_as_array (GHashTable *hash_table,
340 g_return_val_if_fail (hash_table, NULL);
342 ret = g_new0 (gpointer, g_hash_table_size (hash_table) + 1);
343 g_hash_table_iter_init (&iter, hash_table);
345 while (g_hash_table_iter_next (&iter, &key, NULL))
356 #if !GLIB_CHECK_VERSION(2, 40, 0)
357 #define g_hash_table_get_keys_as_array(hash_table, length) \
358 G_GNUC_EXTENSION ({ \
359 _nm_g_hash_table_get_keys_as_array (hash_table, length); \
362 #define g_hash_table_get_keys_as_array(hash_table, length) \
363 G_GNUC_EXTENSION ({ \
364 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
365 (g_hash_table_get_keys_as_array) ((hash_table), (length)); \
366 G_GNUC_END_IGNORE_DEPRECATIONS \
371 /* g_info was only added with 2.39.2 */
372 #define g_info(...) g_log (G_LOG_DOMAIN, \
377 #endif /* __NM_GLIB_H__ */