nm-glib.h 17.8 KB
Newer Older
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
/*
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
Lubomir Rintel's avatar
Lubomir Rintel committed
17
 * Copyright 2008 - 2018 Red Hat, Inc.
18 19
 */

20 21
#ifndef __NM_GLIB_H__
#define __NM_GLIB_H__
22

23 24 25 26 27 28 29 30
/*****************************************************************************/

#ifndef __NM_MACROS_INTERNAL_H__
#error "nm-glib.h requires nm-macros-internal.h. Do not include this directly"
#endif

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

31 32 33 34 35 36 37 38 39 40 41 42 43 44
#ifdef __clang__

#undef G_GNUC_BEGIN_IGNORE_DEPRECATIONS
#undef G_GNUC_END_IGNORE_DEPRECATIONS

#define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
    _Pragma("clang diagnostic push") \
    _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")

#define G_GNUC_END_IGNORE_DEPRECATIONS \
    _Pragma("clang diagnostic pop")

#endif

45 46
/*****************************************************************************/

47 48 49 50 51 52 53 54 55 56 57 58 59 60
static inline void
__g_type_ensure (GType type)
{
#if !GLIB_CHECK_VERSION(2,34,0)
	if (G_UNLIKELY (type == (GType)-1))
		g_error ("can't happen");
#else
	G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
	g_type_ensure (type);
	G_GNUC_END_IGNORE_DEPRECATIONS;
#endif
}
#define g_type_ensure __g_type_ensure

61 62
/*****************************************************************************/

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#if !GLIB_CHECK_VERSION(2,34,0)

#define g_clear_pointer(pp, destroy) \
    G_STMT_START {                                                                 \
        G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer));                       \
        /* Only one access, please */                                              \
        gpointer *_pp = (gpointer *) (pp);                                         \
        gpointer _p;                                                               \
        /* This assignment is needed to avoid a gcc warning */                     \
        GDestroyNotify _destroy = (GDestroyNotify) (destroy);                      \
                                                                                   \
        _p = *_pp;                                                                 \
        if (_p)                                                                    \
        {                                                                          \
            *_pp = NULL;                                                           \
            _destroy (_p);                                                         \
        }                                                                          \
    } G_STMT_END

82 83 84 85 86 87
#endif

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

#if !GLIB_CHECK_VERSION(2,34,0)

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
/* These are used to clean up the output of test programs; we can just let
 * them no-op in older glib.
 */
#define g_test_expect_message(log_domain, log_level, pattern)
#define g_test_assert_expected_messages()

#else

/* We build with -DGLIB_MAX_ALLOWED_VERSION set to 2.32 to make sure we don't
 * accidentally use new API that we shouldn't. But we don't want warnings for
 * the APIs that we emulate above.
 */

#define g_test_expect_message(domain, level, format...) \
	G_STMT_START { \
		G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
		g_test_expect_message (domain, level, format); \
		G_GNUC_END_IGNORE_DEPRECATIONS \
	} G_STMT_END

#define g_test_assert_expected_messages_internal(domain, file, line, func) \
	G_STMT_START { \
		G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
		g_test_assert_expected_messages_internal (domain, file, line, func); \
		G_GNUC_END_IGNORE_DEPRECATIONS \
	} G_STMT_END

#endif

117
/*****************************************************************************/
118 119 120 121 122 123 124 125 126 127

#if GLIB_CHECK_VERSION (2, 35, 0)
/* For glib >= 2.36, g_type_init() is deprecated.
 * But since 2.35.1 (7c42ab23b55c43ab96d0ac2124b550bf1f49c1ec) this function
 * does nothing. Replace the call with empty statement. */
#define nm_g_type_init()     G_STMT_START { (void) 0; } G_STMT_END
#else
#define nm_g_type_init()     G_STMT_START { g_type_init (); } G_STMT_END
#endif

128
/*****************************************************************************/
129 130 131 132 133 134

/* g_test_initialized() is only available since glib 2.36. */
#if !GLIB_CHECK_VERSION (2, 36, 0)
#define g_test_initialized() (g_test_config_vars->test_initialized)
#endif

135 136
/*****************************************************************************/

137 138 139 140 141 142 143 144 145 146 147 148 149 150
/* g_assert_cmpmem() is only available since glib 2.46. */
#if !GLIB_CHECK_VERSION (2, 45, 7)
#define g_assert_cmpmem(m1, l1, m2, l2) G_STMT_START {\
                                             gconstpointer __m1 = m1, __m2 = m2; \
                                             int __l1 = l1, __l2 = l2; \
                                             if (__l1 != __l2) \
                                               g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
                                                                           #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", __l1, "==", __l2, 'i'); \
                                             else if (memcmp (__m1, __m2, __l1) != 0) \
                                               g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
                                                                    "assertion failed (" #m1 " == " #m2 ")"); \
                                        } G_STMT_END
