2011-10-04 15:30:39 -04:00
|
|
|
/*
|
|
|
|
* Copyright © 2011 Canonical Limited
|
|
|
|
*
|
2022-05-18 09:15:38 +01:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
*
|
2017-01-05 12:47:07 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2011-10-04 15:30:39 -04:00
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2014-01-23 12:58:29 +01:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2011-10-04 15:30:39 -04:00
|
|
|
*
|
|
|
|
* Author: Ryan Lortie <desrt@desrt.ca>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "glib-init.h"
|
2024-05-15 12:51:48 +01:00
|
|
|
#include "glib-private.h"
|
2018-12-18 13:59:23 +05:30
|
|
|
#include "gmacros.h"
|
2012-11-16 12:53:39 +00:00
|
|
|
#include "gtypes.h"
|
2011-10-04 15:30:39 -04:00
|
|
|
#include "gutils.h" /* for GDebugKey */
|
2012-03-05 15:05:07 +01:00
|
|
|
#include "gconstructor.h"
|
2023-08-31 21:54:06 +02:00
|
|
|
#include "gconstructorprivate.h"
|
2013-10-20 14:15:25 +01:00
|
|
|
#include "gmem.h" /* for g_mem_gc_friendly */
|
2011-10-04 15:30:39 -04:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2020-10-02 15:06:09 +01:00
|
|
|
/* Deliberately not checking HAVE_STDINT_H here: we officially require a
|
|
|
|
* C99 toolchain, which implies <stdint.h>, int8_t and so on. If your
|
|
|
|
* toolchain does not have this, now would be a good time to upgrade. */
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2012-11-16 12:53:39 +00:00
|
|
|
/* This seems as good a place as any to make static assertions about platform
|
|
|
|
* assumptions we make throughout GLib. */
|
|
|
|
|
2024-05-15 12:51:48 +01:00
|
|
|
/* Test that private macro G_SIGNEDNESS_OF() works as intended */
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (int) == 1);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (unsigned int) == 0);
|
|
|
|
|
2014-07-20 12:41:57 +01:00
|
|
|
/* We do not support 36-bit bytes or other historical curiosities. */
|
|
|
|
G_STATIC_ASSERT (CHAR_BIT == 8);
|
|
|
|
|
2012-11-16 12:53:39 +00:00
|
|
|
/* We assume that data pointers are the same size as function pointers... */
|
|
|
|
G_STATIC_ASSERT (sizeof (gpointer) == sizeof (GFunc));
|
2018-12-18 13:59:23 +05:30
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (gpointer) == G_ALIGNOF (GFunc));
|
2012-11-16 12:53:39 +00:00
|
|
|
/* ... and that all function pointers are the same size. */
|
|
|
|
G_STATIC_ASSERT (sizeof (GFunc) == sizeof (GCompareDataFunc));
|
2018-12-18 13:59:23 +05:30
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (GFunc) == G_ALIGNOF (GCompareDataFunc));
|
2012-11-16 12:53:39 +00:00
|
|
|
|
2014-05-29 12:20:24 +01:00
|
|
|
/* We assume that "small" enums (those where all values fit in INT32_MIN
|
|
|
|
* to INT32_MAX) are exactly int-sized. In particular, we assume that if
|
|
|
|
* an enum has no members that exceed the range of char/short, the
|
|
|
|
* compiler will make it int-sized anyway, so adding a member later that
|
|
|
|
* *does* exceed the range of char/short is not an ABI break. */
|
|
|
|
typedef enum {
|
|
|
|
TEST_CHAR_0 = 0
|
|
|
|
} TestChar;
|
|
|
|
typedef enum {
|
|
|
|
TEST_SHORT_0 = 0,
|
|
|
|
TEST_SHORT_256 = 256
|
|
|
|
} TestShort;
|
|
|
|
typedef enum {
|
|
|
|
TEST_INT32_MIN = G_MININT32,
|
|
|
|
TEST_INT32_MAX = G_MAXINT32
|
|
|
|
} TestInt;
|
|
|
|
G_STATIC_ASSERT (sizeof (TestChar) == sizeof (int));
|
|
|
|
G_STATIC_ASSERT (sizeof (TestShort) == sizeof (int));
|
|
|
|
G_STATIC_ASSERT (sizeof (TestInt) == sizeof (int));
|
2018-12-18 13:59:23 +05:30
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (TestChar) == G_ALIGNOF (int));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (TestShort) == G_ALIGNOF (int));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (TestInt) == G_ALIGNOF (int));
|
2014-05-29 12:20:24 +01:00
|
|
|
|
2020-10-02 15:06:09 +01:00
|
|
|
G_STATIC_ASSERT (sizeof (gchar) == 1);
|
|
|
|
G_STATIC_ASSERT (sizeof (guchar) == 1);
|
2024-05-15 12:56:54 +01:00
|
|
|
|
|
|
|
/* It is platform-dependent whether gchar is signed or unsigned, so there
|
|
|
|
* is no assertion here for it */
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (guchar) == 0);
|
|
|
|
|
2020-10-02 15:06:09 +01:00
|
|
|
G_STATIC_ASSERT (sizeof (gint8) * CHAR_BIT == 8);
|
|
|
|
G_STATIC_ASSERT (sizeof (guint8) * CHAR_BIT == 8);
|
|
|
|
G_STATIC_ASSERT (sizeof (gint16) * CHAR_BIT == 16);
|
|
|
|
G_STATIC_ASSERT (sizeof (guint16) * CHAR_BIT == 16);
|
|
|
|
G_STATIC_ASSERT (sizeof (gint32) * CHAR_BIT == 32);
|
|
|
|
G_STATIC_ASSERT (sizeof (guint32) * CHAR_BIT == 32);
|
|
|
|
G_STATIC_ASSERT (sizeof (gint64) * CHAR_BIT == 64);
|
|
|
|
G_STATIC_ASSERT (sizeof (guint64) * CHAR_BIT == 64);
|
|
|
|
|
|
|
|
G_STATIC_ASSERT (sizeof (void *) == GLIB_SIZEOF_VOID_P);
|
|
|
|
G_STATIC_ASSERT (sizeof (gintptr) == sizeof (void *));
|
|
|
|
G_STATIC_ASSERT (sizeof (guintptr) == sizeof (void *));
|
|
|
|
|
2022-10-21 14:30:13 +01:00
|
|
|
G_STATIC_ASSERT (sizeof (short) == sizeof (gshort));
|
|
|
|
G_STATIC_ASSERT (G_MINSHORT == SHRT_MIN);
|
|
|
|
G_STATIC_ASSERT (G_MAXSHORT == SHRT_MAX);
|
|
|
|
G_STATIC_ASSERT (sizeof (unsigned short) == sizeof (gushort));
|
|
|
|
G_STATIC_ASSERT (G_MAXUSHORT == USHRT_MAX);
|
2024-05-15 12:56:54 +01:00
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (gshort) == 1);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (gushort) == 0);
|
2022-10-21 14:30:13 +01:00
|
|
|
|
|
|
|
G_STATIC_ASSERT (sizeof (int) == sizeof (gint));
|
|
|
|
G_STATIC_ASSERT (G_MININT == INT_MIN);
|
|
|
|
G_STATIC_ASSERT (G_MAXINT == INT_MAX);
|
|
|
|
G_STATIC_ASSERT (sizeof (unsigned int) == sizeof (guint));
|
|
|
|
G_STATIC_ASSERT (G_MAXUINT == UINT_MAX);
|
2024-05-15 12:56:54 +01:00
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (gint) == 1);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (guint) == 0);
|
2022-10-21 14:30:13 +01:00
|
|
|
|
2020-10-02 15:06:09 +01:00
|
|
|
G_STATIC_ASSERT (sizeof (long) == GLIB_SIZEOF_LONG);
|
2022-10-21 14:30:13 +01:00
|
|
|
G_STATIC_ASSERT (sizeof (long) == sizeof (glong));
|
|
|
|
G_STATIC_ASSERT (G_MINLONG == LONG_MIN);
|
|
|
|
G_STATIC_ASSERT (G_MAXLONG == LONG_MAX);
|
|
|
|
G_STATIC_ASSERT (sizeof (unsigned long) == sizeof (gulong));
|
|
|
|
G_STATIC_ASSERT (G_MAXULONG == ULONG_MAX);
|
2024-05-15 12:56:54 +01:00
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (glong) == 1);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (gulong) == 0);
|
2020-10-02 15:06:09 +01:00
|
|
|
|
|
|
|
G_STATIC_ASSERT (G_HAVE_GINT64 == 1);
|
|
|
|
|
|
|
|
G_STATIC_ASSERT (sizeof (size_t) == GLIB_SIZEOF_SIZE_T);
|
|
|
|
/* Not a typo: ssize_t is POSIX, not Standard C, but if it exists then
|
|
|
|
* it's the same size as size_t. */
|
|
|
|
G_STATIC_ASSERT (sizeof (size_t) == GLIB_SIZEOF_SSIZE_T);
|
|
|
|
G_STATIC_ASSERT (sizeof (gsize) == GLIB_SIZEOF_SSIZE_T);
|
|
|
|
G_STATIC_ASSERT (sizeof (gsize) == sizeof (size_t));
|
2022-10-21 14:36:54 +01:00
|
|
|
G_STATIC_ASSERT (G_MAXSIZE == SIZE_MAX);
|
2020-10-02 15:06:09 +01:00
|
|
|
/* Again this is size_t not ssize_t, because ssize_t is POSIX, not C99 */
|
|
|
|
G_STATIC_ASSERT (sizeof (gssize) == sizeof (size_t));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (gsize) == G_ALIGNOF (size_t));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (gssize) == G_ALIGNOF (size_t));
|
2022-10-21 14:36:54 +01:00
|
|
|
/* We assume that GSIZE_TO_POINTER is reversible by GPOINTER_TO_SIZE
|
|
|
|
* without losing information.
|
|
|
|
* However, we do not assume that GPOINTER_TO_SIZE can store an arbitrary
|
|
|
|
* pointer in a gsize (known to be false on CHERI). */
|
|
|
|
G_STATIC_ASSERT (sizeof (size_t) <= sizeof (void *));
|
2024-05-15 12:56:54 +01:00
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (size_t) == 0);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (gsize) == 0);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (gssize) == 1);
|
|
|
|
|
2023-01-28 11:03:29 +00:00
|
|
|
/* Standard C does not guarantee that size_t is the same as uintptr_t,
|
|
|
|
* but GLib currently assumes they are the same: see
|
|
|
|
* <https://gitlab.gnome.org/GNOME/glib/-/issues/2842>.
|
|
|
|
*
|
|
|
|
* To enable working on bringup for new architectures these assertions
|
|
|
|
* can be disabled with -DG_ENABLE_EXPERIMENTAL_ABI_COMPILATION.
|
|
|
|
*
|
|
|
|
* FIXME: remove these assertions once the API/ABI has stabilized. */
|
|
|
|
#ifndef G_ENABLE_EXPERIMENTAL_ABI_COMPILATION
|
|
|
|
G_STATIC_ASSERT (sizeof (size_t) == sizeof (uintptr_t));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (size_t) == G_ALIGNOF (uintptr_t));
|
|
|
|
#endif
|
2024-05-15 12:56:54 +01:00
|
|
|
|
2020-10-02 15:06:09 +01:00
|
|
|
/* goffset is always 64-bit, even if off_t is only 32-bit
|
|
|
|
* (compiling without large-file-support on 32-bit) */
|
|
|
|
G_STATIC_ASSERT (sizeof (goffset) == sizeof (gint64));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (goffset) == G_ALIGNOF (gint64));
|
2024-05-15 12:56:54 +01:00
|
|
|
/* goffset is always signed */
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (goffset) == 1);
|
2020-10-02 15:06:09 +01:00
|
|
|
|
|
|
|
G_STATIC_ASSERT (sizeof (gfloat) == sizeof (float));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (gfloat) == G_ALIGNOF (float));
|
|
|
|
G_STATIC_ASSERT (sizeof (gdouble) == sizeof (double));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (gdouble) == G_ALIGNOF (double));
|
|
|
|
|
|
|
|
G_STATIC_ASSERT (sizeof (gintptr) == sizeof (intptr_t));
|
|
|
|
G_STATIC_ASSERT (sizeof (guintptr) == sizeof (uintptr_t));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (gintptr) == G_ALIGNOF (intptr_t));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (guintptr) == G_ALIGNOF (uintptr_t));
|
2024-05-15 12:56:54 +01:00
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (gintptr) == 1);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (guintptr) == 0);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (intptr_t) == 1);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (uintptr_t) == 0);
|
2020-10-02 15:06:09 +01:00
|
|
|
|
|
|
|
G_STATIC_ASSERT (sizeof (gint8) == sizeof (int8_t));
|
|
|
|
G_STATIC_ASSERT (sizeof (guint8) == sizeof (uint8_t));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (gint8) == G_ALIGNOF (int8_t));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (guint8) == G_ALIGNOF (uint8_t));
|
2024-05-15 12:56:54 +01:00
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (gint8) == 1);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (guint8) == 0);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (int8_t) == 1);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (uint8_t) == 0);
|
2020-10-02 15:06:09 +01:00
|
|
|
|
|
|
|
G_STATIC_ASSERT (sizeof (gint16) == sizeof (int16_t));
|
|
|
|
G_STATIC_ASSERT (sizeof (guint16) == sizeof (uint16_t));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (gint16) == G_ALIGNOF (int16_t));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (guint16) == G_ALIGNOF (uint16_t));
|
2024-05-15 12:56:54 +01:00
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (int16_t) == 1);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (uint16_t) == 0);
|
2020-10-02 15:06:09 +01:00
|
|
|
|
|
|
|
G_STATIC_ASSERT (sizeof (gint32) == sizeof (int32_t));
|
|
|
|
G_STATIC_ASSERT (sizeof (guint32) == sizeof (uint32_t));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (gint32) == G_ALIGNOF (int32_t));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (guint32) == G_ALIGNOF (uint32_t));
|
2024-05-15 12:56:54 +01:00
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (gint32) == 1);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (guint32) == 0);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (int32_t) == 1);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (uint32_t) == 0);
|
2020-10-02 15:06:09 +01:00
|
|
|
|
|
|
|
G_STATIC_ASSERT (sizeof (gint64) == sizeof (int64_t));
|
|
|
|
G_STATIC_ASSERT (sizeof (guint64) == sizeof (uint64_t));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (gint64) == G_ALIGNOF (int64_t));
|
|
|
|
G_STATIC_ASSERT (G_ALIGNOF (guint64) == G_ALIGNOF (uint64_t));
|
2024-05-15 12:56:54 +01:00
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (gint64) == 1);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (guint64) == 0);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (int64_t) == 1);
|
|
|
|
G_STATIC_ASSERT (G_SIGNEDNESS_OF (uint64_t) == 0);
|
2020-10-02 15:06:09 +01:00
|
|
|
|
2023-09-11 11:20:53 +01:00
|
|
|
/* C11 §6.7, item 3 allows us to rely on this being allowed */
|
|
|
|
typedef struct Foo Foo;
|
|
|
|
typedef struct Foo Foo;
|
|
|
|
|
2011-10-04 15:30:39 -04:00
|
|
|
/**
|
|
|
|
* g_mem_gc_friendly:
|
|
|
|
*
|
2014-02-05 19:32:41 -05:00
|
|
|
* This variable is %TRUE if the `G_DEBUG` environment variable
|
2014-02-06 08:04:52 -05:00
|
|
|
* includes the key `gc-friendly`.
|
2011-10-04 15:30:39 -04:00
|
|
|
*/
|
|
|
|
gboolean g_mem_gc_friendly = FALSE;
|
2018-05-31 06:59:10 +02:00
|
|
|
|
2011-10-04 15:30:39 -04:00
|
|
|
GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING |
|
|
|
|
G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
|
|
|
|
GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
debug_key_matches (const gchar *key,
|
|
|
|
const gchar *token,
|
|
|
|
guint length)
|
|
|
|
{
|
|
|
|
/* may not call GLib functions: see note in g_parse_debug_string() */
|
|
|
|
for (; length; length--, key++, token++)
|
|
|
|
{
|
|
|
|
char k = (*key == '_') ? '-' : tolower (*key );
|
|
|
|
char t = (*token == '_') ? '-' : tolower (*token);
|
|
|
|
|
|
|
|
if (k != t)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *key == '\0';
|
|
|
|
}
|
|
|
|
|
glib-init: statically assert that int is exactly 32 bits long
The GVariant documentation says you can assume that types of no more
than 32 bits may be assumed to be promoted to int by the usual
promotions. If we're going to document that, we should statically
assert that it's true, i.e. sizeof (int) >= sizeof (int32_t).
All reasonable modern platforms are either ILP32 (32-bit platforms),
LP64 (64-bit Linux, *BSD etc.), or LLP64 (64-bit Windows): there have
been ILP64 platforms in the past, but ILP64 has the compelling
disadvantage that {signed char, short, int} can't possibly provide
all of {int8_t, int16_t, int32_t} unless int is 32 bits long.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=730932
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: Colin Walters
2015-05-04 10:40:51 +01:00
|
|
|
/* The GVariant documentation indirectly says that int is at least 32 bits
|
|
|
|
* (by saying that b, y, n, q, i, u, h are promoted to int). On any
|
|
|
|
* reasonable platform, int is in fact *exactly* 32 bits long, because
|
|
|
|
* otherwise, {signed char, short, int} wouldn't be sufficient to provide
|
|
|
|
* {int8_t, int16_t, int32_t}. */
|
|
|
|
G_STATIC_ASSERT (sizeof (int) == sizeof (gint32));
|
|
|
|
|
2011-10-04 15:30:39 -04:00
|
|
|
/**
|
|
|
|
* g_parse_debug_string:
|
2016-10-28 18:29:02 -07:00
|
|
|
* @string: (nullable): a list of debug options separated by colons, spaces, or
|
2011-10-04 15:30:39 -04:00
|
|
|
* commas, or %NULL.
|
|
|
|
* @keys: (array length=nkeys): pointer to an array of #GDebugKey which associate
|
|
|
|
* strings with bit flags.
|
2014-01-31 22:05:04 -05:00
|
|
|
* @nkeys: the number of #GDebugKeys in the array.
|
2011-10-04 15:30:39 -04:00
|
|
|
*
|
|
|
|
* Parses a string containing debugging options
|
|
|
|
* into a %guint containing bit flags. This is used
|
2023-05-10 10:11:24 +07:00
|
|
|
* within GDK and GTK to parse the debug options passed on the
|
2011-10-04 15:30:39 -04:00
|
|
|
* command line or through environment variables.
|
|
|
|
*
|
2014-01-31 22:05:04 -05:00
|
|
|
* If @string is equal to "all", all flags are set. Any flags
|
|
|
|
* specified along with "all" in @string are inverted; thus,
|
|
|
|
* "all,foo,bar" or "foo,bar,all" sets all flags except those
|
|
|
|
* corresponding to "foo" and "bar".
|
2011-10-20 16:07:03 +01:00
|
|
|
*
|
2014-01-31 22:05:04 -05:00
|
|
|
* If @string is equal to "help", all the available keys in @keys
|
2011-10-20 16:07:03 +01:00
|
|
|
* are printed out to standard error.
|
2011-10-04 15:30:39 -04:00
|
|
|
*
|
|
|
|
* Returns: the combined set of bit flags.
|
|
|
|
*/
|
|
|
|
guint
|
|
|
|
g_parse_debug_string (const gchar *string,
|
|
|
|
const GDebugKey *keys,
|
|
|
|
guint nkeys)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
guint result = 0;
|
|
|
|
|
|
|
|
if (string == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* this function is used during the initialisation of gmessages, gmem
|
|
|
|
* and gslice, so it may not do anything that causes memory to be
|
|
|
|
* allocated or risks messages being emitted.
|
|
|
|
*
|
|
|
|
* this means, more or less, that this code may not call anything
|
|
|
|
* inside GLib.
|
|
|
|
*/
|
|
|
|
|
2011-10-20 16:07:03 +01:00
|
|
|
if (!strcasecmp (string, "help"))
|
2011-10-04 15:30:39 -04:00
|
|
|
{
|
|
|
|
/* using stdio directly for the reason stated above */
|
2011-11-12 19:13:44 -05:00
|
|
|
fprintf (stderr, "Supported debug values:");
|
2011-10-04 15:30:39 -04:00
|
|
|
for (i = 0; i < nkeys; i++)
|
2016-03-14 08:07:39 -04:00
|
|
|
fprintf (stderr, " %s", keys[i].key);
|
2011-11-12 19:13:44 -05:00
|
|
|
fprintf (stderr, " all help\n");
|
2011-10-04 15:30:39 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const gchar *p = string;
|
|
|
|
const gchar *q;
|
2011-10-20 16:07:03 +01:00
|
|
|
gboolean invert = FALSE;
|
2011-10-04 15:30:39 -04:00
|
|
|
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
q = strpbrk (p, ":;, \t");
|
|
|
|
if (!q)
|
2011-11-12 19:13:44 -05:00
|
|
|
q = p + strlen (p);
|
2011-10-04 15:30:39 -04:00
|
|
|
|
2011-10-20 16:07:03 +01:00
|
|
|
if (debug_key_matches ("all", p, q - p))
|
|
|
|
{
|
|
|
|
invert = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < nkeys; i++)
|
|
|
|
if (debug_key_matches (keys[i].key, p, q - p))
|
|
|
|
result |= keys[i].value;
|
|
|
|
}
|
2011-10-04 15:30:39 -04:00
|
|
|
|
|
|
|
p = q;
|
|
|
|
if (*p)
|
|
|
|
p++;
|
|
|
|
}
|
2011-10-20 16:07:03 +01:00
|
|
|
|
|
|
|
if (invert)
|
|
|
|
{
|
|
|
|
guint all_flags = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < nkeys; i++)
|
|
|
|
all_flags |= keys[i].value;
|
|
|
|
|
|
|
|
result = all_flags & (~result);
|
|
|
|
}
|
2011-10-04 15:30:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
g_parse_debug_envvar (const gchar *envvar,
|
|
|
|
const GDebugKey *keys,
|
2012-03-13 20:08:27 -04:00
|
|
|
gint n_keys,
|
|
|
|
guint default_value)
|
2011-10-04 15:30:39 -04:00
|
|
|
{
|
|
|
|
const gchar *value;
|
|
|
|
|
|
|
|
#ifdef OS_WIN32
|
|
|
|
/* "fatal-warnings,fatal-criticals,all,help" is pretty short */
|
2012-03-13 20:08:01 -04:00
|
|
|
gchar buffer[100];
|
2011-10-04 15:30:39 -04:00
|
|
|
|
|
|
|
if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
|
|
|
|
value = buffer;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
value = getenv (envvar);
|
|
|
|
#endif
|
|
|
|
|
2012-03-13 20:08:27 -04:00
|
|
|
if (value == NULL)
|
|
|
|
return default_value;
|
|
|
|
|
2011-10-04 15:30:39 -04:00
|
|
|
return g_parse_debug_string (value, keys, n_keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_messages_prefixed_init (void)
|
|
|
|
{
|
|
|
|
const GDebugKey keys[] = {
|
|
|
|
{ "error", G_LOG_LEVEL_ERROR },
|
|
|
|
{ "critical", G_LOG_LEVEL_CRITICAL },
|
|
|
|
{ "warning", G_LOG_LEVEL_WARNING },
|
|
|
|
{ "message", G_LOG_LEVEL_MESSAGE },
|
|
|
|
{ "info", G_LOG_LEVEL_INFO },
|
|
|
|
{ "debug", G_LOG_LEVEL_DEBUG }
|
|
|
|
};
|
|
|
|
|
2012-03-13 20:08:27 -04:00
|
|
|
g_log_msg_prefix = g_parse_debug_envvar ("G_MESSAGES_PREFIXED", keys, G_N_ELEMENTS (keys), g_log_msg_prefix);
|
2011-10-04 15:30:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_debug_init (void)
|
|
|
|
{
|
|
|
|
const GDebugKey keys[] = {
|
2011-11-12 18:36:52 -05:00
|
|
|
{ "gc-friendly", 1 },
|
2011-10-04 15:30:39 -04:00
|
|
|
{"fatal-warnings", G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL },
|
|
|
|
{"fatal-criticals", G_LOG_LEVEL_CRITICAL }
|
|
|
|
};
|
|
|
|
GLogLevelFlags flags;
|
|
|
|
|
2012-03-13 20:08:27 -04:00
|
|
|
flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys), 0);
|
2011-10-04 15:30:39 -04:00
|
|
|
|
2011-11-12 18:36:52 -05:00
|
|
|
g_log_always_fatal |= flags & G_LOG_LEVEL_MASK;
|
2011-10-04 15:30:39 -04:00
|
|
|
|
2011-11-12 18:36:52 -05:00
|
|
|
g_mem_gc_friendly = flags & 1;
|
2011-10-04 15:30:39 -04:00
|
|
|
}
|
|
|
|
|
2015-10-06 19:45:38 -04:00
|
|
|
void
|
2011-10-04 15:30:39 -04:00
|
|
|
glib_init (void)
|
|
|
|
{
|
2015-10-06 19:43:38 -04:00
|
|
|
static gboolean glib_inited;
|
|
|
|
|
|
|
|
if (glib_inited)
|
|
|
|
return;
|
|
|
|
|
|
|
|
glib_inited = TRUE;
|
|
|
|
|
2011-10-04 15:30:39 -04:00
|
|
|
g_messages_prefixed_init ();
|
|
|
|
g_debug_init ();
|
2015-09-11 23:59:27 -04:00
|
|
|
g_quark_init ();
|
2019-12-25 13:07:32 +01:00
|
|
|
g_error_init ();
|
2011-10-04 15:30:39 -04:00
|
|
|
}
|
|
|
|
|
2022-01-19 14:15:10 +01:00
|
|
|
#ifdef G_PLATFORM_WIN32
|
2011-10-04 15:30:39 -04:00
|
|
|
|
2022-01-20 16:24:44 +01:00
|
|
|
HMODULE glib_dll = NULL;
|
2022-01-19 14:19:05 +01:00
|
|
|
void glib_win32_init (void);
|
2022-01-20 16:24:44 +01:00
|
|
|
|
2022-01-19 14:19:05 +01:00
|
|
|
void
|
2022-01-20 16:24:44 +01:00
|
|
|
glib_win32_init (void)
|
|
|
|
{
|
2022-01-19 14:19:05 +01:00
|
|
|
/* May be called more than once in static compilation mode */
|
|
|
|
static gboolean win32_already_init = FALSE;
|
|
|
|
if (!win32_already_init)
|
|
|
|
{
|
|
|
|
win32_already_init = TRUE;
|
|
|
|
|
|
|
|
g_crash_handler_win32_init ();
|
2022-01-20 16:24:44 +01:00
|
|
|
#ifdef THREADS_WIN32
|
2022-01-19 14:19:05 +01:00
|
|
|
g_thread_win32_init ();
|
2022-01-20 16:24:44 +01:00
|
|
|
#endif
|
|
|
|
|
2022-01-19 14:19:05 +01:00
|
|
|
g_clock_win32_init ();
|
|
|
|
glib_init ();
|
|
|
|
/* must go after glib_init */
|
|
|
|
g_console_win32_init ();
|
|
|
|
}
|
2022-01-20 16:24:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
glib_win32_deinit (gboolean detach_thread)
|
|
|
|
{
|
|
|
|
#ifdef THREADS_WIN32
|
|
|
|
if (detach_thread)
|
|
|
|
g_thread_win32_process_detach ();
|
|
|
|
#endif
|
|
|
|
g_crash_handler_win32_deinit ();
|
|
|
|
}
|
|
|
|
|
2022-01-19 14:19:05 +01:00
|
|
|
#ifndef GLIB_STATIC_COMPILATION
|
|
|
|
|
2012-11-10 10:58:19 -05:00
|
|
|
BOOL WINAPI DllMain (HINSTANCE hinstDLL,
|
|
|
|
DWORD fdwReason,
|
|
|
|
LPVOID lpvReserved);
|
|
|
|
|
2011-10-04 15:30:39 -04:00
|
|
|
BOOL WINAPI
|
|
|
|
DllMain (HINSTANCE hinstDLL,
|
|
|
|
DWORD fdwReason,
|
|
|
|
LPVOID lpvReserved)
|
|
|
|
{
|
|
|
|
switch (fdwReason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
glib_dll = hinstDLL;
|
2022-01-20 16:24:44 +01:00
|
|
|
glib_win32_init ();
|
2011-10-04 15:30:39 -04:00
|
|
|
break;
|
|
|
|
|
2011-10-04 16:08:27 -04:00
|
|
|
case DLL_THREAD_DETACH:
|
2013-04-09 14:09:33 +02:00
|
|
|
#ifdef THREADS_WIN32
|
2011-10-04 16:08:27 -04:00
|
|
|
g_thread_win32_thread_detach ();
|
2013-04-09 14:09:33 +02:00
|
|
|
#endif
|
2011-10-04 16:08:27 -04:00
|
|
|
break;
|
|
|
|
|
2015-04-07 20:05:45 +00:00
|
|
|
case DLL_PROCESS_DETACH:
|
2022-01-20 16:24:44 +01:00
|
|
|
glib_win32_deinit (lpvReserved == NULL);
|
2015-04-07 20:05:45 +00:00
|
|
|
break;
|
|
|
|
|
2011-10-04 15:30:39 -04:00
|
|
|
default:
|
|
|
|
/* do nothing */
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2023-08-31 21:54:06 +02:00
|
|
|
#else
|
|
|
|
|
|
|
|
#ifndef G_HAS_CONSTRUCTORS
|
|
|
|
#error static compilation on Windows requires constructor support
|
2022-01-19 14:19:05 +01:00
|
|
|
#endif
|
2023-08-31 21:54:06 +02:00
|
|
|
|
|
|
|
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
|
|
|
|
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(glib_priv_constructor)
|
2022-01-19 14:19:05 +01:00
|
|
|
#endif
|
|
|
|
|
2023-08-31 21:54:06 +02:00
|
|
|
static gboolean tls_callback_invoked;
|
|
|
|
|
|
|
|
G_DEFINE_CONSTRUCTOR (glib_priv_constructor)
|
2022-01-19 14:19:05 +01:00
|
|
|
|
|
|
|
static void
|
2023-08-31 21:54:06 +02:00
|
|
|
glib_priv_constructor (void)
|
2022-01-19 14:19:05 +01:00
|
|
|
{
|
|
|
|
glib_win32_init ();
|
2023-08-31 21:54:06 +02:00
|
|
|
|
|
|
|
if (!tls_callback_invoked)
|
|
|
|
g_critical ("TLS callback not invoked");
|
2022-01-19 14:19:05 +01:00
|
|
|
}
|
|
|
|
|
2023-08-31 21:54:06 +02:00
|
|
|
#ifndef G_HAS_TLS_CALLBACKS
|
|
|
|
#error static compilation on Windows requires TLS callbacks support
|
|
|
|
#endif
|
2022-01-19 14:19:05 +01:00
|
|
|
|
2023-08-31 21:54:06 +02:00
|
|
|
G_DEFINE_TLS_CALLBACK (glib_priv_tls_callback)
|
|
|
|
|
|
|
|
static void NTAPI
|
|
|
|
glib_priv_tls_callback (LPVOID hinstance,
|
|
|
|
DWORD reason,
|
|
|
|
LPVOID reserved)
|
2022-01-19 14:19:05 +01:00
|
|
|
{
|
2023-08-31 21:54:06 +02:00
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
glib_dll = hinstance;
|
|
|
|
tls_callback_invoked = TRUE;
|
|
|
|
break;
|
2023-08-31 21:58:17 +02:00
|
|
|
case DLL_THREAD_DETACH:
|
|
|
|
#ifdef THREADS_WIN32
|
|
|
|
g_thread_win32_thread_detach ();
|
|
|
|
#endif
|
|
|
|
break;
|
2023-08-31 21:54:06 +02:00
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
glib_win32_deinit (reserved == NULL);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-01-19 14:19:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* GLIB_STATIC_COMPILATION */
|
|
|
|
|
2022-01-20 16:24:44 +01:00
|
|
|
#elif defined(G_HAS_CONSTRUCTORS) /* && !G_PLATFORM_WIN32 */
|
2011-10-04 15:30:39 -04:00
|
|
|
|
2012-03-05 15:05:07 +01:00
|
|
|
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
|
|
|
|
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(glib_init_ctor)
|
|
|
|
#endif
|
|
|
|
G_DEFINE_CONSTRUCTOR(glib_init_ctor)
|
|
|
|
|
|
|
|
static void
|
2011-10-04 15:30:39 -04:00
|
|
|
glib_init_ctor (void)
|
|
|
|
{
|
|
|
|
glib_init ();
|
|
|
|
}
|
|
|
|
|
2022-01-20 16:24:44 +01:00
|
|
|
#else /* !G_PLATFORM_WIN32 && !G_HAS_CONSTRUCTORS */
|
2011-10-04 15:30:39 -04:00
|
|
|
# error Your platform/compiler is missing constructor support
|
2022-01-20 16:24:44 +01:00
|
|
|
#endif /* G_PLATFORM_WIN32 */
|