#endif

151 152
/*****************************************************************************/

153 154
/* Rumtime check for glib version. First do a compile time check which
 * (if satisfied) shortcuts the runtime check. */
155
static inline gboolean
156 157 158 159 160 161 162 163 164 165
nm_glib_check_version (guint major, guint minor, guint micro)
{
	return    GLIB_CHECK_VERSION (major, minor, micro)
	       || (   (   glib_major_version > major)
	           || (   glib_major_version == major
	               && glib_minor_version > minor)
	           || (   glib_major_version == major
	               && glib_minor_version == minor
	               && glib_micro_version < micro));
}
166

167 168
/*****************************************************************************/

169 170
/* g_test_skip() is only available since glib 2.38. Add a compatibility wrapper. */
static inline void
171
__nmtst_g_test_skip (const char *msg)
172 173 174 175 176 177 178 179 180 181 182
{
#if GLIB_CHECK_VERSION (2, 38, 0)
	G_GNUC_BEGIN_IGNORE_DEPRECATIONS
	g_test_skip (msg);
	G_GNUC_END_IGNORE_DEPRECATIONS
#else
	g_debug ("%s", msg);
#endif
}
#define g_test_skip __nmtst_g_test_skip

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

/* g_test_add_data_func_full() is only available since glib 2.34. Add a compatibility wrapper. */
static inline void
__g_test_add_data_func_full (const char     *testpath,
                             gpointer        test_data,
                             GTestDataFunc   test_func,
                             GDestroyNotify  data_free_func)
{
#if GLIB_CHECK_VERSION (2, 34, 0)
	G_GNUC_BEGIN_IGNORE_DEPRECATIONS
	g_test_add_data_func_full (testpath, test_data, test_func, data_free_func);
	G_GNUC_END_IGNORE_DEPRECATIONS
#else
	g_return_if_fail (testpath != NULL);
	g_return_if_fail (testpath[0] == '/');
	g_return_if_fail (test_func != NULL);

	g_test_add_vtable (testpath, 0, test_data, NULL,
	                   (GTestFixtureFunc) test_func,
	                   (GTestFixtureFunc) data_free_func);
#endif
}
#define g_test_add_data_func_full __g_test_add_data_func_full

208
/*****************************************************************************/
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

#if !GLIB_CHECK_VERSION (2, 34, 0)
#define G_DEFINE_QUARK(QN, q_n)               \
GQuark                                        \
q_n##_quark (void)                            \
{                                             \
	static GQuark q;                          \
                                              \
	if G_UNLIKELY (q == 0)                    \
		q = g_quark_from_static_string (#QN); \
                                              \
	return q;                                 \
}
#endif

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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

static inline gboolean
nm_g_hash_table_replace (GHashTable *hash, gpointer key, gpointer value)
{
	/* glib 2.40 added a return value indicating whether the key already existed
	 * (910191597a6c2e5d5d460e9ce9efb4f47d9cc63c). */
#if GLIB_CHECK_VERSION(2, 40, 0)
	return g_hash_table_replace (hash, key, value);
#else
	gboolean contained = g_hash_table_contains (hash, key);

	g_hash_table_replace (hash, key, value);
	return !contained;
#endif
}

static inline gboolean
nm_g_hash_table_insert (GHashTable *hash, gpointer key, gpointer value)
{
	/* glib 2.40 added a return value indicating whether the key already existed
	 * (910191597a6c2e5d5d460e9ce9efb4f47d9cc63c). */
#if GLIB_CHECK_VERSION(2, 40, 0)
	return g_hash_table_insert (hash, key, value);
#else
	gboolean contained = g_hash_table_contains (hash, key);

	g_hash_table_insert (hash, key, value);
	return !contained;
#endif
}

static inline gboolean
nm_g_hash_table_add (GHashTable *hash, gpointer key)
{
	/* glib 2.40 added a return value indicating whether the key already existed
	 * (910191597a6c2e5d5d460e9ce9efb4f47d9cc63c). */
#if GLIB_CHECK_VERSION(2, 40, 0)
	return g_hash_table_add (hash, key);
#else
	gboolean contained = g_hash_table_contains (hash, key);

	g_hash_table_add (hash, key);
	return !contained;
#endif
}

271 272
/*****************************************************************************/

273 274 275
#if !GLIB_CHECK_VERSION(2, 40, 0) || defined (NM_GLIB_COMPAT_H_TEST)
static inline void
_nm_g_ptr_array_insert (GPtrArray *array,
276
                        int        index_,
277 278 279 280
                        gpointer   data)
{
	g_return_if_fail (array);
	g_return_if_fail (index_ >= -1);
281
	g_return_if_fail (index_ <= (int) array->len);
282 283 284

	g_ptr_array_add (array, data);

285
	if (index_ != -1 && index_ != (int) (array->len - 1)) {
286 287 288 289 290 291 292
		memmove (&(array->pdata[index_ + 1]),
		         &(array->pdata[index_]),
		         (array->len - index_ - 1) * sizeof (gpointer));
		array->pdata[index_] = data;
	}
}
#endif
293

294 295 296 297 298 299 300 301 302 303 304
#if !GLIB_CHECK_VERSION(2, 40, 0)
#define g_ptr_array_insert(array, index, data) G_STMT_START { _nm_g_ptr_array_insert (array, index, data); } G_STMT_END
#else
#define g_ptr_array_insert(array, index, data) \
	G_STMT_START { \
		G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
		g_ptr_array_insert (array, index, data); \
		G_GNUC_END_IGNORE_DEPRECATIONS \
	} G_STMT_END
#endif

305
/*****************************************************************************/
306 307 308 309

#if !GLIB_CHECK_VERSION (2, 40, 0)
static inline gboolean
_g_key_file_save_to_file (GKeyFile     *key_file,
310
                          const char   *filename,
311 312
                          GError      **error)
{
313
	char *contents;
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	gboolean success;
	gsize length;

	g_return_val_if_fail (key_file != NULL, FALSE);
	g_return_val_if_fail (filename != NULL, FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	contents = g_key_file_to_data (key_file, &length, NULL);
	g_assert (contents != NULL);

	success = g_file_set_contents (filename, contents, length, error);
	g_free (contents);

	return success;
}
#define g_key_file_save_to_file(key_file, filename, error) \
	_g_key_file_save_to_file (key_file, filename, error)
#else
#define g_key_file_save_to_file(key_file, filename, error) \
	({ \
		gboolean _success; \
		\
		G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
		_success = g_key_file_save_to_file (key_file, filename, error); \
		G_GNUC_END_IGNORE_DEPRECATIONS \
		_success; \
	})
#endif

343
/*****************************************************************************/
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

#if GLIB_CHECK_VERSION (2, 36, 0)
#define g_credentials_get_unix_pid(creds, error) \
	({ \
		G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
			(g_credentials_get_unix_pid) ((creds), (error)); \
		G_GNUC_END_IGNORE_DEPRECATIONS \
	})
#else
#define g_credentials_get_unix_pid(creds, error) \
	({ \
		struct ucred *native_creds; \
		 \
		native_creds = g_credentials_get_native ((creds), G_CREDENTIALS_TYPE_LINUX_UCRED); \
		g_assert (native_creds); \
		native_creds->pid; \
	})
#endif

363
/*****************************************************************************/
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

#if !GLIB_CHECK_VERSION(2, 40, 0) || defined (NM_GLIB_COMPAT_H_TEST)
static inline gpointer *
_nm_g_hash_table_get_keys_as_array (GHashTable *hash_table,
                                    guint      *length)
{
	GHashTableIter iter;
	gpointer key, *ret;
	guint i = 0;

	g_return_val_if_fail (hash_table, NULL);

	ret = g_new0 (gpointer, g_hash_table_size (hash_table) + 1);
	g_hash_table_iter_init (&iter, hash_table);

	while (g_hash_table_iter_next (&iter, &key, NULL))
		ret[i++] = key;

	ret[i] = NULL;

	if (length)
		*length = i;

	return ret;
}
#endif
#if !GLIB_CHECK_VERSION(2, 40, 0)
#define g_hash_table_get_keys_as_array(hash_table, length) \
	({ \
		_nm_g_hash_table_get_keys_as_array (hash_table, length); \
	})
#else
#define g_hash_table_get_keys_as_array(hash_table, length) \
	({ \
		G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
			(g_hash_table_get_keys_as_array) ((hash_table), (length)); \
		G_GNUC_END_IGNORE_DEPRECATIONS \
	})
#endif

404 405
/*****************************************************************************/

406 407 408 409 410 411 412
#ifndef g_info
/* g_info was only added with 2.39.2 */
#define g_info(...)     g_log (G_LOG_DOMAIN,         \
                               G_LOG_LEVEL_INFO,     \
                               __VA_ARGS__)
#endif

413 414
/*****************************************************************************/

415 416 417 418 419 420 421 422 423 424 425 426
#if !GLIB_CHECK_VERSION(2, 44, 0)
static inline gpointer
g_steal_pointer (gpointer pp)
{
	gpointer *ptr = (gpointer *) pp;
	gpointer ref;

	ref = *ptr;
	*ptr = NULL;

	return ref;
}
427
#endif
428

429 430
#ifdef g_steal_pointer
#undef g_steal_pointer
431
#endif
432 433
#define g_steal_pointer(pp) \
	((typeof (*(pp))) g_steal_pointer (pp))
434

435 436
/*****************************************************************************/

437
static inline gboolean
438 439
_nm_g_strv_contains (const char * const *strv,
                     const char          *str)
440
{
441
#if !GLIB_CHECK_VERSION(2, 44, 0)
442 443 444 445 446 447 448 449 450 451
	g_return_val_if_fail (strv != NULL, FALSE);
	g_return_val_if_fail (str != NULL, FALSE);

	for (; *strv != NULL; strv++) {
		if (g_str_equal (str, *strv))
			return TRUE;
	}

	return FALSE;
#else
452 453 454
	G_GNUC_BEGIN_IGNORE_DEPRECATIONS
	return g_strv_contains (strv, str);
	G_GNUC_END_IGNORE_DEPRECATIONS
455
#endif
456 457
}
#define g_strv_contains _nm_g_strv_contains
458

459 460
/*****************************************************************************/

461
static inline GVariant *
462
_nm_g_variant_new_take_string (char *string)
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
{
#if !GLIB_CHECK_VERSION(2, 36, 0)
	GVariant *value;

	g_return_val_if_fail (string != NULL, NULL);
	g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL);

	value = g_variant_new_string (string);
	g_free (string);
	return value;
#elif !GLIB_CHECK_VERSION(2, 38, 0)
	GVariant *value;
	GBytes *bytes;

	g_return_val_if_fail (string != NULL, NULL);
	g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL);

	bytes = g_bytes_new_take (string, strlen (string) + 1);
	value = g_variant_new_from_bytes (G_VARIANT_TYPE_STRING, bytes, TRUE);
	g_bytes_unref (bytes);

	return value;
#else
	G_GNUC_BEGIN_IGNORE_DEPRECATIONS
	return g_variant_new_take_string (string);
	G_GNUC_END_IGNORE_DEPRECATIONS
#endif
}
#define g_variant_new_take_string _nm_g_variant_new_take_string

493 494
/*****************************************************************************/

495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
#if !GLIB_CHECK_VERSION(2, 38, 0)
_nm_printf (1, 2)
static inline GVariant *
_nm_g_variant_new_printf (const char *format_string, ...)
{
	char *string;
	va_list ap;

	g_return_val_if_fail (format_string, NULL);

	va_start (ap, format_string);
	string = g_strdup_vprintf (format_string, ap);
	va_end (ap);

	return g_variant_new_take_string (string);
}
#define g_variant_new_printf(...) _nm_g_variant_new_printf(__VA_ARGS__)
#else
#define g_variant_new_printf(...) \
	({ \
		GVariant *_v; \
		\
		G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
		_v = g_variant_new_printf (__VA_ARGS__); \
		G_GNUC_END_IGNORE_DEPRECATIONS \
		_v; \
	})
#endif

524 525
/*****************************************************************************/

526 527 528 529 530
#if !GLIB_CHECK_VERSION (2, 56, 0)
#define g_object_ref(Obj)      ((typeof(Obj)) g_object_ref (Obj))
#define g_object_ref_sink(Obj) ((typeof(Obj)) g_object_ref_sink (Obj))
#endif

531 532
/*****************************************************************************/

533 534 535 536 537 538 539 540
#ifndef g_autofree
/* we still don't rely on recent glib to provide g_autofree. Hence, we continue
 * to use our gs_* free macros that we took from libgsystem.
 *
 * To ease migration towards g_auto*, add a compat define for g_autofree. */
#define g_autofree gs_free
#endif

541 542
/*****************************************************************************/

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
#if !GLIB_CHECK_VERSION (2, 47, 1)
/* Older versions of g_value_unset() only allowed to unset a GValue which
 * was initialized previously. This was relaxed ([1], [2], [3]).
 *
 * Our nm_auto_unset_gvalue macro requires to be able to call g_value_unset().
 * Also, it is our general practice to allow for that. Add a compat implementation.
 *
 * [1] https://gitlab.gnome.org/GNOME/glib/commit/4b2d92a864f1505f1b08eb639d74293fa32681da
 * [2] commit "Allow passing unset GValues to g_value_unset()"
 * [3] https://bugzilla.gnome.org/show_bug.cgi?id=755766
 */
static inline void
_nm_g_value_unset (GValue *value)
{
	g_return_if_fail (value);

	if (value->g_type != 0)
		g_value_unset (value);
}
#define g_value_unset _nm_g_value_unset
#endif

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

567
#endif  /* __NM_GLIB_H__ */